Skip to content

Commit

Permalink
Compute volume based protocol fee in surplus token (#346)
Browse files Browse the repository at this point in the history
This PR changes the behavior of protocol fees on volume and volume based
cap be consistent with the change from
cowprotocol/services#2465.

Instead of charging volume based fees in the sell token, they are now
charged in the surplus token. This simplifies the computation of
protocol fees.

The code in dune-sync needs to be changed as well.
  • Loading branch information
fhenneke authored Mar 7, 2024
1 parent 4f101e9 commit 6572151
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
16 changes: 9 additions & 7 deletions queries/orderbook/batch_rewards.sql
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ 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),
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
)
Expand All @@ -96,12 +96,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
Expand Down Expand Up @@ -268,4 +270,4 @@ select
from
aggregate_results
order by
solver
solver
4 changes: 2 additions & 2 deletions tests/queries/test_batch_rewards.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def test_get_batch_rewards(self):
"0x5444444444444444444444444444444444444444",
],
"payment_eth": [
9.534313722772278e15,
9535064849462312.0,
10.5e15,
6600000000000000.00000,
12450000000000000.00000,
Expand All @@ -53,7 +53,7 @@ def test_get_batch_rewards(self):
6,
],
"protocol_fee_eth": [
5.748876684972541e14, # 0.5 / (1 - 0.5) * 1e18 * 5e14 / 1e18 + 0.0015 / (1 + 0.0015) * 1e8 * 5e26 / 1e18
571357035553330.0, # 0.5 / (1 - 0.5) * 1e18 * 5e14 / 1e18 + 0.0015 / (1 - 0.0015) * 95e18 * 5e14 / 1e18
2.0198019801980198e15, # 0.75 / (1 - 0.75) * 1e6 * 5e26 / 1e18 + 0.01 / (1 + 0.01) * 105e6 * 5e26 / 1e18
0.0,
0.0,
Expand Down

0 comments on commit 6572151

Please sign in to comment.