Skip to content
This repository has been archived by the owner on Dec 20, 2024. It is now read-only.

Remove auction_transaction table from queries #76

Merged
merged 12 commits into from
Feb 1, 2024
12 changes: 3 additions & 9 deletions src/sql/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 @@ -35,7 +31,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 @@ -57,8 +53,6 @@ order_surplus AS (
THEN o.sell_token
END AS surplus_token
FROM settlements s -- links block_number and log_index to tx_from and tx_nonce
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: update comment, tx_from and tx_nonce are not relevant anymore (for anything)

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
JOIN settlement_scores ss -- contains block_deadline
ON at.auction_id = ss.auction_id
JOIN trades t -- contains traded amounts
Expand Down
13 changes: 4 additions & 9 deletions src/sql/orderbook/order_rewards.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,19 @@ with trade_hashes as (SELECT settlement.solver,
auction_id
FROM trades t
LEFT OUTER JOIN LATERAL (
SELECT tx_hash, solver, tx_nonce, tx_from
SELECT tx_hash, solver, tx_nonce, tx_from, auction_id, block_number, log_index
FROM settlements s
WHERE s.block_number = t.block_number
AND s.log_index > t.log_index
ORDER BY s.log_index ASC
LIMIT 1
) AS settlement ON true
join auction_transaction
-- This join also eliminates overlapping
-- trades & settlements between barn and prod DB
on settlement.tx_from = auction_transaction.tx_from
and settlement.tx_nonce = auction_transaction.tx_nonce
join settlement_observations so on
settlement.block_number = so.block_number and settlement.log_index = so.log_index
where block_number > {{start_block}} and block_number <= {{end_block}}),
order_surplus AS (
SELECT
at.auction_id,
s.auction_id,
t.order_uid,
o.sell_token,
o.buy_token,
Expand All @@ -42,8 +39,6 @@ order_surplus AS (
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
JOIN settlement_scores ss -- contains block_deadline
ON at.auction_id = ss.auction_id
JOIN trades t -- contains traded amounts
Expand Down
Loading