Query editor
-- =====================================================================================-- Fluid Vault T1 (Ethereum): annual interest expense for ONE position NFT, fully parameterized.-- Dune parameters (set for NFT 2101):-- {{vault_address}} text vault 30 (cbBTC/USDT, type 1) = 0xE6b5D1CdC4935295c84772C4700932b4BFC93274-- {{nft_id}} number 2101-- {{debt_token}} text USDT = 0xdAC17F958D2ee523a2206206994597C13D831ec7-- {{debt_decimals}} number 6-- Works for type 1 vaults (normal collateral, normal debt). Not for type 2-4 smart-debt vaults.---- Method (Instadapp/fluid-contracts-public, vaultT1 coreModule):-- * LogOperate.debtAmt_ is already in debt-token base units. +ve = borrow, -ve = payback.-- Dividing by 10^decimals gives tokens. It is NOT multiplied by any exchange price.-- * Position debt is stored as "raw" debt at the VAULT borrow exchange price (1e12):-- debt_tokens(t) = raw_debt * vaultBorrowExPrice(t) / 1e12-- * vaultBorrowExPrice(t) = storedVaultBorrowEx * (1 + (liqBorrowEx(t)/storedLiqBorrowEx - 1) * magnifier/10000)-- * interest(period) = debt(end) - debt(start) - (borrowed - repaid) during the period.-- Limitation: liquidations cut debt without a LogOperate for the NFT. Reconcile with the Fluid API / app.-- =====================================================================================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),