Skip to content

Commit

Permalink
Add missing transfers for sDAI deposits and withdraws (#327)
Browse files Browse the repository at this point in the history
Conversions DAI<>sDAI are not correctly accounted for in the current
slippage query. This has led to two settlements with computes slippage
even though the slippage was actually small.

-
https://etherscan.io/tx/0xdae82500c69c66db4e4a8c64e1d6a95f3cdc5cb81a5a00228ce6f247b9b8cefd
-
https://etherscan.io/tx/0x8dc62a04eb6b42e571c19cccf7d6503c83f8b1add671ba5b5a9c4c8404b1042a

The reason is that `Withdraw` and `Deposit` events are only accompanied
by _one_ transfer. But in terms buffer changes they correspond to _two_
transfers.


[Tenderly](https://www.tdly.co/tx/mainnet/0xdae82500c69c66db4e4a8c64e1d6a95f3cdc5cb81a5a00228ce6f247b9b8cefd)
and
[eigenphi](https://eigenphi.io/mev/eigentx/0xdae82500c69c66db4e4a8c64e1d6a95f3cdc5cb81a5a00228ce6f247b9b8cefd)
get this wrong as well in their asset change calculations. To see what
happens to buffers one can use
[phalcon](https://explorer.phalcon.xyz/tx/eth/0xdae82500c69c66db4e4a8c64e1d6a95f3cdc5cb81a5a00228ce6f247b9b8cefd)
or look at state changes.

With the proposed change, additional transfers are created by looking at
event logs of the SavingsDAI contract.
- One AMM_IN transfer is created for each `Withdraw` event.
- One AMM_OUT transfer is created for each `Deposit` event.

This is not a general solution and only works for sDAI. Since we want to
support that token now, I added this fix to the query.
Long term it would be nice to have a more general solution.
  • Loading branch information
fhenneke authored Dec 15, 2023
1 parent b04d99e commit 7571656
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion queries/dune_v2/period_slippage.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
-- https://github.com/cowprotocol/solver-rewards/pull/322
-- https://github.com/cowprotocol/solver-rewards/pull/327
-- Query Here: https://dune.com/queries/3093726
with
batch_meta as (
Expand Down Expand Up @@ -125,6 +125,35 @@ batch_meta as (
-- ETH transfers to traders are already part of USER_OUT
and not contains(traders_out, to)
)
-- sDAI emits only one transfer event for deposits and withdrawals.
-- This reconstructs the missing transfer from event logs.
,sdai_deposit_withdrawal_transfers as (
-- withdraw events result in additional AMM_IN transfer
select
tx_hash,
0x9008d19f58aabd9ed0d60971565aa8510560ab41 as sender,
0x0000000000000000000000000000000000000000 as receiver,
contract_address as token,
cast(shares as int256) as amount_wei,
'AMM_IN' as transfer_type
from batch_meta bm
join maker_ethereum.SavingsDai_evt_Withdraw w
on w.evt_tx_hash= bm.tx_hash
where sender = 0x9008d19f58aabd9ed0d60971565aa8510560ab41
union all
-- deposit events result in additional AMM_OUT transfer
select
tx_hash,
0x0000000000000000000000000000000000000000 as sender,
0x9008d19f58aabd9ed0d60971565aa8510560ab41 as receiver,
contract_address as token,
cast(shares as int256) as amount_wei,
'AMM_OUT' as transfer_type
from batch_meta bm
join maker_ethereum.SavingsDai_evt_Deposit w
on w.evt_tx_hash= bm.tx_hash
where owner = 0x9008d19f58aabd9ed0d60971565aa8510560ab41
)
,pre_batch_transfers as (
select * from (
select * from user_in
Expand All @@ -134,6 +163,8 @@ batch_meta as (
select * from other_transfers
union all
select * from eth_transfers
union all
select * from sdai_deposit_withdrawal_transfers
) as _
order by tx_hash
)
Expand Down

0 comments on commit 7571656

Please sign in to comment.