diff --git a/dbt_subprojects/dex/models/_projects/balancer_cowswap_amm/_schema.yml b/dbt_subprojects/dex/models/_projects/balancer_cowswap_amm/_schema.yml index 00c2c437fdd..cb5cf106587 100644 --- a/dbt_subprojects/dex/models/_projects/balancer_cowswap_amm/_schema.yml +++ b/dbt_subprojects/dex/models/_projects/balancer_cowswap_amm/_schema.yml @@ -3,12 +3,12 @@ version: 2 models: - name: balancer_cowswap_amm_trades meta: - blockchain: arbitrum, ethereum, gnosis + blockchain: arbitrum, base, ethereum, gnosis sector: trades project: balancer_cowswap_amm contributors: viniabussafi config: - tags: ['balancer', 'cowswap', 'ethereum', 'gnosis', 'amm', 'trades'] + tags: ['balancer', 'cowswap', 'arbitrum', 'base', 'ethereum', 'gnosis', 'amm', 'trades'] description: "Trades on Balancer CoWSwap AMM pools" columns: - &blockchain @@ -89,12 +89,12 @@ models: - name: labels_balancer_cowswap_amm_pools meta: - blockchain: arbitrum, ethereum, gnosis + blockchain: arbitrum, base, ethereum, gnosis sector: labels project: balancer_cowswap_amm contributors: viniabussafi config: - tags: ['labels', 'ethereum', 'gnosis', 'balancer', 'pools'] + tags: ['labels', 'arbitrum', 'base', 'ethereum', 'gnosis', 'balancer', 'pools'] description: "Balancer CoWSwap AMM liquidity pools created on Ethereum." columns: - *blockchain diff --git a/dbt_subprojects/dex/models/_projects/balancer_cowswap_amm/balancer_cowswap_amm_trades.sql b/dbt_subprojects/dex/models/_projects/balancer_cowswap_amm/balancer_cowswap_amm_trades.sql index 4f0799af9a2..9a40561f718 100644 --- a/dbt_subprojects/dex/models/_projects/balancer_cowswap_amm/balancer_cowswap_amm_trades.sql +++ b/dbt_subprojects/dex/models/_projects/balancer_cowswap_amm/balancer_cowswap_amm_trades.sql @@ -8,6 +8,7 @@ {% set b_cow_amm_models = [ ref('balancer_cowswap_amm_arbitrum_trades'), + ref('balancer_cowswap_amm_base_trades'), ref('balancer_cowswap_amm_ethereum_trades'), ref('balancer_cowswap_amm_gnosis_trades') ] %} diff --git a/dbt_subprojects/dex/models/_projects/balancer_cowswap_amm/base/_schema.yml b/dbt_subprojects/dex/models/_projects/balancer_cowswap_amm/base/_schema.yml new file mode 100644 index 00000000000..d88a53d0d85 --- /dev/null +++ b/dbt_subprojects/dex/models/_projects/balancer_cowswap_amm/base/_schema.yml @@ -0,0 +1,137 @@ +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" + data_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: 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." + data_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)" \ No newline at end of file diff --git a/dbt_subprojects/dex/models/_projects/balancer_cowswap_amm/base/balancer_cowswap_amm_base_trades.sql b/dbt_subprojects/dex/models/_projects/balancer_cowswap_amm/base/balancer_cowswap_amm_base_trades.sql new file mode 100644 index 00000000000..f0bff029252 --- /dev/null +++ b/dbt_subprojects/dex/models/_projects/balancer_cowswap_amm/base/balancer_cowswap_amm_base_trades.sql @@ -0,0 +1,74 @@ +{% 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' 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 + , ts.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 pool_id + , 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 %} diff --git a/dbt_subprojects/dex/models/_projects/balancer_cowswap_amm/base/labels_balancer_cowswap_amm_pools_base.sql b/dbt_subprojects/dex/models/_projects/balancer_cowswap_amm/base/labels_balancer_cowswap_amm_pools_base.sql new file mode 100644 index 00000000000..f920b51e002 --- /dev/null +++ b/dbt_subprojects/dex/models/_projects/balancer_cowswap_amm/base/labels_balancer_cowswap_amm_pools_base.sql @@ -0,0 +1,70 @@ +{% 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' +) + + SELECT + '{{blockchain}}' AS blockchain, + pool AS address, + CONCAT('BCowAMM: ', array_join(array_agg(symbol), '/'), ' ', array_join(array_agg(cast(norm_weight AS varchar)), '/')) 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, cast(100*denorm/total_denorm AS integer) AS norm_weight FROM settings s1 + INNER JOIN (SELECT pool, sum(denorm) AS total_denorm FROM settings GROUP BY pool) s2 + ON s1.pool = s2.pool + ORDER BY 1 ASC , 3 DESC, 2 ASC + ) s + GROUP BY 1, 2 \ No newline at end of file diff --git a/dbt_subprojects/dex/models/_projects/balancer_cowswap_amm/labels_balancer_cowswap_amm_pools.sql b/dbt_subprojects/dex/models/_projects/balancer_cowswap_amm/labels_balancer_cowswap_amm_pools.sql index c4032bb58a7..35dfe348fb2 100644 --- a/dbt_subprojects/dex/models/_projects/balancer_cowswap_amm/labels_balancer_cowswap_amm_pools.sql +++ b/dbt_subprojects/dex/models/_projects/balancer_cowswap_amm/labels_balancer_cowswap_amm_pools.sql @@ -7,6 +7,7 @@ {% set b_cow_amm_models = [ ref('labels_balancer_cowswap_amm_pools_arbitrum'), + ref('labels_balancer_cowswap_amm_pools_base'), ref('labels_balancer_cowswap_amm_pools_ethereum'), ref('labels_balancer_cowswap_amm_pools_gnosis') ] %} diff --git a/sources/balancer_cowswap_amm/base/_sources.yml b/sources/balancer_cowswap_amm/base/_sources.yml new file mode 100644 index 00000000000..a122c59a6ca --- /dev/null +++ b/sources/balancer_cowswap_amm/base/_sources.yml @@ -0,0 +1,9 @@ +version: 2 +sources: + - name: b_cow_amm_base + 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