diff --git a/cowprotocol/accounting/auctions/daily_auction_data_6157692.sql b/cowprotocol/accounting/auctions/daily_auction_data_6157692.sql new file mode 100644 index 00000000..ec406a52 --- /dev/null +++ b/cowprotocol/accounting/auctions/daily_auction_data_6157692.sql @@ -0,0 +1,82 @@ +-- This query provides data related to rewards/payouts on a per auction level +-- for all auctions that had at least one winner. +-- Parameters: +-- blockchain: the chain for which we want to retrieve batch data + +-- The output has the following columns: +-- environment: varchar +-- auction_id: integer +-- block_deadline: integer +-- solver: varbinary +-- block_date: date +-- total_network_fee: decimal(38, 0) +-- total_execution_cost: decimal(38, 0) +-- total_protocol_fee: decimal(38, 0) +-- competition_score: decimal(38, 0) +-- observed_score: decimal(38, 0) +-- uncapped_payment_native_token: decimal(38, 0) +-- capped_payment_native_token: decimal(38, 0) + + +with block_range as ( + select * from "query_3333356(blockchain='{{blockchain}}',start_time='{{start_time}}',end_time='{{end_time}}')" +), + +candidate_batches as ( + select + block_number, + tx_hash, + gas_price * gas_used as execution_cost + from cow_protocol_{{blockchain}}.batches + where + block_date >= cast('{{start_time}}' as timestamp) - interval '1' day + and block_date <= cast('{{end_time}}' as timestamp) + interval '1' day +), + +relevant_txs as ( + select + rbd.tx_hash, + b.execution_cost + from "query_4351957(blockchain='{{blockchain}}')" as rbd inner join candidate_batches as b + on rbd.block_number = b.block_number and rbd.tx_hash = b.tx_hash + where + block_deadline >= (select start_block from block_range) + and block_deadline <= (select end_block from block_range) +) + +select --noqa: ST06 + rbd.environment, + rbd.auction_id, + rbd.block_deadline, + rbd.solver, + date(b."date") as block_date, + cast(sum(coalesce(rbd.network_fee, 0)) as decimal(38, 0)) as total_network_fee, + cast(sum(coalesce(txs.execution_cost, 0)) as decimal(38, 0)) as total_execution_cost, + cast(sum(coalesce(rbd.protocol_fee, 0)) as decimal(38, 0)) as total_protocol_fee, + cast(sum(rbd.winning_score) as decimal(38, 0)) as competition_score, + cast(sum( + case + when rbd.block_number is not null and rbd.block_number <= rbd.block_deadline then winning_score + else 0 + end + ) as decimal(38, 0)) as observed_score, + cast(rbd.reference_score as decimal(38, 0)) as reference_score, + cast(rbd.uncapped_payment_native_token as decimal(38, 0)) as uncapped_payment_native_token, + cast(rbd.capped_payment as decimal(38, 0)) as capped_payment +from "query_4351957(blockchain='{{blockchain}}')" as rbd +left join relevant_txs as txs on rbd.tx_hash = txs.tx_hash +left join {{blockchain}}.blocks as b on rbd.block_deadline = b.number +where + rbd.block_deadline >= (select start_block from block_range) + and rbd.block_deadline <= (select end_block from block_range) +group by + rbd.environment, + rbd.auction_id, + rbd.block_deadline, + rbd.solver, + date(b."date"), + -- the last three columns for grouping are generated per auction and solver, not per solution + -- the group by ensures that we do not double count these entries but just select them + rbd.reference_score, + rbd.uncapped_payment_native_token, + rbd.capped_payment \ No newline at end of file diff --git a/cowprotocol/accounting/daily_rewards_and_fees_6157602.sql b/cowprotocol/accounting/daily_rewards_and_fees_6157602.sql new file mode 100644 index 00000000..84071c0e --- /dev/null +++ b/cowprotocol/accounting/daily_rewards_and_fees_6157602.sql @@ -0,0 +1,150 @@ +with auction_range as ( + select + environment + , min(auction_id) as min_auction_id + , max(auction_id) as max_auction_id + from "query_5270914(blockchain='{{blockchain}}',start_time='{{start_time}}',end_time='{{end_time}}')" + group by + environment +) + , solver_slippage as ( + select + solver_address as solver + , block_date + , slippage_wei * 1.0 / pow(10, 18) as slippage + from "query_6157609(blockchain='{{blockchain}}',start_time='{{start_time}}',end_time='{{end_time}}',slippage_table_name='slippage_per_solver')" +) + -- BEGIN SOLVER REWARDS + , auction_data as ( + , select + ad.solver + , ad.block_date + , ad.total_network_fee + , ad.capped_payment + from "query_6157692(blockchain='{{blockchain}}',start_time='{{start_time}}',end_time='{{end_time}}')" as ad + inner join auction_range on ad.environment = auction_range.environment + where ad.auction_id >= auction_range.min_auction_id + and ad.auction_id <= auction_range.max_auction_id +) + -- AKA Performance Rewards + , primary_rewards as ( + , select + solver + , block_date + , cast(sum(capped_payment) as double) as reward_wei + from auction_data + group by + solver + , block_date +) + , fees_and_costs as ( + select + solver + , block_date + , cast(sum(total_network_fee) as double) as network_fee_wei + from auction_data + group by + solver + , block_date +) + , aggregate_results as ( + select + pr.solver + , pr.block_date + , coalesce(reward_wei, 0) / pow(10, 18) as primary_reward_eth + , coalesce(network_fee_wei, 0) / pow(10, 18) as network_fee_eth + from primary_rewards as pr + left outer join fees_and_costs as fc on pr.solver = fc.solver + and pr.block_date = fc.block_date +) + , combined_data as ( + select + coalesce(ar.solver, ss.solver) as solver + , ar.block_date + , network_fee_eth + , primary_reward_eth + , coalesce(slippage, 0) as slippage_eth + from aggregate_results as ar + full outer join solver_slippage as ss on ar.solver = ss.solver + and ar.block_date = ss.block_date +) + , service_fee_flag as ( + select + solver + , case +when service_fee + then 0.85 +else + 1 +end as service_fee_factor + from "query_4298142(blockchain='{{blockchain}}', start_time='{{start_time}}', end_time='{{end_time}}')" +) + , combined_data_after_service_fee as ( +select --noqa: ST06 cd.solver + , cd.block_date + , cd.network_fee_eth + , case +when cd.primary_reward_eth < 0 + then cd.primary_reward_eth +else + coalesce(sff.service_fee_factor, 1) * cd.primary_reward_eth +end as primary_reward_eth + , cd.slippage_eth + from combined_data as cd + left outer join service_fee_flag as sff on cd.solver = sff.solver +) + , daily_solver_rewards as ( +select --noqa: ST06 block_date + , sum(slippage_eth) as slippage_native_token + , sum(primary_reward_eth) as total_reward_native_token + , sum(network_fee_eth) as network_fee_native_token + from combined_data_after_service_fee as epd + group by + block_date +) + , daily_protocol_fee_native as ( + select + b."date" as block_date + , sum( + protocol_fee * protocol_fee_native_price / pow(10, 18) - coalesce( + case +when partner_fee_recipient is not null + then partner_fee * protocol_fee_native_price / pow(10, 18) +end + , 0 +) +) as protocol_fee_in_native_token --noqa: RF01 + , from "query_4364122(blockchain='{{blockchain}}')" as r + inner join {{blockchain}}.blocks as b on number = block_number + where b.time between timestamp '{{start_time}}' + and timestamp '{{end_time}}' + and r.order_uid not in ( + select + order_uid + from query_3639473 +) + group by + b."date" +) + , partner_fee as ( + select + block_date + , sum(partner_fee_part) as partner_fee_part + , sum(cow_dao_partner_fee_part) as cow_dao_partner_fee_part + from "query_6157807(blockchain='{{blockchain}}',start_time='{{start_time}}',end_time='{{end_time}}')" + group by + block_date +) +select + coalesce(rewards.block_date, fee.block_date, partner_fee.block_date) block_date + , rewards.slippage_native_token + , rewards.total_reward_native_token + , rewards.network_fee_native_token + , fee.protocol_fee_in_native_token + , partner_fee.partner_fee_part + , partner_fee.cow_dao_partner_fee_part +from daily_solver_rewards rewards + full outer join daily_protocol_fee_native fee on rewards.block_date = fee.block_date + full outer join partner_fee on rewards.block_date = partner_fee.block_date +order by + block_date desc \ No newline at end of file diff --git a/cowprotocol/accounting/slippage/daily_slippage_6157609.sql b/cowprotocol/accounting/slippage/daily_slippage_6157609.sql new file mode 100644 index 00000000..e015f2fa --- /dev/null +++ b/cowprotocol/accounting/slippage/daily_slippage_6157609.sql @@ -0,0 +1,45 @@ +-- This query returns slippage per solver and per transaction over a period of time-- evaluated in both usd and the native token of the chain. -- -- Parameters: -- {{start_time}} - the timestamp for which the analysis should start (inclusively) -- {{end_time}} - the timestamp for which the analysis should end (exclusively) -- {{blockchain}} - network to run the analysis on -- {{slippage_table_name}} - slippage_per_transaction for aggregated values per transaction; -- slippage_per_solver for aggregated values per transaction -- -- The columns of slippage_per_transaction are -- - block_time: time of settlement transaction -- - tx_hash: settlement transaction hash -- - solver_address: address of the solver executing the settlement -- - slippage_usd: USD value of slippage -- - slippage_wei: value of slippage in atoms of native token -- - imbalance_usd: USD value of total buffer imbalance -- - protocol_fee_usd: USD value of protocol fees -- - network_fee_usd: USD value of network fees -- -- The columns of daily_slippage_per_solver are -- - solver_address: address of the solver executing the settlement -- - slippage_usd: USD value of slippage -- - slippage_wei: value of slippage in atoms of native token -- -- Results of the query are filtered to not include batches from excluded_batches. -- Batches are also excluded if there is a non-zero imbalance and no value (in native atoms). + , with excluded_batches as ( + select + tx_hash + from query_3490353 +) +, slippage_per_transaction as ( + select + rs.block_time + , rs.tx_hash + , solver_address + , sum(slippage_usd) as slippage_usd + , sum(slippage_wei) as slippage_wei + , sum(if(slippage_type = 'raw_imbalance', slippage_usd, 0)) as imbalance_usd + , sum(if(slippage_type = 'protocol_fee', - slippage_usd, 0)) as protocol_fee_usd + , sum(if(slippage_type = 'network_fee', - slippage_usd, 0)) as network_fee_usd + from "query_4059683(blockchain='{{blockchain}}',start_time='{{start_time}}',end_time='{{end_time}}',raw_slippage_table_name='raw_slippage_breakdown')" as rs + inner join cow_protocol_{{blockchain}}.batches as b on rs.tx_hash = b.tx_hash + where rs.tx_hash not in ( + select + tx_hash + from excluded_batches +) + group by + 1 + , 2 + , 3 + having + bool_and( + slippage_wei is not null + or slippage_atoms = 0 +) +) + , slippage_per_solver as ( + select + solver_address + , date(block_time) block_date + , sum(slippage_usd) as slippage_usd + , sum(slippage_wei) as slippage_wei + from slippage_per_transaction + group by 1, 2 +) +select + * +from {{slippage_table_name}} \ No newline at end of file