Query editor
with pairs as (select token0, token1,CAST(token0 as varchar) || '_' || CAST(token1 as varchar) as native_pair,LEAST(CAST(token0 as varchar), CAST(token1 as varchar)) || '_' || GREATEST(CAST(token0 as varchar), CAST(token1 as varchar)) AS pair_alphafrom carbon_defi_ethereum.CarbonController_evt_PairCreated),created as (select *from carbon_defi_ethereum.CarbonController_evt_StrategyCreated),updated as (select *from carbon_defi_ethereum.CarbonController_evt_StrategyUpdated),deleted as (select *from carbon_defi_ethereum.CarbonController_evt_StrategyDeleted),clean_updated as (SELECT updated.*FROM updatedJOIN (SELECT id, max(evt_block_number) as max_bnFROM updatedwhere updated.id not in (select id from deleted)group by id) t ON (updated.id = t.id) and (updated.evt_block_number = t.max_bn)),clean_created as (select *from createdwhere (created.id not in (select id from deleted))and (created.id not in (select id from updated))),current_orders1 as (select id, order0, order1, token0, token1, LEAST(CAST(token0 as varchar), CAST(token1 as varchar)) || '_' || GREATEST(CAST(token0 as varchar), CAST(token1 as varchar)) AS pair_alpha --evt_tx_hash, evt_block_time, evt_block_number,from clean_updatedUNION ALLselect id, order0, order1, token0, token1, LEAST(CAST(token0 as varchar), CAST(token1 as varchar)) || '_' || GREATEST(CAST(token0 as varchar), CAST(token1 as varchar)) AS pair_alpha --evt_tx_hash, evt_block_time, evt_block_number,from clean_created),current_orders2 as (select curr.id, curr.order0, curr.order1, curr.token0, curr.token1, pair_alpha, ownerfrom current_orders1 currleft join created c on c.id = curr.id),order_correction as (select id, native_pair, owner,CASEWHEN CAST(c.token0 as varchar) || '_' || CAST(c.token1 as varchar) = native_pair then order0ELSE order1END as new_order0,CASEWHEN CAST(c.token0 as varchar) || '_' || CAST(c.token1 as varchar) = native_pair then order1ELSE order0END as new_order1,