From 4018189649b085224a6d82175f076fc54504e0e5 Mon Sep 17 00:00:00 2001 From: Felix Henneke Date: Thu, 7 Mar 2024 18:12:41 +0100 Subject: [PATCH 1/3] copy changes from solver-rewards copy changes to volume based protocol fee from https://github.com/cowprotocol/solver-rewards/pull/346 --- src/sql/orderbook/batch_rewards.sql | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/sql/orderbook/batch_rewards.sql b/src/sql/orderbook/batch_rewards.sql index 1b06ebb0..25947bca 100644 --- a/src/sql/orderbook/batch_rewards.sql +++ b/src/sql/orderbook/batch_rewards.sql @@ -90,20 +90,24 @@ order_protocol_fee AS ( -- impossible anyways. This query will return a division by -- zero error in that case. LEAST( - fp.max_volume_factor * os.sell_amount * os.buy_amount / (os.sell_amount - os.observed_fee), -- at most charge a fraction of volume + fp.max_volume_factor / (1 - fp.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.max_volume_factor / (1 + fp.max_volume_factor) * os.sell_amount, -- at most charge a fraction of volume + fp.max_volume_factor / (1 + fp.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 - WHEN fp.kind = 'volume' THEN fp.volume_factor / (1 + fp.volume_factor) * os.sell_amount + WHEN fp.kind = 'volume' THEN CASE + WHEN os.kind = 'sell' THEN + fp.volume_factor / (1 - fp.volume_factor) * os.buy_amount + WHEN os.kind = 'buy' THEN + fp.volume_factor / (1 + fp.volume_factor) * os.sell_amount + END END AS protocol_fee, - CASE - WHEN fp.kind = 'surplus' THEN os.surplus_token - WHEN fp.kind = 'volume' THEN os.sell_token - END AS protocol_fee_token + os.surplus_token AS protocol_fee_token FROM order_surplus os JOIN fee_policies fp -- contains protocol fee policy From b7c75179cb5227437d553b9f0444f268e9504b8d Mon Sep 17 00:00:00 2001 From: harisang Date: Thu, 14 Mar 2024 14:34:26 +0200 Subject: [PATCH 2/3] update branch from main --- src/sql/orderbook/barn_batch_rewards.sql | 14 ++++++++------ src/sql/orderbook/prod_batch_rewards.sql | 14 ++++++++------ 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/src/sql/orderbook/barn_batch_rewards.sql b/src/sql/orderbook/barn_batch_rewards.sql index 11e1aa99..d2219471 100644 --- a/src/sql/orderbook/barn_batch_rewards.sql +++ b/src/sql/orderbook/barn_batch_rewards.sql @@ -90,7 +90,7 @@ order_protocol_fee AS ( -- impossible anyways. This query will return a division by -- zero error in that case. LEAST( - fp.surplus_max_volume_factor * os.sell_amount * os.buy_amount / (os.sell_amount - os.observed_fee), -- 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 ) WHEN os.kind = 'buy' THEN LEAST( @@ -98,12 +98,14 @@ order_protocol_fee AS ( fp.surplus_factor / (1 - fp.surplus_factor) * surplus -- charge a fraction of surplus ) END - WHEN fp.kind = 'volume' THEN fp.volume_factor / (1 + fp.volume_factor) * os.sell_amount + WHEN fp.kind = 'volume' THEN CASE + WHEN os.kind = 'sell' THEN + fp.volume_factor / (1 - fp.volume_factor) * os.buy_amount + WHEN os.kind = 'buy' THEN + fp.volume_factor / (1 + fp.volume_factor) * os.sell_amount + END END AS protocol_fee, - CASE - WHEN fp.kind = 'surplus' THEN os.surplus_token - WHEN fp.kind = 'volume' THEN os.sell_token - END AS protocol_fee_token + os.surplus_token as protocol_fee_token FROM order_surplus os JOIN fee_policies fp -- contains protocol fee policy diff --git a/src/sql/orderbook/prod_batch_rewards.sql b/src/sql/orderbook/prod_batch_rewards.sql index 11e1aa99..d2219471 100644 --- a/src/sql/orderbook/prod_batch_rewards.sql +++ b/src/sql/orderbook/prod_batch_rewards.sql @@ -90,7 +90,7 @@ order_protocol_fee AS ( -- impossible anyways. This query will return a division by -- zero error in that case. LEAST( - fp.surplus_max_volume_factor * os.sell_amount * os.buy_amount / (os.sell_amount - os.observed_fee), -- 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 ) WHEN os.kind = 'buy' THEN LEAST( @@ -98,12 +98,14 @@ order_protocol_fee AS ( fp.surplus_factor / (1 - fp.surplus_factor) * surplus -- charge a fraction of surplus ) END - WHEN fp.kind = 'volume' THEN fp.volume_factor / (1 + fp.volume_factor) * os.sell_amount + WHEN fp.kind = 'volume' THEN CASE + WHEN os.kind = 'sell' THEN + fp.volume_factor / (1 - fp.volume_factor) * os.buy_amount + WHEN os.kind = 'buy' THEN + fp.volume_factor / (1 + fp.volume_factor) * os.sell_amount + END END AS protocol_fee, - CASE - WHEN fp.kind = 'surplus' THEN os.surplus_token - WHEN fp.kind = 'volume' THEN os.sell_token - END AS protocol_fee_token + os.surplus_token as protocol_fee_token FROM order_surplus os JOIN fee_policies fp -- contains protocol fee policy From 5bccefea012358fbe168792f0f50cb252c5d81a1 Mon Sep 17 00:00:00 2001 From: harisang Date: Thu, 14 Mar 2024 14:36:23 +0200 Subject: [PATCH 3/3] fix merge bug --- src/sql/orderbook/barn_batch_rewards.sql | 2 +- src/sql/orderbook/prod_batch_rewards.sql | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/sql/orderbook/barn_batch_rewards.sql b/src/sql/orderbook/barn_batch_rewards.sql index d2219471..06a76562 100644 --- a/src/sql/orderbook/barn_batch_rewards.sql +++ b/src/sql/orderbook/barn_batch_rewards.sql @@ -90,7 +90,7 @@ 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.sell_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( diff --git a/src/sql/orderbook/prod_batch_rewards.sql b/src/sql/orderbook/prod_batch_rewards.sql index d2219471..06a76562 100644 --- a/src/sql/orderbook/prod_batch_rewards.sql +++ b/src/sql/orderbook/prod_batch_rewards.sql @@ -90,7 +90,7 @@ 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.sell_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(