Skip to content

Commit

Permalink
1inch-U25: upgrade project_swaps & project_orders flow (duneanalytics…
Browse files Browse the repository at this point in the history
…#6155)

* huge upgrade project_swaps & project_orders flow

- fixed calls to proxy contracts
- fixed incorrect parsing contract abi

* fixes

* retrigger ci

* fix bracket

* redesigned lineage to optimize future refills

- created project_calls flow
- some fixes

* fix & added blockchains project_orders spellbooks

- fix unique keys in project_swaps & project_orders flows

* retrigger

* retrigger

* fix

* retrigger

* fix

* ambigous fix

* fix

* retrigger ci

* retrigger ci

* fix

* fix

* fix

* fix

* fix

* true dates returned

---------

Co-authored-by: grkhr <eduardgorkh@gmail.com>
  • Loading branch information
max-morrow and grkhr authored Jun 14, 2024
1 parent 6b28783 commit ac97a0a
Show file tree
Hide file tree
Showing 26 changed files with 659 additions and 184 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
, ('0xa218543cc21ee9388fa1e509f950fd127ca82155', 'true', '1inch' , 'SettlementV1' , ['fantom'])
, ('0x7f069df72b7a39bce9806e3afaf579e54d8cf2b9', 'true', '1inch' , 'SettlementV1' , ['base'])
, ('0x11de482747d1b39e599f120d526af512dd1a9326', 'true', '1inch' , 'SettlementV1' , ['zksync'])
, ('0xfb2809a5314473e1165f6b58018e20ed8f07b840', 'true', '1inch' , 'SettlementV2' , ['ethereum','bnb','polygon','arbitrum','avalanche_c','gnosis','optimism','fantom','base'])
, ('0x0cae51e1032e8461f4806e26332c030e34de3adb', 'true', 'AnySwap' , 'AnyswapV3Router' , ['arbitrum'])
, ('0x7782046601e7b9b05ca55a3899780ce6ee6b8b2b', 'true', 'AnySwap' , 'AnyswapV6Router' , ['ethereum'])
, ('0xb0731d50c681c45856bfc3f7539d5f61d4be81d8', 'true', 'AnySwap' , 'UNDEFINED' , ['avalanche_c'])
Expand Down Expand Up @@ -72,6 +73,7 @@
, ('0x99a58482bd75cbab83b27ec03ca68ff489b5788f', 'true', 'Curvefi' , 'SwapRouter' , ['ethereum'])
, ('0xbebc44782c7db0a1a60cb6fe97d0b483032ff1c7', 'true', 'Curvefi' , 'ThreepoolSwap' , ['ethereum'])
, ('0xdc24316b9ae028f1497c275eb9192a3ea0f67022', 'true', 'Curvefi' , 'StETHSwap' , ['ethereum'])
, ('0xF0d4c12A5768D806021F80a262B4d39d26C58b8D', 'true', 'Curvefi' , 'CurveRouterV1' , ['ethereum'])
, ('0x1d8b86e3d88cdb2d34688e87e72f388cb541b7c8', 'true', 'Curvefi' , 'VyperContract' , ['polygon'])
, ('0x43b4fdfd4ff969587185cdb6f0bd875c5fc83f8c', 'true', 'Curvefi' , 'Vyper' , ['ethereum'])
, ('0x0656fd85364d03b103ceeda192fb2d3906a6ac15', 'true', 'DODO' , 'DODOFeeRouteProxy' , ['bnb'])
Expand Down Expand Up @@ -111,8 +113,7 @@
, ('0x1fc3607fa67b58deddb0faf7a116f417a20c551c', 'true', 'Kyber' , 'AggregationRouter' , ['fantom'])
, ('0x546c79662e028b661dfb4767664d0273184e4dd1', 'true', 'Kyber' , 'AggregationRouter' , ['polygon'])
, ('0x617dee16b86534a5d792a4d7a62fb491b544111e', 'true', 'Kyber' , 'MetaAggregationRouterV1' , ['ethereum','polygon','fantom'])
, ('0x6131b5fae19ea4f9d964eac0408e4408b66337b5', 'true', 'Kyber' , 'MetaAggregationRouterV2' , ['ethereum','bnb','polygon','arbitrum','avalanche_c','fantom'])
, ('0x6131b5fae19ea4f9d964eac0408e4408b66337b5', 'true', 'Kyber' , 'MetaAggregationRouterV2' , ['optimism'])
, ('0x6131b5fae19ea4f9d964eac0408e4408b66337b5', 'true', 'Kyber' , 'MetaAggregationRouterV2' , ['ethereum','bnb','polygon','arbitrum','avalanche_c','optimism','fantom'])
, ('0xdf1a1b60f2d438842916c0adc43748768353ec25', 'true', 'Kyber' , 'AggregationRouterV2' , ['polygon','fantom'])
, ('0xb18d4f69627f8320619a696202ad2c430cef7c53', 'true', 'LevinSwap' , 'UniswapV2Router02' , ['gnosis'])
, ('0x1231deb6f5749ef6ce6943a275a1d3e7486f4eae', 'true', 'LiFi' , 'DiamondV2' , ['ethereum','bnb','polygon','arbitrum','avalanche_c','gnosis','optimism','fantom'])
Expand Down Expand Up @@ -358,21 +359,6 @@ contracts as (
, 'TransitSwap'
, 'ZeroEx'
], project) as multi
, contains(array[
'1inch'
, 'ZeroEx'
, 'Clipper'
, 'Hashflow'
, 'Native'
, 'Swaap'
, 'Paraswap'
], project) as lop
, contains(array[
'1inch'
, 'CoWSwap'
, 'Uniswap'
, 'Bebop'
], project) as auction
, tag
from (values
{% for row in config if blockchain in row[4] %}
Expand Down Expand Up @@ -400,7 +386,7 @@ select
, address
, project
, tag
, map_from_entries(array[('user', user), ('multi', multi), ('lop', lop), ('auction', auction)]) as flags
, map_from_entries(array[('user', user), ('multi', multi), ('recreated', first_created_at <> last_created_at)]) as flags
, first_created_at
, last_created_at
, last_creator
Expand Down
122 changes: 122 additions & 0 deletions macros/models/_project/oneinch/oneinch_project_calls_macro.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
{% macro
oneinch_project_calls_macro(
blockchain
, date_from = '2024-01-01'
)
%}

{% set native_addresses = '(0x0000000000000000000000000000000000000000, 0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee)' %}



with

static as (
select
array['swap', 'settle', 'change', 'exact', 'batch', 'trade', 'sell', 'buy', 'fill', 'route', 'zap', 'symbiosis', 'aggregate', 'multicall', 'execute', 'wrap', 'transform'] as suitables
, array['add', 'remove', 'mint', 'increase', 'decrease', 'cancel', 'destroy', 'claim', 'rescue', 'withdraw', 'simulate', 'join', 'exit', 'interaction', '721', '1155', 'nft', 'create'] as exceptions
)

, meta as (
select
wrapped_native_token_address
, native_token_symbol as native_symbol
from ({{ oneinch_blockchain_macro(blockchain) }})
)

, contracts as (
select
blockchain
, address as call_to
, any_value(project) as project
, any_value(tag) as tag
, any_value(flags) as flags
from {{ ref('oneinch_' + blockchain + '_mapped_contracts') }}
where
project not in ('MEVBot', 'Unknown')
group by 1, 2
)

, signatures as (
select
id as selector
, min(signature) as signature
, min(split_part(signature, '(', 1)) as method
from {{ source('abi', 'signatures') }}
where length(id) = 4
group by 1
)

, calls as (
select *
from (
select
block_number
, block_time
, tx_hash
, "from" as call_from
, "to" as call_to
, trace_address as call_trace_address
, cardinality(trace_address) = 0 as direct
, substr(input, 1, 4) as selector
, success as call_success
, tx_success
from {{ source(blockchain, 'traces') }}
where
{% if is_incremental() %}
{{ incremental_predicate('block_time') }}
{% else %}
block_time >= timestamp '{{date_from}}'
{% endif %}
)
join contracts using(call_to)
join signatures using(selector)
join static on true
join meta on true
join (
select
block_number
, block_time
, hash as tx_hash
, "from" as tx_from
, "to" as tx_to
, gas_used as tx_gas_used
from {{ source(blockchain, 'transactions') }}
where
{% if is_incremental() %}
{{ incremental_predicate('block_time') }}
{% else %}
block_time >= timestamp '{{date_from}}'
{% endif %}

) using(block_number, block_time, tx_hash)
where
not reduce(exceptions, false, (r, x) -> if(position(x in lower(replace(method, '_'))) > 0, true, r), r -> r) -- without "exception" methods
and reduce(suitables, false, (r, x) -> if(position(x in lower(replace(method, '_'))) > 0, true, r), r -> r) -- "suitable" methods only
)

-- output --

select
blockchain
, block_number
, block_time
, tx_hash
, tx_from
, tx_to
, tx_success
, tx_gas_used
, call_trace_address
, project
, tag
, map_concat(flags, map_from_entries(array[('direct', direct)])) as flags
, selector as call_selector
, method
, signature
, call_from
, call_to
, call_success
, date(date_trunc('month', block_time)) as block_month
from calls

{% endmacro %}
77 changes: 51 additions & 26 deletions macros/models/_project/oneinch/oneinch_project_orders_macro.sql
Original file line number Diff line number Diff line change
Expand Up @@ -600,8 +600,8 @@
"receiver": "substr(input, 4 + bytearray_to_bigint(substr(input, 4 + 24 + 1, 8)) + 32*8 + 12 + 1, 32)",
"maker_asset": "substr(input, 4 + bytearray_to_bigint(substr(input, 4 + 24 + 1, 8)) + bytearray_to_bigint(substr(input, 4 + bytearray_to_bigint(substr(input, 4 + 24 + 1, 8)) + 32*5 + 24 + 1, 8)) + 32 * least(x, bytearray_to_bigint(substr(input, 4 + bytearray_to_bigint(substr(input, 4 + 24 + 1, 8)) + 32*5 + 24 + 1, 8))) + 12 + 1, 20)",
"taker_asset": "substr(input, 4 + bytearray_to_bigint(substr(input, 4 + 24 + 1, 8)) + bytearray_to_bigint(substr(input, 4 + bytearray_to_bigint(substr(input, 4 + 24 + 1, 8)) + 32*4 + 24 + 1, 8)) + 32 * least(x, bytearray_to_bigint(substr(input, 4 + bytearray_to_bigint(substr(input, 4 + 24 + 1, 8)) + 32*4 + 24 + 1, 8))) + 12 + 1, 20)",
"maker_max_amount": "substr(input, 4 + bytearray_to_bigint(substr(input, 4 + 24 + 1, 8)) + bytearray_to_bigint(substr(input, 4 + bytearray_to_bigint(substr(input, 4 + 24 + 1, 8)) + 32*7 + 24 + 1, 8)) + 32 * least(x, bytearray_to_bigint(substr(input, 4 + bytearray_to_bigint(substr(input, 4 + 24 + 1, 8)) + 32*7 + 24 + 1, 8))) + 1, 32)",
"taker_max_amount": "substr(input, 4 + bytearray_to_bigint(substr(input, 4 + 24 + 1, 8)) + bytearray_to_bigint(substr(input, 4 + bytearray_to_bigint(substr(input, 4 + 24 + 1, 8)) + 32*6 + 24 + 1, 8)) + 32 * least(x, bytearray_to_bigint(substr(input, 4 + bytearray_to_bigint(substr(input, 4 + 24 + 1, 8)) + 32*6 + 24 + 1, 8))) + 1, 32)",
"making_amount": "substr(input, 4 + bytearray_to_bigint(substr(input, 4 + 24 + 1, 8)) + bytearray_to_bigint(substr(input, 4 + bytearray_to_bigint(substr(input, 4 + 24 + 1, 8)) + 32*7 + 24 + 1, 8)) + 32 * least(x, bytearray_to_bigint(substr(input, 4 + bytearray_to_bigint(substr(input, 4 + 24 + 1, 8)) + 32*7 + 24 + 1, 8))) + 1, 32)",
"taking_amount": "substr(input, 4 + bytearray_to_bigint(substr(input, 4 + 24 + 1, 8)) + bytearray_to_bigint(substr(input, 4 + bytearray_to_bigint(substr(input, 4 + 24 + 1, 8)) + 32*6 + 24 + 1, 8)) + 32 * least(x, bytearray_to_bigint(substr(input, 4 + bytearray_to_bigint(substr(input, 4 + 24 + 1, 8)) + 32*6 + 24 + 1, 8))) + 1, 32)",
"deadline": "substr(input, 4 + bytearray_to_bigint(substr(input, 4 + 24 + 1, 8)) + 32*1 + 24 + 1, 8)",
"nonce": "substr(input, 4 + bytearray_to_bigint(substr(input, 4 + 24 + 1, 8)) + 32*3 + 1, 32)",
"order_hash": "substr(input, 4 + bytearray_to_bigint(substr(input, 4 + 24 + 1, 8)) + 32*10 + 1, 16)",
Expand Down Expand Up @@ -783,6 +783,29 @@ logs as (
, joined as (
select
*
, coalesce(log_maker, call_maker, call_to) as maker
, coalesce(log_taker, call_taker) as taker
, coalesce(log_receiver, call_receiver) as receiver
, coalesce(log_maker_asset, call_maker_asset) as maker_asset
, coalesce(log_taker_asset, call_taker_asset) as taker_asset
, coalesce(log_maker_max_amount, call_maker_max_amount) as maker_max_amount
, coalesce(log_taker_max_amount, call_taker_max_amount) as taker_max_amount
, coalesce(log_maker_min_amount, call_maker_min_amount) as maker_min_amount
, coalesce(log_taker_min_amount, call_taker_min_amount) as taker_min_amount
, coalesce(log_making_amount, call_making_amount) as making_amount
, coalesce(log_taking_amount, call_taking_amount) as taking_amount
, coalesce(log_start, call_start) as order_start
, coalesce(log_end, call_end) as order_end
, coalesce(log_deadline, call_deadline) as order_deadline
, coalesce(log_maker_fee_amount, call_maker_fee_amount) as maker_fee_amount
, coalesce(log_taker_fee_amount, call_taker_fee_amount) as taker_fee_amount
, coalesce(log_fee_asset, call_fee_asset) as fee_asset
, coalesce(log_fee_max_amount, call_fee_max_amount) as fee_max_amount
, coalesce(log_fee_min_amount, call_fee_min_amount) as fee_min_amount
, coalesce(log_fee_amount, call_fee_amount) as fee_amount
, coalesce(log_fee_receiver, call_fee_receiver) as fee_receiver
, coalesce(log_nonce, call_nonce) as order_nonce
, coalesce(log_order_hash, call_order_hash, concat(tx_hash, to_big_endian_32(cast(call_trade_counter as int)))) as order_hash
, count(*) over(partition by blockchain, block_number, tx_hash, call_trace_address, call_trade) as trades
from calls
full join logs using(block_number, tx_hash, topic0)
Expand Down Expand Up @@ -812,7 +835,9 @@ select
blockchain
, project
, tag
, flags
, map_concat(flags, map_from_entries(array[
('auction', coalesce(order_start, uint256 '0') > uint256 '0' or project in ('CoWSwap', 'Bebop'))
])) as flags
, block_number
, block_time
, tx_hash
Expand All @@ -827,29 +852,29 @@ select
, call_error
, call_trade
, method
, coalesce(coalesce(log_maker, call_maker), call_to) as maker
, coalesce(log_taker, call_taker) as taker
, coalesce(log_receiver, call_receiver) as receiver
, coalesce(log_maker_asset, call_maker_asset) as maker_asset
, coalesce(log_taker_asset, call_taker_asset) as taker_asset
, coalesce(log_maker_max_amount, call_maker_max_amount) as maker_max_amount
, coalesce(log_taker_max_amount, call_taker_max_amount) as taker_max_amount
, coalesce(log_maker_min_amount, call_maker_min_amount) as maker_min_amount
, coalesce(log_taker_min_amount, call_taker_min_amount) as taker_min_amount
, coalesce(log_making_amount, call_making_amount) as making_amount
, coalesce(log_taking_amount, call_taking_amount) as taking_amount
, coalesce(log_start, call_start) as order_start
, coalesce(log_end, call_end) as order_end
, coalesce(log_deadline, call_deadline) as order_deadline
, coalesce(log_maker_fee_amount, call_maker_fee_amount) as maker_fee_amount
, coalesce(log_taker_fee_amount, call_taker_fee_amount) as taker_fee_amount
, coalesce(log_fee_asset, call_fee_asset) as fee_asset
, coalesce(log_fee_max_amount, call_fee_max_amount) as fee_max_amount
, coalesce(log_fee_min_amount, call_fee_min_amount) as fee_min_amount
, coalesce(log_fee_amount, call_fee_amount) as fee_amount
, coalesce(log_fee_receiver, call_fee_receiver) as fee_receiver
, coalesce(log_nonce, call_nonce) as order_nonce
, coalesce(log_order_hash, call_order_hash, to_big_endian_64(call_trade_counter)) as order_hash
, maker
, taker
, receiver
, maker_asset
, taker_asset
, maker_max_amount
, taker_max_amount
, maker_min_amount
, taker_min_amount
, coalesce(making_amount, if(order_start = uint256 '0' or order_start = order_end, maker_max_amount, maker_max_amount - cast(to_unixtime(block_time) - order_start as double) / (order_end - order_start) * (cast(maker_max_amount as double) - cast(maker_min_amount as double))), maker_max_amount, maker_min_amount) as making_amount
, coalesce(taking_amount, if(order_start = uint256 '0' or order_start = order_end, taker_max_amount, taker_max_amount - cast(to_unixtime(block_time) - order_start as double) / (order_end - order_start) * (cast(taker_max_amount as double) - cast(taker_min_amount as double))), taker_max_amount, taker_min_amount) as taking_amount
, order_start
, order_end
, order_deadline
, maker_fee_amount
, taker_fee_amount
, fee_asset
, fee_max_amount
, fee_min_amount
, fee_amount
, fee_receiver
, order_nonce
, order_hash
, array[input] as call_input
, array[output] as call_output
, index as event_index
Expand Down
Loading

0 comments on commit ac97a0a

Please sign in to comment.