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),-- 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 = {{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 gapslp_total_supply as (selectday,total_lpfrom (-- join full date range with potentially incomplete data. This results in many rows per day (all total supplies on or before that day)-- rank() is then used to order join candidates by recency (rank = 1 is the latest lp supply)selectdate_range.day,total_lp,rank() over (partition by (date_range.day) order by lp.day desc) as latestfrom date_rangeinner join lp_total_supply_incomplete as lpon