Skip to content

Commit

Permalink
Add firebird_finance_optimism_base_trades (duneanalytics#6816)
Browse files Browse the repository at this point in the history
* Add firebird_finance_optimism_base_trades

* Refactor firebird_finance_optimism_trades
  • Loading branch information
Hosuke committed Sep 27, 2024
1 parent 125a911 commit 92c3fd3
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 114 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,47 @@ models:
description: ""
- &trace_address
name: trace_address
description: ""
description: ""

- name: firebird_finance_optimism_base_trades
meta:
blockchain: optimism
sector: dex
project: firebird_finance
contributors: ARDev097, hosuke
config:
tags: [ 'optimism','trades', 'firebird_finance','dex' ]
description: >
firebird_finance dex base trades on optimism
tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
- block_date
- blockchain
- project
- version
- tx_hash
- evt_index
- check_dex_aggregator_seed:
blockchain: optimism
project: firebird_finance
version: 1
columns:
- *blockchain
- *project
- *version
- *block_date
- *block_time
- *token_bought_amount_raw
- *token_sold_amount_raw
- *token_bought_address
- *token_sold_address
- *taker
- *maker
- *project_contract_address
- *tx_hash
- *tx_from
- *tx_to
- *trace_address
- *evt_index
- *block_month
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 %}
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'
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

{% set base_trade_models = [
ref('lifi_base_trades')
, ref('firebird_finance_optimism_base_trades')
] %}

with base_union as (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
,ref('zeroex_trades')
,ref('kyberswap_aggregator_trades')
,ref('tokenlon_trades')
,ref('firebird_finance_optimism_trades')
,ref('oneinch_ar_trades')
,ref('unidex_optimism_trades')
,ref('odos_trades')
Expand Down

0 comments on commit 92c3fd3

Please sign in to comment.