Query editor
with events as (select *from query_3862559 -- Davos Provider Events (Polygon)),daily_agg as (select date_trunc('day', a.evt_block_time) as "date", a.collateral_address, sum(case when event = 'deposit' then amount else -amount end) as sharesfrom events agroup by 1, 2),days as (with days_seq as (select sequence(date('2023-07-04'), date_trunc('day', cast(now() as timestamp)), interval '1' day) as day)select days.day as "date"from days_seqcross join unnest(day) as days(day) -- Unnesting sequence of days),daily_fillers as (select d."date", p.collateral_addressfrom days dcross join (select distinct collateral_address from query_3845337 where blockchain = 'polygon' and collateral_symbol not in ('ceMATIC') -- Davos Vaults and Collateral List) p),balance_data as (select f."date", f.collateral_address, coalesce(d.shares, 0) as shares,sum(coalesce(d.shares, 0)) over (partition by f.collateral_address order by f."date") as token_shares_collateralfrom daily_fillers fleft join daily_agg d on f."date" = d."date" and f.collateral_address = d.collateral_address),prices as (select token, time as "date", price, decimals, symbolfrom query_3865822 -- Davos Collateral Prices (Polygon)where time >= cast('2023-07-04' as timestamp) -- start fetching),final as (select b."date", b.collateral_address, b.shares, p.symbol, p.price, p.price/wmatic.price as matic_ratio, b.token_shares_collateral as token_shares_collateral_per_symbol,b.token_shares_collateral*p.price/wmatic.price as token_matic_collateral_per_symbol, b.token_shares_collateral*p.price as token_usd_collateral_per_symbol,(sum(b.token_shares_collateral*(p.price/wmatic.price)) over(partition by b.date)) as token_matic_collateral,(sum(b.token_shares_collateral*p.price) over(partition by b.date)) as token_usd_collateralfrom balance_data bjoin prices p on p.date = b."date" and b.collateral_address = p.tokenjoin prices wmatic on wmatic."date" = b."date" and wmatic.token = 0x0d500B1d8E8eF31E21C99d1Db9A6444d3ADf1270 -- WMATIC),done as (select *,casewhen token_usd_collateral/lag(token_usd_collateral,1) over (partition by symbol order by "date") - 1 < 0.0001 then 0else token_usd_collateral/lag(token_usd_collateral,1) over (partition by symbol order by "date") - 1end as change_1d, token_usd_collateral/lag(token_usd_collateral,7) over (partition by symbol order by "date") - 1 as change_7dfrom final where token_matic_collateral is not null)select "date" as time, symbol, price, matic_ratio, token_shares_collateral_per_symbol, token_matic_collateral_per_symbol,token_usd_collateral_per_symbol, token_matic_collateral, token_usd_collateral, change_1d as change_1D, change_7d as change_7Dfrom donewhere token_usd_collateral > 0and token_matic_collateral > 0