From fe325405f6a0d0ef8dc8f4d403eebaaaf654bdb7 Mon Sep 17 00:00:00 2001 From: Felix Henneke Date: Thu, 7 Mar 2024 18:02:32 +0100 Subject: [PATCH 1/2] changes due to CIP-38 --- src/sql/orderbook/barn_batch_rewards.sql | 24 +++++++++++++----------- src/sql/orderbook/prod_batch_rewards.sql | 24 +++++++++++++----------- 2 files changed, 26 insertions(+), 22 deletions(-) diff --git a/src/sql/orderbook/barn_batch_rewards.sql b/src/sql/orderbook/barn_batch_rewards.sql index 06a76562..59a27f56 100644 --- a/src/sql/orderbook/barn_batch_rewards.sql +++ b/src/sql/orderbook/barn_batch_rewards.sql @@ -90,11 +90,13 @@ order_protocol_fee AS ( -- impossible anyways. This query will return a division by -- zero error in that case. LEAST( - fp.surplus_max_volume_factor / (1 - fp.surplus_max_volume_factor) * os.buy_amount, -- at most charge a fraction of volume + fp.surplus_max_volume_factor / (1 - fp.surplus_max_volume_factor) * os.buy_amount, + -- at most charge a fraction of volume fp.surplus_factor / (1 - fp.surplus_factor) * surplus -- charge a fraction of surplus ) WHEN os.kind = 'buy' THEN LEAST( - fp.surplus_max_volume_factor / (1 + fp.surplus_max_volume_factor) * os.sell_amount, -- at most charge a fraction of volume + fp.surplus_max_volume_factor / (1 + fp.surplus_max_volume_factor) * os.sell_amount, + -- at most charge a fraction of volume fp.surplus_factor / (1 - fp.surplus_factor) * surplus -- charge a fraction of surplus ) END @@ -105,7 +107,7 @@ order_protocol_fee AS ( fp.volume_factor / (1 + fp.volume_factor) * os.sell_amount END END AS protocol_fee, - os.surplus_token as protocol_fee_token + os.surplus_token AS protocol_fee_token FROM order_surplus os JOIN fee_policies fp -- contains protocol fee policy @@ -164,17 +166,17 @@ reward_data AS ( case when block_number is not null and block_number > block_deadline then 0 - else coalesce(execution_cost, 0) + else coalesce(execution_cost, 0) -- if block_number is null, execution cost is 0 end as execution_cost, case when block_number is not null and block_number > block_deadline then 0 - else coalesce(surplus, 0) + else coalesce(surplus, 0) -- if block_number is null, surplus is 0 end as surplus, case when block_number is not null and block_number > block_deadline then 0 - else coalesce(fee, 0) + else coalesce(fee, 0) -- if block_number is null, fee is 0 end as fee, -- scores winning_score, @@ -188,7 +190,7 @@ reward_data AS ( 0 ) as network_fee_correction FROM - settlement_scores ss + 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 @@ -207,14 +209,14 @@ reward_per_auction as ( surplus, 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, + surplus + protocol_fee - reference_score as uncapped_payment, -- Capped Reward = CLAMP_[-E, E + exec_cost](uncapped_reward_eth) LEAST( GREATEST( - {{EPSILON_LOWER}}, - surplus + protocol_fee + fee - network_fee_correction - reference_score + surplus + protocol_fee - reference_score ), - {{EPSILON_UPPER}} + execution_cost + {{EPSILON_UPPER}} ) as capped_payment, winning_score, reference_score, @@ -234,7 +236,7 @@ SELECT surplus :: text as surplus, protocol_fee :: text as protocol_fee, network_fee :: text as network_fee, - uncapped_payment_eth :: text as uncapped_payment_eth, + uncapped_payment :: text as uncapped_payment_eth, capped_payment :: text as capped_payment, winning_score :: text as winning_score, reference_score :: text as reference_score, diff --git a/src/sql/orderbook/prod_batch_rewards.sql b/src/sql/orderbook/prod_batch_rewards.sql index 06a76562..59a27f56 100644 --- a/src/sql/orderbook/prod_batch_rewards.sql +++ b/src/sql/orderbook/prod_batch_rewards.sql @@ -90,11 +90,13 @@ order_protocol_fee AS ( -- impossible anyways. This query will return a division by -- zero error in that case. LEAST( - fp.surplus_max_volume_factor / (1 - fp.surplus_max_volume_factor) * os.buy_amount, -- at most charge a fraction of volume + fp.surplus_max_volume_factor / (1 - fp.surplus_max_volume_factor) * os.buy_amount, + -- at most charge a fraction of volume fp.surplus_factor / (1 - fp.surplus_factor) * surplus -- charge a fraction of surplus ) WHEN os.kind = 'buy' THEN LEAST( - fp.surplus_max_volume_factor / (1 + fp.surplus_max_volume_factor) * os.sell_amount, -- at most charge a fraction of volume + fp.surplus_max_volume_factor / (1 + fp.surplus_max_volume_factor) * os.sell_amount, + -- at most charge a fraction of volume fp.surplus_factor / (1 - fp.surplus_factor) * surplus -- charge a fraction of surplus ) END @@ -105,7 +107,7 @@ order_protocol_fee AS ( fp.volume_factor / (1 + fp.volume_factor) * os.sell_amount END END AS protocol_fee, - os.surplus_token as protocol_fee_token + os.surplus_token AS protocol_fee_token FROM order_surplus os JOIN fee_policies fp -- contains protocol fee policy @@ -164,17 +166,17 @@ reward_data AS ( case when block_number is not null and block_number > block_deadline then 0 - else coalesce(execution_cost, 0) + else coalesce(execution_cost, 0) -- if block_number is null, execution cost is 0 end as execution_cost, case when block_number is not null and block_number > block_deadline then 0 - else coalesce(surplus, 0) + else coalesce(surplus, 0) -- if block_number is null, surplus is 0 end as surplus, case when block_number is not null and block_number > block_deadline then 0 - else coalesce(fee, 0) + else coalesce(fee, 0) -- if block_number is null, fee is 0 end as fee, -- scores winning_score, @@ -188,7 +190,7 @@ reward_data AS ( 0 ) as network_fee_correction FROM - settlement_scores ss + 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 @@ -207,14 +209,14 @@ reward_per_auction as ( surplus, 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, + surplus + protocol_fee - reference_score as uncapped_payment, -- Capped Reward = CLAMP_[-E, E + exec_cost](uncapped_reward_eth) LEAST( GREATEST( - {{EPSILON_LOWER}}, - surplus + protocol_fee + fee - network_fee_correction - reference_score + surplus + protocol_fee - reference_score ), - {{EPSILON_UPPER}} + execution_cost + {{EPSILON_UPPER}} ) as capped_payment, winning_score, reference_score, @@ -234,7 +236,7 @@ SELECT surplus :: text as surplus, protocol_fee :: text as protocol_fee, network_fee :: text as network_fee, - uncapped_payment_eth :: text as uncapped_payment_eth, + uncapped_payment :: text as uncapped_payment_eth, capped_payment :: text as capped_payment, winning_score :: text as winning_score, reference_score :: text as reference_score, From 267344fff89befc56b8cf7bfa00da11b522a7bb2 Mon Sep 17 00:00:00 2001 From: Felix Henneke Date: Wed, 20 Mar 2024 18:25:06 +0100 Subject: [PATCH 2/2] updated integration test --- tests/integration/test_fetch_orderbook.py | 163 ++++++++++++---------- 1 file changed, 93 insertions(+), 70 deletions(-) diff --git a/tests/integration/test_fetch_orderbook.py b/tests/integration/test_fetch_orderbook.py index de58d9ed..f526085e 100644 --- a/tests/integration/test_fetch_orderbook.py +++ b/tests/integration/test_fetch_orderbook.py @@ -152,139 +152,162 @@ def test_get_order_rewards(self): self.assertIsNone(pd.testing.assert_frame_equal(expected, rewards_df)) def test_get_batch_rewards(self): - block_number = 19184750 + block_number = 19468910 block_range = BlockRange(block_number, block_number + 25) rewards_df = OrderbookFetcher.get_batch_rewards(block_range) expected = pd.DataFrame( { "block_number": pd.Series( - [pd.NA, 19184745, 19184748, pd.NA, 19184754, 19184763], + [19468907, 19468912, pd.NA, pd.NA, 19468922, 19468924, 19468926], dtype="Int64", ), "block_deadline": [ - 19184752, - 19184754, - 19184757, - 19184760, - 19184763, - 19184771, + 19468915, + 19468919, + 19468927, + 19468928, + 19468930, + 19468933, + 19468935, ], "tx_hash": [ + "0x9b46f8197480f452bf88b928706e6a6d5c0d0fc1f90478b9869d06be8f972602", + "0x2f045a84e3acef4680aa8d27c48b4f8c7639857e6db9333edf627166c7eeb542", None, - "0xe4d32e53b60e830866c1cfdf101c60e61401078f7abdc30d50b442d131aa7bdc", - "0x49a757b3672f25edf2e2df52c9a466c4676e72e3c4c30ac385e065366629c56f", None, - "0x43699f7356df831aa04519ecd9f638303f716ab9e41c6ab1f38aacd1c9cdd3e5", - "0x321703689c7a2743c85ad91788b8ab00447de42591089d2052a3e9cf3be9d5a1", + "0x2702518fba3abac804863e64800c26615d7d14020bd0c6f9e17d98151e987354", + "0xeb4bc0d934c1b87644a52a21e182edda559a44e91c5afe6a08ec4e1fba21a626", + "0xe8f5a56521801c0978c203654940ae6dfd26e414e7018749a0456965168e8e9f", ], "solver": [ - "0xc74b656bd2ebe313d26d1ac02bcf95b137d1c857", - "0x047a2fbe8aef590d4eb8942426a24970af301875", - "0x4339889fd9dfca20a423fba011e9dff1c856caeb", - "0x4339889fd9dfca20a423fba011e9dff1c856caeb", - "0x4339889fd9dfca20a423fba011e9dff1c856caeb", - "0x6f799f4bf6c1c56fb8d890e9e0fff2934b0de157", + "0x9b7e7f21d98f21c0354035798c40e9040e25787f", + "0xbf54079c9bc879ae4dd6bc79bce11d3988fd9c2b", + "0xc9f2e6ea1637e499406986ac50ddc92401ce1f58", + "0xc9f2e6ea1637e499406986ac50ddc92401ce1f58", + "0xc9f2e6ea1637e499406986ac50ddc92401ce1f58", + "0xc9f2e6ea1637e499406986ac50ddc92401ce1f58", + "0x9b7e7f21d98f21c0354035798c40e9040e25787f", ], "execution_cost": [ + "9740476311332981", + "18230688793672800", "0", - "23960377182307338", - "40296202766873088", "0", - "163520173310226150", - "41362753732496120", + "17029862092746496", + "14696413493550464", + "14816441369559104", ], "surplus": [ + "11327896430820094", + "33419292238225043", "0", - "330605523790093503", - "14240247437360446", "0", - "90713565103966099", - "1392789908325076129", + "14062656113719218", + "9557249513545248", + "5267492734979407", ], "protocol_fee": [ + "58945874579", + "0", "0", "0", "0", "0", - "6802253860482580", "0", ], "network_fee": [ + "7715157183718636", + "18639716210784841", "0", - "13933525536949544", - "24543931321337540", "0", - "123496472802428029", - "25234871513629600", + "15011842152833827", + "21801311054855324", + "8912931423922369", ], "uncapped_payment_eth": [ - "-779040158516046364", - "336110126639916826", - "38784178758697986", - "-30158667552343962", - "195046679035667621", - "49058033529596541", + "405658669881356", + "292618683544035", + "-13360904627424245", + "-12938581202699558", + "2823075137791897", + "9345865552057182", + "1607086130097916", ], "capped_payment": [ + "405658669881356", + "292618683544035", "-10000000000000000", - "35960377182307338", - "38784178758697986", "-10000000000000000", - "175520173310226150", - "49058033529596541", + "2823075137791897", + "9345865552057182", + "1607086130097916", ], "winning_score": [ - "781981474524515321", - "322695871042736445", - "2619667276946392", - "46231730550348480", - "50580401693171328", - "1373632036694856921", + "11327955072945657", + "33419292238225043", + "15862581044125306", + "14047809923397763", + "14062656113719218", + "9557249513545248", + "5267492734979406", ], "reference_score": [ - "779040158516046364", - "8428922687126221", - "0", - "30158667552343962", - "25965612731209087", - "1368966746309109188", + "10922296706813317", + "33126673554681008", + "13360904627424245", + "12938581202699558", + "11239580975927321", + "211383961488066", + "3660406604881491", ], "participating_solvers": [ [ + "0x16c473448e770ff647c69cbe19e28528877fba1b", + "0x4fc4a61a3b99a1ad4a61b03f3752ca12b4a17646", + "0x9b7e7f21d98f21c0354035798c40e9040e25787f", + "0xb9332b6301e5983272c30dd5b48a4e3b1664511b", + "0xbf54079c9bc879ae4dd6bc79bce11d3988fd9c2b", + "0xc9f2e6ea1637e499406986ac50ddc92401ce1f58", + "0xd1508a211d98bb81195dc1f9533edcdf68adf036", + "0xd50ecb485dcf5d97266122dfed979587dd8923ac", + ], + [ + "0x16c473448e770ff647c69cbe19e28528877fba1b", + "0xbf54079c9bc879ae4dd6bc79bce11d3988fd9c2b", "0xc74b656bd2ebe313d26d1ac02bcf95b137d1c857", - "0xdd2e786980cd58acc5f64807b354c981f4094936", + "0xc9f2e6ea1637e499406986ac50ddc92401ce1f58", + "0xd1508a211d98bb81195dc1f9533edcdf68adf036", ], [ - "0x047a2fbe8aef590d4eb8942426a24970af301875", - "0x4339889fd9dfca20a423fba011e9dff1c856caeb", + "0x16c473448e770ff647c69cbe19e28528877fba1b", "0xc74b656bd2ebe313d26d1ac02bcf95b137d1c857", + "0xc9f2e6ea1637e499406986ac50ddc92401ce1f58", + "0xd1508a211d98bb81195dc1f9533edcdf68adf036", ], - ["0x4339889fd9dfca20a423fba011e9dff1c856caeb"], [ - "0x047a2fbe8aef590d4eb8942426a24970af301875", - "0x0ddcb0769a3591230caa80f85469240b71442089", "0x16c473448e770ff647c69cbe19e28528877fba1b", - "0x4339889fd9dfca20a423fba011e9dff1c856caeb", - "0x6f799f4bf6c1c56fb8d890e9e0fff2934b0de157", - "0xbf54079c9bc879ae4dd6bc79bce11d3988fd9c2b", "0xc74b656bd2ebe313d26d1ac02bcf95b137d1c857", + "0xc9f2e6ea1637e499406986ac50ddc92401ce1f58", + "0xd1508a211d98bb81195dc1f9533edcdf68adf036", ], [ - "0x047a2fbe8aef590d4eb8942426a24970af301875", "0x16c473448e770ff647c69cbe19e28528877fba1b", - "0x4339889fd9dfca20a423fba011e9dff1c856caeb", - "0x6f799f4bf6c1c56fb8d890e9e0fff2934b0de157", - "0xbf54079c9bc879ae4dd6bc79bce11d3988fd9c2b", "0xc74b656bd2ebe313d26d1ac02bcf95b137d1c857", "0xc9f2e6ea1637e499406986ac50ddc92401ce1f58", + "0xd1508a211d98bb81195dc1f9533edcdf68adf036", + ], + [ + "0xc74b656bd2ebe313d26d1ac02bcf95b137d1c857", + "0xc9f2e6ea1637e499406986ac50ddc92401ce1f58", + "0xd1508a211d98bb81195dc1f9533edcdf68adf036", ], [ - "0x047a2fbe8aef590d4eb8942426a24970af301875", "0x0ddcb0769a3591230caa80f85469240b71442089", - "0x16c473448e770ff647c69cbe19e28528877fba1b", - "0x6f799f4bf6c1c56fb8d890e9e0fff2934b0de157", + "0x4339889fd9dfca20a423fba011e9dff1c856caeb", + "0x4fc4a61a3b99a1ad4a61b03f3752ca12b4a17646", + "0x9b7e7f21d98f21c0354035798c40e9040e25787f", "0xc74b656bd2ebe313d26d1ac02bcf95b137d1c857", - "0xd50ecb485dcf5d97266122dfed979587dd8923ac", + "0xd1508a211d98bb81195dc1f9533edcdf68adf036", ], ], },