-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Yield strategies models and Mantle expansion (including tokens.erc20 …
…for Mantle) (#6118) * Added new yield strategies view model * Replicated/expanded existing models for mantle chain * Refactored the Yield Yak trades models to make use of macros, plus small formatting changes * Added manual additions of tokens for mantle to tokens.erc20 model * Fixes for dbt compile, including adding forgotten sources and some miinor others * Spacing changes to re-trigger dbt tests in PR * Correction to seed entry
- Loading branch information
1 parent
e653942
commit f94d856
Showing
37 changed files
with
1,093 additions
and
290 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
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,97 @@ | ||
{%- macro yield_yak_trades( | ||
blockchain = null, | ||
project_start_date = '2021-09-15' | ||
) | ||
-%} | ||
|
||
{%- set namespace_blockchain = 'yield_yak_' + blockchain -%} | ||
|
||
WITH dexs AS ( | ||
SELECT | ||
evt_block_time AS block_time | ||
-- , '' AS taker commenting this as there's no trader in the event | ||
, CAST(NULL as VARBINARY) AS maker | ||
, _amountIn AS token_sold_amount_raw | ||
, _amountOut AS token_bought_amount_raw | ||
, CAST(NULL AS double) AS amount_usd | ||
, _tokenIn AS token_sold_address | ||
, _tokenOut AS token_bought_address | ||
, contract_address As project_contract_address | ||
, evt_tx_hash AS tx_hash | ||
, ARRAY[-1] AS trace_address | ||
, evt_index | ||
FROM {{ source(namespace_blockchain, 'YakRouter_evt_YakSwap') }} | ||
{%- if is_incremental() %} | ||
WHERE {{ incremental_predicate('evt_block_time') }} | ||
{%- 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 %} | ||
|
||
{%- endmacro -%} |
21 changes: 21 additions & 0 deletions
21
macros/models/_project/yield_yak/yield_yak_yield_strategies.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,21 @@ | ||
{%- macro yield_yak_yield_strategies( | ||
blockchain = null | ||
) | ||
-%} | ||
|
||
SELECT | ||
'{{ blockchain }}' AS blockchain | ||
, c.address AS contract_address | ||
, c.name AS contract_name | ||
, ct.block_time AS created_block_time | ||
, ct.block_number AS created_block_number | ||
FROM {{ source(blockchain, 'contracts') }} c | ||
INNER JOIN {{ source(blockchain, 'creation_traces') }} ct | ||
ON ct.address = c.address | ||
WHERE | ||
c.namespace = 'yield_yak' | ||
-- This next line ensures we're only getting the contracts which have the required Reinvest, Deposit and Withdraw events | ||
-- and are therefore the types of yield strategies in which we are interested for this macro. | ||
AND cardinality(filter(c.abi, x -> json_extract_scalar(x, '$.name') IN ('Reinvest', 'Deposit', 'Withdraw') AND json_extract_scalar(x, '$.type') = 'event')) = 3 | ||
|
||
{%- endmacro -%} |
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
118 changes: 16 additions & 102 deletions
118
models/yield_yak/arbitrum/yield_yak_arbitrum_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,105 +1,19 @@ | ||
{{ config( | ||
schema = 'yield_yak_arbitrum', | ||
alias = 'trades', | ||
partition_by = ['block_month'], | ||
materialized = 'incremental', | ||
file_format = 'delta', | ||
incremental_strategy = 'merge', | ||
unique_key = ['block_date', 'blockchain', 'project', 'version', 'tx_hash', 'evt_index', 'trace_address'], | ||
incremental_predicates = [incremental_predicate('DBT_INTERNAL_DEST.block_time')] | ||
{{ | ||
config( | ||
schema = 'yield_yak_arbitrum', | ||
alias = 'trades', | ||
partition_by = ['block_month'], | ||
materialized = 'incremental', | ||
file_format = 'delta', | ||
incremental_strategy = 'merge', | ||
unique_key = ['block_date', 'blockchain', 'project', 'version', 'tx_hash', 'evt_index', 'trace_address'], | ||
incremental_predicates = [incremental_predicate('DBT_INTERNAL_DEST.block_time')] | ||
) | ||
}} | ||
|
||
|
||
{% set project_start_date = '2023-03-21' %} | ||
|
||
WITH | ||
|
||
dexs as ( | ||
SELECT | ||
evt_block_time as block_time, | ||
-- '' as taker, commenting this as there's no trader in the event | ||
CAST(NULL as VARBINARY) as maker, | ||
_amountIn as token_sold_amount_raw, | ||
_amountOut as token_bought_amount_raw, | ||
CAST(NULL as double) as amount_usd, | ||
_tokenIn as token_sold_address, | ||
_tokenOut as token_bought_address, | ||
contract_address as project_contract_address, | ||
evt_tx_hash as tx_hash, | ||
ARRAY[-1] AS trace_address, | ||
evt_index | ||
FROM | ||
{{ source('yield_yak_arbitrum', 'YakRouter_evt_YakSwap') }} | ||
{% if is_incremental() %} | ||
WHERE {{incremental_predicate('evt_block_time')}} | ||
{% endif %} | ||
) | ||
|
||
SELECT | ||
'arbitrum' 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('arbitrum', '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 = 'arbitrum' | ||
LEFT JOIN {{ source('tokens', 'erc20') }} erc20b | ||
ON erc20b.contract_address = dexs.token_sold_address | ||
AND erc20b.blockchain = 'arbitrum' | ||
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 = 'arbitrum' | ||
{% 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 = 'arbitrum' | ||
{% if not is_incremental() %} | ||
AND p_sold.minute >= TIMESTAMP '{{project_start_date}}' | ||
{% endif %} | ||
{% if is_incremental() %} | ||
AND {{incremental_predicate('p_sold.minute')}} | ||
{% endif %} | ||
{{ | ||
yield_yak_trades( | ||
blockchain = 'arbitrum', | ||
project_start_date = '2023-03-21' | ||
) | ||
}} |
13 changes: 13 additions & 0 deletions
13
models/yield_yak/arbitrum/yield_yak_arbitrum_yield_strategies.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,13 @@ | ||
{{ | ||
config( | ||
schema = 'yield_yak_arbitrum', | ||
alias = 'yield_strategies', | ||
materialized = 'view' | ||
) | ||
}} | ||
|
||
{{ | ||
yield_yak_yield_strategies( | ||
blockchain = 'arbitrum' | ||
) | ||
}} |
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
Oops, something went wrong.