Query editor
/** [Title] Snapshot: Current Total Staked TON & TVL** [Description]* This query calculates the current "Total Staked TON" and its USD value (TVL).* Unlike the `call` tables which may contain outdated data due to infrequent function calls,* this query derives the real-time state by fetching the very latest emitted events* from the sWTON contract.** [Logic]* 1. Fetches the latest global `factor` from `sWTON_evt_ChangedFactor`.* 2. Fetches the latest global `balance` from `sWTON_evt_ChangedBalance`.* 3. Calculates Staked Amount: (Balance * Factor) / 1e54 (Adjusting for Ray decimals).* 4. Multiplies by the latest market price to get TVL in USD.*/WITH latest_factor_evt AS (-- 1. Retrieve the most recent Factor valueSELECTCAST(json_extract_scalar("next", '$.factor') AS DOUBLE) as factorFROM tokamak_network_ethereum.sWTON_evt_ChangedFactorORDER BY evt_block_number DESCLIMIT 1),latest_balance_evt AS (-- 2. Retrieve the most recent Balance valueSELECTCAST(json_extract_scalar(newTotalBalance, '$.balance') AS DOUBLE) as balanceFROM tokamak_network_ethereum.sWTON_evt_ChangedBalanceORDER BY evt_block_number DESCLIMIT 1),latest_price AS (-- 3. Retrieve the latest TON market priceSELECT priceFROM prices.dayWHERE contract_address = 0x2be5e8c109e2197D077D13A82dAead6a9b3433C5ORDER BY timestamp DESCLIMIT 1)-- 4. Final Calculation (Factor * Balance)SELECT-- sWTON typically uses 27 decimals (Ray) units.-- Calculation: Balance(27) * Factor(27) = Internal Amount (54 decimals)-- To normalize to WTON(27), we divide by 1e27.-- To convert to human-readable TON (0 decimals display), we divide by 1e27 again.-- Therefore, we divide by a total of 1e54.(b.balance * f.factor) / 1e54 as current_staked_ton,p.price as current_ton_price_usd,((b.balance * f.factor) / 1e54) * p.price as current_tvl_usdFROM latest_balance_evt bCROSS JOIN latest_factor_evt fCROSS JOIN latest_price p