-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
init bcowamms on base (hourly subproject)
- Loading branch information
1 parent
849e241
commit 30fd353
Showing
7 changed files
with
250 additions
and
4 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
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
80 changes: 80 additions & 0 deletions
80
dbt_subprojects/hourly_spellbook/models/_project/balancer_cowswap_amm/base/_schema.yml
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,80 @@ | ||
version: 2 | ||
|
||
models: | ||
- name: balancer_cowswap_amm_base_balances | ||
meta: | ||
blockchain: base | ||
project: balancer_cowswap_amm | ||
contributors: viniabussafi | ||
config: | ||
tags: ['base', 'balancer', 'balances'] | ||
description: > | ||
ERC20 token rolling sum balances on Balancer, an automated portfolio manager and trading platform. | ||
data_tests: | ||
- dbt_utils.unique_combination_of_columns: | ||
combination_of_columns: | ||
- day | ||
- pool_address | ||
- token_address | ||
columns: | ||
- &day | ||
name: day | ||
description: "UTC event block time truncated to the day mark" | ||
data_tests: | ||
- not_null | ||
- &blockchain | ||
name: blockchain | ||
description: "" | ||
- &pool_address | ||
name: pool_address | ||
description: "Balancer CoWSwap AMM pool contract address" | ||
- &token_address | ||
name: token_address | ||
description: "Token contract address" | ||
- &token_balance_raw | ||
name: token_balance_raw | ||
description: "Balance of a token" | ||
|
||
- name: balancer_cowswap_amm_base_liquidity | ||
meta: | ||
blockchain: base | ||
project: balancer_cowswap_amm | ||
contributors: viniabussafi | ||
config: | ||
tags: ['base', 'balancer', 'pools', 'liquidity'] | ||
description: > | ||
Balancer CoWSwap AMM pools liquidity by token in base. | ||
data_tests: | ||
- dbt_utils.unique_combination_of_columns: | ||
combination_of_columns: | ||
- day | ||
- pool_id | ||
- token_address | ||
columns: | ||
- *day | ||
- name: pool_id | ||
- *pool_address | ||
- name: pool_symbol | ||
- name: version | ||
- *blockchain | ||
- name: pool_type | ||
- *token_address | ||
- &token_symbol | ||
name: token_symbol | ||
description: "Token symbol" | ||
- *token_balance_raw | ||
- &token_balance | ||
name: token_balance | ||
description: "Balance of the token in the pool" | ||
- &protocol_liquidity_usd | ||
name: protocol_liquidity_usd | ||
description: "Protocol liquidity in USD" | ||
- &protocol_liquidity_eth | ||
name: protocol_liquidity_eth | ||
description: "Protocol liquidity in ETH" | ||
- &pool_liquidity_usd | ||
name: pool_liquidity_usd | ||
description: "Pool liquidity in USD" | ||
- &pool_liquidity_eth | ||
name: pool_liquidity_eth | ||
description: "Pool liquidity in ETH" |
77 changes: 77 additions & 0 deletions
77
...pellbook/models/_project/balancer_cowswap_amm/base/balancer_cowswap_amm_base_balances.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,77 @@ | ||
{{ | ||
config( | ||
schema = 'balancer_cowswap_amm_base', | ||
alias = 'balances', | ||
materialized = 'table', | ||
file_format = 'delta' | ||
) | ||
}} | ||
|
||
WITH pools AS ( | ||
SELECT | ||
bPool AS pools | ||
FROM {{ source('b_cow_amm_base', 'BCoWFactory_evt_LOG_NEW_POOL') }} | ||
), | ||
|
||
joins AS ( | ||
SELECT | ||
p.pools AS pool, | ||
DATE_TRUNC('day', e.evt_block_time) AS day, | ||
e.contract_address AS token, | ||
SUM(CAST(value AS int256)) AS amount | ||
FROM {{ source('erc20_base', 'evt_transfer') }} e | ||
INNER JOIN pools p ON e."to" = p.pools | ||
GROUP BY 1, 2, 3 | ||
), | ||
|
||
exits AS ( | ||
SELECT | ||
p.pools AS pool, | ||
DATE_TRUNC('day', e.evt_block_time) AS day, | ||
e.contract_address AS token, | ||
- SUM(CAST(value AS int256)) AS amount | ||
FROM {{ source('erc20_base', 'evt_transfer') }} e | ||
INNER JOIN pools p ON e."from" = p.pools | ||
GROUP BY 1, 2, 3 | ||
), | ||
|
||
daily_delta_balance_by_token AS ( | ||
SELECT | ||
pool, | ||
day, | ||
token, | ||
SUM(COALESCE(amount, CAST(0 AS int256))) AS amount | ||
FROM | ||
(SELECT * | ||
FROM joins j | ||
UNION ALL | ||
SELECT * | ||
FROM exits e) foo | ||
GROUP BY 1, 2, 3 | ||
), | ||
|
||
cumulative_balance_by_token AS ( | ||
SELECT | ||
pool, | ||
token, | ||
day, | ||
LEAD(day, 1, now()) OVER (PARTITION BY pool, token ORDER BY day) AS day_of_next_change, | ||
SUM(amount) OVER (PARTITION BY pool, token ORDER BY day ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS cumulative_amount | ||
FROM daily_delta_balance_by_token | ||
), | ||
|
||
calendar AS ( | ||
SELECT | ||
date_sequence AS day | ||
FROM unnest(sequence(date('2024-12-01'), date(now()), interval '1' day)) AS t(date_sequence) | ||
) | ||
|
||
SELECT | ||
c.day, | ||
'base' AS blockchain, | ||
b.pool AS pool_address, | ||
b.token AS token_address, | ||
b.cumulative_amount AS token_balance_raw | ||
FROM calendar c | ||
LEFT JOIN cumulative_balance_by_token b ON b.day <= c.day AND c.day < b.day_of_next_change | ||
WHERE b.pool IS NOT NULL |
85 changes: 85 additions & 0 deletions
85
...ellbook/models/_project/balancer_cowswap_amm/base/balancer_cowswap_amm_base_liquidity.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,85 @@ | ||
{% set blockchain = 'base' %} | ||
|
||
{{ | ||
config( | ||
schema='balancer_cowswap_amm_' + blockchain, | ||
alias = 'liquidity', | ||
materialized = 'table', | ||
file_format = 'delta' | ||
) | ||
}} | ||
|
||
WITH pool_labels AS ( | ||
SELECT | ||
address, | ||
name | ||
FROM {{ source('labels', 'balancer_cowswap_amm_pools') }} | ||
WHERE blockchain = '{{blockchain}}' | ||
), | ||
|
||
prices AS ( | ||
SELECT | ||
date_trunc('day', minute) AS day, | ||
contract_address AS token, | ||
decimals, | ||
AVG(price) AS price | ||
FROM {{ source('prices', 'usd') }} | ||
WHERE blockchain = '{{blockchain}}' | ||
GROUP BY 1, 2, 3 | ||
), | ||
|
||
eth_prices AS( | ||
SELECT | ||
date_trunc('day', minute) AS day, | ||
APPROX_PERCENTILE(price, 0.5) AS eth_price | ||
FROM {{ source('prices', 'usd') }} | ||
WHERE blockchain = '{{blockchain}}' | ||
AND contract_address = 0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2 | ||
GROUP BY 1 | ||
), | ||
|
||
cumulative_balance AS ( | ||
SELECT | ||
day, | ||
pool_address, | ||
token_address, | ||
token_balance_raw | ||
FROM {{ ref('balancer_cowswap_amm_base_balances') }} b | ||
), | ||
|
||
cumulative_usd_balance AS ( | ||
SELECT | ||
b.day, | ||
b.pool_address, | ||
b.token_address, | ||
t.symbol, | ||
token_balance_raw, | ||
token_balance_raw / POWER(10, COALESCE(t.decimals, p1.decimals)) AS token_balance, | ||
token_balance_raw / POWER(10, COALESCE(t.decimals, p1.decimals)) * COALESCE(p1.price, 0) AS protocol_liquidity_usd | ||
FROM cumulative_balance b | ||
LEFT JOIN {{ source('tokens', 'erc20') }} t ON t.contract_address = b.token_address | ||
AND t.blockchain = '{{blockchain}}' | ||
LEFT JOIN prices p1 ON p1.day = b.day | ||
AND p1.token = b.token_address | ||
) | ||
|
||
SELECT | ||
c.day, | ||
c.pool_address AS pool_id, | ||
c.pool_address, | ||
p.name AS pool_symbol, | ||
'1' AS version, | ||
'{{blockchain}}' AS blockchain, | ||
'balancer_cowswap_amm' AS pool_type, | ||
c.token_address, | ||
c.symbol AS token_symbol, | ||
c.token_balance_raw, | ||
c.token_balance, | ||
c.protocol_liquidity_usd, | ||
(c.protocol_liquidity_usd) / e.eth_price AS protocol_liquidity_eth, | ||
c.protocol_liquidity_usd AS pool_liquidity_usd, | ||
(c.protocol_liquidity_usd) / e.eth_price AS pool_liquidity_eth | ||
FROM cumulative_usd_balance c | ||
LEFT JOIN pool_labels p ON p.address = c.pool_address | ||
LEFT JOIN eth_prices e ON e.day = c.day | ||
WHERE c.pool_address IS NOT NULL |
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