diff --git a/dbt_subprojects/hourly_spellbook/models/_project/balancer_cowswap_amm/_schema.yml b/dbt_subprojects/hourly_spellbook/models/_project/balancer_cowswap_amm/_schema.yml index bf1b22cdf8c..122bf017ff8 100644 --- a/dbt_subprojects/hourly_spellbook/models/_project/balancer_cowswap_amm/_schema.yml +++ b/dbt_subprojects/hourly_spellbook/models/_project/balancer_cowswap_amm/_schema.yml @@ -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: @@ -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: diff --git a/dbt_subprojects/hourly_spellbook/models/_project/balancer_cowswap_amm/balancer_cowswap_amm_balances.sql b/dbt_subprojects/hourly_spellbook/models/_project/balancer_cowswap_amm/balancer_cowswap_amm_balances.sql index 8fe94461c54..ced93887adc 100644 --- a/dbt_subprojects/hourly_spellbook/models/_project/balancer_cowswap_amm/balancer_cowswap_amm_balances.sql +++ b/dbt_subprojects/hourly_spellbook/models/_project/balancer_cowswap_amm/balancer_cowswap_amm_balances.sql @@ -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') ] %} diff --git a/dbt_subprojects/hourly_spellbook/models/_project/balancer_cowswap_amm/balancer_cowswap_amm_liquidity.sql b/dbt_subprojects/hourly_spellbook/models/_project/balancer_cowswap_amm/balancer_cowswap_amm_liquidity.sql index e0c2bf825c8..f33020da31d 100644 --- a/dbt_subprojects/hourly_spellbook/models/_project/balancer_cowswap_amm/balancer_cowswap_amm_liquidity.sql +++ b/dbt_subprojects/hourly_spellbook/models/_project/balancer_cowswap_amm/balancer_cowswap_amm_liquidity.sql @@ -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') ] %} diff --git a/dbt_subprojects/hourly_spellbook/models/_project/balancer_cowswap_amm/base/_schema.yml b/dbt_subprojects/hourly_spellbook/models/_project/balancer_cowswap_amm/base/_schema.yml new file mode 100644 index 00000000000..41ad1496b56 --- /dev/null +++ b/dbt_subprojects/hourly_spellbook/models/_project/balancer_cowswap_amm/base/_schema.yml @@ -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" \ No newline at end of file diff --git a/dbt_subprojects/hourly_spellbook/models/_project/balancer_cowswap_amm/base/balancer_cowswap_amm_base_balances.sql b/dbt_subprojects/hourly_spellbook/models/_project/balancer_cowswap_amm/base/balancer_cowswap_amm_base_balances.sql new file mode 100644 index 00000000000..fd438e86004 --- /dev/null +++ b/dbt_subprojects/hourly_spellbook/models/_project/balancer_cowswap_amm/base/balancer_cowswap_amm_base_balances.sql @@ -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 diff --git a/dbt_subprojects/hourly_spellbook/models/_project/balancer_cowswap_amm/base/balancer_cowswap_amm_base_liquidity.sql b/dbt_subprojects/hourly_spellbook/models/_project/balancer_cowswap_amm/base/balancer_cowswap_amm_base_liquidity.sql new file mode 100644 index 00000000000..7a9557b890f --- /dev/null +++ b/dbt_subprojects/hourly_spellbook/models/_project/balancer_cowswap_amm/base/balancer_cowswap_amm_base_liquidity.sql @@ -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 diff --git a/sources/_subprojects_outputs/dex/_sources.yml b/sources/_subprojects_outputs/dex/_sources.yml index c99a0f0dd97..f14e7d525b7 100644 --- a/sources/_subprojects_outputs/dex/_sources.yml +++ b/sources/_subprojects_outputs/dex/_sources.yml @@ -65,6 +65,8 @@ sources: - name: balancer_cowswap_amm_pools - name: balancer_v3_pools_ethereum - name: balancer_v3_pools_gnosis + - name: balancer_v3_pools_arbitrum + - name: balancer_v3_pools_base - name: balancer_v3_pools - name: uniswap_v3_optimism tables: