Query editor
-- Vault-owned car NFTs, with partial valuation from previously observed single-car buybacks.WITH outer_calls AS (SELECT block_date,block_time,block_slot,tx_index,tx_id,signer,data,executing_account,account_arguments,inner_instructions,outer_indexFROM dune.bwr_analysts.result_ripcars_inventory_transactionsCROSS JOIN UNNEST(instructions) WITH ORDINALITY AS u(data,executing_account,account_arguments,inner_instructions,outer_index)), calls AS (SELECT block_date,block_time,block_slot,tx_index,tx_id,signer,data,executing_account,account_arguments,outer_index*1000 AS instruction_order FROM outer_callsUNION ALLSELECT block_date,block_time,block_slot,tx_index,tx_id,signer,d,p,a,outer_index*1000+inner_indexFROM outer_calls CROSS JOIN UNNEST(inner_instructions) WITH ORDINALITY AS u(d,p,a,stack_height,inner_index)), relevant AS (SELECT *, varbinary_substring(from_base58(data),1,1) AS opcode,element_at(account_arguments,1) AS asset,block_slot*BIGINT '1000000000'+tx_index*BIGINT '100000'+instruction_order AS event_orderFROM callsWHERE executing_account='CoREENxT6tW1HoK8ypY1SxRMZTcVPm7R94rH4PZNhX7d'AND element_at(account_arguments,2)='DiRqf7eyipQ7nPKtjWzFUZRyWT3M63GC75qyMDHe9Z81'), events AS (SELECT block_date,block_time,tx_id,asset,event_order,opcode,CASE WHEN opcode=0x0c THEN 'BURNED'WHEN opcode IN (0x00,0x14) THEN CASE WHEN element_at(account_arguments,5)='CoREENxT6tW1HoK8ypY1SxRMZTcVPm7R94rH4PZNhX7d' THEN element_at(account_arguments,4) ELSE element_at(account_arguments,5) ENDELSE element_at(account_arguments,5) END AS ownerFROM relevant WHERE opcode IN (0x00,0x14,0x0e,0x0c)), bounds AS (SELECT min(block_date) AS first_day,max(block_date) AS last_day,max(block_time) AS ownership_cutoffFROM dune.bwr_analysts.result_ripcars_inventory_transactions), days AS (SELECT day,ownership_cutoff FROM bounds CROSS JOIN UNNEST(sequence(first_day,last_day,INTERVAL '1' DAY)) AS u(day)), positions AS (SELECT d.day,e.asset,max_by(e.owner,e.event_order) AS ownerFROM days d JOIN events e ON e.block_date<=d.day GROUP BY 1,2), vault_positions AS (SELECT * FROM positions WHERE owner='8u9xv8kf1pDcPeKkxJ45xV3rFUahbWtrQHA6vNm1aP9A'), prices AS (SELECT e.asset,e.block_date,e.event_order,l.amount_usdcFROM events e JOIN query_8838898 l ON e.tx_id=l.tx_idWHERE l.activity='Buyback' AND l.units=1 AND e.opcode=0x0eAND e.owner='8u9xv8kf1pDcPeKkxJ45xV3rFUahbWtrQHA6vNm1aP9A'), valued AS (SELECT v.day,v.asset,max_by(p.amount_usdc,p.event_order) AS last_single_buyback_usdcFROM vault_positions v LEFT JOIN prices p ON p.asset=v.asset AND p.block_date<=v.dayGROUP BY 1,2), balances AS (SELECT day,count(*) AS inventory_cars,count(last_single_buyback_usdc) AS priced_cars,count(*)-count(last_single_buyback_usdc) AS unpriced_cars,sum(last_single_buyback_usdc) AS last_buyback_value,sum(last_single_buyback_usdc)/0.9 AS estimated_inventory_valueFROM valued GROUP BY 1), with_previous AS (SELECT *,lag(owner) OVER(PARTITION BY asset ORDER BY event_order) AS previous_owner FROM events), flows AS (SELECT block_date AS day,count_if(opcode IN (0x00,0x14) AND owner='8u9xv8kf1pDcPeKkxJ45xV3rFUahbWtrQHA6vNm1aP9A') AS newly_minted_to_vault,count_if(opcode=0x0e AND owner='8u9xv8kf1pDcPeKkxJ45xV3rFUahbWtrQHA6vNm1aP9A' AND previous_owner<>'8u9xv8kf1pDcPeKkxJ45xV3rFUahbWtrQHA6vNm1aP9A') AS transferred_into_vault,count_if(previous_owner='8u9xv8kf1pDcPeKkxJ45xV3rFUahbWtrQHA6vNm1aP9A' AND owner<>'8u9xv8kf1pDcPeKkxJ45xV3rFUahbWtrQHA6vNm1aP9A') AS left_vaultFROM with_previous GROUP BY 1), daily AS (SELECT d.day,d.ownership_cutoff,