Nige7777Copy of Positions out of range
    Updated 2021-11-10

    WITH max_pos_blocks AS (
    SELECT
    max(block_id) AS block_id,
    pool_address,
    liquidity_provider,
    nf_token_id

    FROM
    uniswapv3.positions
    GROUP BY
    pool_address,
    liquidity_provider,
    nf_token_id
    ),

    max_stats_blocks AS (
    SELECT
    max(block_id) AS block_id,
    pool_address

    FROM
    uniswapv3.pool_stats
    GROUP BY
    pool_address
    ),

    current_prices AS (
    SELECT
    price_0_1 AS current_price_0_1,
    token0_balance_usd / token0_balance_adjusted AS price0_usd,
    price_1_0 AS current_price_1_0,
    token1_balance_usd / token1_balance_adjusted AS price1_usd,
    p.pool_address
    FROM
    uniswapv3.pool_stats p
    INNER JOIN max_stats_blocks mb ON
    mb.block_id = p.block_id AND
    mb.pool_address = p.pool_address
    --WHERE token0_balance_adjusted > 0 AND token1_balance_adjusted > 0
    )
    ,cte_results as(

    SELECT

    p.pool_address,
    pool_name,
    left(pool_name, REGEXP_INSTR(pool_name,'\\s\\d')) Pool_,
    p.liquidity_provider,
    p.nf_token_id,
    liquidity_adjusted,
    current_price_1_0,
    price_lower_1_0,
    price_upper_1_0,

    CASE
    WHEN current_price_1_0 <= price_lower_1_0 THEN 'above price'
    WHEN current_price_1_0 < price_upper_1_0 and price_lower_1_0 <= current_price_1_0 THEN 'in price'
    ELSE 'below price'
    END AS position_state,


    CASE
    WHEN current_price_1_0 <= price_lower_1_0 THEN liquidity_adjusted / sqrt(price_lower_1_0) - liquidity_adjusted / sqrt(price_upper_1_0)
    WHEN current_price_1_0 < price_upper_1_0 THEN liquidity_adjusted / sqrt(current_price_1_0) - liquidity_adjusted / sqrt(price_upper_1_0)
    ELSE 0
    END AS current_amount_0,

    CASE
    WHEN current_price_1_0 <= price_lower_1_0 THEN ( liquidity_adjusted / sqrt(price_lower_1_0) - liquidity_adjusted / sqrt(price_upper_1_0) ) * COALESCE(price0_usd, 0)
    WHEN current_price_1_0 < price_upper_1_0 THEN ( liquidity_adjusted / sqrt(current_price_1_0) - liquidity_adjusted / sqrt(price_upper_1_0) ) * COALESCE(price0_usd, 0)
    ELSE 0
    END AS current_amount_0_usd,

    CASE
    WHEN current_price_1_0 <= price_lower_1_0 THEN 0
    WHEN current_price_1_0 < price_upper_1_0 THEN liquidity_adjusted * sqrt(current_price_1_0) - liquidity_adjusted * sqrt(price_lower_1_0)
    ELSE liquidity_adjusted * sqrt(price_upper_1_0) - liquidity_adjusted * sqrt(price_lower_1_0)
    END AS current_amount_1,

    CASE
    WHEN current_price_1_0 <= price_lower_1_0 THEN 0
    WHEN current_price_1_0 < price_upper_1_0 THEN ( liquidity_adjusted * sqrt(current_price_1_0) - liquidity_adjusted * sqrt(price_lower_1_0) ) * COALESCE(price1_usd, 0)
    ELSE ( liquidity_adjusted * sqrt(price_upper_1_0) - liquidity_adjusted * sqrt(price_lower_1_0) ) * COALESCE(price1_usd, 0)
    END AS current_amount_1_usd


    FROM uniswapv3.positions p
    INNER JOIN max_pos_blocks mb ON
    mb.block_id = p.block_id AND
    mb.pool_address = p.pool_address AND
    mb.liquidity_provider = p.liquidity_provider AND
    mb.nf_token_id = p.nf_token_id

    JOIN current_prices cp ON p.pool_address = cp.pool_address

    WHERE liquidity_adjusted > 0


    )


    , cte_totals as (
    select distinct

    SUM(current_amount_1_usd+current_amount_0_usd) OVER (partition by pool_ ORDER BY pool_) Total_USD_Amount,
    sum(current_amount_1_usd+current_amount_0_usd) OVER (partition by pool_, position_state ORDER BY pool_) AS Total_USD_IN_STATE,
    pool_address as Tpool_address,
    --pool_name as TPool_Name,
    Pool_ AS Pool_Name,
    position_state
    FROM cte_results

    order by Total_USD_Amount desc

    )
    , cte_last as(
    select distinct
    dense_rank() over ( ORDER BY Total_USD_Amount desc , pool_name ) Ranking
    ,Total_USD_Amount
    ,pool_name
    from cte_totals t
    order by Total_USD_Amount desc
    )


    select
    Pool_ AS Pool_Name
    ,SUM(
    case WHEN (current_amount_1_usd =0 and current_amount_0_usd> 0) OR (current_amount_0_usd = 0 AND current_amount_1_usd >0) then 1 ELSE 0 end
    ) AS Count_Single_Sided_Liquidity
    ,SUM(
    case WHEN current_amount_1_usd > 0 AND current_amount_0_usd > 0 then 1 ELSE 0 end
    ) AS Count_Split_Liquidity
    , count(distinct liquidity_provider) LP_Count



    from
    cte_results r
    left join cte_last l on l.pool_name = r.pool_

    where r.position_state in ('above price' ,'below price')
    and r.pool_ in (select Pool_Name from cte_last where Ranking <=20)
    and current_amount_1_usd + current_amount_0_usd > 100000

    GROUP by pool_ ,l.Ranking
    order by l.Ranking



    Run a query to Download Data