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.

4.4 KB
Raw Blame
// GET /api/defi/stablecoins
// ---------------------------------------------------------------------------
// Live stablecoin market-cap board for the /stablecoins page. Reads the free,
// keyless DeFiLlama stablecoins API and reshapes it into a lean, ranked list:
// each pegged asset's circulating supply (denominated in its own peg unit) is
// its on-chain market cap. We surface price (for peg-health), peg mechanism,
// and the chains it lives on. Aggregate market cap sums every finite asset,
// then the list is sorted by size and capped at the top 100. Cached 5m
// in-memory + CDN. The source refreshes on the order of minutes, not seconds.

import { cors, json, method, wrap, error, rateLimited } from '../_lib/http.js';
import { limits, clientIp } from '../_lib/rate-limit.js';
import { createCache, cached } from '../_lib/mem-cache.js';
import { fetchUpstream } from '../_lib/upstream-fetch.js';

const UPSTREAM = 'https://stablecoins.llama.fi/stablecoins?includePrices=true';
const TTL_MS = 300_000; // 5 minutes, matches the CDN s-maxage below.
const TOP_N = 100;

// Single-entry cache with single-flight de-dup, so a burst of concurrent
// requests on a cold or just-expired entry shares one upstream fetch.
const _cache = createCache({ max: 1, ttlMs: TTL_MS });
const CACHE_KEY = 'stablecoins';

// peg_type keeps DeFiLlama's raw token (`peggedUSD`, `peggedEUR`, …) so the
// client can render the pegged currency (USD, EUR) without a lossy remap; the
// "pegged" prefix is stripped for display client-side.
function shape(assets) {
	const rows = [];
	let totalMcap = 0;

	for (const a of assets) {
		if (!a || typeof a !== 'object') continue;
		const pegType = typeof a.pegType === 'string' ? a.pegType : null;
		// Circulating is nested under the asset's own peg unit, e.g.
		// circulating.peggedUSD. No peg type → no way to read it; skip.
		const circulating = pegType && a.circulating ? Number(a.circulating[pegType]) : NaN;
		if (!Number.isFinite(circulating)) continue;

		totalMcap += circulating;

		const price = Number(a.price);
		const chains = Array.isArray(a.chains) ? a.chains.filter((c) => typeof c === 'string') : [];

		rows.push({
			// DeFiLlama's numeric asset id ("1", "2", …). The /stablecoin/:id
			// detail page keys off it; null when upstream omits it.
			id: a.id != null ? String(a.id) : null,
			name: typeof a.name === 'string' ? a.name : 'Unknown',
			symbol: typeof a.symbol === 'string' ? a.symbol : '',
			price: Number.isFinite(price) ? price : null,
			peg_type: pegType,
			peg_mechanism: typeof a.pegMechanism === 'string' ? a.pegMechanism : null,
			circulating_usd: circulating,
			chains,
			chain_count: chains.length,
		});
	}

	rows.sort((x, y) => y.circulating_usd - x.circulating_usd);

	return {
		total_mcap: totalMcap,
		count: rows.length,
		stablecoins: rows.slice(0, TOP_N),
		updated_at: Date.now(),
	};
}

// Exported for the paid Market Data API (api/_lib/market-data/), the x402
// market-stablecoins endpoint sells the same peg board this page renders.
export async function buildStablecoins() {
	return cached(_cache, CACHE_KEY, loadStablecoins);
}

async function loadStablecoins() {
	const resp = await fetchUpstream(UPSTREAM, {
		headers: { accept: 'application/json', 'user-agent': 'three.ws/1.0' },
	}, { timeoutMs: 10_000, attempts: 2 });

	const body = await resp.json();
	const assets = Array.isArray(body?.peggedAssets) ? body.peggedAssets : null;
	if (!assets) throw new Error('unexpected upstream shape');

	return shape(assets);
}

// `cached()` serves its last-good copy (without re-populating the fresh slot)
// when the loader throws, so a fresh miss after a resolved value means the
// payload is stale. Label it so the page can say so instead of passing it off
// as live.
function staleHeaders() {
	return _cache.has(CACHE_KEY) ? {} : { 'x-three-stale': '1' };
}

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);

	try {
		const payload = await buildStablecoins();
		return json(res, 200, payload, {
			'cache-control': 'public, max-age=120, s-maxage=300, stale-while-revalidate=600',
			...staleHeaders(),
		});
	} catch {
		return error(
			res,
			502,
			'upstream_error',
			'stablecoin data is unavailable right now. Retry shortly',
		);
	}
});