Query editor
withtokens_data AS (SELECTpool_contract, pool_symbol, stable,token0_contract, token1_contract,token0_symbol, token1_symbol,token0_decimals, token1_decimalsFROM query_3283233),pools_data AS (SELECTbl.block_date, bl.tx_hash,t.pool_contract, t.stable, t.pool_symbol,bl.amount0Out = CAST(0 AS uint256) isZeroOut,bl.amount0In / POW(10, t.token0_decimals) AS amount0In_scaled,bl.amount0Out / POW(10, t.token0_decimals) AS amount0Out_scaled,bl.amount1In / POW(10, t.token1_decimals) AS amount1In_scaled,bl.amount1Out / POW(10, t.token1_decimals) AS amount1Out_scaled,t.token0_symbol, t.token1_symbol, t.token0_contract, t.token1_contractFROM (SELECTblock_date, tx_hash, contract_address,bytearray_to_uint256(bytearray_substring(data,2,31)) AS amount0In,bytearray_to_uint256(bytearray_substring(data,2+32,31)) AS amount1In,bytearray_to_uint256(bytearray_substring(data,2+32+32,31)) AS amount0Out,bytearray_to_uint256(bytearray_substring(data,2+32+32+32,31)) AS amount1OutFROM base.logs blWHERE bl.topic0 = 0xd78ad95fa46c994b6551d0da85fc275fe613ce37657fb8d5e3d130840159d822AND bl.contract_address IN (SELECT pool_contractFROM tokens_data)) blLEFT JOIN tokens_data t ON t.pool_contract = bl.contract_addressWHERE (bl.amount0In > 0 AND bl.amount1Out > 0) OR (bl.amount1In > 0 AND bl.amount0Out > 0)),weth_price AS (SELECT AVG(price) AS avg_price, DATE_TRUNC('day', minute) AS truncated_time, contract_addressFROM prices.usdWHERE symbol = 'WETH' AND blockchain = 'linea'GROUP BY DATE_TRUNC('day', minute),3),price_feed AS (SELECTblock_date, stable, token0_symbol, token1_symbol, token0_contract, token1_contract, token0_amount, token1_amount,CASE WHEN stable = false THENCASEWHEN token0_symbol = 'WETH' THEN weth_price.avg_priceWHEN token1_symbol = 'WETH' THEN weth_price.avg_price * pf.token0_priceWHEN token0_symbol != 'WETH' AND token1_symbol != 'WETH' THEN AVG(pf.token0_price)ENDELSE AVG(pf.token0_price)END AS token0_avgPrice,CASE WHEN stable = false THENCASEWHEN token1_symbol = 'WETH' THEN weth_price.avg_priceWHEN token0_symbol = 'WETH' THEN weth_price.avg_price * pf.token1_priceWHEN token0_symbol != 'WETH' AND token1_symbol != 'WETH' THEN AVG(pf.token1_price)END