Query editor
-- Computes the TVL, lp token total supply and lp token price of a balancer v2 pool over time-- Parameters-- {{pool}} - pool address-- {{start}} - date as of which the analysis should run-- {{blockchain}} - chain for which the query is running-- Given that we might not have records every day in the source data (e.g. not every day the lp supply may change),-- but still want to visualize development on a per day basis, we create an auxiliary table with one record per-- day between `start` and `now`with date_range as (select t.dayfromunnest(sequence(date(timestamp '{{start}}'),date(now()))) t (day) --noqa: AL01),-- get the pool with the largest TVLpool as (selectblockchain,pool_address,sum(pool_liquidity_usd) as tvlfrom balancer.liquiditywherepool_address = {{pool}}and blockchain = '{{blockchain}}'and day = current_date - interval '1' daygroup by 1, 2),-- per day lp token total supply changes of the uniswap pool by looking at burn/mint eventslp_supply_delta as (selectdate(evt_block_time) as "day",sum(case when "from" = 0x0000000000000000000000000000000000000000 then value else -value end) as deltafrom erc20_{{blockchain}}.evt_transferwherecontract_address = (select pool_address from pool)and ("from" = 0x0000000000000000000000000000000000000000 or "to" = 0x0000000000000000000000000000000000000000)group by 1),-- per day lp token total supply by summing up all deltas (may have missing records for some days)lp_total_supply_incomplete as (selectday,sum(delta) over (order by day) as total_lpfrom lp_supply_delta),-- performance optimisation: reduce the total range of the join below to the last value before the start periodlp_total_supply_start as (select max(day) as "start"from lp_total_supply_incompletewhere day <= date(timestamp '{{start}}')),-- lp token total supply without date gaps