Query editor
WITHL (contract, position, asset, market, decimals) AS (VALUES(0x39f31e78a81f5cda2fe9516e3fdf5c528db2f828, 'hedge', 'wbtc', 'wbtc-usdt', 8),(0x5238180c5e282ca6e83c9e54aea80e0993b0ad73, 'earn', 'usdt', 'wbtc-usdt', 6),(0xc600fd019805b77a0f4c1b8970e772dd6f692eb4, 'hedge', 'wsteth', 'wsteth-usdt', 18),(0x6800455fd5d18d3ce3ed76ac34199a9a704be257, 'earn', 'usdt', 'wsteth-usdt', 6)),A AS (SELECT block_time, market, asset, position,bytearray_to_int256(bytearray_substring(data, 33, 32)) AS tokenID,bytearray_to_int256(bytearray_substring(data, 161, 32)) AS actionType,bytearray_to_int256(bytearray_substring(data, 65, 32)) / pow(10, decimals) AS amount,bytearray_to_int256(bytearray_substring(data, 129, 32)) / 1e18 AS price,tx_from AS caller, tx_hashFROM arbitrum.logs AS aJOIN L AS b ON a.contract_address = b.contractWHERE topic0 = 0x62fa5b44efc3da606f89f1259545ef1592661a87182c1b4f02189513f26e0caf),B AS (SELECT block_time, market, position, asset,CASE WHEN actionType IN (0, 5) THEN 'OPEN - open'WHEN actionType IN (3, 7) THEN 'OPEN - renew'WHEN actionType = 1 THEN 'CLOSE - cancel'WHEN actionType = 2 THEN 'CLOSE - claim'WHEN actionType = 9 THEN 'CLOSE - close'WHEN actionType = 6 THEN 'CLOSE'WHEN actionType = 8 THEN 'CLOSE'WHEN actionType = 4 THEN ''ELSE '' END AS event,actionType, tokenID, amount, price, caller, tx_hash,CASE WHEN c.tier IS NOT NULL THEN c.tierWHEN d.tier IS NOT NULL THEN d.tierELSE NULL END AS tierFROM A AS aLEFT JOIN bumper_arbitrum.wbtc_usdt_market_evt_MakerPositionCreated AS c ON a.tx_hash = c.evt_tx_hashLEFT JOIN bumper_arbitrum.wsteth_usdt_market_evt_MakerPositionCreated AS d ON a.tx_hash = d.evt_tx_hash),OpenPositions AS (SELECT DISTINCT caller, market, position, asset, tokenIDFROM BWHERE actionType IN (0, 5, 3, 7)),ClosedPositions AS (SELECT DISTINCT caller, market, position, asset, tokenIDFROM BWHERE actionType IN (1, 2, 9, 6, 8, 4)),ActivePositions AS (SELECT o.*FROM OpenPositions oLEFT JOIN ClosedPositions cON o.caller = c.caller AND o.market = c.market AND o.position = c.position AND o.tokenID = c.tokenIDWHERE c.tokenID IS NULL),