Query editor
-- Fee estimates from swap input. Game LP income is not assumed withdrawable by Stonk treasury.WITH pools AS (SELECT DISTINCT topic3 pool_id,varbinary_substring(topic2,13,20) base_token,'Civilization game LP' surfaceFROM robinhood.logs WHERE block_date>=date '2026-09-01' AND block_date<current_dateAND contract_address=0xfff716727d7e80e29eab5d3498b7f28431e65c58AND topic0=keccak(to_utf8('FactionRegistered(bytes32,address,bytes32,address,uint256,address)'))UNION ALL SELECT 0xd33c8fd38b06e989cdbd4dffdefab71c4bdd415b24964c8d69e38ff35b068f92,0xe934e36a439c94017b64a3fece66af12099abf50,'STONK/ETH POL accrual estimate'), swaps AS (SELECT block_date day,p.surface,p.base_token,cast(varbinary_to_int256(varbinary_substring(data,1,32)) AS double) a0,cast(varbinary_to_int256(varbinary_substring(data,33,32)) AS double) a1,cast(varbinary_to_uint256(varbinary_substring(data,97,32)) AS double) liquidity,varbinary_to_int256(varbinary_substring(data,129,32)) tick,cast(varbinary_to_uint256(varbinary_substring(data,161,32)) AS double)/1e6 feeFROM robinhood.logs l JOIN pools p ON l.topic1=p.pool_idWHERE block_date>=date '2026-07-17' AND block_date<current_dateAND l.contract_address=0x8366a39cc670b4001a1121b8f6a443a643e40951AND topic0=0x40e9cecb9f5f1f1c5b9c97dec2917b7ee92e57ba5563708daca94dd84ad7112fAND varbinary_substring(topic2,13,20)<>0xfff716727d7e80e29eab5d3498b7f28431e65c58AND (p.surface<>'STONK/ETH POL accrual estimate' OR block_time>=timestamp '2026-07-18 03:50:03')), amounts AS (SELECT *,CASE WHEN surface='STONK/ETH POL accrual estimate' OR 0x0bd7d308f8e1639fab988df18a8011f41eacad73<base_token THEN a0 ELSE a1 END weth_amountFROM swaps), legs AS (SELECT day,surface,CASE WHEN surface='Civilization game LP' OR a0<0 THEN 0x0000000000000000000000000000000000000000 ELSE base_token END token,CASE WHEN surface='Civilization game LP' THEN abs(weth_amount)*fee/CASE WHEN weth_amount<0 THEN 1e0 ELSE 1e0-fee ENDELSE (CASE WHEN a0<0 THEN -a0 ELSE -a1 END)*fee*least(1e0,280237775147893445521073e0/nullif(liquidity,0))*CASE WHEN tick>=69000 AND tick<164600 THEN 1e0 ELSE 0e0 END END fee_raw,CASE WHEN surface='STONK/ETH POL accrual estimate' AND (tick<69000 OR tick>=164600) THEN 1 ELSE 0 END out_of_range_swapsFROM amounts WHERE fee>0 AND fee<1), p AS (SELECT cast(timestamp AS date) day,contract_address,max(price) price,max(decimals) decimalsFROM prices.day WHERE blockchain='robinhood' AND timestamp>=timestamp '2026-07-17' AND timestamp<current_date GROUP BY 1,2), eth AS (SELECT cast(timestamp AS date) day,price FROM prices.day WHERE blockchain='ethereum' AND contract_address=0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2AND timestamp>=timestamp '2026-07-17' AND timestamp<current_date)SELECT l.day,l.surface,sum(fee_raw*CASE WHEN token=0x0000000000000000000000000000000000000000 THEN e.price/1e18 ELSE p.price/pow(10,p.decimals) END) estimated_fees_usd,count(*) swaps,sum(out_of_range_swaps) out_of_range_swapsFROM legs l LEFT JOIN p ON p.day=l.day AND p.contract_address=l.token LEFT JOIN eth e ON e.day=l.day GROUP BY 1,2 ORDER BY 1,2