Query editor
WITHparams AS (SELECTfrom_hex(lower(replace(trim('{{vault_address}}'), '0x', ''))) AS vault, -- vault contract0x52aa899454998be5b000ad077a46bbe360f4e497 AS liquidity, -- Fluid Liquidity (same for all vaults)-- topic2 (indexed token) of Liquidity LogOperate = debt token, left-padded to 32 bytes0x000000000000000000000000 || from_hex(lower(replace(trim('{{debt_token}}'), '0x', ''))) AS debt_token_topic,CAST({{nft_id}} AS bigint) AS nft_id,power(10e0, CAST({{debt_decimals}} AS integer)) AS debt_unit, -- USDC/USDT = 6TIMESTAMP '2024-02-01 00:00:00 UTC' AS scan_from -- before any Fluid Ethereum vault launched),-- Year boundaries. They are UTC here; swap in e.g. 'America/Chicago' if your tax year is local.periods AS (SELECTperiod,CAST(start_ts AS timestamp(3) with time zone) AS start_ts,COALESCE(CAST(end_ts AS timestamp(3) with time zone), CAST(now() AS timestamp(3) with time zone)) AS end_tsFROM (VALUES('2024', TIMESTAMP '2024-01-01 00:00:00 UTC', TIMESTAMP '2025-01-01 00:00:00 UTC'),('2025', TIMESTAMP '2025-01-01 00:00:00 UTC', TIMESTAMP '2026-01-01 00:00:00 UTC'),('2026 YTD', TIMESTAMP '2026-01-01 00:00:00 UTC', CAST(NULL AS timestamp(0) with time zone))) AS t(period, start_ts, end_ts)),boundary_ts AS (SELECT start_ts AS ts FROM periodsUNIONSELECT end_ts AS ts FROM periods),-- last block strictly before each boundary timestampboundary_blocks AS (SELECT b.ts,MAX(bl.number) AS block_number,MAX(bl.number) * 1000000 + 999999 AS k -- "end of block" ordering keyFROM boundary_ts bJOIN ethereum.blocks blON bl.time < b.tsAND bl.time >= b.ts - INTERVAL '2' HOURGROUP BY b.ts),-- Position operations for the NFT (vault LogOperate(address,uint256,int256,int256,address))pos_ops AS (SELECTl.block_time,l.block_number,l.block_number * 1000000 + l.index AS k,l.tx_hash,CAST(varbinary_to_int256(varbinary_substring(l.data, 65, 32)) AS double) AS col_amt_raw, -- collateral, token base units (cbBTC 8 dp)CAST(varbinary_to_int256(varbinary_substring(l.data, 97, 32)) AS double)/ p.debt_unit AS debt_tokens -- debt tokens; +borrow / -paybackFROM ethereum.logs lCROSS JOIN params pWHERE l.contract_address = p.vaultAND l.topic0 = 0xfef64760e30a41b9d5ba7dd65ff7236a61d89ed8b44c67a29e84db1a67513a1cAND l.block_time >= p.scan_fromAND varbinary_to_bigint(varbinary_substring(l.data, 57, 8)) = p.nft_id -- nftId_ (low 8 bytes of word 2)),