Query editor
WITHaddress as (SELECT 0x95C4Bdc54501c1496B8213F81F30CA95760Be793 as wallet_address),redemption_transactions as (SELECT *FROM ethos_reserve_v2_1_optimism.TroveManager_call_burnLUSDAndEmitRedemptionEventWHERE call_success = true),prep_ern_block as (SELECT median_price as oracle_price, hour as block_time, cast(to_unixtime(hour) AS bigint) as block_numberFROM dex.pricesWHERE blockchain = 'optimism'and contract_address = 0xc5b001DC33727F8F26880B184090D3E252470D45AND DATE_TRUNC('day', hour) > TIMESTAMP '2023-12-20'),prep_tx_block as (SELECT call_block_time as evt_block_time, cast(to_unixtime(call_block_time) AS bigint) as evt_block_number, call_block_number as original_evt_block_numberFROM redemption_transactions),liq_ern_block as (SELECTDATE_TRUNC('day', tp.block_time) as price_day, DATE_TRUNC('day', tx.evt_block_time) as liquidation_day, 'ERN' as token,tx.evt_block_number as liq_block,tp.block_number as price_block,tx.original_evt_block_number,ABS(LEAST(tx.evt_block_number - tp.block_number, tp.block_number - tx.evt_block_number, tx.evt_block_number - tp.block_number -1, tx.evt_block_number - tp.block_number + 1)) as smallest_diffFROM prep_tx_block as txCROSS JOIN prep_ern_block AS tp), -- outputs a list of liquidation and price feed blocks and the difference between them in a variable called smallest_diffmin_ern as (SELECT liq_block, MIN(smallest_diff) AS smallest_diffFROM liq_ern_blockGROUP BY liq_block), -- finds the liquidated_block and it's smallest_diff associated with itmin_ern_2 as (SELECT a.*FROM liq_ern_block as aINNER JOIN min_ern as b on a.liq_block = b.liq_block and a.smallest_diff = b.smallest_diff), -- uses the smallest_diff to pull more info from the origial liq_weth_block tablemin_ern_3 as (SELECT a.liq_block, a.original_evt_block_number, b.oracle_price as ern_priceFROM min_ern_2 as aINNER JOIN prep_ern_block as b on a.price_block = b.block_number),price_tx as (-- SELECT b.*, a.symbol as token, a.priceSELECT b._collateral as token_address, b.call_block_time as "timestamp", DATE_TRUNC('day', b.call_block_time) as call_block_time, b.call_block_number, b.call_tx_hash,_collSent - _collFee as amount_redeemed, _collFee as redemption_fee, _actualLUSDAmount / 1e18 as ern_redeemed, price as oracle_priceFROM prices.usd as aINNER JOIN redemption_transactions as b on a.minute = DATE_TRUNC('minute', b.call_block_time) and a.contract_address = b._collateralWHERE blockchain = 'optimism'AND a.contract_address IN(0x4200000000000000000000000000000000000006, 0x68f180fcCe6836688e9084f035309E29Bf0A2095, 0x4200000000000000000000000000000000000042, 0x1F32b1c2345538c0c6f582fCB022739c4A194Ebb)AND DATE_TRUNC('minute', b.call_block_time) > TIMESTAMP '2023-12-20'),holding_basic_stats as (