Skip to content

Commit

Permalink
Removed auction_transaction (#334)
Browse files Browse the repository at this point in the history
In the [PR](cowprotocol/services#2283) we
removed the `auction_transaction` table and added a new column
`auction_id` to `settlements` table, so we need to redirect all queries
to fetch the `auction_id` from `settlements` going forward.

### When to merge this?

The db change is already applied to staging (migration of data from
`auction_transaction` to `settlements` is also done) so we should merge
the PR before the next script run on Tuesday (otherwise the accounting
will try to join on <from,nonce> which are not populated anymore).

OTOH, the db change is not applied to prod yet (also it won't be applied
until next script run on Tuesday), so we don't want to have this PR
merged before the next script run on Tuesday.

Can we use two different versions of script for staging and prod?

---------

Co-authored-by: Felix Henneke <[email protected]>
Co-authored-by: Haris Angelidakis <[email protected]>
Co-authored-by: harisang <[email protected]>
  • Loading branch information
4 people authored Feb 5, 2024
1 parent fa54e13 commit 6319c81
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 56 deletions.
28 changes: 11 additions & 17 deletions queries/orderbook/batch_rewards.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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}}),

Expand All @@ -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,
Expand All @@ -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}}
)
Expand Down Expand Up @@ -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,
Expand Down
53 changes: 14 additions & 39 deletions tests/queries/batch_rewards_test_db.sql
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -132,7 +124,6 @@ CREATE TABLE fee_policies (


TRUNCATE settlements;
TRUNCATE auction_transaction;
TRUNCATE auction_participants;
TRUNCATE settlement_scores;
TRUNCATE settlement_observations;
Expand All @@ -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),
Expand Down

0 comments on commit 6319c81

Please sign in to comment.