Skip to content

Commit

Permalink
1inch-U48: updates for project.* linage (#7159)
Browse files Browse the repository at this point in the history
* updates for project.* linage

- updated cross-chain swaps allocation
- added second side for user limits
- added counters of user swaps

* fix

* another way fix

* removed counters

* try easy dates

* fix

* fix

* added swap modes & updates & fixes

* easy dates to orders too

* optimized logical expression

* fix

* fix

* fix

* fix

* fix

* fix

* fix

* distinct to orders macro

* revert easy dates
  • Loading branch information
max-morrow authored Nov 22, 2024
1 parent 2239b92 commit f774b8d
Show file tree
Hide file tree
Showing 15 changed files with 80 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
-- this macro helps to optimize the etl in case of adding new contract to a certain chain so it triggers pipeline only for this chain
{% macro oneinch_mapped_contracts_macro(blockchain) %}



{% set
config = [
('0xc586bef4a0992c495cf22e1aeee4e446cecdee0e', 'true', '1inch' , 'OneSplit' , ['ethereum'])
Expand Down Expand Up @@ -560,7 +562,7 @@
with

contracts as (
select
select distinct
'{{blockchain}}' as blockchain
, address
, project
Expand All @@ -585,6 +587,7 @@ contracts as (
, 'CrossCurve'
, 'Stargate'
, 'Orbiter'
, 'LiFi'
], project) or position('bridge' in lower(concat(project, tag))) > 0 as cross_chain
, tag
from (values
Expand Down Expand Up @@ -617,7 +620,7 @@ select
('user', user)
, ('multi', multi)
, ('recreated', first_created_at <> last_created_at)
, ('cross_chain', cross_chain)
, ('cross_chain', cross_chain) -- a project/contract that implements a cross-chain swap protocol
]) as flags
, first_created_at
, last_created_at
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ with
static as (
select
array['swap', 'settle', 'change', 'exact', 'batch', 'trade', 'sell', 'buy', 'fill', 'route', 'zap', 'symbiosis', 'aggregate', 'multicall', 'execute', 'wrap', 'transform', 'bridge', 'outboundtransfer', 'deposit', 'start', 'transfer'] as suitables
, array['add', 'remove', 'mint', 'increase', 'decrease', 'cancel', 'destroy', 'claim', 'rescue', 'withdraw', 'simulate', 'join', 'exit', 'interaction', '721', '1155', 'nft', 'create'] as exceptions
, array['add', 'remove', 'mint', 'increase', 'decrease', 'cancel', 'destroy', 'claim', 'rescue', 'withdraw', 'simulate', 'join', 'exit', 'interaction', '721', '1155', 'nft', 'create', 'finalize'] as exceptions
, array['bridge', 'outboundtransfer', 'deposit', 'start', 'transfer'] as cross_chain_suitables
)

Expand Down Expand Up @@ -69,7 +69,6 @@ static as (
, "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
Expand Down Expand Up @@ -117,7 +116,10 @@ select
, call_trace_address
, project
, tag
, map_concat(flags, map_from_entries(array[('direct', direct), ('cross_chain_method', cross_chain_method)])) as flags
, map_concat(flags, map_from_entries(array[
('direct', cardinality(call_trace_address) = 0 or blockchain = 'zksync' and call_from = tx_from and call_to = tx_to)
, ('cross_chain_method', cross_chain_method)
])) as flags
, selector as call_selector
, method
, signature
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ logs as (
{% for event, event_data in oneinch_project_orders_cfg_events_macro().items() %}
select
'{{ blockchain }}' as blockchain
, '{{event_data["project"]}}' as project
, block_number
, block_time
, tx_hash
Expand All @@ -21,7 +22,7 @@ logs as (
, index
, contract_address
, topic0
, '{{event_data["name"]}}' as event
, '{{ event_data["name"] }}' as event
, {{ event_data.get("maker", "null") }} as log_maker
, {{ event_data.get("taker", "null") }} as log_taker
, {{ event_data.get("receiver", "null") }} as log_receiver
Expand Down Expand Up @@ -215,7 +216,7 @@ logs as (
, count(*) over(partition by block_number, tx_hash, call_trace_address, call_trade) as call_trade_logs -- logs for each trade
, count(*) over(partition by block_number, tx_hash, index) as log_call_trades -- trades for each log
from calls
full join logs using(blockchain, block_number, block_time, tx_hash, topic0)
full join logs using(blockchain, block_number, block_time, tx_hash, topic0, project)
join (
select * from {{ source('oneinch', 'blockchains') }}
where blockchain = '{{blockchain}}'
Expand Down Expand Up @@ -243,7 +244,7 @@ logs as (
-- output --

select
blockchain
distinct blockchain
, project
, tag
, map_concat(flags, map_from_entries(array[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ meta as (
, project
, order_hash
, maker
, taker
, maker_asset
, making_amount
, taker_asset
Expand All @@ -53,11 +54,12 @@ meta as (
, '1inch' as project
, coalesce(order_hash, concat(tx_hash, to_big_endian_32(cast(counter as int)))) as order_hash
, maker
, receiver as taker
, maker_asset
, making_amount
, taker_asset
, taking_amount
, flags as order_flags
, map_concat(flags, map_from_entries(array[('cross_chain', hashlock is not null)])) as order_flags
from (
select *, row_number() over(partition by block_number, tx_hash order by call_trace_address) as counter
from {{ source('oneinch_' + blockchain, 'lop') }}
Expand Down Expand Up @@ -89,6 +91,7 @@ meta as (
, order_hash
, order_flags
, maker
, taker
, replace(maker_asset, {{ zero_address }}, {{ native_address }}) as maker_asset
, making_amount
, replace(taker_asset, {{ zero_address }}, {{ native_address }}) as taker_asset
Expand All @@ -111,6 +114,7 @@ meta as (
{% endif %}
and (tx_success or tx_success is null)
and call_success
and (flags['cross_chain'] or not flags['cross_chain_method']) -- without cross-chain methods calls in non cross-chain protocols
)
left join orders using(block_number, tx_hash, call_trace_address, project)
join meta on true
Expand Down Expand Up @@ -208,6 +212,15 @@ meta as (
) -- the user_amount & caller_amount of untrusted tokens takes precedence over the call_amount of trusted tokens
, greatest(user_amount_usd_trusted, caller_amount_usd_trusted)
) as amount_usd
, coalesce(element_at(order_flags, 'fusion'), false) or coalesce(element_at(order_flags, 'auction'), false) as auction -- 1inch Fusion or any other auction
, coalesce(element_at(order_flags, 'cross_chain'), false) -- 1inch cross-chain
or coalesce(element_at(flags, 'cross_chain'), false) and not coalesce(element_at(flags, 'multi'), false) -- any suitable swap method call of exclusively cross-chain protocol
or coalesce(element_at(flags, 'cross_chain'), false) and coalesce(element_at(flags, 'cross_chain_method'), false) -- calls of exclusively cross-chain methods of any cross-chain protocol
as cross_chain_swap
, not flags['user']
or position('RFQ' in method) > 0
or coalesce(element_at(order_flags, 'partial') and not element_at(order_flags, 'multiple'), false)
as contracts_only
from (
select
blockchain
Expand Down Expand Up @@ -287,7 +300,7 @@ meta as (
and calls.tx_hash = transfers.tx_hash
and slice(transfer_trace_address, 1, cardinality(call_trace_address)) = call_trace_address -- nested transfers only
and reduce(array_distinct(call_trace_addresses), call_trace_address, (r, x) -> if(slice(transfer_trace_address, 1, cardinality(x)) = x and x > r, x, r), r -> r) = call_trace_address -- transfers related to the call only
and (order_hash is null or contract_address in (_maker_asset, _taker_asset) and maker in (transfer_from, transfer_to)) -- transfers related to the order only
and (order_hash is null or contract_address in (_maker_asset, _taker_asset) and cardinality(array_intersect(array[call_from, maker, taker], array[transfer_from, transfer_to])) > 0) -- transfers related to the order only
left join prices using(contract_address, minute)
left join tokens using(contract_address)
left join trusted_tokens using(contract_address)
Expand All @@ -297,6 +310,35 @@ meta as (
)
)

, sides as (
select
*
, map_from_entries(array[
('classic: direct', flags['direct'] and order_hash is null and not auction and not cross_chain_swap or second_side)
, ('classic: external', not flags['direct'] and order_hash is null and not auction and not cross_chain_swap)
, ('intent: intra-chain auction', auction and not cross_chain_swap)
, ('intent: intra-chain user limit order', order_hash is not null and not auction and not cross_chain_swap and not contracts_only)
, ('intent: intra-chain contracts only', contracts_only)
, ('cross-chain', cross_chain_swap)
]) as modes
from (
select *, coalesce(maker, tx_from) as user, false as second_side
from swaps

union all

select *, tx_from as user, true as second_side
from swaps
where
true
and flags['direct']
and order_hash is not null -- intent
and maker is not null
and not auction
and not cross_chain_swap
)
)

-- output --

select
Expand All @@ -309,11 +351,16 @@ select
, call_trace_address
, project
, tag
, flags
, map_concat(flags, map_from_entries(array[
('intent', order_hash is not null and not second_side)
, ('auction', auction and not cross_chain_swap)
, ('cross_chain_swap', cross_chain_swap)
])) as flags
, call_selector
, method
, call_from
, call_to
, user
, order_hash
, maker
, maker_asset
Expand All @@ -338,6 +385,10 @@ select
, receivers
, date(date_trunc('month', block_time)) as block_month
, call_trade_id
from swaps
, second_side
, modes
, reduce(map_keys(modes), 0, (r, x) -> r + if(modes[x], 1, 0), r -> r) as modes_count
, reduce(map_keys(modes), 'other', (r, x) -> if(r = 'other' and modes[x], x, r), r -> r) as mode
from sides

{% endmacro %}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
file_format = 'delta',
incremental_strategy = 'merge',
incremental_predicates = [incremental_predicate('DBT_INTERNAL_DEST.block_time')],
unique_key = ['blockchain', 'block_number', 'tx_hash', 'call_trace_address', 'call_trade_id']
unique_key = ['blockchain', 'block_number', 'tx_hash', 'second_side', 'call_trace_address', 'call_trade_id']
)
}}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
file_format = 'delta',
incremental_strategy = 'merge',
incremental_predicates = [incremental_predicate('DBT_INTERNAL_DEST.block_time')],
unique_key = ['blockchain', 'block_number', 'tx_hash', 'call_trace_address', 'call_trade_id']
unique_key = ['blockchain', 'block_number', 'tx_hash', 'second_side', 'call_trace_address', 'call_trade_id']
)
}}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
file_format = 'delta',
incremental_strategy = 'merge',
incremental_predicates = [incremental_predicate('DBT_INTERNAL_DEST.block_time')],
unique_key = ['blockchain', 'block_number', 'tx_hash', 'call_trace_address', 'call_trade_id']
unique_key = ['blockchain', 'block_number', 'tx_hash', 'second_side', 'call_trace_address', 'call_trade_id']
)
}}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
file_format = 'delta',
incremental_strategy = 'merge',
incremental_predicates = [incremental_predicate('DBT_INTERNAL_DEST.block_time')],
unique_key = ['blockchain', 'block_number', 'tx_hash', 'call_trace_address', 'call_trade_id']
unique_key = ['blockchain', 'block_number', 'tx_hash', 'second_side', 'call_trace_address', 'call_trade_id']
)
}}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
file_format = 'delta',
incremental_strategy = 'merge',
incremental_predicates = [incremental_predicate('DBT_INTERNAL_DEST.block_time')],
unique_key = ['blockchain', 'block_number', 'tx_hash', 'call_trace_address', 'call_trade_id']
unique_key = ['blockchain', 'block_number', 'tx_hash', 'second_side', 'call_trace_address', 'call_trade_id']
)
}}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
file_format = 'delta',
incremental_strategy = 'merge',
incremental_predicates = [incremental_predicate('DBT_INTERNAL_DEST.block_time')],
unique_key = ['blockchain', 'block_number', 'tx_hash', 'call_trace_address', 'call_trade_id']
unique_key = ['blockchain', 'block_number', 'tx_hash', 'second_side', 'call_trace_address', 'call_trade_id']
)
}}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
file_format = 'delta',
incremental_strategy = 'merge',
incremental_predicates = [incremental_predicate('DBT_INTERNAL_DEST.block_time')],
unique_key = ['blockchain', 'block_number', 'tx_hash', 'call_trace_address', 'call_trade_id']
unique_key = ['blockchain', 'block_number', 'tx_hash', 'second_side', 'call_trace_address', 'call_trade_id']
)
}}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
schema = 'oneinch',
alias = 'project_swaps',
materialized = 'view',
unique_key = ['blockchain', 'block_number', 'tx_hash', 'call_trace_address', 'call_trade_id']
unique_key = ['blockchain', 'block_number', 'tx_hash', 'second_side', 'call_trace_address', 'call_trade_id']
)
}}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
file_format = 'delta',
incremental_strategy = 'merge',
incremental_predicates = [incremental_predicate('DBT_INTERNAL_DEST.block_time')],
unique_key = ['blockchain', 'block_number', 'tx_hash', 'call_trace_address', 'call_trade_id']
unique_key = ['blockchain', 'block_number', 'tx_hash', 'second_side', 'call_trace_address', 'call_trade_id']
)
}}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
file_format = 'delta',
incremental_strategy = 'merge',
incremental_predicates = [incremental_predicate('DBT_INTERNAL_DEST.block_time')],
unique_key = ['blockchain', 'block_number', 'tx_hash', 'call_trace_address', 'call_trade_id']
unique_key = ['blockchain', 'block_number', 'tx_hash', 'second_side', 'call_trace_address', 'call_trade_id']
)
}}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
file_format = 'delta',
incremental_strategy = 'merge',
incremental_predicates = [incremental_predicate('DBT_INTERNAL_DEST.block_time')],
unique_key = ['blockchain', 'block_number', 'tx_hash', 'call_trace_address', 'call_trade_id']
unique_key = ['blockchain', 'block_number', 'tx_hash', 'second_side', 'call_trace_address', 'call_trade_id']
)
}}

Expand Down

0 comments on commit f774b8d

Please sign in to comment.