Skip to content

Commit

Permalink
Remove amount_usd from chain-level spells
Browse files Browse the repository at this point in the history
  • Loading branch information
Hosuke committed Jun 20, 2024
1 parent 840f69b commit 379fbc1
Show file tree
Hide file tree
Showing 4 changed files with 2 additions and 178 deletions.
5 changes: 0 additions & 5 deletions dex/models/_projects/paraswap/avalanche_c/_schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,6 @@ models:
- &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"
Expand Down Expand Up @@ -134,7 +131,6 @@ models:
- *token_sold_amount
- *token_bought_amount_raw
- *token_sold_amount_raw
- *amount_usd
- *token_bought_address
- *token_sold_address
- *taker
Expand Down Expand Up @@ -172,7 +168,6 @@ models:
- *token_sold_amount
- *token_bought_amount_raw
- *token_sold_amount_raw
- *amount_usd
- *token_bought_address
- *token_sold_address
- *taker
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ FROM (
token_sold_amount,
token_bought_amount_raw,
token_sold_amount_raw,
amount_usd,
token_bought_address,
token_sold_address,
taker,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,60 +60,6 @@ dexs as (
UNION ALL
{% endif %}
{% endfor %}
),

-- USDC.e AND USDT.e price are missing before 2022-10
price_missed_previous AS (
WITH usdc_price AS (
SELECT minute, contract_address, decimals, symbol, price
FROM {{ source('prices', 'usd') }}
WHERE contract_address = 0xa7d7079b0fead91f3e65f86e8915cb59c1a4c664 -- USDC.e
ORDER BY minute
LIMIT 1
),

usdt_price AS (
SELECT minute, contract_address, decimals, symbol, price
FROM {{ source('prices', 'usd') }}
WHERE contract_address = 0xc7198437980c041c805a1edcba50c1ce5db95118 -- USDT.e
ORDER BY minute
LIMIT 1
)

SELECT minute, contract_address, decimals, symbol, price
FROM usdc_price

UNION ALL

SELECT minute, contract_address, decimals, symbol, price
FROM usdt_price
),

-- USDC.e AND USDT.e price may be missed for latest swaps
price_missed_next AS (
WITH usdc_price AS (
SELECT minute, contract_address, decimals, symbol, price
FROM {{ source('prices', 'usd') }}
WHERE contract_address = 0xa7d7079b0fead91f3e65f86e8915cb59c1a4c664 -- USDC.e
ORDER BY minute DESC
LIMIT 1
),

usdt_price AS (
SELECT minute, contract_address, decimals, symbol, price
FROM {{ source('prices', 'usd') }}
WHERE contract_address = 0xc7198437980c041c805a1edcba50c1ce5db95118 -- USDT.e
ORDER BY minute DESC
LIMIT 1
)

SELECT minute, contract_address, decimals, symbol, price
FROM usdc_price

UNION ALL

SELECT minute, contract_address, decimals, symbol, price
FROM usdt_price
)

SELECT
Expand All @@ -133,11 +79,6 @@ SELECT
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, erc20a.decimals)) * coalesce(p_bought.price, p_prev1.price, p_next1.price)
,(dexs.token_sold_amount_raw / power(10, erc20b.decimals)) * coalesce(p_sold.price, p_prev2.price, p_next2.price)
) AS amount_usd,
dexs.token_bought_address,
dexs.token_sold_address,
coalesce(dexs.taker, tx."from") AS taker,
Expand All @@ -163,32 +104,4 @@ LEFT JOIN {{ source('tokens', 'erc20') }} erc20a
AND erc20a.blockchain = 'avalanche_c'
LEFT JOIN {{ source('tokens', 'erc20') }} erc20b
ON erc20b.contract_address = dexs.token_sold_address
AND erc20b.blockchain = 'avalanche_c'
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 = 'avalanche_c'
{% 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 price_missed_previous p_prev1 ON dexs.token_bought_address = p_prev1.contract_address
AND dexs.block_time < p_prev1.minute -- Swap before first price record time
LEFT JOIN price_missed_next p_next1 ON dexs.token_bought_address = p_next1.contract_address
AND dexs.block_time > p_next1.minute -- Swap after last price record time
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 = 'avalanche_c'
{% 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 %}
LEFT JOIN price_missed_previous p_prev2 ON dexs.token_sold_address = p_prev2.contract_address
AND dexs.block_time < p_prev2.minute -- Swap before first price record time
LEFT JOIN price_missed_next p_next2 ON dexs.token_sold_address = p_next2.contract_address
AND dexs.block_time > p_next2.minute -- Swap after last price record time
AND erc20b.blockchain = 'avalanche_c'
Original file line number Diff line number Diff line change
Expand Up @@ -43,58 +43,6 @@ with dexs AS (
{% if is_incremental() %}
WHERE blockTime >= date_trunc('day', now() - interval '7' day)
{% endif %}
),

-- USDC.e AND USDT.e price are missing before 2022-10
price_missed_previous AS (
WITH usdc_price AS (
SELECT minute, contract_address, decimals, symbol, price
FROM {{ source('prices', 'usd') }}
WHERE contract_address = 0xa7d7079b0fead91f3e65f86e8915cb59c1a4c664 -- USDC.e
ORDER BY minute
LIMIT 1
),

usdt_price AS (
SELECT minute, contract_address, decimals, symbol, price
FROM {{ source('prices', 'usd') }}
WHERE contract_address = 0xc7198437980c041c805a1edcba50c1ce5db95118 -- USDT.e
ORDER BY minute
LIMIT 1
)

SELECT minute, contract_address, decimals, symbol, price
FROM usdc_price
UNION ALL

SELECT minute, contract_address, decimals, symbol, price
FROM usdt_price
),
-- USDC.e AND USDT.e price may be missed for latest swaps
price_missed_next AS (
WITH usdc_price AS (
SELECT minute, contract_address, decimals, symbol, price
FROM {{ source('prices', 'usd') }}
WHERE contract_address = 0xa7d7079b0fead91f3e65f86e8915cb59c1a4c664 -- USDC.e
ORDER BY minute DESC
LIMIT 1
),

usdt_price AS (
SELECT minute, contract_address, decimals, symbol, price
FROM {{ source('prices', 'usd') }}
WHERE contract_address = 0xc7198437980c041c805a1edcba50c1ce5db95118 -- USDT.e
ORDER BY minute DESC
LIMIT 1
)

SELECT minute, contract_address, decimals, symbol, price
FROM usdc_price

UNION ALL

SELECT minute, contract_address, decimals, symbol, price
FROM usdt_price
)

SELECT 'avalanche_c' AS blockchain,
Expand All @@ -113,12 +61,7 @@ SELECT 'avalanche_c' AS blockchain,
d.token_bought_amount_raw / power(10, e1.decimals) AS token_bought_amount,
d.token_sold_amount_raw / power(10, e2.decimals) AS token_sold_amount,
d.token_bought_amount_raw,
d.token_sold_amount_raw,
coalesce(
d.amount_usd
,(d.token_bought_amount_raw / power(10, e1.decimals)) * coalesce(p1.price, p_prev1.price, p_next1.price)
,(d.token_sold_amount_raw / power(10, e2.decimals)) * coalesce(p2.price, p_prev2.price, p_next2.price)
) AS amount_usd,
d.token_sold_amount_raw,
d.token_bought_address,
d.token_sold_address,
coalesce(d.taker, tx."from") AS taker,
Expand All @@ -142,29 +85,3 @@ LEFT JOIN {{ source('tokens', 'erc20') }} e1 ON e1.contract_address = d.token_bo
AND e1.blockchain = 'avalanche_c'
LEFT JOIN {{ source('tokens', 'erc20') }} e2 on e2.contract_address = d.token_sold_address
AND e2.blockchain = 'avalanche_c'
LEFT JOIN {{ source('prices', 'usd') }} p1 ON p1.minute = date_trunc('minute', d.block_time)
AND p1.contract_address = d.token_bought_address
AND p1.blockchain = 'avalanche_c'
{% if not is_incremental() %}
AND p1.minute >= TIMESTAMP '{{project_start_date}}'
{% endif %}
{% if is_incremental() %}
AND p1.minute >= date_trunc('day', now() - interval '7' day)
{% endif %}
LEFT JOIN price_missed_previous p_prev1 ON d.token_bought_address = p_prev1.contract_address
AND d.block_time < p_prev1.minute -- Swap before first price record time
LEFT JOIN price_missed_next p_next1 ON d.token_bought_address = p_next1.contract_address
AND d.block_time > p_next1.minute -- Swap after last price record time
LEFT JOIN {{ source('prices', 'usd') }} p2 ON p2.minute = date_trunc('minute', d.block_time)
AND p2.contract_address = d.token_sold_address
AND p2.blockchain = 'avalanche_c'
{% if not is_incremental() %}
AND p2.minute >= TIMESTAMP '{{project_start_date}}'
{% endif %}
{% if is_incremental() %}
AND p2.minute >= date_trunc('day', now() - interval '7' day)
{% endif %}
LEFT JOIN price_missed_previous p_prev2 ON d.token_sold_address = p_prev2.contract_address
AND d.block_time < p_prev2.minute -- Swap before first price record time
LEFT JOIN price_missed_next p_next2 ON d.token_sold_address = p_next2.contract_address
AND d.block_time > p_next2.minute -- Swap after last price record time

0 comments on commit 379fbc1

Please sign in to comment.