diff --git a/.github/workflows/pull-request.yaml b/.github/workflows/pull-request.yaml index 2c925932..96269e95 100644 --- a/.github/workflows/pull-request.yaml +++ b/.github/workflows/pull-request.yaml @@ -30,3 +30,16 @@ jobs: NETWORK: 'mainnet' NODE_URL: https://rpc.ankr.com/eth PAYOUTS_SAFE_ADDRESS: '0x0000000000000000000000000000000000000000' + + sqlfluff: + name: SQLFluff + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-python@v4 + with: + python-version: "3.12" + - name: Install SQLFluff + run: pip install sqlfluff + - name: Run SQLFluff + run: sqlfluff lint queries/ diff --git a/queries/.sqlfluff b/queries/.sqlfluff new file mode 100644 index 00000000..4002a3d0 --- /dev/null +++ b/queries/.sqlfluff @@ -0,0 +1,4 @@ +[sqlfluff] +dialect = postgres +exclude_rules = L046, RF02, RF06, CP02 +max_line_length = 0 diff --git a/queries/orderbook/.sqlfluff b/queries/orderbook/.sqlfluff new file mode 100644 index 00000000..3efa57a9 --- /dev/null +++ b/queries/orderbook/.sqlfluff @@ -0,0 +1,5 @@ +[sqlfluff:templater:jinja:context] +start_block=100 +end_block=100000000 +EPSILON_LOWER=10000000000000000 +EPSILON_UPPER=12000000000000000 diff --git a/queries/orderbook/barn_batch_rewards.sql b/queries/orderbook/barn_batch_rewards.sql index a586de53..d6e654fd 100644 --- a/queries/orderbook/barn_batch_rewards.sql +++ b/queries/orderbook/barn_batch_rewards.sql @@ -1,25 +1,22 @@ -WITH observed_settlements AS ( - SELECT +with observed_settlements as ( + select --noqa: ST06 -- settlement tx_hash, solver, s.block_number, -- settlement_observations - effective_gas_price * gas_used AS execution_cost, + effective_gas_price * gas_used as execution_cost, surplus, s.auction_id - FROM - settlement_observations so - JOIN settlements s ON s.block_number = so.block_number - AND s.log_index = so.log_index - JOIN settlement_scores ss ON s.auction_id = ss.auction_id - WHERE - ss.block_deadline >= {{start_block}} - AND ss.block_deadline <= {{end_block}} + from settlements as s inner join settlement_observations as so + on s.block_number = so.block_number and s.log_index = so.log_index + inner join settlement_scores as ss on s.auction_id = ss.auction_id + where ss.block_deadline >= {{start_block}} and ss.block_deadline <= {{end_block}} ), + -- order data -order_data AS ( - SELECT +order_data as ( + select uid, sell_token, buy_token, @@ -27,9 +24,9 @@ order_data AS ( buy_amount, kind, app_data - FROM orders - UNION ALL - SELECT + from orders + union all + select uid, sell_token, buy_token, @@ -37,12 +34,13 @@ order_data AS ( buy_amount, kind, app_data - FROM jit_orders + from jit_orders ), + -- unprocessed trade data -trade_data_unprocessed AS ( - SELECT - ss.winner AS solver, +trade_data_unprocessed as ( + select --noqa: ST06 + ss.winner as solver, s.auction_id, s.tx_hash, t.order_uid, @@ -50,35 +48,31 @@ trade_data_unprocessed AS ( od.buy_token, t.sell_amount, -- the total amount the user sends t.buy_amount, -- the total amount the user receives - oe.surplus_fee AS observed_fee, -- the total discrepancy between what the user sends and what they would have send if they traded at clearing price + oe.surplus_fee as observed_fee, -- the total discrepancy between what the user sends and what they would have send if they traded at clearing price od.kind, - CASE - WHEN od.kind = 'sell' THEN od.buy_token - WHEN od.kind = 'buy' THEN od.sell_token - END AS surplus_token, - convert_from(ad.full_app_data, 'UTF8')::JSONB->'metadata'->'partnerFee'->>'recipient' AS partner_fee_recipient, - COALESCE(oe.protocol_fee_amounts[1], 0) AS first_protocol_fee_amount, - COALESCE(oe.protocol_fee_amounts[2], 0) AS second_protocol_fee_amount - FROM - settlements s - JOIN settlement_scores ss -- contains block_deadline - ON s.auction_id = ss.auction_id - JOIN trades t -- contains traded amounts - ON s.block_number = t.block_number -- given the join that follows with the order execution table, this works even when multiple txs appear in the same block - JOIN order_data od -- contains tokens and limit amounts - ON t.order_uid = od.uid - JOIN order_execution oe -- contains surplus fee - ON t.order_uid = oe.order_uid - AND s.auction_id = oe.auction_id - LEFT OUTER JOIN app_data ad -- contains full app data - ON od.app_data = ad.contract_app_data - WHERE - ss.block_deadline >= {{start_block}} - AND ss.block_deadline <= {{end_block}} + case + when od.kind = 'sell' then od.buy_token + when od.kind = 'buy' then od.sell_token + end as surplus_token, + cast(convert_from(ad.full_app_data, 'UTF8') as jsonb) -> 'metadata' -> 'partnerFee' ->> 'recipient' as partner_fee_recipient, + coalesce(oe.protocol_fee_amounts[1], 0) as first_protocol_fee_amount, + coalesce(oe.protocol_fee_amounts[2], 0) as second_protocol_fee_amount + from settlements as s inner join settlement_scores as ss -- contains block_deadline + on s.auction_id = ss.auction_id + inner join trades as t -- contains traded amounts + on s.block_number = t.block_number -- given the join that follows with the order execution table, this works even when multiple txs appear in the same block + inner join order_data as od -- contains tokens and limit amounts + on t.order_uid = od.uid + inner join order_execution as oe -- contains surplus fee + on t.order_uid = oe.order_uid and s.auction_id = oe.auction_id + left outer join app_data as ad -- contains full app data + on od.app_data = ad.contract_app_data + where ss.block_deadline >= {{start_block}} and ss.block_deadline <= {{end_block}} ), + -- processed trade data: -trade_data_processed AS ( - SELECT +trade_data_processed as ( + select --noqa: ST06 auction_id, solver, tx_hash, @@ -89,37 +83,33 @@ trade_data_processed AS ( observed_fee, surplus_token, second_protocol_fee_amount, - first_protocol_fee_amount + second_protocol_fee_amount AS protocol_fee, + first_protocol_fee_amount + second_protocol_fee_amount as protocol_fee, partner_fee_recipient, - CASE - WHEN partner_fee_recipient IS NOT NULL THEN second_protocol_fee_amount - ELSE 0 - END AS partner_fee, - surplus_token AS protocol_fee_token - FROM - trade_data_unprocessed + case + when partner_fee_recipient is not null then second_protocol_fee_amount + else 0 + end as partner_fee, + surplus_token as protocol_fee_token + from trade_data_unprocessed ), -price_data AS ( - SELECT + +price_data as ( + select tdp.auction_id, tdp.order_uid, - ap_surplus.price / pow(10, 18) AS surplus_token_native_price, - ap_protocol.price / pow(10, 18) AS protocol_fee_token_native_price, - ap_sell.price / pow(10, 18) AS network_fee_token_native_price - FROM - trade_data_processed AS tdp - LEFT OUTER JOIN auction_prices ap_sell -- contains price: sell token - ON tdp.auction_id = ap_sell.auction_id - AND tdp.sell_token = ap_sell.token - LEFT OUTER JOIN auction_prices ap_surplus -- contains price: surplus token - ON tdp.auction_id = ap_surplus.auction_id - AND tdp.surplus_token = ap_surplus.token - LEFT OUTER JOIN auction_prices ap_protocol -- contains price: protocol fee token - ON tdp.auction_id = ap_protocol.auction_id - AND tdp.surplus_token = ap_protocol.token + ap_surplus.price / pow(10, 18) as surplus_token_native_price, + ap_protocol.price / pow(10, 18) as protocol_fee_token_native_price, + ap_sell.price / pow(10, 18) as network_fee_token_native_price + from trade_data_processed as tdp left outer join auction_prices as ap_sell -- contains price: sell token + on tdp.auction_id = ap_sell.auction_id and tdp.sell_token = ap_sell.token + left outer join auction_prices as ap_surplus -- contains price: surplus token + on tdp.auction_id = ap_surplus.auction_id and tdp.surplus_token = ap_surplus.token + left outer join auction_prices as ap_protocol -- contains price: protocol fee token + on tdp.auction_id = ap_protocol.auction_id and tdp.surplus_token = ap_protocol.token ), -trade_data_processed_with_prices AS ( - SELECT + +trade_data_processed_with_prices as ( + select --noqa: ST06 tdp.auction_id, tdp.solver, tdp.tx_hash, @@ -129,80 +119,68 @@ trade_data_processed_with_prices AS ( tdp.protocol_fee_token, tdp.partner_fee, tdp.partner_fee_recipient, - CASE - WHEN tdp.sell_token != tdp.surplus_token THEN tdp.observed_fee - (tdp.sell_amount - tdp.observed_fee) / tdp.buy_amount * COALESCE(tdp.protocol_fee, 0) - ELSE tdp.observed_fee - COALESCE(tdp.protocol_fee, 0) - END AS network_fee, - tdp.sell_token AS network_fee_token, + case + when tdp.sell_token != tdp.surplus_token then tdp.observed_fee - (tdp.sell_amount - tdp.observed_fee) / tdp.buy_amount * coalesce(tdp.protocol_fee, 0) + else tdp.observed_fee - coalesce(tdp.protocol_fee, 0) + end as network_fee, + tdp.sell_token as network_fee_token, surplus_token_native_price, protocol_fee_token_native_price, network_fee_token_native_price - FROM - trade_data_processed AS tdp - JOIN price_data pd - ON tdp.auction_id = pd.auction_id - AND tdp.order_uid = pd.order_uid + from trade_data_processed as tdp inner join price_data as pd + on tdp.auction_id = pd.auction_id and tdp.order_uid = pd.order_uid ), -batch_protocol_fees AS ( - SELECT + +batch_protocol_fees as ( + select solver, tx_hash, - sum(protocol_fee * protocol_fee_token_native_price) AS protocol_fee - FROM - trade_data_processed_with_prices - GROUP BY - solver, - tx_hash + sum(protocol_fee * protocol_fee_token_native_price) as protocol_fee + from trade_data_processed_with_prices + group by solver, tx_hash ), -batch_network_fees AS ( - SELECT + +batch_network_fees as ( + select solver, tx_hash, - sum(network_fee * network_fee_token_native_price) AS network_fee - FROM - trade_data_processed_with_prices - GROUP BY - solver, - tx_hash + sum(network_fee * network_fee_token_native_price) as network_fee + from trade_data_processed_with_prices + group by solver, tx_hash ), -reward_data AS ( - SELECT + +reward_data as ( + select --noqa: ST06 -- observations - os.tx_hash, ss.auction_id, - -- TODO - Assuming that `solver == winner` when both not null + os.tx_hash, + -- TODO - assuming that `solver == winner` when both not null -- We will need to monitor that `solver == winner`! - ss.winner AS solver, - block_number AS settlement_block, + ss.winner as solver, + block_number as settlement_block, block_deadline, - COALESCE(execution_cost, 0) AS execution_cost, - COALESCE(surplus, 0) AS surplus, + coalesce(execution_cost, 0) as execution_cost, + coalesce(surplus, 0) as surplus, -- scores winning_score, - CASE - WHEN block_number IS NOT NULL - AND block_number <= block_deadline + 1 THEN winning_score -- this includes a grace period of one block for settling a batch - ELSE 0 - END AS observed_score, + case + when block_number is not null and block_number <= block_deadline + 1 then winning_score -- this includes a grace period of one block for settling a batch + else 0 + end as observed_score, reference_score, -- protocol_fees - COALESCE(CAST(protocol_fee AS NUMERIC(78, 0)), 0) AS protocol_fee, - COALESCE( - CAST(network_fee AS NUMERIC(78, 0)), - 0 - ) AS network_fee - FROM - settlement_scores ss - -- outer joins made in order to capture non-existent settlements. - LEFT OUTER JOIN observed_settlements os ON os.auction_id = ss.auction_id - LEFT OUTER JOIN batch_protocol_fees bpf ON bpf.tx_hash = os.tx_hash - LEFT OUTER JOIN batch_network_fees bnf ON bnf.tx_hash = os.tx_hash - WHERE - ss.block_deadline >= {{start_block}} - AND ss.block_deadline <= {{end_block}} + coalesce(cast(protocol_fee as numeric(78, 0)), 0) as protocol_fee, + coalesce(cast(network_fee as numeric(78, 0)), 0) as network_fee + from settlement_scores as ss + -- outer joins made in order to capture non-existent settlements. + left outer join observed_settlements as os on ss.auction_id = os.auction_id + left outer join batch_protocol_fees as bpf on os.tx_hash = bpf.tx_hash + left outer join batch_network_fees as bnf on os.tx_hash = bnf.tx_hash + where ss.block_deadline >= {{start_block}} and ss.block_deadline <= {{end_block}} ), -reward_per_auction AS ( - SELECT + +reward_per_auction as ( + select tx_hash, auction_id, settlement_block, @@ -212,63 +190,60 @@ reward_per_auction AS ( surplus, protocol_fee, -- the protocol fee network_fee, -- the network fee - observed_score - reference_score AS uncapped_payment, + observed_score - reference_score as uncapped_payment, -- Capped Reward = CLAMP_[-E, E + exec_cost](uncapped_reward_eth) - LEAST( - GREATEST( - - {{EPSILON_LOWER}}, + least( + greatest( + -{{EPSILON_LOWER}}, observed_score - reference_score ), {{EPSILON_UPPER}} - ) AS capped_payment, + ) as capped_payment, winning_score, reference_score - FROM - reward_data + from reward_data ), -primary_rewards AS ( - SELECT - rpt.solver, - SUM(capped_payment) AS payment, - SUM(protocol_fee) AS protocol_fee, - SUM(network_fee) AS network_fee - FROM - reward_per_auction rpt - GROUP BY - solver + +primary_rewards as ( + select + solver, + sum(capped_payment) as payment, + sum(protocol_fee) as protocol_fee, + sum(network_fee) as network_fee + from reward_per_auction + group by solver ), -partner_fees_per_solver AS ( - SELECT + +partner_fees_per_solver as ( + select solver, partner_fee_recipient, - sum(partner_fee * protocol_fee_token_native_price) AS partner_fee - FROM - trade_data_processed_with_prices - WHERE partner_fee_recipient IS NOT NULL - GROUP BY solver,partner_fee_recipient + sum(partner_fee * protocol_fee_token_native_price) as partner_fee + from trade_data_processed_with_prices + where partner_fee_recipient is not null + group by solver, partner_fee_recipient ), -aggregate_partner_fees_per_solver AS ( - SELECT + +aggregate_partner_fees_per_solver as ( + select solver, - array_agg(partner_fee_recipient) AS partner_list, - array_agg(partner_fee) AS partner_fee - FROM partner_fees_per_solver - GROUP BY solver + array_agg(partner_fee_recipient) as partner_list, + array_agg(partner_fee) as partner_fee + from partner_fees_per_solver + group by solver ), -aggregate_results AS ( - SELECT - CONCAT('0x', encode(pr.solver, 'hex')) AS solver, - COALESCE(payment, 0) AS primary_reward_eth, - COALESCE(protocol_fee, 0) AS protocol_fee_eth, - COALESCE(network_fee, 0) AS network_fee_eth, + +aggregate_results as ( + select --noqa: ST06 + concat('0x', encode(pr.solver, 'hex')) as solver, + coalesce(payment, 0) as primary_reward_eth, + coalesce(protocol_fee, 0) as protocol_fee_eth, + coalesce(network_fee, 0) as network_fee_eth, partner_list, - partner_fee AS partner_fee_eth - FROM primary_rewards pr - LEFT OUTER JOIN aggregate_partner_fees_per_solver aif ON pr.solver = aif.solver -) -- -SELECT - * -FROM - aggregate_results -ORDER BY - solver + partner_fee as partner_fee_eth + from primary_rewards as pr left outer join aggregate_partner_fees_per_solver as aif on pr.solver = aif.solver +) + +select * +from aggregate_results +order by solver diff --git a/queries/orderbook/prod_batch_rewards.sql b/queries/orderbook/prod_batch_rewards.sql index 015d2675..d6e654fd 100644 --- a/queries/orderbook/prod_batch_rewards.sql +++ b/queries/orderbook/prod_batch_rewards.sql @@ -1,25 +1,22 @@ -WITH observed_settlements AS ( - SELECT +with observed_settlements as ( + select --noqa: ST06 -- settlement tx_hash, solver, s.block_number, -- settlement_observations - effective_gas_price * gas_used AS execution_cost, + effective_gas_price * gas_used as execution_cost, surplus, s.auction_id - FROM - settlement_observations so - JOIN settlements s ON s.block_number = so.block_number - AND s.log_index = so.log_index - JOIN settlement_scores ss ON s.auction_id = ss.auction_id - WHERE - ss.block_deadline >= {{start_block}} - AND ss.block_deadline <= {{end_block}} + from settlements as s inner join settlement_observations as so + on s.block_number = so.block_number and s.log_index = so.log_index + inner join settlement_scores as ss on s.auction_id = ss.auction_id + where ss.block_deadline >= {{start_block}} and ss.block_deadline <= {{end_block}} ), + -- order data -order_data AS ( - SELECT +order_data as ( + select uid, sell_token, buy_token, @@ -27,9 +24,9 @@ order_data AS ( buy_amount, kind, app_data - FROM orders - UNION ALL - SELECT + from orders + union all + select uid, sell_token, buy_token, @@ -37,12 +34,13 @@ order_data AS ( buy_amount, kind, app_data - FROM jit_orders + from jit_orders ), + -- unprocessed trade data -trade_data_unprocessed AS ( - SELECT - ss.winner AS solver, +trade_data_unprocessed as ( + select --noqa: ST06 + ss.winner as solver, s.auction_id, s.tx_hash, t.order_uid, @@ -50,35 +48,31 @@ trade_data_unprocessed AS ( od.buy_token, t.sell_amount, -- the total amount the user sends t.buy_amount, -- the total amount the user receives - oe.surplus_fee AS observed_fee, -- the total discrepancy between what the user sends and what they would have send if they traded at clearing price + oe.surplus_fee as observed_fee, -- the total discrepancy between what the user sends and what they would have send if they traded at clearing price od.kind, - CASE - WHEN od.kind = 'sell' THEN od.buy_token - WHEN od.kind = 'buy' THEN od.sell_token - END AS surplus_token, - convert_from(ad.full_app_data, 'UTF8')::JSONB->'metadata'->'partnerFee'->>'recipient' AS partner_fee_recipient, - COALESCE(oe.protocol_fee_amounts[1], 0) AS first_protocol_fee_amount, - COALESCE(oe.protocol_fee_amounts[2], 0) AS second_protocol_fee_amount - FROM - settlements s - JOIN settlement_scores ss -- contains block_deadline - ON s.auction_id = ss.auction_id - JOIN trades t -- contains traded amounts - ON s.block_number = t.block_number -- given the join that follows with the order execution table, this works even when multiple txs appear in the same block - JOIN order_data od -- contains tokens and limit amounts - ON t.order_uid = od.uid - JOIN order_execution oe -- contains surplus fee - ON t.order_uid = oe.order_uid - AND s.auction_id = oe.auction_id - LEFT OUTER JOIN app_data ad -- contains full app data - ON od.app_data = ad.contract_app_data - WHERE - ss.block_deadline >= {{start_block}} - AND ss.block_deadline <= {{end_block}} + case + when od.kind = 'sell' then od.buy_token + when od.kind = 'buy' then od.sell_token + end as surplus_token, + cast(convert_from(ad.full_app_data, 'UTF8') as jsonb) -> 'metadata' -> 'partnerFee' ->> 'recipient' as partner_fee_recipient, + coalesce(oe.protocol_fee_amounts[1], 0) as first_protocol_fee_amount, + coalesce(oe.protocol_fee_amounts[2], 0) as second_protocol_fee_amount + from settlements as s inner join settlement_scores as ss -- contains block_deadline + on s.auction_id = ss.auction_id + inner join trades as t -- contains traded amounts + on s.block_number = t.block_number -- given the join that follows with the order execution table, this works even when multiple txs appear in the same block + inner join order_data as od -- contains tokens and limit amounts + on t.order_uid = od.uid + inner join order_execution as oe -- contains surplus fee + on t.order_uid = oe.order_uid and s.auction_id = oe.auction_id + left outer join app_data as ad -- contains full app data + on od.app_data = ad.contract_app_data + where ss.block_deadline >= {{start_block}} and ss.block_deadline <= {{end_block}} ), + -- processed trade data: -trade_data_processed AS ( - SELECT +trade_data_processed as ( + select --noqa: ST06 auction_id, solver, tx_hash, @@ -89,37 +83,33 @@ trade_data_processed AS ( observed_fee, surplus_token, second_protocol_fee_amount, - first_protocol_fee_amount + second_protocol_fee_amount AS protocol_fee, + first_protocol_fee_amount + second_protocol_fee_amount as protocol_fee, partner_fee_recipient, - CASE - WHEN partner_fee_recipient IS NOT NULL THEN second_protocol_fee_amount - ELSE 0 - END AS partner_fee, - surplus_token AS protocol_fee_token - FROM - trade_data_unprocessed + case + when partner_fee_recipient is not null then second_protocol_fee_amount + else 0 + end as partner_fee, + surplus_token as protocol_fee_token + from trade_data_unprocessed ), -price_data AS ( - SELECT + +price_data as ( + select tdp.auction_id, tdp.order_uid, - ap_surplus.price / pow(10, 18) AS surplus_token_native_price, - ap_protocol.price / pow(10, 18) AS protocol_fee_token_native_price, - ap_sell.price / pow(10, 18) AS network_fee_token_native_price - FROM - trade_data_processed AS tdp - LEFT OUTER JOIN auction_prices ap_sell -- contains price: sell token - ON tdp.auction_id = ap_sell.auction_id - AND tdp.sell_token = ap_sell.token - LEFT OUTER JOIN auction_prices ap_surplus -- contains price: surplus token - ON tdp.auction_id = ap_surplus.auction_id - AND tdp.surplus_token = ap_surplus.token - LEFT OUTER JOIN auction_prices ap_protocol -- contains price: protocol fee token - ON tdp.auction_id = ap_protocol.auction_id - AND tdp.surplus_token = ap_protocol.token + ap_surplus.price / pow(10, 18) as surplus_token_native_price, + ap_protocol.price / pow(10, 18) as protocol_fee_token_native_price, + ap_sell.price / pow(10, 18) as network_fee_token_native_price + from trade_data_processed as tdp left outer join auction_prices as ap_sell -- contains price: sell token + on tdp.auction_id = ap_sell.auction_id and tdp.sell_token = ap_sell.token + left outer join auction_prices as ap_surplus -- contains price: surplus token + on tdp.auction_id = ap_surplus.auction_id and tdp.surplus_token = ap_surplus.token + left outer join auction_prices as ap_protocol -- contains price: protocol fee token + on tdp.auction_id = ap_protocol.auction_id and tdp.surplus_token = ap_protocol.token ), -trade_data_processed_with_prices AS ( - SELECT + +trade_data_processed_with_prices as ( + select --noqa: ST06 tdp.auction_id, tdp.solver, tdp.tx_hash, @@ -129,83 +119,68 @@ trade_data_processed_with_prices AS ( tdp.protocol_fee_token, tdp.partner_fee, tdp.partner_fee_recipient, - CASE - WHEN tdp.sell_token != tdp.surplus_token THEN tdp.observed_fee - (tdp.sell_amount - tdp.observed_fee) / tdp.buy_amount * COALESCE(tdp.protocol_fee, 0) - ELSE tdp.observed_fee - COALESCE(tdp.protocol_fee, 0) - END AS network_fee, - tdp.sell_token AS network_fee_token, + case + when tdp.sell_token != tdp.surplus_token then tdp.observed_fee - (tdp.sell_amount - tdp.observed_fee) / tdp.buy_amount * coalesce(tdp.protocol_fee, 0) + else tdp.observed_fee - coalesce(tdp.protocol_fee, 0) + end as network_fee, + tdp.sell_token as network_fee_token, surplus_token_native_price, - CASE - WHEN tdp.order_uid = '\xd6dda5a9dc263af80b6b4155d61f3cd172432fb0e3564fefa537f90603aea78bffff8298631efa764238485543fcff82b878ce1e66fcdfc0' THEN 2.13762621005e-7 - ELSE protocol_fee_token_native_price - END AS protocol_fee_token_native_price, + protocol_fee_token_native_price, network_fee_token_native_price - FROM - trade_data_processed AS tdp - JOIN price_data pd - ON tdp.auction_id = pd.auction_id - AND tdp.order_uid = pd.order_uid + from trade_data_processed as tdp inner join price_data as pd + on tdp.auction_id = pd.auction_id and tdp.order_uid = pd.order_uid ), -batch_protocol_fees AS ( - SELECT + +batch_protocol_fees as ( + select solver, tx_hash, - sum(protocol_fee * protocol_fee_token_native_price) AS protocol_fee - FROM - trade_data_processed_with_prices - GROUP BY - solver, - tx_hash + sum(protocol_fee * protocol_fee_token_native_price) as protocol_fee + from trade_data_processed_with_prices + group by solver, tx_hash ), -batch_network_fees AS ( - SELECT + +batch_network_fees as ( + select solver, tx_hash, - sum(network_fee * network_fee_token_native_price) AS network_fee - FROM - trade_data_processed_with_prices - GROUP BY - solver, - tx_hash + sum(network_fee * network_fee_token_native_price) as network_fee + from trade_data_processed_with_prices + group by solver, tx_hash ), -reward_data AS ( - SELECT + +reward_data as ( + select --noqa: ST06 -- observations - os.tx_hash, ss.auction_id, - -- TODO - Assuming that `solver == winner` when both not null + os.tx_hash, + -- TODO - assuming that `solver == winner` when both not null -- We will need to monitor that `solver == winner`! - ss.winner AS solver, - block_number AS settlement_block, + ss.winner as solver, + block_number as settlement_block, block_deadline, - COALESCE(execution_cost, 0) AS execution_cost, - COALESCE(surplus, 0) AS surplus, + coalesce(execution_cost, 0) as execution_cost, + coalesce(surplus, 0) as surplus, -- scores winning_score, - CASE - WHEN block_number IS NOT NULL - AND block_number <= block_deadline + 1 THEN winning_score -- this includes a grace period of one block for settling a batch - ELSE 0 - END AS observed_score, + case + when block_number is not null and block_number <= block_deadline + 1 then winning_score -- this includes a grace period of one block for settling a batch + else 0 + end as observed_score, reference_score, -- protocol_fees - COALESCE(CAST(protocol_fee AS NUMERIC(78, 0)), 0) AS protocol_fee, - COALESCE( - CAST(network_fee AS NUMERIC(78, 0)), - 0 - ) AS network_fee - FROM - settlement_scores ss - -- outer joins made in order to capture non-existent settlements. - LEFT OUTER JOIN observed_settlements os ON os.auction_id = ss.auction_id - LEFT OUTER JOIN batch_protocol_fees bpf ON bpf.tx_hash = os.tx_hash - LEFT OUTER JOIN batch_network_fees bnf ON bnf.tx_hash = os.tx_hash - WHERE - ss.block_deadline >= {{start_block}} - AND ss.block_deadline <= {{end_block}} + coalesce(cast(protocol_fee as numeric(78, 0)), 0) as protocol_fee, + coalesce(cast(network_fee as numeric(78, 0)), 0) as network_fee + from settlement_scores as ss + -- outer joins made in order to capture non-existent settlements. + left outer join observed_settlements as os on ss.auction_id = os.auction_id + left outer join batch_protocol_fees as bpf on os.tx_hash = bpf.tx_hash + left outer join batch_network_fees as bnf on os.tx_hash = bnf.tx_hash + where ss.block_deadline >= {{start_block}} and ss.block_deadline <= {{end_block}} ), -reward_per_auction AS ( - SELECT + +reward_per_auction as ( + select tx_hash, auction_id, settlement_block, @@ -215,63 +190,60 @@ reward_per_auction AS ( surplus, protocol_fee, -- the protocol fee network_fee, -- the network fee - observed_score - reference_score AS uncapped_payment, + observed_score - reference_score as uncapped_payment, -- Capped Reward = CLAMP_[-E, E + exec_cost](uncapped_reward_eth) - LEAST( - GREATEST( - - {{EPSILON_LOWER}}, + least( + greatest( + -{{EPSILON_LOWER}}, observed_score - reference_score ), {{EPSILON_UPPER}} - ) AS capped_payment, + ) as capped_payment, winning_score, reference_score - FROM - reward_data + from reward_data ), -primary_rewards AS ( - SELECT - rpt.solver, - SUM(capped_payment) AS payment, - SUM(protocol_fee) AS protocol_fee, - SUM(network_fee) AS network_fee - FROM - reward_per_auction rpt - GROUP BY - solver + +primary_rewards as ( + select + solver, + sum(capped_payment) as payment, + sum(protocol_fee) as protocol_fee, + sum(network_fee) as network_fee + from reward_per_auction + group by solver ), -partner_fees_per_solver AS ( - SELECT + +partner_fees_per_solver as ( + select solver, partner_fee_recipient, - sum(partner_fee * protocol_fee_token_native_price) AS partner_fee - FROM - trade_data_processed_with_prices - WHERE partner_fee_recipient IS NOT NULL - GROUP BY solver,partner_fee_recipient + sum(partner_fee * protocol_fee_token_native_price) as partner_fee + from trade_data_processed_with_prices + where partner_fee_recipient is not null + group by solver, partner_fee_recipient ), -aggregate_partner_fees_per_solver AS ( - SELECT + +aggregate_partner_fees_per_solver as ( + select solver, - array_agg(partner_fee_recipient) AS partner_list, - array_agg(partner_fee) AS partner_fee - FROM partner_fees_per_solver - GROUP BY solver + array_agg(partner_fee_recipient) as partner_list, + array_agg(partner_fee) as partner_fee + from partner_fees_per_solver + group by solver ), -aggregate_results AS ( - SELECT - CONCAT('0x', encode(pr.solver, 'hex')) AS solver, - COALESCE(payment, 0) AS primary_reward_eth, - COALESCE(protocol_fee, 0) AS protocol_fee_eth, - COALESCE(network_fee, 0) AS network_fee_eth, + +aggregate_results as ( + select --noqa: ST06 + concat('0x', encode(pr.solver, 'hex')) as solver, + coalesce(payment, 0) as primary_reward_eth, + coalesce(protocol_fee, 0) as protocol_fee_eth, + coalesce(network_fee, 0) as network_fee_eth, partner_list, - partner_fee AS partner_fee_eth - FROM primary_rewards pr - LEFT OUTER JOIN aggregate_partner_fees_per_solver aif ON pr.solver = aif.solver -) -- -SELECT - * -FROM - aggregate_results -ORDER BY - solver + partner_fee as partner_fee_eth + from primary_rewards as pr left outer join aggregate_partner_fees_per_solver as aif on pr.solver = aif.solver +) + +select * +from aggregate_results +order by solver diff --git a/queries/orderbook/quote_rewards.sql b/queries/orderbook/quote_rewards.sql index 592ffafa..cce475f0 100644 --- a/queries/orderbook/quote_rewards.sql +++ b/queries/orderbook/quote_rewards.sql @@ -1,34 +1,32 @@ with winning_quotes as ( - SELECT + select --noqa: ST06 concat('0x', encode(oq.solver, 'hex')) as solver, oq.order_uid - FROM - trades t - INNER JOIN orders o ON order_uid = uid - JOIN order_quotes oq ON t.order_uid = oq.order_uid - WHERE + from trades as t + inner join orders as o on order_uid = uid + inner join order_quotes as oq on t.order_uid = oq.order_uid + where ( o.class = 'market' - OR ( + or ( o.kind = 'sell' - AND ( + and ( oq.sell_amount - oq.gas_amount * oq.gas_price / oq.sell_token_price ) * oq.buy_amount >= o.buy_amount * oq.sell_amount ) - OR ( + or ( o.kind = 'buy' - AND o.sell_amount >= oq.sell_amount + oq.gas_amount * oq.gas_price / oq.sell_token_price + and o.sell_amount >= oq.sell_amount + oq.gas_amount * oq.gas_price / oq.sell_token_price ) ) - AND o.partially_fillable = 'f' -- the code above might fail for partially fillable orders - AND block_number >= {{start_block}} - AND block_number <= {{end_block}} - AND oq.solver != '\x0000000000000000000000000000000000000000' + and o.partially_fillable = 'f' -- the code above might fail for partially fillable orders + and block_number >= {{start_block}} + and block_number <= {{end_block}} + and oq.solver != '\x0000000000000000000000000000000000000000' ) -SELECT + +select solver, - count(*) AS num_quotes -FROM - winning_quotes -GROUP BY - solver \ No newline at end of file + count(*) as num_quotes +from winning_quotes +group by solver