balancer / dune-spellbook

SQL views for Dune Analytics
Other
7 stars 12 forks source link

Analyse percentage of BPT staked #78

Closed viniabussafi closed 1 month ago

viniabussafi commented 2 months ago

A request came up from the maxis in order to check what's the % of BPTs staked per pool on mainnet and, in turn, to what % of our TVL it amounts to.

viniabussafi commented 2 months ago

Current snapshot

mendesfabio commented 2 months ago
viniabussafi commented 2 months ago

Analysing the outliers (results where the number of BPTs staked > BPT supply), me and Fábio identified an error on the bpt_supply spell, which was addressed on this PR.

Previous code:

` 
   joins_and_exits AS (
        SELECT 
            j.block_date, 
            j.tokenOut AS bpt, 
            SUM(COALESCE(ajoins, 0) - COALESCE(aexits, 0)) OVER (PARTITION BY j.tokenOut ORDER BY j.block_date ASC) AS adelta
        FROM joins j
        FULL OUTER JOIN exits e ON j.block_date = e.block_date AND e.tokenIn = j.tokenOut
    ),

Now:

   joins_and_exits_1 AS (
        SELECT 
            *
        FROM joins 

        UNION ALL

        SELECT 
            *
        FROM exits
            ),

    joins_and_exits AS (
        SELECT 
            block_date, 
            token AS bpt, 
            LEAD(block_date, 1, NOW()) OVER (PARTITION BY token ORDER BY block_date) AS day_of_next_change,
            SUM(amount) OVER (PARTITION BY token ORDER BY block_date ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS adelta
        FROM joins_and_exits_1
    ),