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.
Add firebird_finance_optimism_base_trades (duneanalytics#6816)
* Add firebird_finance_optimism_base_trades * Refactor firebird_finance_optimism_trades
- Loading branch information
Showing
5 changed files
with
127 additions
and
114 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
73 changes: 73 additions & 0 deletions
73
.../dex/models/_projects/firebird_finance/optimism/firebird_finance_optimism_base_trades.sql
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,73 @@ | ||
{{ config( | ||
alias='base_trades', | ||
schema='firebird_finance_optimism', | ||
partition_by=['block_month'], | ||
materialized='incremental', | ||
file_format='delta', | ||
incremental_strategy='merge', | ||
unique_key=['block_date', 'blockchain', 'project', 'version', 'tx_hash', 'evt_index'] | ||
) | ||
}} | ||
|
||
{% set project_start_date = '2022-07-11' %} | ||
{% set firebird_finance_optimism_evt_trade_tables = [ | ||
source('firebird_finance_optimism', 'FireBirdRouter_evt_Swapped') | ||
] %} | ||
|
||
with dexs as ( | ||
{% for evt_trade_table in firebird_finance_optimism_evt_trade_tables %} | ||
SELECT | ||
evt_block_time AS block_time, | ||
sender AS taker, | ||
CAST(NULL as VARBINARY) AS maker, | ||
returnAmount AS token_bought_amount_raw, | ||
spentAmount AS token_sold_amount_raw, | ||
CAST(NULL AS double) AS amount_usd, | ||
dstToken AS token_bought_address, | ||
srcToken AS token_sold_address, | ||
contract_address AS project_contract_address, | ||
evt_tx_hash AS tx_hash, | ||
array[-1] AS trace_address, | ||
evt_index | ||
FROM {{ evt_trade_table }} | ||
{% if is_incremental() %} | ||
WHERE {{ incremental_predicate('evt_block_time') }} | ||
{% else %} | ||
WHERE evt_block_time >= TIMESTAMP '{{project_start_date}}' | ||
{% endif %} | ||
|
||
{% if not loop.last %} | ||
UNION ALL | ||
{% endif %} | ||
|
||
{% endfor %} | ||
) | ||
|
||
SELECT | ||
'optimism' AS blockchain, | ||
'firebird_finance' AS project, | ||
'1' AS version, | ||
TRY_CAST(date_trunc('DAY', dexs.block_time) AS date) AS block_date, | ||
TRY_CAST(date_trunc('MONTH', dexs.block_time) AS date) AS block_month, | ||
dexs.block_time, | ||
dexs.token_bought_amount_raw AS token_bought_amount_raw, | ||
dexs.token_sold_amount_raw AS token_sold_amount_raw, | ||
dexs.token_bought_address, | ||
dexs.token_sold_address, | ||
COALESCE(dexs.taker, tx."from") AS taker, | ||
dexs.maker, | ||
dexs.project_contract_address, | ||
dexs.tx_hash, | ||
tx."from" AS tx_from, | ||
tx.to AS tx_to, | ||
dexs.evt_index, | ||
dexs.trace_address | ||
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 {{ incremental_predicate('tx.block_time') }} | ||
{% endif %} |
121 changes: 9 additions & 112 deletions
121
...jects/dex/models/_projects/firebird_finance/optimism/firebird_finance_optimism_trades.sql
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 |
---|---|---|
@@ -1,118 +1,15 @@ | ||
{{ config( | ||
alias='trades', | ||
schema='firebird_finance_optimism', | ||
partition_by=['block_month'], | ||
materialized='incremental', | ||
file_format='delta', | ||
incremental_strategy='merge', | ||
unique_key=['block_date', 'blockchain', 'project', 'version', 'tx_hash', 'evt_index'], | ||
post_hook='{{ expose_spells(\'["optimism"]\', | ||
"project", | ||
"firebird_finance", | ||
\'["ARDev097"]\') }}' | ||
materialized='view', | ||
post_hook='{{ expose_spells(blockchains = \'["optimism"]\', | ||
spell_type = "project", | ||
spell_name = "firebird_finance", | ||
contributors = \'["ARDev097", "hosuke"]\') }}' | ||
) | ||
}} | ||
|
||
{% set project_start_date = '2022-07-11' %} | ||
{% set firebird_finance_optimism_evt_trade_tables = [ | ||
source('firebird_finance_optimism', 'FireBirdRouter_evt_Swapped') | ||
] %} | ||
|
||
with dexs as ( | ||
{% for evt_trade_table in firebird_finance_optimism_evt_trade_tables %} | ||
SELECT | ||
evt_block_time AS block_time, | ||
sender AS taker, | ||
CAST(NULL as VARBINARY) AS maker, | ||
returnAmount AS token_bought_amount_raw, | ||
spentAmount AS token_sold_amount_raw, | ||
CAST(NULL AS double) AS amount_usd, | ||
dstToken AS token_bought_address, | ||
srcToken AS token_sold_address, | ||
contract_address AS project_contract_address, | ||
evt_tx_hash AS tx_hash, | ||
array[-1] AS trace_address, | ||
evt_index | ||
FROM {{ evt_trade_table }} | ||
{% if is_incremental() %} | ||
WHERE evt_block_time >= date_trunc('day', now() - interval '7' day) | ||
{% else %} | ||
WHERE evt_block_time >= TIMESTAMP '{{project_start_date}}' | ||
{% endif %} | ||
|
||
{% if not loop.last %} | ||
UNION ALL | ||
{% endif %} | ||
|
||
{% endfor %} | ||
) | ||
|
||
SELECT | ||
'optimism' AS blockchain, | ||
'firebird_finance' AS project, | ||
'1' AS version, | ||
TRY_CAST(date_trunc('DAY', dexs.block_time) AS date) AS block_date, | ||
TRY_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 AS token_bought_amount_raw, | ||
dexs.token_sold_amount_raw AS token_sold_amount_raw, | ||
COALESCE( | ||
dexs.amount_usd, | ||
(dexs.token_bought_amount_raw / power(10, erc20a.decimals)) * p_bought.price, | ||
(dexs.token_sold_amount_raw / power(10, erc20b.decimals)) * p_sold.price | ||
) AS amount_usd, | ||
dexs.token_bought_address, | ||
dexs.token_sold_address, | ||
COALESCE(dexs.taker, tx."from") AS taker, | ||
dexs.maker, | ||
dexs.project_contract_address, | ||
dexs.tx_hash, | ||
tx."from" AS tx_from, | ||
tx.to AS tx_to, | ||
dexs.evt_index, | ||
dexs.trace_address | ||
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 {{ source('tokens', 'erc20') }} erc20a | ||
ON erc20a.contract_address = dexs.token_bought_address | ||
AND erc20a.blockchain = 'optimism' | ||
LEFT JOIN {{ source('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 %} | ||
|
||
SELECT * | ||
FROM {{ ref('dex_aggregator_trades') }} | ||
WHERE project = 'firebird_finance' | ||
AND blockchain = 'optimism' |
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