Query editor
/*──────────────────────────────────────────────────────────────XTAR Price in USD (Stellar, Latest Direct Trade)▸ Finds the most recent trade between XTAR and USDC on the Stellar DEXand calculates the price from that single trade.▸ Note: Price might be stale if direct trades are infrequent.──────────────────────────────────────────────────────────────*/WITHparams AS (SELECT'XTAR' AS target_code,'GAORYJ3KBDGIM7FFSKVUJHJ5NEFWIRDIAGGBJBJS7TY6ECZS53257IG4' AS target_issuer,'USDC' AS quote_code,'GA5ZSEJYB37JRC5AVCIA5MOP4RHTM335X2KGX3IHOJAPP5RE34K4KZVN' AS quote_issuer),-- Find the very last trade directly between XTAR and USDClatest_direct_trade AS (SELECTht.selling_asset_code, ht.selling_asset_issuer, ht.selling_amount,ht.buying_asset_code, ht.buying_asset_issuer, ht.buying_amount,ht.closed_atFROM stellar.history_trades htCROSS JOIN params p -- Make params available for filteringWHERE-- Trade must involve XTAR and USDC directly((ht.selling_asset_code = p.target_code AND ht.selling_asset_issuer = p.target_issuer AND ht.buying_asset_code = p.quote_code AND ht.buying_asset_issuer = p.quote_issuer)OR(ht.buying_asset_code = p.target_code AND ht.buying_asset_issuer = p.target_issuer AND ht.selling_asset_code = p.quote_code AND ht.selling_asset_issuer = p.quote_issuer))AND ht.buying_amount > 0 AND ht.selling_amount > 0 -- Ensure valid trade amountsORDER BY ht.closed_at DESC -- Get the most recent one firstLIMIT 1 -- Only need the very latest trade)-- Calculate the price based on the single latest trade foundSELECTCASE-- If XTAR was sold for USDC: Price = Amount USDC Bought / Amount XTAR SoldWHEN ldt.selling_asset_code = p.target_code THEN ldt.buying_amount / ldt.selling_amount-- If XTAR was bought with USDC: Price = Amount USDC Sold / Amount XTAR BoughtWHEN ldt.buying_asset_code = p.target_code THEN ldt.selling_amount / ldt.buying_amountELSE NULL -- Handles the case where no trade is found by LIMIT 1END AS latest_xtar_price_usd,ldt.closed_at AS last_trade_time -- Shows when the price was last updatedFROM latest_direct_trade ldtCROSS JOIN params p -- Make params available for CASE statement