Query editor
WITHswaps AS (SELECTdate_trunc('minute', call_block_time) AS time, t2."from" AS raw_source, t1."from" AS direct_source, row_number() OVER (PARTITION BY s.call_tx_hash, t1.trace_address ORDER BY cardinality(t2.trace_address) desc) AS rn -- there are only one trace1, and multiple trace2 that matches trace1, s.call_tx_hash AS tx_hash, ABS(s.output_amount0) AS token0_amount, pc.token0 AS token0_addressFROM uniswap_v3_ethereum.Pair_call_swap sLEFT JOIN ethereum.traces t1ON t1.tx_hash = s.call_tx_hash-- AND t1.to = s.contract_address -> i put this in, but maybe duplicative (already filtered by s.contract_address)AND t1.trace_address = s.call_trace_addressAND t1.tx_successAND s.call_block_time = t1.block_time -- to reduce query timeLEFT JOIN ethereum.traces t2ON t1."from" = t2.toAND t1."from" != t2."from" -- actual caller != direct callerAND COALESCE(cardinality(t1.trace_address), 0) >= COALESCE(cardinality(t2.trace_address), 0) -- t1 comes laterAND t1.tx_hash = t2.tx_hash -- in the same txAND t2.tx_successAND t2.block_time = t1.block_timeLEFT JOIN uniswap_v3_ethereum.Factory_evt_PoolCreated pcON pc.pool = s.contract_addressWHERE 1=1AND s.contract_address = {{D5_Pool Address}} --default is USDC/WETHAND s.call_block_time > NOW() - interval '{{D5_months ago}}' monthAND s.call_successAND t1.call_type = 'call' AND t2.call_type = 'call'), inter_swaps AS (SELECT *FROM swapsWHERE rn = 1 -- the one with the deepest trace in t2 is the last trace before t1), swap_labels AS (SELECTtime,CASEWHEN ct.address IS NULL THENCOALESCE(e.name,a.name,'wallet/EOA')ELSECOALESCE(l.name, 'other contracts')END AS source,s.tx_hash,s.token0_amount,s.token0_addressFROM inter_swaps sLEFT JOIN ethereum.creation_traces ct ON ct.address = s.raw_sourceLEFT JOIN labels.contracts l ON l.address = s.raw_source AND l.blockchain = 'ethereum'LEFT JOIN labels.ens e ON e.address = s.raw_source AND e.blockchain = 'ethereum' AND ct.address IS NULLLEFT JOIN labels.addresses a ON a.address = s.raw_source AND a.blockchain = 'ethereum' AND ct.address IS NULL