diff --git a/dbt_subprojects/daily_spellbook/models/_projects/balancer_cowswap_amm/_schema.yml b/dbt_subprojects/daily_spellbook/models/_projects/balancer_cowswap_amm/_schema.yml new file mode 100644 index 00000000000..d84c0e4093a --- /dev/null +++ b/dbt_subprojects/daily_spellbook/models/_projects/balancer_cowswap_amm/_schema.yml @@ -0,0 +1,184 @@ +version: 2 + +models: + - name: balancer_cowswap_amm_trades + meta: + blockchain: ethereum + sector: trades + project: balancer_cowswap_amm + contributors: viniabussafi + config: + tags: ['balancer', 'cowswap', 'ethereum', 'amm', 'trades'] + description: "Trades on Balancer CoWSwap AMM pools" + columns: + - &blockchain + name: blockchain + description: "Blockchain" + - &project + name: project + description: "Project name (balancer)" + - &version + name: version + description: "Version of the project" + - &block_month + name: block_month + description: "Block month in UTC" + - &block_date + name: block_date + description: "Block date in UTC" + - &block_time + name: block_time + description: 'Block time in UTC' + - &block_number + name: block_number + description: 'Block number' + - &token_bought_symbol + name: token_bought_symbol + description: "Token symbol for token bought in the trade" + - &token_sold_symbol + name: token_sold_symbol + description: "Token symbol for token sold in the trade" + - &token_pair + name: token_pair + description: "Token symbol pair for each token involved in the trade" + - &token_bought_amount + name: token_bought_amount + description: "Value of the token bought at time of execution in the original currency" + - &token_sold_amount + name: token_sold_amount + description: "Value of the token sold at time of execution in the original currency" + - &token_bought_amount_raw + name: token_bought_amount_raw + description: "Raw value of the token bought at time of execution in the original currency" + - &token_sold_amount_raw + name: token_sold_amount_raw + description: "Raw value of the token sold at time of execution in the original currency" + - &amount_usd + name: amount_usd + description: "USD value of the trade at time of execution" + - &token_bought_address + name: token_bought_address + description: "Contract address of the token bought" + - &token_sold_address + name: token_sold_address + description: "Contract address of the token sold" + - &taker + name: taker + description: "Address of trader who purchased a token" + - &maker + name: maker + description: "Address of trader who sold a token" + - &project_contract_address + name: project_contract_address + description: "Pool address" + - &tx_hash + name: tx_hash + description: "Tx. Hash" + - &tx_from + name: tx_from + description: "transaction.from" + - &tx_to + name: tx_to + description: "transaction.to" + - &evt_index + name: evt_index + description: 'Event index' + - name: pool_type + - name: pool_symbol + - name: swap_fee + + - name: balancer_cowswap_amm_balances + meta: + blockchain: ethereum + project: balancer_cowswap_amm + contributors: viniabussafi + config: + tags: ['ethereum', 'balancer', 'balances'] + description: > + ERC20 token rolling sum balances on Balancer, an automated portfolio manager and trading platform. + columns: + - &day + name: day + description: "UTC event block time truncated to the day mark" + tests: + - not_null + - &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: labels_balancer_cowswap_amm_pools + meta: + blockchain: ethereum + sector: labels + project: balancer_cowswap_amm + contributors: viniabussafi + config: + tags: ['labels', 'ethereum', 'balancer', 'pools'] + description: "Balancer CoWSwap AMM liquidity pools created on Ethereum." + columns: + - *blockchain + - &address + name: address + description: "Address of liquidity pool" + - &name + name: name + description: "Label name of pool containing the token symbols and their respective weights (if applicable)" + - &category + name: category + description: "Label category" + - &contributor + name: contributor + description: "Wizard(s) contributing to labels" + - &source + name: source + description: "How were labels generated (could be static or query)" + - &created_at + name: created_at + description: "When were labels created" + - &updated_at + name: updated_at + description: "When were labels updated for the last time" + - &model_name + name: model_name + description: "Name of the label model sourced from" + - &label_type + name: label_type + description: "Type of label (see labels overall readme)" + + - name: balancer_cowswap_amm_liquidity + meta: + blockchain: ethereum + project: balancer_cowswap_amm + contributors: viniabussafi + config: + tags: ['ethereum', 'balancer', 'pools', 'liquidity'] + description: > + Balancer CoWSwap AMM pools liquidity by token in Ethereum. + columns: + - *day + - name: pool_id + - *pool_address + - name: pool_symbol + - *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" diff --git a/dbt_subprojects/daily_spellbook/models/_projects/balancer_cowswap_amm/balancer_cowswap_amm_balances.sql b/dbt_subprojects/daily_spellbook/models/_projects/balancer_cowswap_amm/balancer_cowswap_amm_balances.sql new file mode 100644 index 00000000000..d210bd00fec --- /dev/null +++ b/dbt_subprojects/daily_spellbook/models/_projects/balancer_cowswap_amm/balancer_cowswap_amm_balances.sql @@ -0,0 +1,27 @@ +{{ + config( + schema = 'balancer_cowswap_amm', + alias = 'balances', + materialized = 'view' + ) +}} + + +{% set b_cow_amm_models = [ + ref('balancer_cowswap_amm_ethereum_balances') +] %} + +SELECT * +FROM ( + {% for model in b_cow_amm_models %} + SELECT + day, + pool_address, + token_address, + token_balance_raw + FROM {{ model }} + {% if not loop.last %} + UNION ALL + {% endif %} + {% endfor %} +) \ No newline at end of file diff --git a/dbt_subprojects/daily_spellbook/models/_projects/balancer_cowswap_amm/balancer_cowswap_amm_liquidity.sql b/dbt_subprojects/daily_spellbook/models/_projects/balancer_cowswap_amm/balancer_cowswap_amm_liquidity.sql new file mode 100644 index 00000000000..9663d721366 --- /dev/null +++ b/dbt_subprojects/daily_spellbook/models/_projects/balancer_cowswap_amm/balancer_cowswap_amm_liquidity.sql @@ -0,0 +1,35 @@ +{{ + config( + schema='balancer_cowswap_amm', + alias = 'liquidity', + materialized = 'view' + ) +}} + +{% set b_cow_amm_models = [ + ref('balancer_cowswap_amm_ethereum_liquidity') +] %} + +SELECT * +FROM ( + {% for model in b_cow_amm_models %} + SELECT + day, + pool_id, + pool_address, + pool_symbol, + version, + blockchain, + pool_type, + token_address, + token_symbol, + token_balance_raw, + token_balance, + protocol_liquidity_usd, + protocol_liquidity_eth + FROM {{ model }} + {% if not loop.last %} + UNION ALL + {% endif %} + {% endfor %} +) \ No newline at end of file diff --git a/dbt_subprojects/daily_spellbook/models/_projects/balancer_cowswap_amm/balancer_cowswap_amm_trades.sql b/dbt_subprojects/daily_spellbook/models/_projects/balancer_cowswap_amm/balancer_cowswap_amm_trades.sql new file mode 100644 index 00000000000..e958525ff29 --- /dev/null +++ b/dbt_subprojects/daily_spellbook/models/_projects/balancer_cowswap_amm/balancer_cowswap_amm_trades.sql @@ -0,0 +1,48 @@ +{{ + config( + schema = 'balancer_cowswap_amm', + alias = 'trades', + materialized = 'view' + ) +}} + +{% set b_cow_amm_models = [ + ref('balancer_cowswap_amm_ethereum_trades') +] %} + +SELECT * +FROM ( + {% for model in b_cow_amm_models %} + SELECT + blockchain + , project + , version + , block_month + , block_date + , block_time + , block_number + , token_bought_symbol + , token_sold_symbol + , token_pair + , token_bought_amount + , token_sold_amount + , token_bought_amount_raw + , token_sold_amount_raw + , amount_usd + , token_bought_address + , token_sold_address + , taker + , maker + , project_contract_address + , tx_hash + , tx_from + , tx_to + , evt_index + , pool_type + , pool_symbol + FROM {{ model }} + {% if not loop.last %} + UNION ALL + {% endif %} + {% endfor %} +) \ No newline at end of file diff --git a/dbt_subprojects/daily_spellbook/models/_projects/balancer_cowswap_amm/ethereum/_schema.yml b/dbt_subprojects/daily_spellbook/models/_projects/balancer_cowswap_amm/ethereum/_schema.yml new file mode 100644 index 00000000000..499448a01f7 --- /dev/null +++ b/dbt_subprojects/daily_spellbook/models/_projects/balancer_cowswap_amm/ethereum/_schema.yml @@ -0,0 +1,206 @@ +version: 2 + +models: + - name: balancer_cowswap_amm_ethereum_trades + meta: + blockchain: ethereum + sector: trades + project: balancer_cowswap_amm + contributors: viniabussafi + config: + tags: ['balancer', 'cowswap', 'ethereum', 'amm', 'trades'] + description: "Trades on Balancer CoWSwap AMM pools" + tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - tx_hash + - evt_index + columns: + - &blockchain + name: blockchain + description: "Blockchain" + - &project + name: project + description: "Project name (balancer)" + - &version + name: version + description: "Version of the project" + - &block_month + name: block_month + description: "Block month in UTC" + - &block_date + name: block_date + description: "Block date in UTC" + - &block_time + name: block_time + description: 'Block time in UTC' + - &block_number + name: block_number + description: 'Block number' + - &token_bought_symbol + name: token_bought_symbol + description: "Token symbol for token bought in the trade" + - &token_sold_symbol + name: token_sold_symbol + description: "Token symbol for token sold in the trade" + - &token_pair + name: token_pair + description: "Token symbol pair for each token involved in the trade" + - &token_bought_amount + name: token_bought_amount + description: "Value of the token bought at time of execution in the original currency" + - &token_sold_amount + name: token_sold_amount + description: "Value of the token sold at time of execution in the original currency" + - &token_bought_amount_raw + name: token_bought_amount_raw + description: "Raw value of the token bought at time of execution in the original currency" + - &token_sold_amount_raw + name: token_sold_amount_raw + description: "Raw value of the token sold at time of execution in the original currency" + - &amount_usd + name: amount_usd + description: "USD value of the trade at time of execution" + - &token_bought_address + name: token_bought_address + description: "Contract address of the token bought" + - &token_sold_address + name: token_sold_address + description: "Contract address of the token sold" + - &taker + name: taker + description: "Address of trader who purchased a token" + - &maker + name: maker + description: "Address of trader who sold a token" + - &project_contract_address + name: project_contract_address + description: "Pool address" + - &tx_hash + name: tx_hash + description: "Tx. Hash" + - &tx_from + name: tx_from + description: "transaction.from" + - &tx_to + name: tx_to + description: "transaction.to" + - &evt_index + name: evt_index + description: 'Event index' + - name: pool_type + - name: pool_symbol + - name: swap_fee + + - name: balancer_cowswap_amm_ethereum_balances + meta: + blockchain: ethereum + project: balancer_cowswap_amm + contributors: viniabussafi + config: + tags: ['ethereum', 'balancer', 'balances'] + description: > + ERC20 token rolling sum balances on Balancer, an automated portfolio manager and trading platform. + 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" + tests: + - not_null + - &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: labels_balancer_cowswap_amm_pools_ethereum + meta: + blockchain: ethereum + sector: labels + project: balancer_cowswap_amm + contributors: viniabussafi + config: + tags: ['labels', 'ethereum', 'balancer', 'pools'] + description: "Balancer CoWSwap AMM liquidity pools created on Ethereum." + tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - address + columns: + - *blockchain + - &address + name: address + description: "Address of liquidity pool" + - &name + name: name + description: "Label name of pool containing the token symbols and their respective weights (if applicable)" + - name: pool_type + - &category + name: category + description: "Label category" + - &contributor + name: contributor + description: "Wizard(s) contributing to labels" + - &source + name: source + description: "How were labels generated (could be static or query)" + - &created_at + name: created_at + description: "When were labels created" + - &updated_at + name: updated_at + description: "When were labels updated for the last time" + - &model_name + name: model_name + description: "Name of the label model sourced from" + - &label_type + name: label_type + description: "Type of label (see labels overall readme)" + + - name: balancer_cowswap_amm_ethereum_liquidity + meta: + blockchain: ethereum + project: balancer_cowswap_amm + contributors: viniabussafi + config: + tags: ['ethereum', 'balancer', 'pools', 'liquidity'] + description: > + Balancer CoWSwap AMM pools liquidity by token in Ethereum. + 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 + - *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" diff --git a/dbt_subprojects/daily_spellbook/models/_projects/balancer_cowswap_amm/ethereum/balancer_cowswap_amm_ethereum_balances.sql b/dbt_subprojects/daily_spellbook/models/_projects/balancer_cowswap_amm/ethereum/balancer_cowswap_amm_ethereum_balances.sql new file mode 100644 index 00000000000..f28343b5f38 --- /dev/null +++ b/dbt_subprojects/daily_spellbook/models/_projects/balancer_cowswap_amm/ethereum/balancer_cowswap_amm_ethereum_balances.sql @@ -0,0 +1,76 @@ +{{ + config( + schema = 'balancer_cowswap_amm_ethereum', + alias = 'balances', + materialized = 'table', + file_format = 'delta' + ) +}} + +WITH pools AS ( + SELECT + bPool AS pools + FROM {{ source('b_cow_amm_ethereum', '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_ethereum', '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_ethereum', '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-07-01'), date(now()), interval '1' day)) AS t(date_sequence) +) + +SELECT + c.day, + 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/daily_spellbook/models/_projects/balancer_cowswap_amm/ethereum/balancer_cowswap_amm_ethereum_liquidity.sql b/dbt_subprojects/daily_spellbook/models/_projects/balancer_cowswap_amm/ethereum/balancer_cowswap_amm_ethereum_liquidity.sql new file mode 100644 index 00000000000..ab7b91a7660 --- /dev/null +++ b/dbt_subprojects/daily_spellbook/models/_projects/balancer_cowswap_amm/ethereum/balancer_cowswap_amm_ethereum_liquidity.sql @@ -0,0 +1,82 @@ +{% set blockchain = 'ethereum' %} + +{{ + config( + schema='balancer_cowswap_amm_' + blockchain, + alias = 'liquidity', + materialized = 'table', + file_format = 'delta' + ) +}} + +WITH pool_labels AS ( + SELECT + address, + name + FROM {{ ref('labels_balancer_cowswap_amm_pools_ethereum') }} + ), + + 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, + AVG(price) AS eth_price + FROM {{ source('prices', 'usd') }} + WHERE blockchain = '{{blockchain}}' + AND symbol = 'ETH' + GROUP BY 1 + ), + + cumulative_balance AS ( + SELECT + day, + pool_address, + token_address, + token_balance_raw + FROM {{ ref('balancer_cowswap_amm_ethereum_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 +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/dbt_subprojects/daily_spellbook/models/_projects/balancer_cowswap_amm/ethereum/balancer_cowswap_amm_ethereum_trades.sql b/dbt_subprojects/daily_spellbook/models/_projects/balancer_cowswap_amm/ethereum/balancer_cowswap_amm_ethereum_trades.sql new file mode 100644 index 00000000000..1d17d5770fb --- /dev/null +++ b/dbt_subprojects/daily_spellbook/models/_projects/balancer_cowswap_amm/ethereum/balancer_cowswap_amm_ethereum_trades.sql @@ -0,0 +1,73 @@ +{% set blockchain = 'ethereum' %} + +{{ + config( + schema = 'balancer_cowswap_amm_' + blockchain, + alias = 'trades', + materialized = 'incremental', + file_format = 'delta', + incremental_strategy = 'merge', + incremental_predicates = [incremental_predicate('DBT_INTERNAL_DEST.block_time')], + unique_key = ['tx_hash', 'evt_index'] + ) +}} + + SELECT + '{{blockchain}}' AS blockchain + , 'balancer_cowswap_amm' AS project + , '1' AS version + , CAST(date_trunc('month', trade.evt_block_time) AS DATE) AS block_month + , CAST(date_trunc('day', trade.evt_block_time) AS DATE) AS block_date + , trade.evt_block_time AS block_time + , trade.evt_block_number block_number + , tb.symbol AS token_bought_symbol + , tb.symbol AS token_sold_symbol + , CONCAT(ts.symbol, '-', tb.symbol) AS token_pair + , (trade.buyAmount / POWER(10, COALESCE(pb.decimals, tb.decimals))) AS token_bought_amount + , ((trade.sellAmount - trade.feeAmount) / POWER(10, COALESCE(ps.decimals, ts.decimals))) AS token_sold_amount + , trade.buyAmount AS token_bought_amount_raw + , trade.sellAmount AS token_sold_amount_raw + , COALESCE(trade.buyAmount / POWER(10, COALESCE(pb.decimals, tb.decimals)) * pb.price, + trade.sellAmount / POWER(10, COALESCE(ps.decimals, ts.decimals)) * ps.price) + AS amount_usd + ,trade.buyToken AS token_bought_address + ,trade.sellToken AS token_sold_address + ,CAST(NULL AS VARBINARY) AS taker + ,CAST(NULL AS VARBINARY) AS maker + , pool.bPool AS project_contract_address + , trade.evt_tx_hash AS tx_hash + , settlement.solver AS tx_from + , trade.contract_address AS tx_to + , trade.evt_index AS evt_index + , p.name AS pool_symbol + , p.pool_type + , (trade.feeAmount / POWER (10, ts.decimals)) AS swap_fee + FROM {{ source('gnosis_protocol_v2_ethereum', 'GPv2Settlement_evt_Trade') }} trade + INNER JOIN {{ source('b_cow_amm_ethereum', 'BCoWFactory_evt_LOG_NEW_POOL') }} pool + ON trade.owner = pool.bPool + LEFT JOIN {{ source('prices', 'usd') }} AS ps + ON sellToken = ps.contract_address + AND ps.minute = date_trunc('minute', trade.evt_block_time) + AND ps.blockchain = '{{blockchain}}' + {% if is_incremental() %} + AND {{ incremental_predicate('ps.minute') }} + {% endif %} + LEFT JOIN {{ source('prices', 'usd') }} AS pb + ON pb.contract_address = buyToken + AND pb.minute = date_trunc('minute', trade.evt_block_time) + AND pb.blockchain = '{{blockchain}}' + {% if is_incremental() %} + AND {{ incremental_predicate('pb.minute') }} + {% endif %} + LEFT JOIN {{ source('tokens', 'erc20') }} AS ts + ON trade.sellToken = ts.contract_address + AND ts.blockchain = '{{blockchain}}' + LEFT JOIN {{ source('tokens', 'erc20') }} AS tb + ON trade.buyToken = tb.contract_address + AND tb.blockchain = '{{blockchain}}' + LEFT JOIN {{ source('gnosis_protocol_v2_ethereum', 'GPv2Settlement_evt_Settlement') }} AS settlement + ON trade.evt_tx_hash = settlement.evt_tx_hash + LEFT JOIN {{ ref('labels_balancer_cowswap_amm_pools_ethereum') }} p ON p.address = trade.owner + {% if is_incremental() %} + WHERE {{ incremental_predicate('trade.evt_block_time') }} + {% endif %} \ No newline at end of file diff --git a/dbt_subprojects/daily_spellbook/models/_projects/balancer_cowswap_amm/ethereum/labels_balancer_cowswap_amm_pools_ethereum.sql b/dbt_subprojects/daily_spellbook/models/_projects/balancer_cowswap_amm/ethereum/labels_balancer_cowswap_amm_pools_ethereum.sql new file mode 100644 index 00000000000..f99ac40c9c8 --- /dev/null +++ b/dbt_subprojects/daily_spellbook/models/_projects/balancer_cowswap_amm/ethereum/labels_balancer_cowswap_amm_pools_ethereum.sql @@ -0,0 +1,73 @@ +{% set blockchain = 'ethereum' %} + +{{config( + schema = 'labels', + alias = 'balancer_cowswap_amm_pools_' + blockchain, + materialized = 'table', + file_format = 'delta' + ) +}} + +WITH events AS ( + -- binds + SELECT call_block_number AS block_number, + contract_address AS pool, + token, + denorm + FROM {{ source('b_cow_amm_ethereum', 'BCoWPool_call_bind') }} + WHERE call_success + + UNION all + + -- unbinds + SELECT call_block_number AS block_number, + contract_address AS pool, + token, + uint256 '0' AS denorm + FROM {{ source('b_cow_amm_ethereum', 'BCoWPool_call_unbind') }} + WHERE call_success +), + +state_with_gaps AS ( + SELECT events.block_number + , events.pool + , events.token + , CAST(events.denorm AS uint256) AS denorm, + LEAD(events.block_number, 1, 99999999) OVER (PARTITION BY events.pool, events.token ORDER BY events.block_number) AS next_block_number + FROM events +), + +settings AS ( + SELECT pool, + coalesce(t.symbol,'?') AS symbol, + denorm, + next_block_number + FROM state_with_gaps s + LEFT JOIN {{ source('tokens', 'erc20') }} t ON s.token = t.contract_address + AND t.blockchain = '{{blockchain}}' + WHERE next_block_number = 99999999 + AND denorm > uint256 '0' +), + +final AS ( + SELECT + '{{blockchain}}' AS blockchain, + pool AS address, + CONCAT('BCowAMM: ', array_join(array_agg(symbol), '/')) AS name, + 'Balancer CoWSwap AMM' AS pool_type, + 'balancer_cowswap_amm_pool' AS category, + 'balancerlabs' AS contributor, + 'query' AS source, + timestamp '2024-07-20' AS created_at, + now() AS updated_at, + 'balancer_cowswap_amm_pools_ethereum' AS model_name, + 'identifier' as label_type + FROM ( + SELECT s1.pool, symbol FROM settings s1 + ) s + + GROUP BY 1, 2 +) + +SELECT * +FROM final \ No newline at end of file diff --git a/dbt_subprojects/daily_spellbook/models/_projects/balancer_cowswap_amm/labels_balancer_cowswap_amm_pools.sql b/dbt_subprojects/daily_spellbook/models/_projects/balancer_cowswap_amm/labels_balancer_cowswap_amm_pools.sql new file mode 100644 index 00000000000..a5c0e046dcc --- /dev/null +++ b/dbt_subprojects/daily_spellbook/models/_projects/balancer_cowswap_amm/labels_balancer_cowswap_amm_pools.sql @@ -0,0 +1,31 @@ +{{config( + schema = 'labels', + alias = 'balancer_cowswap_amm_pool', + materialized = 'view' + ) +}} + +{% set b_cow_amm_models = [ + ref('labels_balancer_cowswap_amm_pools_ethereum') +] %} + +SELECT * +FROM ( + {% for model in b_cow_amm_models %} + SELECT + blockchain, + address, + name, + category, + contributor, + source, + created_at, + updated_at, + model_name, + label_type + FROM {{ model }} + {% if not loop.last %} + UNION ALL + {% endif %} + {% endfor %} +) \ No newline at end of file diff --git a/sources/_subprojects_outputs/daily_spellbook/_sources.yml b/sources/_subprojects_outputs/daily_spellbook/_sources.yml index e4a89722e62..efb81d12706 100644 --- a/sources/_subprojects_outputs/daily_spellbook/_sources.yml +++ b/sources/_subprojects_outputs/daily_spellbook/_sources.yml @@ -16,8 +16,8 @@ sources: tables: - name: disperse_contracts - name: contract_creator_project_mapping - + - name: labels tables: - - name: addresses - - name: jelly_swap_pools_sei + - name: jelly_swap_pools_sei + - name: addresses \ No newline at end of file diff --git a/sources/_subprojects_outputs/dex/_sources.yml b/sources/_subprojects_outputs/dex/_sources.yml index 7882e594e4d..fc198a87fd1 100644 --- a/sources/_subprojects_outputs/dex/_sources.yml +++ b/sources/_subprojects_outputs/dex/_sources.yml @@ -67,4 +67,4 @@ sources: - name: trades - name: cow_protocol_arbitrum tables: - - name: trades \ No newline at end of file + - name: trades diff --git a/sources/balancer_cowswap_amm/ethereum/_sources.yml b/sources/balancer_cowswap_amm/ethereum/_sources.yml new file mode 100644 index 00000000000..7282c119ef1 --- /dev/null +++ b/sources/balancer_cowswap_amm/ethereum/_sources.yml @@ -0,0 +1,9 @@ +version: 2 +sources: + - name: b_cow_amm_ethereum + tables: + - name: BCoWFactory_evt_LOG_NEW_POOL + - name: BCoWFactory_evt_COWAMMPoolCreated + - name: BCoWFactory_call_logBCoWPool + - name: BCoWPool_call_bind + - name: BCoWPool_call_unbind \ No newline at end of file