diff --git a/queries/orderbook/batch_rewards.sql b/queries/orderbook/batch_rewards.sql index 664c0f01..f99e388f 100644 --- a/queries/orderbook/batch_rewards.sql +++ b/queries/orderbook/batch_rewards.sql @@ -7,17 +7,13 @@ WITH observed_settlements AS (SELECT effective_gas_price * gas_used AS execution_cost, surplus, fee, - -- auction_transaction - at.auction_id + 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 auction_transaction at - ON s.tx_from = at.tx_from - AND s.tx_nonce = at.tx_nonce JOIN settlement_scores ss - ON at.auction_id = ss.auction_id + ON s.auction_id = ss.auction_id WHERE ss.block_deadline >= {{start_block}} AND ss.block_deadline <= {{end_block}}), @@ -32,7 +28,7 @@ WITH observed_settlements AS (SELECT order_surplus AS ( SELECT ss.winner as solver, - at.auction_id, + s.auction_id, s.tx_hash, t.order_uid, o.sell_token, @@ -53,17 +49,15 @@ order_surplus AS ( WHEN o.kind = 'buy' THEN o.sell_token END AS surplus_token - FROM settlements s -- links block_number and log_index to tx_from and tx_nonce - JOIN auction_transaction at -- links auction_id to tx_from and tx_nonce - ON s.tx_from = at.tx_from AND s.tx_nonce = at.tx_nonce + FROM settlements s JOIN settlement_scores ss -- contains block_deadline - ON at.auction_id = ss.auction_id + ON s.auction_id = ss.auction_id JOIN trades t -- contains traded amounts ON s.block_number = t.block_number -- log_index cannot be checked, does not work correctly with multiple auctions on the same block JOIN orders o -- contains tokens and limit amounts ON t.order_uid = o.uid JOIN order_execution oe -- contains surplus fee - ON t.order_uid = oe.order_uid AND at.auction_id = oe.auction_id + ON t.order_uid = oe.order_uid AND s.auction_id = oe.auction_id WHERE ss.block_deadline >= {{start_block}} AND ss.block_deadline <= {{end_block}} ) @@ -188,11 +182,11 @@ batch_protocol_fees AS ( solver, execution_cost, surplus, - protocol_fee, - fee - network_fee_correction as network_fee, - surplus + protocol_fee + fee - network_fee_correction - reference_score as uncapped_reward_eth, - -- capped Reward = CLAMP_[-E, E + exec_cost](uncapped_reward_eth) - LEAST(GREATEST(-{{EPSILON}}, surplus + coalesce(protocol_fee - network_fee_correction, 0) + fee - reference_score), + protocol_fee, -- the protocol fee + fee - network_fee_correction as network_fee,-- the network fee + surplus + protocol_fee + fee - network_fee_correction - reference_score as uncapped_payment_eth, + -- Capped Reward = CLAMP_[-E, E + exec_cost](uncapped_reward_eth) + LEAST(GREATEST(-{{EPSILON}}, surplus + protocol_fee + fee - network_fee_correction - reference_score), {{EPSILON}} + execution_cost) as capped_payment, winning_score, reference_score, diff --git a/tests/queries/batch_rewards_test_db.sql b/tests/queries/batch_rewards_test_db.sql index 05f92e5c..70f39f50 100644 --- a/tests/queries/batch_rewards_test_db.sql +++ b/tests/queries/batch_rewards_test_db.sql @@ -1,5 +1,4 @@ DROP TABLE IF EXISTS settlements; -DROP TABLE IF EXISTS auction_transaction; DROP TABLE IF EXISTS auction_participants; DROP TABLE IF EXISTS settlement_scores; DROP TABLE IF EXISTS settlement_observations; @@ -20,20 +19,13 @@ CREATE TABLE IF NOT EXISTS settlements tx_hash bytea NOT NULL, tx_from bytea NOT NULL, tx_nonce bigint NOT NULL, + auction_id bigint, PRIMARY KEY (block_number, log_index) ); CREATE INDEX settlements_tx_from_tx_nonce ON settlements (tx_from, tx_nonce); -CREATE TABLE IF NOT EXISTS auction_transaction -( - auction_id bigint PRIMARY KEY, - tx_from bytea NOT NULL, - tx_nonce bigint NOT NULL, - UNIQUE (tx_from, tx_nonce) -); - CREATE TABLE IF NOT EXISTS auction_participants ( auction_id bigint, @@ -132,7 +124,6 @@ CREATE TABLE fee_policies ( TRUNCATE settlements; -TRUNCATE auction_transaction; TRUNCATE auction_participants; TRUNCATE settlement_scores; TRUNCATE settlement_observations; @@ -142,36 +133,20 @@ TRUNCATE trades; TRUNCATE fee_policies; -INSERT INTO settlements (block_number, log_index, solver, tx_hash, tx_from, tx_nonce) -VALUES (1, 10, '\x5111111111111111111111111111111111111111'::bytea, '\x7111'::bytea, '\x5111111111111111111111111111111111111111'::bytea, 1), - (2, 10, '\x5222222222222222222222222222222222222222'::bytea, '\x7222'::bytea, '\x5222222222222222222222222222222222222222'::bytea, 1), - (5, 10, '\x5111111111111111111111111111111111111111'::bytea, '\x7333'::bytea, '\x5111111111111111111111111111111111111111'::bytea, 2), +INSERT INTO settlements (block_number, log_index, solver, tx_hash, tx_from, tx_nonce, auction_id) +VALUES (1, 10, '\x5111111111111111111111111111111111111111'::bytea, '\x7111'::bytea, '\x5111111111111111111111111111111111111111'::bytea, 1, 1), + (2, 10, '\x5222222222222222222222222222222222222222'::bytea, '\x7222'::bytea, '\x5222222222222222222222222222222222222222'::bytea, 1, 2), + (5, 10, '\x5111111111111111111111111111111111111111'::bytea, '\x7333'::bytea, '\x5111111111111111111111111111111111111111'::bytea, 2, 5), -- would the following entry be in the data base? (submitted too late) -- YES - (20, 10, '\x5111111111111111111111111111111111111111'::bytea, '\x7444'::bytea, '\x5111111111111111111111111111111111111111'::bytea, 3), - (25, 10, '\x5111111111111111111111111111111111111111'::bytea, '\x7555'::bytea, '\x5111111111111111111111111111111111111111'::bytea, 4), - (26, 10, '\x5111111111111111111111111111111111111111'::bytea, '\x7666'::bytea, '\x5111111111111111111111111111111111111111'::bytea, 6), - (51, 10, '\x01'::bytea, '\x01'::bytea, '\x01'::bytea, 1), - (52, 10, '\x02'::bytea, '\x02'::bytea, '\x02'::bytea, 1), - (53, 10, '\x01'::bytea, '\x03'::bytea, '\x01'::bytea, 2), - (54, 10, '\x02'::bytea, '\x04'::bytea, '\x02'::bytea, 2), - (55, 10, '\x01'::bytea, '\x05'::bytea, '\x01'::bytea, 3), - (56, 10, '\x02'::bytea, '\x06'::bytea, '\x02'::bytea, 3); - -INSERT INTO auction_transaction (auction_id, tx_from, tx_nonce) -VALUES (1, '\x5111111111111111111111111111111111111111'::bytea, 1), - (2, '\x5222222222222222222222222222222222222222'::bytea, 1), - (5, '\x5111111111111111111111111111111111111111'::bytea, 2), - (6, '\x5111111111111111111111111111111111111111'::bytea, 3), -- would that entry be in the data base? (submitted too late) - (7, '\x5111111111111111111111111111111111111111'::bytea, 4), - (8, '\x5111111111111111111111111111111111111111'::bytea, 5), -- would that entry be in the data base? (failed transaction) - (9, '\x5111111111111111111111111111111111111111'::bytea, 6), - (10, '\x5333333333333333333333333333333333333333'::bytea, 1), -- would that entry be in the data base? (failed transaction) - (51, '\x01'::bytea, 1), - (52, '\x02'::bytea, 1), - (53, '\x01'::bytea, 2), - (54, '\x02'::bytea, 2), - (55, '\x01'::bytea, 3), - (56, '\x02'::bytea, 3); + (20, 10, '\x5111111111111111111111111111111111111111'::bytea, '\x7444'::bytea, '\x5111111111111111111111111111111111111111'::bytea, 3, 6), + (25, 10, '\x5111111111111111111111111111111111111111'::bytea, '\x7555'::bytea, '\x5111111111111111111111111111111111111111'::bytea, 4, 7), + (26, 10, '\x5111111111111111111111111111111111111111'::bytea, '\x7666'::bytea, '\x5111111111111111111111111111111111111111'::bytea, 6, 9), + (51, 10, '\x01'::bytea, '\x01'::bytea, '\x01'::bytea, 1, 51), + (52, 10, '\x02'::bytea, '\x02'::bytea, '\x02'::bytea, 1, 52), + (53, 10, '\x01'::bytea, '\x03'::bytea, '\x01'::bytea, 2, 53), + (54, 10, '\x02'::bytea, '\x04'::bytea, '\x02'::bytea, 2, 54), + (55, 10, '\x01'::bytea, '\x05'::bytea, '\x01'::bytea, 3, 55), + (56, 10, '\x02'::bytea, '\x06'::bytea, '\x02'::bytea, 3, 56); INSERT INTO auction_participants (auction_id, participant) VALUES (1, '\x5222222222222222222222222222222222222222'::bytea),