Query editor
-- This query is an attempt at a trades table abstraction for synthetixwith trades as (WITH liq_evts AS (SELECT*FROM erc20_optimism.evt_TransferWHEREcontract_address = 0x8c6f28f2F1A3C87F0f938b96d27520d9751ec8d9AND to = 0xfeEFEEfeefEeFeefEEFEEfEeFeefEEFeeFEEFEeF), liq_table_appended as (selectcoalesce(cast(stakersFee as double), cast(lq.value as double), 0) + coalesce(cast(liquidatorFee as double), cast(fee as double)) + coalesce(cast(flaggerFee as double), 0) as totalFee, coalesce(cast(stakersFee as double), cast(lq.value as double), 0) as stakersFee_appended, coalesce(cast(liquidatorFee as double), cast(fee as double)) as liquidatorFee_appended, coalesce(cast(flaggerFee as double), 0) as flaggerFee_appended, lq.evt_tx_hash as stakerEvt_tx_hash, pl.*from synthetix_futuresmarket_optimism.ProxyPerpsV2_evt_PositionLiquidated plleft JOIN liq_evts lq on pl.evt_tx_hash = lq.evt_tx_hash and pl.evt_index = (lq.evt_index - 1)), liquidation_pre AS (SELECTNULL as tracking_code, COALESCE(ft.baseAsset, NULL) AS PERP, pm.contract_address, pm.evt_tx_hash, pm.evt_block_time, pm.account-- to fee address, CAST(lq.stakersFee_appended AS DOUBLE) / 1e18 AS fee, CAST(pm.price AS DOUBLE) / 1e18 AS lastPrice, 0 as margin, 0 as size, -CAST(pm.size AS DOUBLE) / 1e18 AS tradeSize-- includes stakerFee, liquidatorFee, flaggerFee, cast(totalFee as double) / 1e18 as totalFeeFROM synthetix_futuresmarket_optimism.ProxyPerpsV2_evt_PositionLiquidated pmLEFT JOIN synthetix_futuresmarket_optimism.ProxyPerpsV2_evt_PerpsTracking ft ON pm.evt_tx_hash=ft.evt_tx_hash AND pm.fee=ft.feeleft JOIN liq_table_appended lq on pm.evt_tx_hash = lq.evt_tx_hash and pm.account = (lq.account)), perps_table AS (SELECTmarket as marketKey, underlying_asset as PERP, market_address as contract_addressFROM synthetix.perpetual_tradesgroup by 1,2,3), liquidation_table AS (SELECTa.tracking_code, cast(b.marketKey as varchar) as marketKey, cast(b.perp as varchar) as PERP, a.contract_address