Skip to content

Commit

Permalink
Remove consistency rewards calculations from sql (#404)
Browse files Browse the repository at this point in the history
This PR removes the sql code that computes consistency rewards, as it is
not used anymore. Together with the merged PR #395, this concludes the
removal of consistency rewards from the script.
  • Loading branch information
harisang authored Oct 7, 2024
1 parent 5163d0f commit c4f9c6c
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 164 deletions.
61 changes: 7 additions & 54 deletions queries/orderbook/barn_batch_rewards.sql
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,6 @@ WITH observed_settlements AS (
ss.block_deadline >= {{start_block}}
AND ss.block_deadline <= {{end_block}}
),
auction_participation AS (
SELECT
ss.auction_id,
array_agg(participant) AS participating_solvers
FROM
auction_participants
JOIN settlement_scores ss ON auction_participants.auction_id = ss.auction_id
WHERE
block_deadline >= {{start_block}}
AND block_deadline <= {{end_block}}
GROUP BY
ss.auction_id
),
-- order data
order_data AS (
SELECT
Expand Down Expand Up @@ -185,7 +172,7 @@ reward_data AS (
ss.auction_id,
-- TODO - Assuming that `solver == winner` when both not null
-- We will need to monitor that `solver == winner`!
COALESCE(os.solver, winner) AS solver,
ss.winner AS solver,
block_number AS settlement_block,
block_deadline,
COALESCE(execution_cost, 0) AS execution_cost,
Expand All @@ -198,8 +185,6 @@ reward_data AS (
ELSE 0
END AS observed_score,
reference_score,
-- auction_participation
participating_solvers,
-- protocol_fees
COALESCE(CAST(protocol_fee AS NUMERIC(78, 0)), 0) AS protocol_fee,
COALESCE(
Expand All @@ -208,13 +193,13 @@ reward_data AS (
) AS network_fee
FROM
settlement_scores ss
-- If there are reported scores,
-- there will always be a record of auction participants
JOIN auction_participation ap ON ss.auction_id = ap.auction_id
-- 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}}
),
reward_per_auction AS (
SELECT
Expand All @@ -237,39 +222,10 @@ reward_per_auction AS (
{{EPSILON_UPPER}}
) AS capped_payment,
winning_score,
reference_score,
participating_solvers AS participating_solvers
reference_score
FROM
reward_data
),
participation_data AS (
SELECT
tx_hash,
block_deadline,
unnest(participating_solvers) AS participant
FROM
reward_per_auction
),
participation_data_intermediate AS (
SELECT
tx_hash,
CASE
WHEN block_deadline <= 20365510 THEN 1 -- final block deadline of accounting week of July 16 - July 23, 2024
ELSE 0
END AS count_participation,
participant
FROM
participation_data
),
participation_counts AS (
SELECT
participant AS solver,
sum(count_participation) AS num_participating_batches
FROM
participation_data_intermediate
GROUP BY
participant
),
primary_rewards AS (
SELECT
rpt.solver,
Expand Down Expand Up @@ -301,16 +257,13 @@ aggregate_partner_fees_per_solver AS (
),
aggregate_results AS (
SELECT
CONCAT('0x', encode(pc.solver, 'hex')) AS solver,
CONCAT('0x', encode(pr.solver, 'hex')) AS solver,
COALESCE(payment, 0) AS primary_reward_eth,
num_participating_batches,
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
participation_counts pc
LEFT OUTER JOIN primary_rewards pr ON pr.solver = pc.solver
FROM primary_rewards pr
LEFT OUTER JOIN aggregate_partner_fees_per_solver aif ON pr.solver = aif.solver
) --
SELECT
Expand Down
61 changes: 7 additions & 54 deletions queries/orderbook/prod_batch_rewards.sql
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,6 @@ WITH observed_settlements AS (
ss.block_deadline >= {{start_block}}
AND ss.block_deadline <= {{end_block}}
),
auction_participation AS (
SELECT
ss.auction_id,
array_agg(participant) AS participating_solvers
FROM
auction_participants
JOIN settlement_scores ss ON auction_participants.auction_id = ss.auction_id
WHERE
block_deadline >= {{start_block}}
AND block_deadline <= {{end_block}}
GROUP BY
ss.auction_id
),
-- order data
order_data AS (
SELECT
Expand Down Expand Up @@ -185,7 +172,7 @@ reward_data AS (
ss.auction_id,
-- TODO - Assuming that `solver == winner` when both not null
-- We will need to monitor that `solver == winner`!
COALESCE(os.solver, winner) AS solver,
ss.winner AS solver,
block_number AS settlement_block,
block_deadline,
COALESCE(execution_cost, 0) AS execution_cost,
Expand All @@ -198,8 +185,6 @@ reward_data AS (
ELSE 0
END AS observed_score,
reference_score,
-- auction_participation
participating_solvers,
-- protocol_fees
COALESCE(CAST(protocol_fee AS NUMERIC(78, 0)), 0) AS protocol_fee,
COALESCE(
Expand All @@ -208,13 +193,13 @@ reward_data AS (
) AS network_fee
FROM
settlement_scores ss
-- If there are reported scores,
-- there will always be a record of auction participants
JOIN auction_participation ap ON ss.auction_id = ap.auction_id
-- 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}}
),
reward_per_auction AS (
SELECT
Expand All @@ -237,39 +222,10 @@ reward_per_auction AS (
{{EPSILON_UPPER}}
) AS capped_payment,
winning_score,
reference_score,
participating_solvers AS participating_solvers
reference_score
FROM
reward_data
),
participation_data AS (
SELECT
tx_hash,
block_deadline,
unnest(participating_solvers) AS participant
FROM
reward_per_auction
),
participation_data_intermediate AS (
SELECT
tx_hash,
CASE
WHEN block_deadline <= 20365510 THEN 1 -- final block deadline of accounting week of July 16 - July 23, 2024
ELSE 0
END AS count_participation,
participant
FROM
participation_data
),
participation_counts AS (
SELECT
participant AS solver,
sum(count_participation) AS num_participating_batches
FROM
participation_data_intermediate
GROUP BY
participant
),
primary_rewards AS (
SELECT
rpt.solver,
Expand Down Expand Up @@ -301,16 +257,13 @@ aggregate_partner_fees_per_solver AS (
),
aggregate_results AS (
SELECT
CONCAT('0x', encode(pc.solver, 'hex')) AS solver,
CONCAT('0x', encode(pr.solver, 'hex')) AS solver,
COALESCE(payment, 0) AS primary_reward_eth,
num_participating_batches,
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
participation_counts pc
LEFT OUTER JOIN primary_rewards pr ON pr.solver = pc.solver
FROM primary_rewards pr
LEFT OUTER JOIN aggregate_partner_fees_per_solver aif ON pr.solver = aif.solver
) --
SELECT
Expand Down
41 changes: 0 additions & 41 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_participants;
DROP TABLE IF EXISTS settlement_scores;
DROP TABLE IF EXISTS settlement_observations;
DROP TABLE IF EXISTS auction_prices;
Expand Down Expand Up @@ -27,12 +26,6 @@ CREATE TABLE IF NOT EXISTS settlements

CREATE INDEX settlements_tx_from_tx_nonce ON settlements (tx_from, tx_nonce);

CREATE TABLE IF NOT EXISTS auction_participants
(
auction_id bigint,
participant bytea
);

CREATE TABLE IF NOT EXISTS settlement_scores
(
auction_id bigint PRIMARY KEY,
Expand Down Expand Up @@ -136,7 +129,6 @@ CREATE TABLE app_data (


TRUNCATE settlements;
TRUNCATE auction_participants;
TRUNCATE settlement_scores;
TRUNCATE settlement_observations;
TRUNCATE auction_prices;
Expand Down Expand Up @@ -166,39 +158,6 @@ VALUES (1, 10, '\x5111111111111111111111111111111111111111'::bytea, '\x7111'::by
(59, 10, '\x03'::bytea, '\x09'::bytea, '\x03'::bytea, 2, 59),
(60, 10, '\x03'::bytea, '\x0a'::bytea, '\x03'::bytea, 3, 60);

INSERT INTO auction_participants (auction_id, participant)
VALUES (1, '\x5222222222222222222222222222222222222222'::bytea),
(1, '\x5333333333333333333333333333333333333333'::bytea),
(1, '\x5111111111111111111111111111111111111111'::bytea),
(2, '\x5444444444444444444444444444444444444444'::bytea),
(2, '\x5333333333333333333333333333333333333333'::bytea),
(2, '\x5222222222222222222222222222222222222222'::bytea),
(3, '\x5444444444444444444444444444444444444444'::bytea),
(3, '\x5333333333333333333333333333333333333333'::bytea),
(3, '\x5111111111111111111111111111111111111111'::bytea),
(5, '\x5444444444444444444444444444444444444444'::bytea),
(5, '\x5333333333333333333333333333333333333333'::bytea),
(5, '\x5111111111111111111111111111111111111111'::bytea),
(6, '\x5444444444444444444444444444444444444444'::bytea),
(6, '\x5333333333333333333333333333333333333333'::bytea),
(6, '\x5111111111111111111111111111111111111111'::bytea),
(7, '\x5111111111111111111111111111111111111111'::bytea),
(8, '\x5111111111111111111111111111111111111111'::bytea),
(9, '\x5444444444444444444444444444444444444444'::bytea),
(9, '\x5333333333333333333333333333333333333333'::bytea),
(9, '\x5111111111111111111111111111111111111111'::bytea),
(10, '\x5444444444444444444444444444444444444444'::bytea),
(10, '\x5333333333333333333333333333333333333333'::bytea),
(51, '\x01'::bytea),
(52, '\x02'::bytea),
(53, '\x01'::bytea),
(54, '\x02'::bytea),
(55, '\x01'::bytea),
(56, '\x02'::bytea),
(57, '\x03'::bytea),
(58, '\x03'::bytea),
(59, '\x03'::bytea),
(60, '\x03'::bytea);

INSERT INTO settlement_scores (auction_id, winning_score, reference_score, winner, block_deadline, simulation_block)
VALUES (1, 6000000000000000000, 4000000000000000000, '\x5111111111111111111111111111111111111111'::bytea, 10, 0),
Expand Down
15 changes: 0 additions & 15 deletions tests/queries/test_batch_rewards.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ def test_get_batch_rewards(self):
"0x5111111111111111111111111111111111111111",
"0x5222222222222222222222222222222222222222",
"0x5333333333333333333333333333333333333333",
"0x5444444444444444444444444444444444444444",
],
"primary_reward_eth": [
2000000000000000.0,
Expand All @@ -36,16 +35,6 @@ def test_get_batch_rewards(self):
28000000000000000.0,
12000000000000000.0,
-10000000000000000.0,
0.0,
],
"num_participating_batches": [
3,
3,
4,
7,
2,
7,
6,
],
"protocol_fee_eth": [
1250000000000000.0, # 0.5 / (1 - 0.5) * 1e18 * 5e14 / 1e18 + 0.0045 / (1 - 0.0045) * 95e18 * 5e14 / 1e18
Expand All @@ -54,7 +43,6 @@ def test_get_batch_rewards(self):
0.0,
0.0,
0.0,
0.0,
],
"network_fee_eth": [
4792548202188630.0, # around 2 * 6_000_000 * 5e26 / 1e18 - 1250000000000000.0
Expand All @@ -63,7 +51,6 @@ def test_get_batch_rewards(self):
0.0, # zero due to missing surplus fee data
0.0,
0.0,
0.0,
],
"partner_list": [
["0x90a48d5cf7343b08da12e067680b4c6dbfe551be"],
Expand All @@ -72,7 +59,6 @@ def test_get_batch_rewards(self):
None,
None,
None,
None,
],
"partner_fee_eth": [
[250000000000000.0],
Expand All @@ -81,7 +67,6 @@ def test_get_batch_rewards(self):
None,
None,
None,
None,
],
}
)
Expand Down

0 comments on commit c4f9c6c

Please sign in to comment.