cypherRUNE LP-retention
    Updated 2022-04-24
    with first_month_liquidity_adding as (select
    date_trunc('month', min(block_timestamp)) as month,
    from_address as address
    from thorchain.liquidity_actions
    where lp_action = 'add_liquidity'
    and from_address is not null
    group by address
    ),

    n_users_month as (select month, count(*) as n_users
    from first_month_liquidity_adding
    group by month),

    liquidity_add as (select
    from_address,
    sum(rune_amount) as total_rune_added
    from thorchain.liquidity_actions
    where lp_action = 'add_liquidity'
    and from_address is not null
    group by from_address
    ),

    liquidity_remove as ( select
    from_address,
    sum(rune_amount) as total_rune_withdrawn
    from thorchain.liquidity_actions
    where lp_action = 'remove_liquidity'
    and from_address is not null
    group by from_address
    ),

    net_liquidity_balance as (select
    a.from_address as address,
    a.total_rune_added - r.total_rune_withdrawn as lp_balance,
    iff(lp_balance < 0, 0, lp_balance ) as real_lp_balance
    from liquidity_add a, liquidity_remove r