forked from duneanalytics/spellbook
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Dex Aggregator Feat: Lifi Optimism - Ready for review (duneanalytics#…
…4082) * Dex Aggregator Migration - #1 - Lifi * push dex aggregator seed * revert changes to legacy file * test fix * test * test * smol fix * Dex Aggregator Feat: Lifi Optimism * CLEAN * adjust seed
- Loading branch information
1 parent
738ae41
commit 5926f1c
Showing
10 changed files
with
347 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
version: 2 | ||
|
||
models: | ||
- name: lifi_v2_optimism_trades | ||
meta: | ||
blockchain: optimism | ||
sector: dex | ||
project: lifi_v2 | ||
contributors: Henrystats | ||
config: | ||
tags: ['optimism','lifi_v2','trades', 'lifi','dex'] | ||
description: > | ||
lifi v2 contract aggregator trades on optimism | ||
tests: | ||
- dbt_utils.unique_combination_of_columns: | ||
combination_of_columns: | ||
- block_date | ||
- blockchain | ||
- project | ||
- version | ||
- tx_hash | ||
- evt_index | ||
- trace_address | ||
- check_dex_aggregator_seed: | ||
blockchain: optimism | ||
project: lifi | ||
version: 2 | ||
columns: | ||
- &blockchain | ||
name: blockchain | ||
description: "Blockchain which the DEX is deployed" | ||
- &project | ||
name: project | ||
description: "Project name of the DEX" | ||
- &version | ||
name: version | ||
description: "Version of the contract built and deployed by the DEX project" | ||
- &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" | ||
- &token_bought_symbol | ||
name: token_bought_symbol | ||
description: "Token symbol for token bought in the transaction" | ||
- &token_sold_symbol | ||
name: token_sold_symbol | ||
description: "Token symbol for token sold in the transaction" | ||
- &token_pair | ||
name: token_pair | ||
description: "Token symbol pair for each token involved in the transaction" | ||
- &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" | ||
- &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 transaction" | ||
- &tx_to | ||
name: tx_to | ||
description: "Address which received the transaction" | ||
- &trace_address | ||
name: trace_address | ||
description: "" | ||
- &evt_index | ||
name: evt_index | ||
description: "" | ||
- &block_month | ||
name: block_month | ||
description: "UTC event block month of each DEX trade" | ||
|
||
- name: lifi_optimism_trades | ||
meta: | ||
blockchain: optimism | ||
sector: dex | ||
project: lifi | ||
contributors: Henrystats | ||
config: | ||
tags: ['optimism','dex','trades', 'lifi'] | ||
description: > | ||
lifi aggregator trades on optimism across all contracts and versions. This table will load dex trades downstream. | ||
columns: | ||
- *blockchain | ||
- *project | ||
- *version | ||
- *block_date | ||
- *block_time | ||
- *token_bought_symbol | ||
- *token_sold_symbol | ||
- *token_pair | ||
- *token_bought_amount | ||
- *token_sold_amount | ||
- *token_bought_amount_raw | ||
- *token_sold_amount_raw | ||
- *amount_usd | ||
- *token_bought_address | ||
- *token_sold_address | ||
- *taker | ||
- *maker | ||
- *project_contract_address | ||
- *tx_hash | ||
- *tx_from | ||
- *tx_to | ||
- *trace_address | ||
- *evt_index | ||
- *block_month |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
version: 2 | ||
|
||
sources: | ||
- name: lifi_optimism | ||
freshness: | ||
warn_after: { count: 12, period: hour } | ||
description: > | ||
Decoded event table for swaps on lifi | ||
tables: | ||
- name: LiFiDiamond_v2_evt_AssetSwapped | ||
loaded_at_field: evt_block_time | ||
- name: LiFiDiamond_v2_evt_LiFiSwappedGeneric | ||
loaded_at_field: evt_block_time |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
|
||
{{ config( | ||
tags=['dunesql'], | ||
alias = alias('trades') | ||
) | ||
}} | ||
|
||
{% set lifi_models = [ | ||
ref('lifi_v2_optimism_trades') | ||
] %} | ||
|
||
|
||
SELECT * | ||
FROM ( | ||
{% for dex_model in lifi_models %} | ||
SELECT | ||
blockchain, | ||
project, | ||
version, | ||
block_date, | ||
block_month, | ||
block_time, | ||
token_bought_symbol, | ||
token_sold_symbol, | ||
token_pair, | ||
token_bought_amount, | ||
token_sold_amount, | ||
token_bought_amount_raw, | ||
token_sold_amount_raw, | ||
amount_usd, | ||
token_bought_address, | ||
token_sold_address, | ||
taker, | ||
maker, | ||
project_contract_address, | ||
tx_hash, | ||
tx_from, | ||
tx_to, | ||
trace_address, --ensure field is explicitly cast as array<bigint> in base models | ||
evt_index | ||
FROM {{ dex_model }} | ||
{% if not loop.last %} | ||
UNION ALL | ||
{% endif %} | ||
{% endfor %} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
|
||
{{ config( | ||
tags=['legacy'], | ||
|
||
alias = alias('trades', legacy_model=True) | ||
) | ||
}} | ||
|
||
SELECT | ||
1 as dummy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
{{ config( | ||
tags=['dunesql'], | ||
schema = 'lifi_v2_optimism', | ||
alias = alias('trades'), | ||
partition_by = ['block_month'], | ||
materialized = 'incremental', | ||
file_format = 'delta', | ||
incremental_strategy = 'merge', | ||
unique_key = ['block_date', 'blockchain', 'project', 'version', 'tx_hash', 'evt_index', 'trace_address'], | ||
post_hook='{{ expose_spells(\'["optimism"]\', | ||
"project", | ||
"lifi_v2", | ||
\'["Henrystats"]\') }}' | ||
) | ||
}} | ||
|
||
{% set project_start_date = '2022-10-27' %} -- min(evet_block_time) in swapped & swapped generic events | ||
|
||
WITH | ||
|
||
{% set trade_event_tables = [ | ||
source('lifi_optimism', 'LiFiDiamond_v2_evt_AssetSwapped') | ||
,source('lifi_optimism', 'LiFiDiamond_v2_evt_LiFiSwappedGeneric') | ||
] %} | ||
|
||
dexs as ( | ||
{% for trade_tables in trade_event_tables %} | ||
SELECT | ||
evt_block_time as block_time, | ||
CAST(NULL as VARBINARY) as maker, | ||
toAmount as token_bought_amount_raw, | ||
fromAmount as token_sold_amount_raw, | ||
CAST(NULL as double) as amount_usd, | ||
CASE | ||
WHEN toAssetId IN (0x, 0x0000000000000000000000000000000000000000) | ||
THEN 0x4200000000000000000000000000000000000006 -- weth | ||
ELSE toAssetId | ||
END as token_bought_address, | ||
CASE | ||
WHEN fromAssetId IN (0x, 0x0000000000000000000000000000000000000000) | ||
THEN 0x4200000000000000000000000000000000000006 -- weth | ||
ELSE fromAssetId | ||
END as token_sold_address, | ||
contract_address as project_contract_address, | ||
evt_tx_hash as tx_hash, | ||
ARRAY[-1] AS trace_address, | ||
evt_index | ||
FROM {{ trade_tables }} p | ||
{% if is_incremental() %} | ||
WHERE p.evt_block_time >= date_trunc('day', now() - interval '7' Day) | ||
{% endif %} | ||
{% if not loop.last %} | ||
UNION ALL | ||
{% endif %} | ||
{% endfor %} | ||
) | ||
SELECT | ||
'optimism' as blockchain, | ||
'lifi' as project, | ||
'2' as version, | ||
cast(date_trunc('DAY', dexs.block_time) as date) as block_date, | ||
cast(date_trunc('MONTH', dexs.block_time) as date) as block_month, | ||
dexs.block_time, | ||
erc20a.symbol as token_bought_symbol, | ||
erc20b.symbol as token_sold_symbol, | ||
case | ||
when lower(erc20a.symbol) > lower(erc20b.symbol) then concat(erc20b.symbol, '-', erc20a.symbol) | ||
else concat(erc20a.symbol, '-', erc20b.symbol) | ||
end as token_pair, | ||
dexs.token_bought_amount_raw / power(10, erc20a.decimals) AS token_bought_amount, | ||
dexs.token_sold_amount_raw / power(10, erc20b.decimals) AS token_sold_amount, | ||
dexs.token_bought_amount_raw, | ||
dexs.token_sold_amount_raw, | ||
coalesce( | ||
dexs.amount_usd | ||
,(dexs.token_bought_amount_raw / power(10, p_bought.decimals)) * p_bought.price | ||
,(dexs.token_sold_amount_raw / power(10, p_sold.decimals)) * p_sold.price | ||
) AS amount_usd, | ||
dexs.token_bought_address, | ||
dexs.token_sold_address, | ||
tx."from" AS taker, -- no taker in swap event | ||
dexs.maker, | ||
dexs.project_contract_address, | ||
dexs.tx_hash, | ||
tx."from" AS tx_from, | ||
tx.to AS tx_to, | ||
dexs.trace_address, | ||
dexs.evt_index | ||
from dexs | ||
inner join {{ source('optimism', 'transactions') }} tx | ||
on dexs.tx_hash = tx.hash | ||
{% if not is_incremental() %} | ||
and tx.block_time >= TIMESTAMP '{{project_start_date}}' | ||
{% endif %} | ||
{% if is_incremental() %} | ||
and tx.block_time >= date_trunc('day', now() - interval '7' Day) | ||
{% endif %} | ||
left join {{ ref('tokens_erc20') }} erc20a | ||
on erc20a.contract_address = dexs.token_bought_address | ||
and erc20a.blockchain = 'optimism' | ||
left join {{ ref('tokens_erc20') }} erc20b | ||
on erc20b.contract_address = dexs.token_sold_address | ||
and erc20b.blockchain = 'optimism' | ||
left join {{ source('prices', 'usd') }} p_bought | ||
on p_bought.minute = date_trunc('minute', dexs.block_time) | ||
and p_bought.contract_address = dexs.token_bought_address | ||
and p_bought.blockchain = 'optimism' | ||
{% if not is_incremental() %} | ||
and p_bought.minute >= TIMESTAMP '{{project_start_date}}' | ||
{% endif %} | ||
{% if is_incremental() %} | ||
and p_bought.minute >= date_trunc('day', now() - interval '7' Day) | ||
{% endif %} | ||
left join {{ source('prices', 'usd') }} p_sold | ||
on p_sold.minute = date_trunc('minute', dexs.block_time) | ||
and p_sold.contract_address = dexs.token_sold_address | ||
and p_sold.blockchain = 'optimism' | ||
{% if not is_incremental() %} | ||
and p_sold.minute >= TIMESTAMP '{{project_start_date}}' | ||
{% endif %} | ||
{% if is_incremental() %} | ||
and p_sold.minute >= date_trunc('day', now() - interval '7' Day) | ||
{% endif %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{{ config( | ||
tags=['legacy'], | ||
|
||
schema = 'lifi_v2_optimism', | ||
alias = alias('trades', legacy_model=True) | ||
) | ||
}} | ||
|
||
SELECT | ||
1 as dummy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters