Skip to content

Commit

Permalink
1inch: hot fix to swaps spellbook (duneanalytics#5171)
Browse files Browse the repository at this point in the history
* hot fix to swaps spellbook

* retrigger ci

* fix src & dst amounts in lop swaps

- fix src & dst amounts in lop swaps (added second_side to grouping)

---------

Co-authored-by: grkhr <[email protected]>
Co-authored-by: jeff-dude <[email protected]>
  • Loading branch information
3 people authored Jan 15, 2024
1 parent e5ddc5d commit aee1aa9
Showing 1 changed file with 16 additions and 17 deletions.
33 changes: 16 additions & 17 deletions models/oneinch/oneinch_swaps.sql
Original file line number Diff line number Diff line change
Expand Up @@ -130,25 +130,24 @@ tokens as (

{% set src_condition = 'contract_address = _src_token_address' %}
{% set dst_condition = 'contract_address = _dst_token_address' %}
{% set user_condition = 'user in (transfer_from, transfer_to)' %}
{% set receiver_condition = 'receiver in (transfer_from, transfer_to)' %}
{% set symbol = 'coalesce(symbol, token_symbol)' %}
{% set decimals = 'coalesce(decimals, token_decimals)' %}

, amounts as (
select
blockchain
, block_number
, tx_hash
, call_trace_address
, block_number
, second_side

-- what the user actually gave and received, judging by the transfers
, any_value(if(transfer_native, {{ true_native_address }}, contract_address)) filter(where {{ src_condition }} and {{ user_condition }}) as _src_token_address_true
, any_value(if(transfer_native, {{ true_native_address }}, contract_address)) filter(where {{ dst_condition }} and {{ user_condition }}) as _dst_token_address_to_user
, any_value(if(transfer_native, {{ true_native_address }}, contract_address)) filter(where {{ dst_condition }} and {{ receiver_condition }}) as _dst_token_address_to_receiver
, any_value(if(transfer_native, native_token_symbol, {{ symbol }})) filter(where {{ src_condition }} and {{ user_condition }}) as _src_token_symbol_true
, any_value(if(transfer_native, native_token_symbol, {{ symbol }})) filter(where {{ dst_condition }} and {{ user_condition }}) as _dst_token_symbol_to_user
, any_value(if(transfer_native, native_token_symbol, {{ symbol }})) filter(where {{ dst_condition }} and {{ receiver_condition }}) as _dst_token_symbol_to_receiver
, any_value(if(transfer_native, {{ true_native_address }}, contract_address)) filter(where {{ src_condition }} and transfer_from = user) as _src_token_address_true
, any_value(if(transfer_native, {{ true_native_address }}, contract_address)) filter(where {{ dst_condition }} and transfer_to = user) as _dst_token_address_to_user
, any_value(if(transfer_native, {{ true_native_address }}, contract_address)) filter(where {{ dst_condition }} and transfer_to = receiver) as _dst_token_address_to_receiver
, any_value(if(transfer_native, native_token_symbol, {{ symbol }})) filter(where {{ src_condition }} and transfer_from = user) as _src_token_symbol_true
, any_value(if(transfer_native, native_token_symbol, {{ symbol }})) filter(where {{ dst_condition }} and transfer_to = user) as _dst_token_symbol_to_user
, any_value(if(transfer_native, native_token_symbol, {{ symbol }})) filter(where {{ dst_condition }} and transfer_to = receiver) as _dst_token_symbol_to_receiver
, any_value({{ decimals }}) filter(where {{ src_condition }}) as src_token_decimals
, any_value({{ decimals }}) filter(where {{ dst_condition }}) as dst_token_decimals

Expand All @@ -162,11 +161,11 @@ tokens as (
, max(amount * price / pow(10, decimals)) as transfers_amount_usd

-- $ amount from user to any
, sum(amount * if(user = transfer_from, price, -price) / pow(10, decimals)) filter(where {{ src_condition }} and {{ user_condition }}) as _amount_usd_from_user
, sum(amount * if(user = transfer_from, price, -price) / pow(10, decimals)) filter(where {{ src_condition }} and transfer_from = user) as _amount_usd_from_user
-- $ amount from any to user
, sum(amount * if(user = transfer_to, price, -price) / pow(10, decimals)) filter(where {{ dst_condition }} and {{ user_condition }}) as _amount_usd_to_user
, sum(amount * if(user = transfer_to, price, -price) / pow(10, decimals)) filter(where {{ dst_condition }} and transfer_to = user) as _amount_usd_to_user
-- $ amount from any to receiver
, sum(amount * if(receiver = transfer_to, price, -price) / pow(10, decimals)) filter(where {{ dst_condition }} and {{ receiver_condition }}) as _amount_usd_to_receiver
, sum(amount * if(receiver = transfer_to, price, -price) / pow(10, decimals)) filter(where {{ dst_condition }} and transfer_to = receiver) as _amount_usd_to_receiver

, count(distinct (contract_address, transfer_native)) as tokens -- count distinct tokens in transfers
, count(*) as transfers -- count transfers
Expand All @@ -176,10 +175,10 @@ tokens as (
{% if is_incremental() %}
where {{ incremental_predicate('block_time') }}
{% endif %}
) using(blockchain, tx_hash, call_trace_address, block_number) -- block_number is needed for performance
) using(blockchain, block_number, tx_hash, call_trace_address) -- block_number is needed for performance
left join prices using(blockchain, contract_address, minute)
left join tokens using(blockchain, contract_address)
group by 1, 2, 3, 4
group by 1, 2, 3, 4, 5
)

-- output --
Expand Down Expand Up @@ -212,11 +211,11 @@ select
, coalesce(_src_token_address_true, if(_src_token_native, {{ true_native_address }}, _src_token_address)) as src_token_address
, coalesce(_src_token_symbol_true, _src_token_symbol) as src_token_symbol
, src_token_decimals
, _src_token_amount_true as src_token_amount
, coalesce(_src_token_amount_true, src_token_amount) as src_token_amount
, coalesce(_dst_token_address_to_user, _dst_token_address_to_receiver, if(_dst_token_native, {{ true_native_address }}, _dst_token_address)) as dst_token_address
, coalesce(_dst_token_symbol_to_user, _dst_token_symbol_to_receiver, _dst_token_symbol) as dst_token_symbol
, dst_token_decimals
, _dst_token_amount_true as dst_token_amount
, coalesce(_dst_token_amount_true, dst_token_amount) as dst_token_amount
, coalesce(sources_amount_usd, transfers_amount_usd) as amount_usd -- sources $ amount first if found prices, then $ amount of connector tokens
, sources_amount_usd
, transfers_amount_usd
Expand All @@ -226,4 +225,4 @@ select
, date_trunc('minute', block_time) as minute
, date(date_trunc('month', block_time)) as block_month
from swaps
join amounts using(blockchain, tx_hash, call_trace_address, block_number)
join amounts using(blockchain, block_number, tx_hash, call_trace_address, second_side)

0 comments on commit aee1aa9

Please sign in to comment.