Add Balancer trades models - #10
Open
tiagoponciano wants to merge 1 commit into
Open
Conversation
…nhance trade macros
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR introduces a complete Balancer trades pipeline in dbt, aligned with the existing Dune spell structure. It covers V1, V2, and V3 swaps across 11 blockchains, plus CoWSwap AMM trades on Ethereum. The implementation uses a layered macro-driven architecture with incremental enrichment, supporting pool swap-fee models required for accurate V2 fee attribution.
Motivation
Balancer trades are a core downstream dependency for protocol analytics (volume, fees, pool activity). This PR ports that logic into dbt so trades can be built incrementally, tested, and composed with other Balancer models in the same project.
Architecture
The pipeline follows a three-tier model structure:
balancer_v{1,2,3}_{{chain}}_base_tradesbalancer_v{1,2,3}_{{chain}}_tradesamount_usdand token amountsbalancer_{{chain}}_tradesbalancer_tradesEnriched V2/V3 models join the curated
dex.tradestable for token symbols, amounts, and baseline USD pricing, then merge Balancer-specific fields (pool_id,swap_fee,pool_symbol,pool_type) from the corresponding base model. V1 and CoWSwap AMM models build the full output schema directly from decoded events and price sources.Macros (
balancer_trades_macro.sql)balancer_compatible_v1_tradesBPool_evt_LOG_SWAP; resolve swap fee from latestsetSwapFeecall per poolbalancer_compatible_v2_tradesVault_evt_Swap; join historical swap fees from per-chainpools_feesmodelsbalancer_compatible_v3_tradesVault_evt_Swap; handle buffer swaps by matching nearestWrap/Unwrapevents; attach V3 pool labelsbalancer_v1_enriched_tradesprices.usd, ERC20 decimals, and V1 pool labelsbalancer_v2_enriched_tradesdex.trades+ base trades; BPT price fallback foramount_usdwhen spot price is unavailablebalancer_v3_enriched_tradesdex.trades+ base trades; ERC-4626 price fallback for wrapped-token swapsbalancer_cowswap_amm_tradesGPv2Settlement_evt_Trade, linked to BCoW pools and settlement solverbalancer_trades_unionUNION ALLacross chain or version modelsbalancer_trades_dev_time_predicateChain and version coverage
Total: 50+ model files across
models/_project/balancer/trades/andmodels/_project/balancer_cowswap_amm/.Output schema
All trade models expose a consistent column set compatible with
dex.trades: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,pool_id,swap_fee,project_contract_address,pool_symbol,pool_type,tx_hash,tx_from,tx_to,evt_indexPool swap fees (supporting models)
V2 trades require historical swap-fee lookups at the time of each swap. This PR adds the supporting
pools_feeslayer:balancer_pools_fees_macro.sqlSwapFeeChangedevents from raw chainlogs(filtered to registered Balancer pools)Vault_evt_SwapFeePercentageChangedfrom decoded sourcesbalancer_v2_pools_feesandbalancer_pools_feesCoWSwap AMM trades
balancer_cowswap_amm_ethereum_trades, included inbalancer_ethereum_tradesGPv2Settlement_evt_Trade, resolves pool viaBCoWFactory_evt_LOG_NEW_POOL, and enriches with settlement solver, token metadata, and USD pricingbalancer_cowswap_amm/ethereum/_schema.ymlSources
sources/balancer/ethereum/balancer_ethereum_sources.ymlBPool_evt_LOG_SWAP,BPool_call_setSwapFee)sources/gnosis_protocol_v2_sources.ymlGPv2Settlement_evt_Trade,GPv2Settlement_evt_Settlementsources/chain_logs_sources.ymllogstables for V2 swap-fee decoding (Ethereum, Arbitrum, Base, Gnosis, Optimism, Polygon, Avalanche C, zkEVM)sources/balancer/avalanche_c/balancer_avalanche_c_sources.ymlVault_evt_Swap,Vault_evt_Wrap,Vault_evt_Unwrap, and related)sources/balancer/hyperevm/balancer_hyperevm_sources.ymlsources/balancer/monad/balancer_monad_sources.yml