Skip to content

Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.

5.7 KB
Raw Blame
// GET /api/coin/tickers?id=<coingecko-id>&page=1
// ---------------------------------------------------------------------------
// Exchange listings for one coin — powers the Markets table on the /coin/:id
// detail page. Proxies CoinGecko /coins/{id}/tickers ordered by converted
// volume with ±2% depth, slims each ticker to what the table renders, and
// truncates contract-address pair symbols (DEX listings report raw 0x/base58
// addresses as symbols) so every pair stays legible. Cached in-memory 120s +
// CDN s-maxage.
//
// CoinGecko's keyless tier rate-limits per egress IP and Cloud Run's is shared,
// so the Markets table falls back to CoinPaprika's per-coin markets feed
// (api/_lib/coin-fallbacks.js) rather than 502ing the whole section. A 404 is
// an answer about a real coin id and never falls back.
//
// CoinPaprika does not list every coin, and those Markets tables still 502'd
// whenever CoinGecko was throttled. The last rung is the coin's DEX pairs from
// DexScreener, looked up by the contract addresses the caller passes in
// `contracts` (comma-separated; the coin page has them from /api/coin/detail).
// Without addresses, or when nothing trades, the 502 stands: an honest outage
// beats an invented listing.

import { cors, json, method, wrap, error, rateLimited } from '../_lib/http.js';
import { limits, clientIp } from '../_lib/rate-limit.js';
import { geckoFetch, isPlausibleCoinId } from '../_lib/coingecko.js';
import {
	fetchFallbackTickers,
	fetchDexTickers,
	isDexLookupAddress,
	DEX_TICKER_MAX_ADDRESSES,
} from '../_lib/coin-fallbacks.js';

const num = (v) => (typeof v === 'number' && Number.isFinite(v) ? v : null);
const str = (v) => (typeof v === 'string' && v.trim() ? v.trim() : null);

// A pair symbol that is really a contract address (EVM 0x… or a base58 mint)
// renders as an unreadable wall of characters — shorten to a 6-char prefix.
const ADDRESS_SYMBOL_RE = /^(0x[0-9a-fA-F]{8,}|[1-9A-HJ-NP-Za-km-z]{32,64})$/;

function pairSymbol(s) {
	const v = str(s);
	if (!v) return null;
	return ADDRESS_SYMBOL_RE.test(v) ? `${v.slice(0, 6)}…` : v.toUpperCase();
}

function shapeTicker(t) {
	const base = pairSymbol(t.base);
	const target = pairSymbol(t.target);
	const tradeUrl = str(t.trade_url);
	return {
		exchange: {
			id: str(t.market?.identifier),
			name: str(t.market?.name),
			logo: str(t.market?.logo),
		},
		base,
		target,
		pair: base && target ? `${base}/${target}` : null,
		price_usd: num(t.converted_last?.usd),
		volume_usd: num(t.converted_volume?.usd),
		spread_pct: num(t.bid_ask_spread_percentage),
		depth_up_usd: num(t.cost_to_move_up_usd),
		depth_down_usd: num(t.cost_to_move_down_usd),
		trust: t.trust_score === 'green' || t.trust_score === 'yellow' || t.trust_score === 'red' ? t.trust_score : null,
		stale: Boolean(t.is_stale || t.is_anomaly),
		// Only ever hand the client an http(s) URL — trade_url is upstream-controlled.
		trade_url: tradeUrl && /^https?:\/\//i.test(tradeUrl) ? tradeUrl : null,
		coin_id: str(t.coin_id),
		target_coin_id: str(t.target_coin_id),
		last_traded: str(t.timestamp),
	};
}

export default wrap(async (req, res) => {
	if (cors(req, res, { methods: 'GET,OPTIONS', origins: '*' })) return;
	if (!method(req, res, ['GET'])) return;

	const rl = await limits.marketDataIp(clientIp(req));
	if (!rl.success) return rateLimited(res, rl);

	const params = new URL(req.url, 'http://x').searchParams;
	const id = (params.get('id') || '').trim().toLowerCase();
	if (!isPlausibleCoinId(id)) {
		return error(res, 400, 'bad_id', 'id must be a CoinGecko coin id (lowercase slug)');
	}
	// Validate the raw value, not a coerced one. `parseInt(v) || 1` silently
	// rewrote every falsy parse to page 1, so `page=0`, `page=abc`, and `page=2.9`
	// all answered 200 with a page the caller never asked for while `page=11`
	// answered 400 (and the `page < 1` guard was unreachable for 0). One rule now:
	// absent means 1, anything else must be an integer in range or it is a 400.
	const rawPage = params.get('page');
	const page = rawPage === null || rawPage.trim() === '' ? 1 : Number(rawPage);
	if (!Number.isInteger(page) || page < 1 || page > 10) {
		return error(res, 400, 'bad_page', 'page must be an integer between 1 and 10');
	}

	// Optional DEX-rung hint. Malformed entries are dropped rather than
	// rejected: the hint only matters on the fallback path.
	const contracts = (params.get('contracts') || '')
		.split(',')
		.map((v) => v.trim())
		.filter(isDexLookupAddress)
		.slice(0, DEX_TICKER_MAX_ADDRESSES);

	const cacheHeaders = {
		'cache-control': 'public, max-age=60, s-maxage=120, stale-while-revalidate=600',
	};

	try {
		const raw = await geckoFetch(
			`/coins/${id}/tickers?page=${page}&order=converted_volume_desc&depth=true&include_exchange_logo=true`,
			{ ttlMs: 120_000, timeoutMs: 10_000 },
		);
		const tickers = (raw?.tickers || []).map(shapeTicker);
		return json(res, 200, { tickers, page, count: tickers.length, source: 'coingecko' }, cacheHeaders);
	} catch (err) {
		if (err?.status === 404)
			return error(res, 404, 'not_found', `no coin found for "${id}"`);
		const tickers = await fetchFallbackTickers(id, { page });
		if (tickers) {
			console.warn(`[coin/tickers] ${id}: coingecko failed (${err?.status || err?.name}); served from coinpaprika`);
			return json(res, 200, { tickers, page, count: tickers.length, source: 'coinpaprika' }, cacheHeaders);
		}
		const dexTickers = contracts.length ? await fetchDexTickers(contracts, { page }) : null;
		if (dexTickers) {
			console.warn(`[coin/tickers] ${id}: coingecko failed (${err?.status || err?.name}); served from dexscreener`);
			return json(res, 200, { tickers: dexTickers, page, count: dexTickers.length, source: 'dexscreener' }, cacheHeaders);
		}
		return error(res, 502, 'upstream_error', 'exchange listings are unavailable right now — retry shortly');
	}
});