Skip to content

Commit ef3c048

Browse files
authored
1inch-U84: project_* lineage optimization & updates (#8898)
1 parent 59e1ec0 commit ef3c048

File tree

117 files changed

+1287
-1130
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

117 files changed

+1287
-1130
lines changed

dbt_subprojects/daily_spellbook/macros/project/oneinch/_meta/oneinch_mapped_contracts_macro.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
, ('0x8317c460c22a9958c27b4b6403b98d2ef4e2ad32', 'true', 'BabySwap' , 'UNDEFINED' , ['bnb'])
7171
, ('0xba12222222228d8ba445958a75a0704d566bf2c8', 'true', 'Balancer' , 'Vault' , ['ethereum','bnb','polygon','arbitrum','avalanche_c','gnosis','optimism','base'])
7272
, ('0x6093aebac87d62b1a5a4ceec91204e35020e38be', 'true', 'Baoswap' , 'UniswapV2Router02' , ['gnosis'])
73-
, ('0xbbbbbBB520d69a9775E85b458C58c648259FAD5F', 'true', 'Bebop' , 'BebopSettlement' , ['ethereum','arbitrum','base'])
73+
, ('0xbbbbbBB520d69a9775E85b458C58c648259FAD5F', 'true', 'Bebop' , 'BebopSettlement' , ['ethereum','arbitrum','base','optimism'])
7474
, ('0xf4D3269fACF1FfD633195715e5d1357f0d4489d5', 'true', 'Bebop' , 'BebopSettlement' , ['base'])
7575
, ('0xbebebeb035351f58602e0c1c8b59ecbff5d5f47b', 'true', 'Bebop' , 'JamSettlement' , ['arbitrum'])
7676
, ('0xb300000b72DEAEb607a12d5f54773D1C19c7028d', 'true', 'Binance' , 'DEXRouter' , ['ethereum','bnb','polygon','arbitrum','avalanche_c','base','optimism','fantom','gnosis'])
@@ -482,7 +482,7 @@
482482
, ('0x8c42cf13fbea2ac15b0fe5a5f3cf35eec65d7d7d', 'false', 'Native' , 'NativeRouterV1' , ['polygon'])
483483
, ('0x41d7b3abcfecf1f1b4b1b962da8f086114b6cc5a', 'false', 'Native' , 'NativeRouterV1' , ['base'])
484484
, ('0xc6f7a7ba5388bfb5774bfaa87d350b7793fd9ef1', 'false', 'Native' , 'NativeRouterV1' , ['mantle'])
485-
, ('0xd315a9c38ec871068fec378e4ce78af528c76293', 'false', 'Swaap' , 'Vault' , ['ethereum','polygon','arbitrum','linea'])
485+
, ('0xd315a9c38ec871068fec378e4ce78af528c76293', 'false', 'Swaap' , 'Vault' , ['ethereum','polygon','arbitrum','linea','optimism','avalanche_c','sonic'])
486486
, ('0x03C01Acae3D0173a93d819efDc832C7C4F153B06', 'false', 'Swaap' , 'Vault' , ['bnb','base'])
487487
, ('0xa57bd00134b2850b2a1c55860c9e9ea100fdd6cf', 'false', 'MEVBot' , 'MEVBot' , ['ethereum'])
488488
, ('0xa69babef1ca67a37ffaf7a485dfff3382056e78c', 'false', 'MEVBot' , 'MEVBot' , ['ethereum'])

dbt_subprojects/daily_spellbook/macros/project/oneinch/project/cfg/oneinch_project_orders_cfg_methods_macro.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -741,7 +741,7 @@
741741
"taker_asset": "substr(input, 4 + 32*4 + 12 + 1 , 20)",
742742
"maker_max_amount": "substr(input, 4 + 32*7 + 1 , 32)",
743743
"taker_max_amount": "substr(input, 4 + 32*6 + 1 , 32)",
744-
"taking_amount": "substr(input, 4 + 32*12 + 1 , 32)",
744+
"taking_amount": "if(bytearray_to_uint256(substr(input, 4 + 32*12 + 1, 32)) = 0, substr(input, 4 + 32*6 + 1, 32), substr(input, 4 + 32*12 + 1, 32))",
745745
"receiver": "substr(input, 4 + 32*8 + 12 + 1 , 20)",
746746
"order_hash": "substr(input, 4 + 32*10 + 1 , 16)",
747747
}] %}
@@ -765,7 +765,7 @@
765765
"taker_asset": "substr(input, " ~ _beginning ~ " + " ~ _taker_tokens ~ " + 32*(" ~ _taker_tokens_number ~ "+2) + 12 + 1, 20)",
766766
"maker_max_amount": "substr(input, " ~ _signatures ~ " - 32*3 + 1, 32)",
767767
"taker_max_amount": "substr(input, " ~ _beginning ~ " + " ~ _taker_amounts ~ " + 32*(" ~ _taker_amounts_number ~ "+2) + 1, 32)",
768-
"taking_amount": "substr(input, 4 + 32*2 + 1, 32)",
768+
"taking_amount": "if(bytearray_to_uint256(substr(input, 4 + 32*2 + 1, 32)) = 0, substr(input, " ~ _beginning ~ " + " ~ _taker_amounts ~ " + 32*(" ~ _taker_amounts_number ~ "+2) + 1, 32), substr(input, 4 + 32*2 + 1, 32))",
769769
"deadline": "substr(input, " ~ _beginning ~ " + 32*0 + 1, 32)",
770770
"order_hash": "substr(input, " ~ _beginning ~ " + 32*10 + 1, 16)",
771771
}] %}

dbt_subprojects/daily_spellbook/macros/project/oneinch/project/oneinch_project_calls_macro.sql

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
{% macro
1+
{%- macro
22
oneinch_project_calls_macro(
3-
blockchain
4-
, date_from = '2019-01-01'
3+
blockchain,
4+
date_from = '2019-01-01'
55
)
6-
%}
6+
-%}
77

8-
{% set native_addresses = '(0x0000000000000000000000000000000000000000, 0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee)' %}
8+
{%- set native_addresses = '(0x0000000000000000000000000000000000000000, 0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee)' -%}
99

1010

1111

@@ -26,13 +26,14 @@ static as (
2626
, any_value(tag) as tag
2727
, any_value(flags) as flags
2828
from {{ ref('oneinch_' + blockchain + '_mapped_contracts') }}
29-
where
30-
project not in ('MEVBot', 'Unknown')
29+
where true
30+
and project not in ('MEVBot', 'Unknown')
3131
group by 1, 2
3232
)
3333

3434
, signatures as (
35-
select *, reduce(cross_chain_suitables, false, (r, x) -> if(position(x in lower(replace(method, '_'))) > 0, true, r), r -> r) as cross_chain_method
35+
select *
36+
, reduce(cross_chain_suitables, false, (r, x) -> if(position(x in lower(replace(method, '_'))) > 0, true, r), r -> r) as cross_chain_method
3637
from (
3738
select
3839
id as selector
@@ -65,12 +66,9 @@ static as (
6566
, success as call_success
6667
, tx_success
6768
from {{ source(blockchain, 'traces') }}
68-
where
69-
{% if is_incremental() %}
70-
{{ incremental_predicate('block_time') }}
71-
{% else %}
72-
block_time >= timestamp '{{date_from}}'
73-
{% endif %}
69+
where true
70+
and block_time >= timestamp '{{ date_from }}'
71+
{% if is_incremental() -%} and {{ incremental_predicate('block_time') }} {%- endif %}
7472
)
7573
join contracts using(call_to)
7674
join signatures using(selector)
@@ -83,12 +81,9 @@ static as (
8381
, "to" as tx_to
8482
, gas_used as tx_gas_used
8583
from {{ source(blockchain, 'transactions') }}
86-
where
87-
{% if is_incremental() %}
88-
{{ incremental_predicate('block_time') }}
89-
{% else %}
90-
block_time >= timestamp '{{date_from}}'
91-
{% endif %}
84+
where true
85+
and block_time >= timestamp '{{ date_from }}'
86+
{% if is_incremental() -%} and {{ incremental_predicate('block_time') }} {%- endif %}
9287

9388
) using(block_number, block_time, tx_hash)
9489
)
@@ -120,4 +115,4 @@ select
120115
, date(date_trunc('month', block_time)) as block_month
121116
from calls
122117

123-
{% endmacro %}
118+
{%- endmacro %}

dbt_subprojects/daily_spellbook/macros/project/oneinch/project/oneinch_project_orders_macro.sql

Lines changed: 32 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
1-
{% macro oneinch_project_orders_macro(
2-
blockchain
3-
, date_from = '2019-01-01'
4-
)%}
1+
{%- macro
2+
oneinch_project_orders_macro(
3+
blockchain,
4+
date_from = '2019-01-01'
5+
)
6+
-%}
57

68

79

8-
{% set wrapping = 'array[0x0000000000000000000000000000000000000000, 0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee, wrapped_native_token_address]' %}
10+
{%- set wrapping = 'array[0x0000000000000000000000000000000000000000, 0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee, wrapped_native_token_address]' -%}
911

1012
with
1113

1214
logs as (
1315
{% for event, event_data in oneinch_project_orders_cfg_events_macro().items() %}
1416
select
1517
'{{ blockchain }}' as blockchain
16-
, '{{event_data["project"]}}' as project
18+
, '{{ event_data["project"] }}' as project
19+
, block_date
1720
, block_number
18-
, block_time
1921
, tx_hash
2022
, tx_from
2123
, tx_to
@@ -51,25 +53,26 @@ logs as (
5153
, topic2
5254
, topic3
5355
, data
54-
, row_number() over(partition by block_number, tx_hash order by index) as log_counter
56+
, row_number() over(partition by block_number, tx_hash order by index) as trade_counter
5557
from {{ ref('oneinch_' + blockchain + '_project_orders_raw_logs') }}
56-
where
57-
topic0 = {{ event }}
58-
and {% if is_incremental() %}{{ incremental_predicate('block_time') }}
59-
{% else %}block_time >= timestamp '{{date_from}}'
60-
{% endif %}
58+
where true
59+
and topic0 = {{ event }}
60+
and block_time >= timestamp '{{ date_from }}'
61+
{% if is_incremental() %}and {{ incremental_predicate('block_time') }}{% endif %}
6162
{% if not loop.last %}union all{% endif %}
6263
{% endfor %}
6364
)
6465

6566
, calls as (
66-
select *, row_number() over(partition by block_number, tx_hash, call_to, method order by call_trace_address, call_trade) as call_trade_counter -- trade counter in the tx: there may be multiple calls and multiple trades within a call in a single transaction
67+
select *
68+
, row_number() over(partition by block_number, tx_hash, call_to, method order by call_trace_address, call_trade) as trade_counter -- trade counter in the tx: there may be multiple calls and multiple trades within a call in a single transaction
6769
from (
6870
select
6971
blockchain
7072
, project
7173
, tag
7274
, flags
75+
, block_date
7376
, block_number
7477
, block_time
7578
, tx_hash
@@ -120,6 +123,7 @@ logs as (
120123
, project
121124
, tag
122125
, flags
126+
, block_date
123127
, block_number
124128
, block_time
125129
, tx_hash
@@ -137,7 +141,7 @@ logs as (
137141
, {{ item.get("event", "null") }} as topic0
138142
, coalesce({{ item.get("number", "1") }}, 1) as call_trades -- total trades in the call
139143
, transform(sequence(1, coalesce({{ item.get("number", "1") }}, 1)), x -> map_from_entries(array[
140-
('trade', try(to_big_endian_64(x)))
144+
('trade', try(to_big_endian_64(x)))
141145
, ('maker', {{ item.get("maker", "null") }})
142146
, ('taker', {{ item.get("taker", "null") }})
143147
, ('receiver', {{ item.get("receiver", "null") }})
@@ -167,18 +171,19 @@ logs as (
167171
, output
168172
from {{ ref('oneinch_' + blockchain + '_project_orders_raw_traces') }}
169173
join (
170-
select *, address as "to"
174+
select *
175+
, address as "to"
171176
from {{ ref('oneinch_' + blockchain + '_mapped_contracts') }}
172-
where
173-
blockchain = '{{ blockchain }}'
177+
where true
178+
and blockchain = '{{ blockchain }}'
174179
and project = '{{ item["project"] }}'
175180
and coalesce({{ item.get("tag", "null") }} = tag, true)
176181
) using("to")
177-
where
178-
{% if is_incremental() %}{{ incremental_predicate('block_time') }}
179-
{% else %}block_time > greatest(first_created_at, timestamp '{{date_from}}'){% endif %}
182+
where true
180183
and substr(input, 1, 4) = {{ item["selector"] }}
181184
and {{ item.get("condition", "true") }}
185+
and block_time > greatest(first_created_at, timestamp '{{ date_from }}')
186+
{% if is_incremental() %}and {{ incremental_predicate('block_time') }}{% endif %}
182187

183188
{% if not loop.last %}union all{% endif %}
184189
{% endfor %}
@@ -187,8 +192,7 @@ logs as (
187192
)
188193

189194
, joined as (
190-
select
191-
*
195+
select *
192196
, coalesce(log_maker, call_maker, call_to) as maker
193197
, coalesce(log_taker, call_taker) as taker
194198
, coalesce(log_receiver, call_receiver) as receiver
@@ -212,15 +216,10 @@ logs as (
212216
, coalesce(log_fee_amount, call_fee_amount) as fee_amount
213217
, coalesce(log_fee_receiver, call_fee_receiver) as fee_receiver
214218
, coalesce(log_nonce, call_nonce) as order_nonce
215-
, coalesce(log_order_hash, call_order_hash, concat(tx_hash, to_big_endian_32(cast(call_trade_counter as int))), concat(tx_hash, to_big_endian_32(cast(index as int)))) as order_hash
216-
, count(*) over(partition by block_number, tx_hash, call_trace_address, call_trade) as call_trade_logs -- logs for each trade
217-
, count(*) over(partition by block_number, tx_hash, index) as log_call_trades -- trades for each log
219+
, coalesce(log_order_hash, call_order_hash, concat(tx_hash, to_utf8(array_join(call_trace_address, ',')), to_utf8('|'), substr(call_trade, 5))) as order_hash -- | required to eliminate duplicates: concat('0,1', '11') == concat('0,11', '1')
218220
from calls
219-
full join logs using(blockchain, block_number, block_time, tx_hash, topic0, project)
220-
join (
221-
select * from {{ source('oneinch', 'blockchains') }}
222-
where blockchain = '{{blockchain}}'
223-
) using(blockchain)
221+
left join logs using(blockchain, block_date, block_number, tx_hash, topic0, project, trade_counter)
222+
join (select * from {{ source('oneinch', 'blockchains') }} where blockchain = '{{blockchain}}') using(blockchain)
224223
where
225224
coalesce(call_maker = log_maker, true)
226225
and coalesce(call_taker = log_taker, true)
@@ -264,7 +263,7 @@ select
264263
, call_gas_used
265264
, call_type
266265
, call_error
267-
, coalesce(bytearray_to_bigint(call_trade), 1) as call_trade
266+
, from_big_endian_64(call_trade) as call_trade
268267
, call_trades
269268
, method
270269
, maker
@@ -303,9 +302,5 @@ select
303302
, to_unixtime(block_time) as block_unixtime
304303
, date(date_trunc('month', block_time)) as block_month
305304
from joined
306-
where
307-
call_trade_logs = 1 -- if the log was not found or if found the only one log for a trade in the call
308-
or log_call_trades = 1 -- if the call was not found or if found the only one call trade for the log
309-
or call_trade_counter = log_counter -- if found several logs
310305

311-
{% endmacro %}
306+
{%- endmacro -%}
Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
{% macro oneinch_project_orders_raw_logs_macro(
2-
blockchain
3-
, date_from = '2019-01-01'
4-
)%}
1+
{%- macro
2+
oneinch_project_orders_raw_logs_macro(
3+
blockchain,
4+
date_from = '2019-01-01'
5+
)
6+
-%}
57

68
select
79
block_number
@@ -19,15 +21,9 @@ select
1921
, row_number() over(partition by block_number, tx_hash order by index) as log_counter
2022
, date(block_time) as block_date
2123
from {{ source(blockchain, 'logs') }}
22-
where
23-
{% if is_incremental() %}
24-
{{ incremental_predicate('block_time') }}
25-
{% else %}
26-
block_time >= timestamp '{{date_from}}'
27-
{% endif %}
28-
29-
and topic0 in (
30-
{{ oneinch_project_orders_cfg_events_macro().keys() | join(', ') }}
31-
)
24+
where true
25+
and block_time >= timestamp '{{ date_from }}'
26+
and topic0 in ({{ oneinch_project_orders_cfg_events_macro().keys() | join(', ') }})
27+
{% if is_incremental() -%} and {{ incremental_predicate('block_time') }} {%- endif %}
3228

33-
{% endmacro %}
29+
{%- endmacro -%}
Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
{% macro oneinch_project_orders_raw_traces_macro(
2-
blockchain
3-
, date_from = '2019-01-01'
4-
)%}
1+
{%- macro
2+
oneinch_project_orders_raw_traces_macro(
3+
blockchain,
4+
date_from = '2019-01-01'
5+
)
6+
-%}
57

68
select
79
block_number
@@ -20,19 +22,15 @@ select
2022
, output
2123
, block_date
2224
from {{ source(blockchain, 'traces') }}
23-
where
24-
{% if is_incremental() %}
25-
{{ incremental_predicate('block_time') }}
26-
{% else %}
27-
block_time >= timestamp '{{date_from}}'
28-
{% endif %}
29-
25+
where true
26+
and block_time >= timestamp '{{ date_from }}'
3027
and substr(input, 1, 4) in (
3128
{% set selectors = [] %}
3229
{% for item in oneinch_project_orders_cfg_methods_macro() %}
3330
{% do selectors.append(item["selector"]) %}
3431
{% endfor %}
3532
{{ ','.join(selectors) }}
3633
)
34+
{% if is_incremental() -%} and {{ incremental_predicate('block_time') }} {%- endif %}
3735

38-
{% endmacro %}
36+
{%- endmacro -%}

0 commit comments

Comments
 (0)