primo_datasushi_weth_usdc_impermanent_loss_poly
    Updated 2022-07-22
    -- polygon
    with depositers as (
    select date(block_timestamp) deposit_dt
    , block_id deposit_block
    , tx_id deposit_tx_hash
    , origin_address deposit_wallet
    , sum(case when symbol='WETH' then amount else null end) deposit_weth_amount
    , sum(case when symbol='USDC' then amount else null end) deposit_usdc_amount
    , sum(amount_usd) deposit_usd_total
    , avg(case when symbol='WETH' then (amount_usd / amount) else null end) deposit_weth_price
    from flipside_prod_db.polygon.udm_events
    where date(block_timestamp) >= date('2022-01-13') and date(block_timestamp) <= date('2022-07-21')
    and origin_function_name = 'addLiquidity'
    and to_address = '0x34965ba0ac2451a34a0471f04cca3f990b8dea27' --USDC/WETH sushiswap pool_address
    and symbol in ('WETH','USDC')
    and amount > 0
    group by 1,2,3,4
    having count(tx_id) = 2
    ),
    final_price as (
    --- avg USDC/WETH price on date of block 15180000
    select avg((case when symbol_in='USDC' then amount_in else amount_out end) / (case when symbol_out='WETH' then amount_out else amount_in end)) final_weth_price
    FROM polygon.sushi.ez_swaps
    where date(block_timestamp) = date('2022-07-21')
    and contract_address = '0x34965ba0ac2451a34a0471f04cca3f990b8dea27'
    ),
    results as (
    select *
    , final_price.final_weth_price
    , (deposit_usdc_amount + (deposit_weth_amount * final_price.final_weth_price)) final_total_baseline_usd
    , ((deposit_usdc_amount * deposit_weth_amount) / sqrt(deposit_usdc_amount * deposit_weth_amount* final_price.final_weth_price)) final_weth_amount
    , ((deposit_usdc_amount * deposit_weth_amount) / final_weth_amount) final_usdc_amount
    , ((final_weth_amount * final_price.final_weth_price) + final_usdc_amount) final_total_depositer_usd
    , (1 - final_total_depositer_usd / final_total_baseline_usd)*100 impermanent_loss
    from depositers, final_price
    )
    Run a query to Download Data