CryptoLionRanges_Rewards
    Updated 2022-02-02
    -- Current Top 5 liquidity pools FROM pool_stats
    -- Key columns: pool_name, liquidity
    WITH a as (
    SELECT DISTINCT
    date_trunc('day', ps1.block_timestamp) as day,
    pool_name,
    last_value(token0_balance_usd + token1_balance_usd) over (partition by pool_name order by day) as liq,
    last_value(virtual_liquidity_adjusted) over (partition by pool_name order by day) as vliq,
    last_value(price_0_1) over (partition by pool_name order by day) as cp
    FROM uniswapv3.pool_stats ps1
    WHERE block_timestamp >= getdate() - interval '1 day'
    AND token0_balance_usd >= 0
    AND token1_balance_usd >= 0
    --
    -- AND pool_name LIKE 'WBTC-WETH%'
    --
    HAVING day = '2021-06-06'
    ORDER BY 3 DESC
    LIMIT 5
    )
    ,
    -- Get latest position information for each LP
    b as (
    SELECT DISTINCT
    -- block_timestamp,
    last_value(liquidity_provider) over (partition by liquidity_provider, po.pool_name order by block_timestamp) as liq_provider,
    last_value(po.pool_name) over (partition by liquidity_provider, po.pool_name order by block_timestamp) as pool_name,
    last_value(liquidity_adjusted) over (partition by liquidity_provider, po.pool_name order by block_timestamp) as liq_adj,
    last_value(price_lower_0_1) over (partition by liquidity_provider, po.pool_name order by block_timestamp) as pl,
    last_value(price_upper_0_1) over (partition by liquidity_provider, po.pool_name order by block_timestamp) as pu
    FROM uniswapv3.positions po
    INNER JOIN a on po.pool_name = a.pool_name
    WHERE
    is_active = 'TRUE'
    ),
    c as (