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 yield_yak_trades macro #6235

Closed
wants to merge 4 commits into from
Closed
Changes from 1 commit
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
144 changes: 77 additions & 67 deletions dex/macros/models/_project/yield_yak_trades.sql
Original file line number Diff line number Diff line change
Expand Up @@ -26,72 +26,82 @@ WITH dexs AS (
{%- endif %}
)

SELECT
'{{ blockchain }}' AS blockchain
, 'yield_yak' AS project
, '1' 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 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, 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
, 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(blockchain, 'transactions') }} tx
ON tx.hash = dexs.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 %}
LEFT JOIN {{ source('tokens', 'erc20') }} erc20a
ON erc20a.contract_address = dexs.token_bought_address
AND erc20a.blockchain = '{{ blockchain }}'
LEFT JOIN {{ source('tokens', 'erc20') }} erc20b
ON erc20b.contract_address = dexs.token_sold_address
AND erc20b.blockchain = '{{ blockchain }}'
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 = '{{ blockchain }}'
{%- if not is_incremental() %}
AND p_bought.minute >= TIMESTAMP '{{ project_start_date }}'
{%- endif %}
{%- if is_incremental() %}
AND {{ incremental_predicate('p_bought.minute') }}
{%- 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 = '{{ blockchain }}'
{%- if not is_incremental() %}
AND p_sold.minute >= TIMESTAMP '{{ project_start_date }}'
{%- endif %}
{%- if is_incremental() %}
AND {{ incremental_predicate('p_sold.minute') }}
{%- endif %}
, basic_trades AS (
SELECT
'{{ blockchain }}' AS blockchain
, 'yield_yak' AS project
, '1' 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 AS token_bought_amount_raw
, dexs.token_sold_amount_raw AS token_sold_amount_raw
, dexs.token_bought_address
, dexs.token_sold_address
, tx."from" AS taker
, 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(blockchain, 'transactions') }} tx
ON tx.hash = dexs.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 %}
LEFT JOIN {{ source('tokens', 'erc20') }} erc20a
ON erc20a.contract_address = dexs.token_bought_address
AND erc20a.blockchain = '{{ blockchain }}'
LEFT JOIN {{ source('tokens', 'erc20') }} erc20b
ON erc20b.contract_address = dexs.token_sold_address
AND erc20b.blockchain = '{{ blockchain }}'
)

, enrichments_with_prices AS (
{{
add_amount_usd(
trades_cte = 'basic_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
{%- endmacro -%}
Loading