Query editor
WITH params AS (-- Health filter: token is considered "generally healthy" if-- its price is outside the peg band (max_peg_deviation)-- no more than max_out_of_band_share of the timeSELECT0.05 AS max_peg_deviation, -- 5% deviation from $1 for health filter0.05 AS max_out_of_band_share, -- max 5% of hours outside the deviation band-- Depeg threshold (absolute deviation from $1) used for counting depeg hours-- Example: 0.01 = 1% deviation from $1{{depeg_threshold}} AS depeg_threshold), top_tokens_7d AS (-- 7d volume per (blockchain, contract) for all active stablecoinsSELECTtt.blockchain,tt.contract_address,ANY_VALUE(tt.symbol) AS symbol,SUM(tt.amount_usd) AS volume_7dFROM tokens.transfers ttJOIN tokens.erc20_stablecoins sON tt.contract_address = s.contract_addressAND tt.blockchain = s.blockchainWHERE tt.block_time > NOW() - INTERVAL '7' DAYGROUP BY 1,2), raw_prices AS (-- Raw hourly prices for the lookback window for these active tokensSELECTp.blockchain,p.contract_address,tt.symbol,p.timestamp AS ts_hour,p.priceFROM prices.hour pJOIN top_tokens_7d ttON p.contract_address = tt.contract_addressAND p.blockchain = tt.blockchainWHERE p.timestamp > NOW() - INTERVAL '{{lookback_period}}' DAY), token_health AS (-- Health check: compute out_of_band_share for each token-- (share of hours where deviation > max_peg_deviation)SELECTrp.blockchain,rp.contract_address,AVG(CASEWHEN ABS(1 - rp.price) > pr.max_peg_deviation THEN 1.0ELSE 0.0END) AS out_of_band_shareFROM raw_prices rpCROSS JOIN params prGROUP BY 1,2