Updated 2023-05-18
    with base as (SELECT

    date_trunc ('day',BLOCK_TIMESTAMP) as Time,

    sum (AMOUNT) as amount,

    sum (CASE WHEN ACTION = 'staking' then AMOUNT end) as stake_amount,
    sum(stake_amount) over (order by Time) as cum_stake_amount,

    sum (CASE WHEN ACTION = 'unstaking' then AMOUNT end) as unstake_amount,
    sum(unstake_amount) over (order by Time) as cum_unstake_amount,


    stake_amount - unstake_amount as Net_amount,
    cum_stake_amount - cum_unstake_amount as Staking_amount

    FROM near.core.fact_staking_actions

    WHERE ACTION in ('staking','unstaking')
    GROUP BY 1)

    SELECT Staking_amount,Price, Staking_amount * Price as USD
    FROM base
    JOIN (SELECT min (PRICE_USD) as Price from near.core.fact_prices WHERE SYMBOL = 'LINEAR' and TIMESTAMP >= current_date -1 and PRICE_USD IS NOT NULL)
    WHERE Time = current_date


    /*

    SELECT
    '406547210.093106' as Current_stake,
    '1.76' as Near_price,
    Current_stake * Near_price as Current_USD_stake
    */

    Run a query to Download Data