Skip to content

Commit

Permalink
Yield strategies models and Mantle expansion (including tokens.erc20 …
Browse files Browse the repository at this point in the history
…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
yy-analytics authored Jun 11, 2024
1 parent e653942 commit f94d856
Show file tree
Hide file tree
Showing 37 changed files with 1,093 additions and 290 deletions.
13 changes: 13 additions & 0 deletions macros/models/_project/yield_yak/yield_yak_strategies.sql
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,19 @@
])
}}

{% elif blockchain == 'mantle' %}

{{
return([
'CompoundingJoeMantle',
'CompoundingMoe',
'CompoundingYYStaking',
'LendleStrategy',
'MoeStrategy',
'StargateV2Strategy'
])
}}

{% endif %}

{% endmacro %}
97 changes: 97 additions & 0 deletions macros/models/_project/yield_yak/yield_yak_trades.sql
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 macros/models/_project/yield_yak/yield_yak_yield_strategies.sql
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 -%}
28 changes: 27 additions & 1 deletion models/yield_yak/arbitrum/yield_yak_arbitrum_schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -379,4 +379,30 @@ models:
description: "Amount of the sold token being swapped from / traded in the YakAdapterSwap event"
- &adapter_amount_out
name: adapter_amount_out
description: "Amount of the bought token being swapped to / bought in the YakAdapterSwap event"
description: "Amount of the bought token being swapped to / bought in the YakAdapterSwap event"

- name: yield_yak_arbitrum_yield_strategies
meta:
blockchain: arbitrum
project: yield_yak
contributors: angus_1
config:
tags: ['arbitrum', 'yield_yak', 'strategies', 'yield']
description: >
yield yak strategies on arbitrum
tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
- contract_address
columns:
- *blockchain
- *contract_address
- &contract_name
name: contract_name
description: "Name of the deployed contract"
- &created_block_time
name: created_block_time
description: "Timestamp at which the contract was deployed/created"
- &created_block_number
name: created_block_number
description: "Block number at which the contract was deployed/created"
118 changes: 16 additions & 102 deletions models/yield_yak/arbitrum/yield_yak_arbitrum_trades.sql
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 models/yield_yak/arbitrum/yield_yak_arbitrum_yield_strategies.sql
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'
)
}}
28 changes: 27 additions & 1 deletion models/yield_yak/avalanche_c/yield_yak_avalanche_c_schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -379,4 +379,30 @@ models:
description: "Amount of the sold token being swapped from / traded in the YakAdapterSwap event"
- &adapter_amount_out
name: adapter_amount_out
description: "Amount of the bought token being swapped to / bought in the YakAdapterSwap event"
description: "Amount of the bought token being swapped to / bought in the YakAdapterSwap event"

- name: yield_yak_avalanche_c_yield_strategies
meta:
blockchain: avalanche_c
project: yield_yak
contributors: angus_1
config:
tags: ['avalanche_c', 'yield_yak', 'strategies', 'yield']
description: >
yield yak strategies on avalanche_c
tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
- contract_address
columns:
- *blockchain
- *contract_address
- &contract_name
name: contract_name
description: "Name of the deployed contract"
- &created_block_time
name: created_block_time
description: "Timestamp at which the contract was deployed/created"
- &created_block_number
name: created_block_number
description: "Block number at which the contract was deployed/created"
Loading

0 comments on commit f94d856

Please sign in to comment.