Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

zereox - v2 macro replacements 2 #7168

Open
wants to merge 38 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
d75fb29
add/replace bnb
RantumBits Nov 20, 2024
ed9f8e2
add linea
RantumBits Nov 20, 2024
701976b
add op
RantumBits Nov 20, 2024
eed6ca5
update existing v2 models
RantumBits Nov 20, 2024
f43bd08
add op file
RantumBits Nov 20, 2024
e49e9ad
update linea seed
RantumBits Nov 20, 2024
3c780d9
update macro
RantumBits Nov 21, 2024
8ae009c
Merge branch 'main' into v2_macros2
RantumBits Nov 26, 2024
fd5ac50
Merge branch 'v2_macros2' of https://github.com/RantumBits/dune-spell…
RantumBits Nov 26, 2024
cf2be72
update macro
RantumBits Nov 26, 2024
5dcd5ec
fix endif
RantumBits Nov 26, 2024
72e3e31
version from settler to v2
RantumBits Nov 26, 2024
2f84ae8
update arbitrum to macro
RantumBits Nov 26, 2024
6b1bedd
add avax to v2 macro
RantumBits Nov 26, 2024
a68b4db
add blast, mantle
RantumBits Nov 26, 2024
0b74b87
cross chain model to v2
RantumBits Nov 26, 2024
b2f0fae
zx_v2_arb
RantumBits Nov 26, 2024
0022c77
row_number
RantumBits Nov 26, 2024
e1d4edf
taker_token_
RantumBits Nov 26, 2024
7425907
taker_token_
RantumBits Nov 26, 2024
d7cf59c
taker fix
RantumBits Nov 27, 2024
84a4bae
add date filter to setter address selection
RantumBits Nov 27, 2024
b55737e
remove join in results, add tx_to, tx_from to upstream cte
RantumBits Nov 27, 2024
4a46c56
revise taker, update linea seed with verified txs, revise taker_amount
RantumBits Nov 27, 2024
f69512d
update blast seed file with verified txs
RantumBits Nov 27, 2024
7a9ece6
better taker
RantumBits Nov 27, 2024
283fe36
add missing taker value in indirect cte
RantumBits Nov 27, 2024
c607a62
replace trade via lifi with wrong taker in internal data
RantumBits Nov 27, 2024
ceeec45
rever taker field
RantumBits Nov 27, 2024
3ff5c5c
-endif
RantumBits Nov 27, 2024
b59c0da
-endif
RantumBits Nov 27, 2024
b731a74
taker
RantumBits Nov 27, 2024
c9e86ea
add mode
RantumBits Nov 27, 2024
b428c8e
add mode seed
RantumBits Nov 27, 2024
778e8a2
remove fills cte for mode trades
RantumBits Nov 27, 2024
8fd3e13
-nulls
RantumBits Nov 27, 2024
7f57412
cast null for fills_within in mode
RantumBits Nov 27, 2024
c501ca5
adjust seed from eth to weth
RantumBits Nov 27, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 69 additions & 67 deletions dbt_subprojects/dex/macros/models/_project/zeroex/zeroex_v2.sql
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
{% macro zeroex_settler_txs_cte(blockchain, start_date) %}
WITH tbl_addresses AS (
SELECT
token_id,
"to" AS settler_address,
varbinary_to_int256 (topic1) as token_id,
bytearray_substring(logs.topic3,13,20) as settler_address,
block_time AS begin_block_time,
block_number AS begin_block_number
FROM
{{ source('nft', 'transfers') }}
{{ source( blockchain, 'logs') }}
WHERE
contract_address = 0x00000000000004533fe15556b1e086bb1a72ceae
AND blockchain = '{{ blockchain }}'
and topic0 = 0xaa94c583a45742b26ac5274d230aea34ab334ed5722264aa5673010e612bc0b2
AND block_time >= DATE '2024-04-04'
),

tbl_end_times AS (
Expand Down Expand Up @@ -82,14 +83,19 @@ WITH tbl_all_logs AS (
logs.block_time,
logs.block_number,
index,
CASE WHEN ((varbinary_substring(logs.topic2, 13, 20) = logs.tx_from) OR
(varbinary_substring(logs.topic2, 13, 20) = first_value(bytearray_substring(logs.topic1,13,20)) OVER (PARTITION BY logs.tx_hash ORDER BY index)) OR
topic0 = 0x7fcf532c15f0a6db0bd6d0e038bea71d30d808c7d98cb3bf7268a95bf5081b65)
THEN 1 END AS valid,
COALESCE(bytearray_substring(logs.topic2,13,20), first_value(bytearray_substring(logs.topic1,13,20)) OVER (PARTITION BY logs.tx_hash ORDER BY index)) AS taker,
logs.contract_address AS maker_token,
first_value(logs.contract_address) OVER (PARTITION BY logs.tx_hash ORDER BY index) AS taker_token,
first_value(try_cast(bytearray_to_uint256(bytearray_substring(DATA, 22,11)) AS int256)) OVER (PARTITION BY logs.tx_hash ORDER BY index) AS taker_amount,
case when (varbinary_substring(logs.topic2, 13, 20) in (settler_address))
and varbinary_substring(logs.topic1, 13, 20) != 0x0000000000000000000000000000000000000000
then varbinary_substring(logs.topic1, 13, 20) else tx_from
end as taker_,
case when (varbinary_substring(logs.topic1, 13, 20) in (tx_from, settler_address))
then logs.contract_address
end as taker_token_,
case when (varbinary_substring(logs.topic2, 13, 20) in (settler_address, tx_from))
then logs.contract_address
end as maker_token_,
case when (varbinary_substring(logs.topic1, 13, 20) in (tx_from, settler_address))
then try_cast(bytearray_to_uint256(bytearray_substring(DATA, 22,11)) as int256)
end as taker_amount_,
try_cast(bytearray_to_uint256(bytearray_substring(DATA, 22,11)) AS int256) AS maker_amount,
method_id,
tag,
Expand All @@ -98,7 +104,8 @@ WITH tbl_all_logs AS (
st.settler_address AS contract_address,
topic1,
topic2,
tx_to
tx_to,
tx_from
FROM
{{ source(blockchain, 'logs') }} AS logs
JOIN
Expand All @@ -114,32 +121,37 @@ WITH tbl_all_logs AS (
AND topic0 IN (0x7fcf532c15f0a6db0bd6d0e038bea71d30d808c7d98cb3bf7268a95bf5081b65,
0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef,
0xe1fffcc4923d04b559f4d29a8bfc6cda04eb5b0d3c460751c2402c5c5cc9109c)
AND topic1 != 0x0000000000000000000000000000000000000000000000000000000000000000
AND zid != 0xa00000000000000000000000

{% if is_direct %}
AND (logs.tx_to = settler_address)
{% else %}
AND (tx_to = settler_address OR varbinary_substring(logs.topic2, 13, 20) != 0x0000000000000000000000000000000000000000)
AND logs.tx_to != varbinary_substring(logs.topic1,13,20)
AND logs.tx_to != settler_address
AND (tx_from in (bytearray_substring(logs.topic2,13,20), bytearray_substring(logs.topic1,13,20))
OR tx_to in (bytearray_substring(logs.topic2,13,20), bytearray_substring(logs.topic1,13,20))
)
{% endif %}
{% if is_direct %}
AND (st.settler_address = bytearray_substring(logs.topic1,13,20)
OR st.settler_address = bytearray_substring(logs.topic2,13,20)
OR logs.tx_from = varbinary_substring(logs.topic1,13,20)
OR logs.tx_from = varbinary_substring(logs.topic2,13,20)
OR logs.tx_to = varbinary_substring(logs.topic1,13,20))
{% if not is_direct %}
AND logs.tx_to != settler_address
and ( settler_address in (bytearray_substring(logs.topic2,13,20), bytearray_substring(logs.topic1,13,20))
OR tx_from in (bytearray_substring(logs.topic2,13,20), bytearray_substring(logs.topic1,13,20))
OR tx_to in (bytearray_substring(logs.topic2,13,20), bytearray_substring(logs.topic1,13,20))
)
{% endif %}
),

tbl_valid_logs AS (
SELECT
*,
ROW_NUMBER() OVER (PARTITION BY tx_hash ORDER BY valid, index DESC) AS rn
*
,FIRST_VALUE(taker_) IGNORE NULLS OVER (PARTITION BY tx_hash ORDER BY index
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS taker
,FIRST_VALUE(taker_amount_) IGNORE NULLS OVER (PARTITION BY tx_hash ORDER BY index
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS taker_amount
,LAST_VALUE(maker_token_) IGNORE NULLS OVER (PARTITION BY tx_hash ORDER BY index
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS maker_token
,FIRST_VALUE(taker_token_) IGNORE NULLS OVER (PARTITION BY tx_hash ORDER BY index
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS taker_token
,ROW_NUMBER() OVER (PARTITION BY tx_hash ORDER BY index DESC) AS rn
FROM
tbl_all_logs
WHERE
taker_token != maker_token
)

SELECT * FROM tbl_valid_logs
Expand Down Expand Up @@ -171,30 +183,6 @@ prices AS (
{% endif %}
),

fills AS (
WITH signatures AS (
SELECT DISTINCT signature
FROM {{ source(blockchain, 'logs_decoded') }} l
JOIN tbl_trades tt ON tt.tx_hash = l.tx_hash AND l.block_time = tt.block_time AND l.block_number = tt.block_number
WHERE event_name IN ('TokenExchange', 'OtcOrderFilled', 'SellBaseToken', 'Swap', 'BuyGem', 'DODOSwap', 'SellGem', 'Submitted')
{% if is_incremental() %}
AND {{ incremental_predicate('l.block_time') }}
{% else %}
AND l.block_time >= DATE '{{start_date}}'
{% endif %}
)
SELECT tt.tx_hash, tt.block_number, tt.block_time, COUNT(*) AS fills_within
FROM {{ source(blockchain, 'logs') }} l
JOIN signatures ON signature = topic0
JOIN tbl_trades tt ON tt.tx_hash = l.tx_hash AND l.block_time = tt.block_time AND l.block_number = tt.block_number
{% if is_incremental() %}
WHERE {{ incremental_predicate('l.block_time') }}
{% else %}
WHERE l.block_time >= DATE '{{start_date}}'
{% endif %}
GROUP BY 1,2,3
),

results AS (
SELECT
'{{blockchain}}' AS blockchain,
Expand All @@ -204,10 +192,10 @@ results AS (
trades.contract_address,
method_id,
trades.tx_hash,
"from" AS tx_from,
"to" AS tx_to,
tx_from,
tx_to,
trades.index AS tx_index,
CASE WHEN varbinary_substring(tr.data,1,4) = 0x500c22bc THEN "from" ELSE taker END AS taker,
taker,
CAST(NULL AS varbinary) AS maker,
taker_token,
taker_token AS token_sold_address,
Expand All @@ -224,19 +212,9 @@ results AS (
maker_amount / POW(10,COALESCE(tm.decimals,pm.decimals)) AS maker_token_amount,
maker_amount / POW(10,COALESCE(tm.decimals,pm.decimals)) AS token_bought_amount,
maker_amount / POW(10,COALESCE(tm.decimals,pm.decimals)) * pm.price AS maker_amount,
tag,
fills_within
tag
FROM
tbl_trades trades
JOIN
{{ source(blockchain, 'transactions') }} tr ON tr.hash = trades.tx_hash AND tr.block_time = trades.block_time AND tr.block_number = trades.block_number
{% if is_incremental() %}
AND {{ incremental_predicate('tr.block_time') }}
{% else %}
AND tr.block_time >= DATE '{{start_date}}'
{% endif %}
LEFT JOIN
fills f ON f.tx_hash = trades.tx_hash AND f.block_time = trades.block_time AND f.block_number = trades.block_number
LEFT JOIN
tokens tt ON tt.blockchain = '{{blockchain}}' AND tt.contract_address = taker_token
LEFT JOIN
Expand All @@ -258,7 +236,7 @@ results_usd AS (
SELECT
'{{blockchain}}' AS blockchain,
'0x-API' AS project,
'settler' AS version,
'v2' AS version,
DATE_TRUNC('day', block_time) block_date,
DATE_TRUNC('month', block_time) AS block_month,
block_time,
Expand Down Expand Up @@ -296,4 +274,28 @@ order by block_time desc

{% macro zeroex_v2_trades_indirect(blockchain, start_date) %}
{{ zeroex_v2_trades(blockchain, start_date, false) }}
{% endmacro %}

{% macro zeroex_v2_trades_fills_count(blockchain, start_date) %}
WITH signatures AS (
SELECT DISTINCT signature
FROM {{ source(blockchain, 'logs_decoded') }} l
JOIN tbl_trades tt ON tt.tx_hash = l.tx_hash AND l.block_time = tt.block_time AND l.block_number = tt.block_number
WHERE event_name IN ('TokenExchange', 'OtcOrderFilled', 'SellBaseToken', 'Swap', 'BuyGem', 'DODOSwap', 'SellGem', 'Submitted')
{% if is_incremental() %}
AND {{ incremental_predicate('l.block_time') }}
{% else %}
AND l.block_time >= DATE '{{start_date}}'
{% endif %}
)
SELECT tt.tx_hash, tt.block_number, tt.block_time, COUNT(*) AS fills_within
FROM {{ source(blockchain, 'logs') }} l
JOIN signatures ON signature = topic0
JOIN tbl_trades tt ON tt.tx_hash = l.tx_hash AND l.block_time = tt.block_time AND l.block_number = tt.block_number
{% if is_incremental() %}
WHERE {{ incremental_predicate('l.block_time') }}
{% else %}
WHERE l.block_time >= DATE '{{start_date}}'
{% endif %}
GROUP BY 1,2,3
{% endmacro %}
Original file line number Diff line number Diff line change
Expand Up @@ -206,15 +206,15 @@ models:
name: protocol_fee_paid_eth
description: "The protocol fee paid in ETH"

- name: zeroex_arbitrum_settler_trades
- name: zeroex_v2_arbitrum_trades
meta:
blockchain: arbitrum
project: zeroex
contributors: rantum
config:
tags: ['arbitrum','0x','dex_aggregator','dex','aggregator']
description: >
0x API erc20 trades through 0x Settler contracts. No fills, only deduped transactions.
0x API erc20 trades through 0x v2 contracts. No fills, only deduped transactions.
data_tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
Expand Down
Loading