Skip to content

Commit 3c13903

Browse files
Dex Roundtrip trades (#9027)
* made a mistake lol * Revert "made a mistake lol" This reverts commit 0a5f0d9. * test ethereum and bnb models * typo * hmmp * remove project contract address * add other chains * update pool address * simplify unique keys * force short run time * update post hook chains * fix division by zero order of operations * Revert "force short run time" This reverts commit ad98366. --------- Co-authored-by: jeff-dude <[email protected]>
1 parent d5f24e1 commit 3c13903

23 files changed

+910
-0
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
{% macro dex_roundtrip_trades(blockchain) %}
2+
3+
with
4+
5+
dex_trades as (
6+
select
7+
*,
8+
case
9+
when length(maker) = 32 then maker -- univ4 virtual pool id length, also same for euler, swaap
10+
else project_contract_address
11+
end as pool_address -- for singletons
12+
from
13+
{{ ref('dex_trades') }}
14+
where blockchain = '{{blockchain}}'
15+
{% if is_incremental() %}
16+
and {{ incremental_predicate('block_time') }}
17+
{% endif %}
18+
),
19+
20+
front_back as (
21+
select
22+
distinct
23+
front.block_time
24+
, front.pool_address
25+
, front.block_date
26+
, front.blockchain
27+
, t.tx_hash_all AS tx_hash
28+
, front.project
29+
, front.version
30+
, t.evt_index_all AS evt_index
31+
from
32+
dex_trades front
33+
inner join
34+
dex_trades back
35+
on front.block_date = back.block_date
36+
and front.project = back.project
37+
and front.version = back.version
38+
and front.pool_address = back.pool_address
39+
and front.block_number = back.block_number
40+
and front.evt_index < back.evt_index
41+
and front.tx_from = back.tx_from
42+
and front.token_sold_address = back.token_bought_address
43+
and front.token_bought_address = back.token_sold_address
44+
45+
cross join UNNEST(ARRAY[(front.tx_hash, front.evt_index), (back.tx_hash, back.evt_index)]) AS t(tx_hash_all, evt_index_all)
46+
47+
where front.token_sold_amount > 0
48+
and front.token_bought_amount > 0
49+
and back.token_sold_amount > 0
50+
and back.token_bought_amount > 0 -- sanity for division below
51+
and front.token_sold_amount / back.token_bought_amount BETWEEN 0.99 AND 1.01
52+
and front.token_bought_amount / back.token_sold_amount BETWEEN 0.99 AND 1.01
53+
)
54+
55+
select
56+
t.blockchain
57+
, t.project
58+
, t.version
59+
, t.block_time
60+
, t.block_date
61+
, t.block_month
62+
, t.block_number
63+
, t.token_sold_address
64+
, t.token_bought_address
65+
, t.token_sold_symbol
66+
, t.token_bought_symbol
67+
, t.maker
68+
, t.taker
69+
, t.tx_hash
70+
, t.tx_from
71+
, t.tx_to
72+
, t.project_contract_address
73+
, t.pool_address
74+
, t.token_pair
75+
, t.token_sold_amount_raw
76+
, t.token_bought_amount_raw
77+
, t.token_sold_amount
78+
, t.token_bought_amount
79+
, t.amount_usd
80+
, t.evt_index
81+
from
82+
dex_trades t
83+
inner join
84+
front_back fb
85+
on t.block_date = fb.block_date
86+
and t.blockchain = fb.blockchain
87+
and t.block_time = fb.block_time
88+
and t.tx_hash = fb.tx_hash
89+
and t.pool_address = fb.pool_address
90+
and t.evt_index = fb.evt_index
91+
and t.project = fb.project
92+
93+
{% endmacro %}

0 commit comments

Comments
 (0)