Query editor
-- works only for univ3 WBTC-ETH (or WBTC-WETH) positions on ethereum after position is closed.-- for all practical purpose, ETH and WETH are interchangeable in univ3 pools. I'll use ETH because it's shorter.-- WBTC decimals: 8-- WETH decimals: 18-- ETH gas calc: (t.gas_price * t.gas_used)/1e18with add_liquidity as (select tokenId, evt_tx_hash, evt_block_time,-cast(amount0 as double)/1e8 as amount_wbtc, -- negative cuz token left wallet-cast(amount1 as double)/1e18 as amount_ethfrom uniswap_v3_ethereum.NonfungiblePositionManager_evt_IncreaseLiquiditywhere tokenId = cast({{Ethereum UniV3 WBTC-ETH LP NFT id}} as uint256) -- NFT id),remove_liquidity as (select tokenId, evt_tx_hash, evt_block_time,cast(amount0 as double)/1e8 as amount_wbtc, -- positive cuz token returned to walletcast(amount1 as double)/1e18 as amount_ethfrom uniswap_v3_ethereum.NonfungiblePositionManager_evt_DecreaseLiquiditywhere tokenId = cast({{Ethereum UniV3 WBTC-ETH LP NFT id}} as uint256) -- NFT id),add_n_remove_liquidity as (select a.*,-- add gas column(t.gas_price * t.gas_used)/1e18 as gas_ethfrom (select * from add_liquidityUNIONselect * from remove_liquidity) aleft join ethereum.transactions t on t.hash = a.evt_tx_hash),claim_fees_helper as (select tokenId, evt_tx_hash, evt_block_time,cast(amount0 as double)/1e8 as amount_wbtc, -- positive cuz receice fee tokenscast(amount1 as double)/1e18 as amount_ethfrom uniswap_v3_ethereum.NonfungiblePositionManager_evt_Collectwhere tokenId = cast({{Ethereum UniV3 WBTC-ETH LP NFT id}} as uint256) -- NFT id),claim_fees as (select c.tokenId, c.evt_tx_hash, c.evt_block_time,-- remove double counting from liquidity removal, which auto-collects feesc.amount_eth - COALESCE(a.amount_eth, 0) as amount_eth,c.amount_wbtc - COALESCE(a.amount_wbtc, 0) as amount_wbtc,(t.gas_price * t.gas_used)/1e18 - COALESCE(a.gas_eth, 0) as gas_ethfrom claim_fees_helper cleft join add_n_remove_liquidity a on a.evt_tx_hash = c.evt_tx_hashleft join ethereum.transactions t on t.hash = c.evt_tx_hash),tot_lp as (select tokenId as nft_id,min(evt_block_time) as open_datetime,max(evt_block_time) as close_datetime,sum(if(amount_wbtc < 0, amount_wbtc, 0)) as sent_wbtc,sum(if(amount_eth < 0, amount_eth, 0)) as sent_eth,sum(if(amount_wbtc > 0, amount_wbtc, 0)) as withdrawn_wbtc,sum(if(amount_eth > 0, amount_eth, 0)) as withdrawn_eth,sum(gas_eth) as lp_gas_ethfrom add_n_remove_liquiditygroup by 1),