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

SPE-200 restructuring dex.trades with a macro approach #4533

Merged
merged 142 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
142 commits
Select commit Hold shift + click to select a range
59e06ab
Init dex macro structure and implement defiswap demo
Hosuke May 3, 2023
c347ef4
Reformat enrich_trades
Hosuke May 3, 2023
cbdb93b
Add schema for beta
Hosuke May 3, 2023
77f6223
Remove extra comma
Hosuke May 3, 2023
3ecab2e
Rename enrich_trades
Hosuke May 3, 2023
c91c9d8
Fix join
Hosuke May 3, 2023
f43494c
Add amount_usd
Hosuke May 3, 2023
9008809
Add amount_usd
Hosuke May 3, 2023
13ef8b7
Merge branch 'main' into restructing-dex-dev
Hosuke May 4, 2023
cd9b7df
Add equal_rowcount test
Hosuke May 4, 2023
61430bd
Fix typo
Hosuke May 4, 2023
4cdc505
Add seed and check_seed test
Hosuke May 5, 2023
35cd024
Implement uniswap v1 and seed
Hosuke May 5, 2023
559d5d7
Implement uniswap v2 and seed
Hosuke May 5, 2023
8ddf5c7
Implement uniswap v3 and seed
Hosuke May 5, 2023
0ec3839
Merge branch 'main' into restructing-dex-dev
Hosuke May 9, 2023
b774de1
Merge branch 'duneanalytics:main' into restructing-dex-dev
Hosuke Oct 5, 2023
d60f549
Migrate defiswap_ethereum_base_trades
Hosuke Oct 5, 2023
d74ec39
Migrate uniswap_ethereum_*_base_trades
Hosuke Oct 5, 2023
1741252
Remove trace_address
Hosuke Oct 5, 2023
44e437b
Add dune sql tag
Hosuke Oct 5, 2023
4c237c4
Fix amount_usd
Hosuke Oct 5, 2023
f3d13d4
Fix typo
Hosuke Oct 5, 2023
ca1b40b
Migrate dex_enrich_trades macro
Hosuke Oct 5, 2023
eda7224
Remove trace_address
Hosuke Oct 5, 2023
fe39a91
Add block_month column
Hosuke Oct 5, 2023
816306d
Add block_month column
Hosuke Oct 5, 2023
af22366
Add uniswap in dex_ethereum_trades_beta
Hosuke Oct 5, 2023
3988bc8
Remove comments in jinja list
Hosuke Oct 5, 2023
807e102
Refresh seed
Hosuke Oct 5, 2023
96cafc6
Refresh seed
Hosuke Oct 5, 2023
61b6a1c
Fix seed schema
Hosuke Oct 5, 2023
ae699d7
Merge branch 'main' into restructing-dex-dev
jeff-dude Oct 5, 2023
337f02a
incremental logic, cleaner aliases in macro
jeff-dude Oct 5, 2023
9d202c1
Merge branch 'main' into restructing-dex-dev
jeff-dude Oct 6, 2023
466e623
reorg directories, clean up yml files
jeff-dude Oct 6, 2023
5425fd7
clean up project-level base trade models
jeff-dude Oct 6, 2023
a76ae09
make dex_enrich_trades cross-chain
jeff-dude Oct 6, 2023
ce75eb8
Update prices CTE to slice incremental filters only once
Hosuke Oct 7, 2023
efa41bf
Update test_equal_rowcount test name
Hosuke Oct 7, 2023
b459753
Revert "Update test_equal_rowcount test name"
Hosuke Oct 7, 2023
7be4e2b
Add equal_rowcount test in generic tests
Hosuke Oct 7, 2023
0f95f45
Add equal_rowcount test in generic tests
Hosuke Oct 7, 2023
8f7a895
Add uniswap_v3_ethereum_trades_beta
Hosuke Oct 9, 2023
08c1be8
Update uniswap_v3_ethereum_trades_beta
Hosuke Oct 9, 2023
2f9f066
Merge branch 'main' into restructing-dex-dev
Hosuke Oct 9, 2023
599a902
Simplify uniswap_v3_ethereum_trades_beta
Hosuke Oct 9, 2023
0ad6f34
Merge branch 'restructing-dex-dev' of https://github.com/Hosuke/spell…
Hosuke Oct 9, 2023
ff00618
Fix uniswap_v3_ethereum_trades_beta
Hosuke Oct 9, 2023
4f1519b
Merge branch 'main' into restructing-dex-dev
Hosuke Oct 19, 2023
2e26261
Merge branch 'main' into restructing-dex-dev
Hosuke Oct 21, 2023
c98c940
Add dex_ethereum_trades_beta.sql
Hosuke Oct 21, 2023
fe6508c
Add uniswap_trades_beta.sql
Hosuke Oct 21, 2023
0ebd5d5
Rearrange schema.yml
Hosuke Oct 21, 2023
253cf29
Add relationship test
Hosuke Oct 22, 2023
49ef011
Add uniswap_*_ethereum_trades_beta
Hosuke Oct 22, 2023
f21cf89
Add defiswap_ethereum_trades_beta
Hosuke Oct 22, 2023
ab8cde5
Add uniswap optimism trades
Hosuke Oct 23, 2023
e6d4e61
Rearrange schema.yml
Hosuke Oct 23, 2023
88e6e1f
Merge branch 'main' into restructing-dex-dev
Hosuke Oct 24, 2023
3be3970
Add uniswap_v3_polygon_trades_beta
Hosuke Oct 24, 2023
51ebd5a
Merge branch 'restructing-dex-dev' of https://github.com/Hosuke/spell…
Hosuke Oct 24, 2023
9c16cce
Rewrite defiswap_ethereum_base_trades using uniswap_v2_forked_base_tr…
Hosuke Oct 24, 2023
626f836
Rewrite uniswap_ethereum_v2_base_trades. using uniswap_v2_forked_base…
Hosuke Oct 24, 2023
e7ea84b
Rewrite v3_base_trades
Hosuke Oct 24, 2023
db3f6a9
Fix typo
Hosuke Oct 24, 2023
53748ab
Merge branch 'main' into restructing-dex-dev
Hosuke Oct 24, 2023
ea526c9
Fix uniswap_optimism_v3_base_trades
Hosuke Oct 24, 2023
331be0a
Merge branch 'restructing-dex-dev' of https://github.com/Hosuke/spell…
Hosuke Oct 24, 2023
34c2b4c
Add dex_arbitrum_trades_beta
Hosuke Oct 24, 2023
fbf921e
Add uniswap_v3_base_trades_beta
Hosuke Oct 24, 2023
d28979b
Fix config blocks
Hosuke Oct 24, 2023
8a79426
Fix config blocks
Hosuke Oct 24, 2023
762a326
Add base and celo uniswap.trades
Hosuke Oct 24, 2023
a2092aa
Refresh seed
Hosuke Oct 24, 2023
d8f0004
Fix typo
Hosuke Oct 24, 2023
03b540a
Enlarge tests acceptance range
Hosuke Oct 24, 2023
11cee1c
Remove some tests due to unsync of tables
Hosuke Oct 24, 2023
18dd6fb
Add blockchain, project, version in the base models
Hosuke Oct 25, 2023
9e66234
Add base union trades
Hosuke Oct 25, 2023
ba9c194
Simplify macro signature and functionality
Hosuke Oct 25, 2023
7ac7804
Rewrite dex_*_trades_beta using enrich_dex_trades
Hosuke Oct 25, 2023
e379cc1
Add block_number
Hosuke Oct 25, 2023
b3debfd
Add missing with
Hosuke Oct 25, 2023
1537342
Minor adjustments
Hosuke Oct 25, 2023
29ab166
Add block_number in uniswap_schema
Hosuke Oct 25, 2023
788bbcf
Add block_number
Hosuke Oct 25, 2023
36e1a91
Merge branch 'main' into restructing-dex-dev
Hosuke Oct 26, 2023
db9b1c1
Merge branch 'main' into restructing-dex-dev
jeff-dude Oct 26, 2023
56f44d0
remove versioned views, rename base trades files
jeff-dude Oct 26, 2023
685a5fc
consolidate schema files
jeff-dude Oct 26, 2023
e19711f
update downstream ref names
jeff-dude Oct 26, 2023
88bbcad
clean up model configs, remove project variables
jeff-dude Oct 26, 2023
cbbf1bd
make defiswap view crosschain, cleanup schema file
jeff-dude Oct 26, 2023
1262d53
reorg block columns, cleanup column aliases
jeff-dude Oct 26, 2023
0ef6547
update dex_<blockchain> spells to be union views
jeff-dude Oct 26, 2023
1d09704
remove some spells, schema cleanup, macro cleanup
jeff-dude Oct 26, 2023
554cb54
Fix dex_base_trades
Hosuke Oct 27, 2023
eecfadd
Add dex_base_trades schema
Hosuke Oct 27, 2023
e92a726
Merge branch 'main' into restructing-dex-dev
Hosuke Oct 29, 2023
00373ad
Init compare_dex_trades_sample test
Hosuke Oct 29, 2023
6ec25f3
Add order by block_time desc in test
Hosuke Oct 29, 2023
fea2253
Use compare_dex_trades_sample to replace equal_rowcount test
Hosuke Oct 29, 2023
a67f2e6
Add with_needed parameter
Hosuke Oct 29, 2023
6b440d9
Add missing comma
Hosuke Oct 29, 2023
d53bd68
Update test and refresh seed
Hosuke Oct 29, 2023
003bc8d
Rewrite compare_dex_trades_sample without using macro
Hosuke Oct 30, 2023
5188e7c
Recover check_seed_macro
Hosuke Oct 30, 2023
6bb6fd7
Fix typo
Hosuke Oct 30, 2023
8e03f00
Update test
Hosuke Oct 30, 2023
3b9fbfd
Update test
Hosuke Oct 30, 2023
7af6213
Update test
Hosuke Oct 30, 2023
2ea51b2
Update test
Hosuke Oct 30, 2023
bd6ff59
Update test
Hosuke Oct 30, 2023
d8231bb
Remove extra tests to keep the PR clean
Hosuke Nov 1, 2023
c626590
Merge branch 'main' into restructing-dex-dev
Hosuke Nov 7, 2023
d57a894
Merge branch 'main' into restructing-dex-dev
jeff-dude Nov 7, 2023
9c3b025
consolidate dex fork macros, rename macro
jeff-dude Nov 7, 2023
cb09d58
update downstream macro refs to new names
jeff-dude Nov 7, 2023
c0e10af
update spell naming standards
jeff-dude Nov 7, 2023
8636dd6
cleanup incr logic in views, move duplicates_rank
jeff-dude Nov 7, 2023
c5d78e0
remove base from macro names
jeff-dude Nov 7, 2023
e03bb33
Merge branch 'main' into restructing-dex-dev
jeff-dude Nov 7, 2023
730c80b
formatting
jeff-dude Nov 7, 2023
c5dd40b
Merge branch 'restructing-dex-dev' of https://github.com/Hosuke/spell…
jeff-dude Nov 7, 2023
460a605
fix config bug
jeff-dude Nov 7, 2023
01b6b60
Merge branch 'main' into restructing-dex-dev
jeff-dude Nov 7, 2023
f212cc1
update variable names to stg_ and formatting
jeff-dude Nov 7, 2023
3778344
try dex_stg_trades as a view
jeff-dude Nov 7, 2023
f00e2d5
Revert "try dex_stg_trades as a view"
jeff-dude Nov 8, 2023
daef2a3
Merge branch 'main' into restructing-dex-dev
jeff-dude Nov 8, 2023
08ec69a
update macro name back to uniswap_fork_
jeff-dude Nov 8, 2023
ac8ecc7
add incr predicates
jeff-dude Nov 8, 2023
02fd6dc
add missing incr filter
jeff-dude Nov 8, 2023
7a5b010
remove new generic test, revert old one back
jeff-dude Nov 8, 2023
5c38dd2
remove commented out tests
jeff-dude Nov 8, 2023
a6a03da
remove source count tests
jeff-dude Nov 8, 2023
5b5f205
cleanup enrich macro
jeff-dude Nov 8, 2023
52ebace
Merge branch 'main' into restructing-dex-dev
Hosuke Nov 9, 2023
9129059
Merge branch 'main' into restructing-dex-dev
jeff-dude Nov 9, 2023
d24c083
finalize naming standards
jeff-dude Nov 9, 2023
e35294e
fix variable name
jeff-dude Nov 9, 2023
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
89 changes: 89 additions & 0 deletions macros/models/_sector/dex/enrich_dex_trades.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
{% macro enrich_dex_trades(
base_trades = null
jeff-dude marked this conversation as resolved.
Show resolved Hide resolved
, tokens_erc20_model = null
, prices_model = null
)
%}

WITH base_trades as (
SELECT
*
FROM
{{ base_trades }}
{% if is_incremental() %}
WHERE
{{ incremental_predicate('block_time') }}
{% endif %}
)
, prices AS (
SELECT
blockchain
, contract_address
, minute
, price
FROM
{{ prices_model }}
{% if is_incremental() %}
WHERE
{{ incremental_predicate('minute') }}
{% endif %}
)
, enrichments AS (
SELECT
base_trades.blockchain
, base_trades.project
, base_trades.version
, base_trades.block_month
, base_trades.block_date
, base_trades.block_time
, base_trades.block_number
, erc20_bought.symbol AS token_bought_symbol
, erc20_sold.symbol AS token_sold_symbol
, case
when lower(erc20_bought.symbol) > lower(erc20_sold.symbol) then concat(erc20_sold.symbol, '-', erc20_bought.symbol)
else concat(erc20_bought.symbol, '-', erc20_sold.symbol)
end AS token_pair
, base_trades.token_bought_amount_raw / power(10, erc20_bought.decimals) AS token_bought_amount
, base_trades.token_sold_amount_raw / power(10, erc20_sold.decimals) AS token_sold_amount
, base_trades.token_bought_amount_raw
, base_trades.token_sold_amount_raw
, coalesce(
base_trades.token_bought_amount_raw / power(10, erc20_bought.decimals) * p_bought.price,
base_trades.token_sold_amount_raw / power(10, erc20_sold.decimals) * p_sold.price
) AS amount_usd
, base_trades.token_bought_address
, base_trades.token_sold_address
, coalesce(base_trades.taker, base_trades.tx_from) AS taker
, base_trades.maker
, base_trades.project_contract_address
, base_trades.tx_hash
, base_trades.tx_from
, base_trades.tx_to
, base_trades.evt_index
FROM
base_trades
LEFT JOIN
{{ tokens_erc20_model }} as erc20_bought
ON erc20_bought.contract_address = base_trades.token_bought_address
AND erc20_bought.blockchain = base_trades.blockchain
LEFT JOIN
{{ tokens_erc20_model }} as erc20_sold
ON erc20_sold.contract_address = base_trades.token_sold_address
AND erc20_sold.blockchain = base_trades.blockchain
LEFT JOIN
prices as p_bought
ON p_bought.minute = date_trunc('minute', base_trades.block_time)
AND p_bought.contract_address = base_trades.token_bought_address
AND p_bought.blockchain = base_trades.blockchain
LEFT JOIN
prices as p_sold
ON p_sold.minute = date_trunc('minute', base_trades.block_time)
AND p_sold.contract_address = base_trades.token_sold_address
AND p_sold.blockchain = base_trades.blockchain
)

select
*
from
enrichments
{% endmacro %}
108 changes: 108 additions & 0 deletions macros/models/_sector/dex/uniswap_compatible_trades.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
{% macro uniswap_compatible_v2_trades(
blockchain = null
, project = null
, version = null
, Pair_evt_Swap = null
, Factory_evt_PairCreated = null
)
%}
WITH dexs AS
(
SELECT
t.evt_block_number AS block_number
, t.evt_block_time AS block_time
, t.to AS taker
, t.contract_address AS maker
, CASE WHEN amount0Out = UINT256 '0' THEN amount1Out ELSE amount0Out END AS token_bought_amount_raw
, CASE WHEN amount0In = UINT256 '0' OR amount1Out = UINT256 '0' THEN amount1In ELSE amount0In END AS token_sold_amount_raw
, CASE WHEN amount0Out = UINT256 '0' THEN f.token1 ELSE f.token0 END AS token_bought_address
, CASE WHEN amount0In = UINT256 '0' OR amount1Out = UINT256 '0' THEN f.token1 ELSE f.token0 END AS token_sold_address
, t.contract_address AS project_contract_address
, t.evt_tx_hash AS tx_hash
, t.evt_index AS evt_index
FROM
{{ Pair_evt_Swap }} t
INNER JOIN
{{ Factory_evt_PairCreated }} f
ON f.pair = t.contract_address
{% if is_incremental() %}
WHERE
{{ incremental_predicate('t.evt_block_time') }}
{% endif %}
)

SELECT
'{{ blockchain }}' AS blockchain
, '{{ project }}' AS project
, '{{ version }}' AS version
, CAST(date_trunc('month', dexs.block_time) AS date) AS block_month
, CAST(date_trunc('day', dexs.block_time) AS date) AS block_date
, dexs.block_time
, dexs.block_number
, dexs.token_bought_amount_raw
, dexs.token_sold_amount_raw
, dexs.token_bought_address
, dexs.token_sold_address
, dexs.taker
, dexs.maker
, dexs.project_contract_address
, dexs.tx_hash
, dexs.evt_index
FROM
dexs
{% endmacro %}

{% macro uniswap_compatible_v3_trades(
blockchain = null
, project = null
, version = null
, Pair_evt_Swap = null
, Factory_evt_PoolCreated = null
)
%}
WITH dexs AS
(
SELECT
t.evt_block_number AS block_number
, t.evt_block_time AS block_time
, t.recipient AS taker
, CAST(NULL as VARBINARY) as maker
, CASE WHEN amount0 < INT256 '0' THEN abs(amount0) ELSE abs(amount1) END AS token_bought_amount_raw -- when amount0 is negative it means trader_a is buying token0 from the pool
, CASE WHEN amount0 < INT256 '0' THEN abs(amount1) ELSE abs(amount0) END AS token_sold_amount_raw
, CASE WHEN amount0 < INT256 '0' THEN f.token0 ELSE f.token1 END AS token_bought_address
, CASE WHEN amount0 < INT256 '0' THEN f.token1 ELSE f.token0 END AS token_sold_address
, t.contract_address as project_contract_address
, f.fee
, t.evt_tx_hash AS tx_hash
, t.evt_index
FROM
{{ Pair_evt_Swap }} t
INNER JOIN
{{ Factory_evt_PoolCreated }} f
ON f.pool = t.contract_address
{% if is_incremental() %}
WHERE
{{ incremental_predicate('t.evt_block_time') }}
{% endif %}
)

SELECT
'{{ blockchain }}' AS blockchain
, '{{ project }}' AS project
, '{{ version }}' AS version
, CAST(date_trunc('month', dexs.block_time) AS date) AS block_month
, CAST(date_trunc('day', dexs.block_time) AS date) AS block_date
, dexs.block_time
, dexs.block_number
, CAST(dexs.token_bought_amount_raw AS UINT256) AS token_bought_amount_raw
, CAST(dexs.token_sold_amount_raw AS UINT256) AS token_sold_amount_raw
, dexs.token_bought_address
, dexs.token_sold_address
, dexs.taker
, dexs.maker
, dexs.project_contract_address
, dexs.tx_hash
, dexs.evt_index
FROM
dexs
{% endmacro %}
133 changes: 133 additions & 0 deletions models/_sector/dex/trades/_schema.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
version: 2

models:
- name: dex_trades_beta
meta:
blockchain: arbitrum, base, bnb, celo, ethereum, optimism, polygon
sector: dex
contributors: 0xRob, hosuke, jeff-dude
config:
tags: [ 'dex', 'trades', 'beta' ]
tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
- blockchain
- project
- version
- tx_hash
- evt_index
columns:
- &blockchain
name: blockchain
description: "Blockchain which the DEX is deployed"
- &project
name: project
description: "Project name of the DEX"
tests:
- relationships:
to: ref('dex_info')
field: project
- &version
name: version
description: "Version of the contract built and deployed by the DEX project"
- &block_month
name: block_month
description: "UTC event block month of each DEX trade"
- &block_date
name: block_date
description: "UTC event block date of each DEX trade"
- &block_time
name: block_time
description: "UTC event block time of each DEX trade"
- &block_number
name: block_number
description: "Block number of each DEX trade"
- &token_bought_symbol
name: token_bought_symbol
description: "Token symbol for token bought in the trade"
- &token_sold_symbol
name: token_sold_symbol
description: "Token symbol for token sold in the trade"
- &token_pair
name: token_pair
description: "Token symbol pair for each token involved in the trade"
- &token_bought_amount
name: token_bought_amount
description: "Value of the token bought at time of execution in the original currency"
- &token_sold_amount
name: token_sold_amount
description: "Value of the token sold at time of execution in the original currency"
- &token_bought_amount_raw
name: token_bought_amount_raw
description: "Raw value of the token bought at time of execution in the original currency"
- &token_sold_amount_raw
name: token_sold_amount_raw
description: "Raw value of the token sold at time of execution in the original currency"
- &amount_usd
name: amount_usd
description: "USD value of the trade at time of execution"
tests:
- dbt_utils.accepted_range:
max_value: 1000000000 # $1b is an arbitrary number, intended to flag outlier amounts early
- &token_bought_address
name: token_bought_address
description: "Contract address of the token bought"
- &token_sold_address
name: token_sold_address
description: "Contract address of the token sold"
- &taker
name: taker
description: "Address of trader who purchased a token"
- &maker
name: maker
description: "Address of trader who sold a token"
- &project_contract_address
name: project_contract_address
description: "Project contract address which executed the trade on the blockchain"
- &tx_hash
name: tx_hash
description: "Unique transaction hash value tied to each transaction on the DEX"
- &tx_from
name: tx_from
description: "Address which initiated the trade"
- &tx_to
name: tx_to
description: "Address which received the trade"
- &evt_index
name: evt_index
description: "Index of the corresponding trade event"

- name: dex_base_trades
meta:
blockchain: arbitrum, base, bnb, celo, ethereum, optimism, polygon
sector: dex
contributors: 0xRob, hosuke, jeff-dude
config:
tags: [ 'dex' ]
tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
- blockchain
- project
- version
- tx_hash
- evt_index
columns:
- *blockchain
- *project
- *version
- *block_month
- *block_date
- *block_time
- *block_number
- *token_bought_amount_raw
- *token_sold_amount_raw
- *token_bought_address
- *token_sold_address
- *taker
- *maker
- *project_contract_address
- *tx_hash
- *evt_index
- *tx_from
- *tx_to
19 changes: 19 additions & 0 deletions models/_sector/dex/trades/arbitrum/_schema.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
version: 2

models:
- name: uniswap_v3_arbitrum_base_trades
meta:
blockchain: arbitrum
sector: dex
project: uniswap
contributors: jeff-dude, masquot, soispoke, mtitus6
config:
tags: [ 'arbitrum', 'dex', 'trades', 'uniswap', 'v3' ]
description: "uniswap arbitrum v3 base trades"
tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
- tx_hash
- evt_index

- name: dex_arbitrum_base_trades
4 changes: 4 additions & 0 deletions models/_sector/dex/trades/arbitrum/_sources.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
version: 2

sources:
- name: uniswap_v3_arbitrum
Loading