Query editor
-- Title: Total TVL of Stellar Liquidity Pools Containing XTAR-- Description: Calculates the total USD value locked (TVL) across all active Stellar liquidity pools where XTAR (GAORYJ3KBDGIM7FFSKVUJHJ5NEFWIRDIAGGBJBJS7TY6ECZS53257IG4) is one of the paired assets. Prices for all involved assets (XTAR, XLM, AQUA, USDC, etc.) are derived by looking at the latest trades within the last day (configurable via `params.price_window`) from the `stellar.history_trades` table. It prioritizes direct prices against USDC (GA5ZSEJYB37JRC5AVCIA5MOP4RHTM335X2KGX3IHOJAPP5RE34K4KZVN), assuming USDC=$1. If a direct USDC price is unavailable, it uses the latest price against XLM and bridges it using the latest XLM/USDC price. Assets without recent trades might have a price of $0, potentially underestimating the total TVL.WITHparams AS (SELECT'XTAR' AS target_code,'GAORYJ3KBDGIM7FFSKVUJHJ5NEFWIRDIAGGBJBJS7TY6ECZS53257IG4' AS target_issuer,'USDC' AS quote_code, -- The quote asset (usually stablecoin)'GA5ZSEJYB37JRC5AVCIA5MOP4RHTM335X2KGX3IHOJAPP5RE34K4KZVN' AS quote_issuer, -- Issuer for the quote assetinterval '1' day AS price_window -- How far back to look for trades to determine price),-- Step 1: Get the latest state of active liquidity pools containing the target asset (XTAR)latest_xtar_pools AS (SELECTlp.liquidity_pool_id,lp.asset_a_type, lp.asset_a_code, lp.asset_a_issuer, lp.asset_a_amount,lp.asset_b_type, lp.asset_b_code, lp.asset_b_issuer, lp.asset_b_amount,-- Assign a row number to get only the most recent state per poolROW_NUMBER() OVER(PARTITION BY lp.liquidity_pool_id ORDER BY lp.ledger_sequence DESC, lp.ingested_at DESC) as rnFROM stellar.liquidity_pools lpJOIN params p ON (lp.asset_a_code = p.target_code AND lp.asset_a_issuer = p.target_issuer)OR (lp.asset_b_code = p.target_code AND lp.asset_b_issuer = p.target_issuer)WHERE lp.deleted = false -- Only consider active pools),-- Step 2: Filter to the absolute latest state and also create a list of all unique assets involved in these poolspools_and_assets AS (SELECTliquidity_pool_id,asset_a_type, asset_a_code, asset_a_issuer, asset_a_amount,asset_b_type, asset_b_code, asset_b_issuer, asset_b_amountFROM latest_xtar_poolsWHERE rn = 1),unique_assets AS (-- Combine assets from both sides (A and B) of the pools and get distinct onesSELECT DISTINCT asset_type, asset_code, asset_issuer FROM (SELECT asset_a_type AS asset_type, asset_a_code AS asset_code, asset_a_issuer AS asset_issuer FROM pools_and_assetsUNION ALLSELECT asset_b_type AS asset_type, asset_b_code AS asset_code, asset_b_issuer AS asset_issuer FROM pools_and_assets) assetsWHERE asset_code IS NOT NULL -- Filter out any potential invalid entries),-- Step 3: Get the latest XLM/USDC price from recent trades to use as a bridge if neededlatest_xlm_usdc_price AS (SELECT-- Calculate the price of 1 XLM in terms of USDC based on the trade directionCASEWHEN selling_asset_type = 'native' THEN buying_amount / selling_amount -- Selling XLM for USDC: Price = USDC_received / XLM_soldWHEN buying_asset_type = 'native' THEN selling_amount / buying_amount -- Buying XLM with USDC: Price = USDC_spent / XLM_boughtEND AS xlm_price_in_usdcFROM stellar.history_trades htJOIN params p ON (ht.selling_asset_code = p.quote_code AND ht.selling_asset_issuer = p.quote_issuer AND ht.buying_asset_type = 'native')OR (ht.buying_asset_code = p.quote_code AND ht.buying_asset_issuer = p.quote_issuer AND ht.selling_asset_type = 'native')WHERE ht.closed_at >= current_timestamp - p.price_window -- Look within the defined time windowAND ht.selling_amount > 0 AND ht.buying_amount > 0 -- Avoid division by zero errorsORDER BY ht.closed_at DESC, ht.history_operation_id DESC -- Try history_operation_id as tie-breaker