Skip to content

Commit

Permalink
Adapt slippage to handle protocol fees (#342)
Browse files Browse the repository at this point in the history
This PR changes the handling of protocol fees in slippage. Slippage is
now computed accounting for protocol fees in the correct token.
  • Loading branch information
fhenneke authored Feb 13, 2024
1 parent 5aae18b commit 4f101e9
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 5 deletions.
64 changes: 60 additions & 4 deletions queries/dune_v2/period_slippage.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
-- https://github.com/cowprotocol/solver-rewards/pull/340
-- Query Here: https://dune.com/queries/3427668
-- https://github.com/cowprotocol/solver-rewards/pull/342
-- Query Here: https://dune.com/queries/3427730
with
block_range as (
select
Expand Down Expand Up @@ -244,7 +244,7 @@ block_range as (
and ('{{SolverAddress}}' = '0x' or b.solver_address = from_hex('{{SolverAddress}}'))
and ('{{TxHash}}' = '0x' or b.tx_hash = from_hex('{{TxHash}}'))
)
,incoming_and_outgoing_with_internalized_imbalances as (
,incoming_and_outgoing_with_internalized_imbalances_temp as (
select * from (
select block_time,
tx_hash,
Expand All @@ -259,6 +259,62 @@ block_range as (
) as _
order by block_time
)
-- add correction for protocol fees
,raw_protocol_fee_data as (
select
order_uid,
tx_hash,
cast(cast(data.protocol_fee as varchar) as int256) as protocol_fee,
data.protocol_fee_token as protocol_fee_token,
cast(cast(data.surplus_fee as varchar) as int256) as surplus_fee,
solver,
symbol
from cowswap.raw_order_rewards ror
join tokens.erc20 t
on t.contract_address = from_hex(ror.data.protocol_fee_token)
and blockchain = 'ethereum'
where
block_number >= (select start_block from block_range) and block_number <= (select end_block from block_range)
and data.protocol_fee_native_price > 0
)
,buy_token_imbalance_due_to_protocol_fee as (
select
t.block_time as block_time,
from_hex(r.tx_hash) as tx_hash,
from_hex(r.solver) as solver_address,
symbol,
t.buy_token_address as token,
(-1) * r.protocol_fee as amount,
'protocol_fee_correction' as transfer_type
from raw_protocol_fee_data r
join cow_protocol_ethereum.trades t
on from_hex(r.order_uid) = t.order_uid and from_hex(r.tx_hash) = t.tx_hash
where t.order_type='SELL'
)
,sell_token_imbalance_due_to_protocol_fee as (
select
t.block_time as block_time,
from_hex(r.tx_hash) as tx_hash,
from_hex(r.solver) as solver_address,
symbol,
t.sell_token_address as token,
r.protocol_fee * (t.atoms_sold - r.surplus_fee) / t.atoms_bought as amount,
'protocol_fee_correction' as transfer_type
from raw_protocol_fee_data r
join cow_protocol_ethereum.trades t
on from_hex(r.order_uid) = t.order_uid and from_hex(r.tx_hash) = t.tx_hash
where t.order_type='SELL'
)
,incoming_and_outgoing_with_internalized_imbalances as (
select * from (
select * from incoming_and_outgoing_with_internalized_imbalances_temp
union all
select * from buy_token_imbalance_due_to_protocol_fee
union all
select * from sell_token_imbalance_due_to_protocol_fee
) as _
order by block_time
)
-- These batches involve a token who do not emit standard transfer events.
,excluded_batches as (
select tx_hash from filtered_trades
Expand Down Expand Up @@ -399,7 +455,7 @@ block_range as (
sum(usd_value) as usd_value,
sum(eth_slippage_wei) as eth_slippage_wei,
concat(
'<a href="https://dune.com/queries/3427668?SolverAddress=',
'<a href="https://dune.com/queries/3427730?SolverAddress=',
cast(solver_address as varchar),
'&CTE_NAME=results_per_tx',
'&StartTime={{StartTime}}',
Expand Down
2 changes: 1 addition & 1 deletion src/queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def with_params(self, params: list[QueryParameter]) -> Query:
"PERIOD_SLIPPAGE": QueryData(
name="Solver Slippage for Period",
filepath="period_slippage.sql",
q_id=3427668,
q_id=3427730,
),
"DASHBOARD_SLIPPAGE": QueryData(
name="Period Solver Rewards",
Expand Down

0 comments on commit 4f101e9

Please sign in to comment.