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

Add balancer cowswap amm spells on base (hourly spellbook) #7362

Merged
merged 3 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ version: 2
models:
- name: balancer_cowswap_amm_balances
meta:
blockchain: arbitrum, ethereum, gnosis
blockchain: arbitrum, base, ethereum, gnosis
project: balancer_cowswap_amm
contributors: viniabussafi
config:
tags: ['ethereum', 'gnosis', 'balancer', 'balances']
tags: ['arbitrum', 'base', 'ethereum', 'gnosis', 'balancer', 'balances']
description: >
ERC20 token rolling sum balances on Balancer, an automated portfolio manager and trading platform.
columns:
Expand All @@ -31,11 +31,11 @@ models:

- name: balancer_cowswap_amm_liquidity
meta:
blockchain: arbitrum, ethereum, gnosis
blockchain: arbitrum, base, ethereum, gnosis
project: balancer_cowswap_amm
contributors: viniabussafi
config:
tags: ['ethereum', 'gnosis', 'balancer', 'pools', 'liquidity']
tags: ['arbitrum', 'base', 'ethereum', 'gnosis', 'balancer', 'pools', 'liquidity']
description: >
Balancer CoWSwap AMM pools liquidity by token in Ethereum.
columns:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

{% set b_cow_amm_models = [
ref('balancer_cowswap_amm_arbitrum_balances'),
ref('balancer_cowswap_amm_base_balances'),
ref('balancer_cowswap_amm_ethereum_balances'),
ref('balancer_cowswap_amm_gnosis_balances')
] %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

{% set b_cow_amm_models = [
ref('balancer_cowswap_amm_arbitrum_liquidity'),
ref('balancer_cowswap_amm_base_liquidity'),
ref('balancer_cowswap_amm_ethereum_liquidity'),
ref('balancer_cowswap_amm_gnosis_liquidity')
] %}
Expand Down
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"
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
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
Loading