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

Refactor amount_usd calculation in unidex_optimism.trades #6242

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
file_format='delta',
incremental_strategy='merge',
unique_key=['block_date', 'blockchain', 'project', 'version', 'tx_hash', 'evt_index'],
post_hook='{{ expose_spells(\'["optimism"]\',
"project",
"unidex",
\'["ARDev097"]\') }}'
post_hook='{{ expose_spells(blockchains = \'["optimism"]\',
spell_type = "project",
spell_name = "unidex",
contributors = \'["ARDev097"]\') }}'
)
}}

Expand All @@ -35,7 +35,7 @@ with dexs as (
evt_index
FROM {{ evt_trade_table }}
{% if is_incremental() %}
WHERE evt_block_time >= date_trunc('day', now() - interval '7' day)
WHERE {{ incremental_predicate('evt_block_time') }}
{% else %}
WHERE evt_block_time >= TIMESTAMP '{{project_start_date}}'
{% endif %}
Expand All @@ -47,6 +47,7 @@ with dexs as (
{% endfor %}
)

, trades AS (
SELECT
'optimism' AS blockchain,
'unidex' AS project,
Expand All @@ -65,11 +66,6 @@ SELECT
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,
Expand All @@ -87,31 +83,46 @@ INNER JOIN {{ source('optimism', 'transactions') }} tx
AND tx.block_time >= TIMESTAMP '{{project_start_date}}'
{% endif %}
{% if is_incremental() %}
AND tx.block_time >= date_trunc('day', now() - interval '7' day)
AND {{ incremental_predicate('tx.block_time') }}
{% 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 %}
)

, enrichments_with_prices AS (
{{
add_amount_usd(
trades_cte = 'trades'
)
}}
)

SELECT blockchain,
project,
version,
block_month,
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
FROM enrichments_with_prices
Loading