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.

2.7 KB
Raw Blame
// GET /api/coin/global
// ---------------------------------------------------------------------------
// Global market stats bar for the /coins page: total market cap, 24h volume,
// top-2 dominance shares, active coin count plus the Fear & Greed index
// (alternative.me — the standard free source). The market stats read failover
// across three free sources (CoinGecko → CoinPaprika → CoinLore, see
// api/_lib/market-fallbacks.js) so a single-provider rate-limit no longer
// blanks the bar. Each half is independent: if one upstream fails the other
// still renders. Cached 120s.

import { cors, json, method, wrap, error, rateLimited } from '../_lib/http.js';
import { limits, clientIp } from '../_lib/rate-limit.js';
import { fetchGlobalMarket } from '../_lib/market-fallbacks.js';
import { fetchUpstreamJson, lastGoodValue } from '../_lib/upstream-fetch.js';

let _fng = null; // { value, expiresAt }
const FNG_TTL_MS = 300_000;
// The index updates once a day, so a day-old reading is still the current one.
const FNG_STALE_MAX_AGE_MS = 24 * 60 * 60_000;

// Exported for the paid Market Data API (api/_lib/market-data/) — the x402
// market-global endpoint sells the same fear & greed reading this page renders.
export async function fetchFearGreed() {
	const now = Date.now();
	if (_fng && _fng.expiresAt > now) return _fng.value;
	const value = await lastGoodValue('coin-global:fng', fetchFearGreedLive, { maxAgeMs: FNG_STALE_MAX_AGE_MS });
	_fng = { value, expiresAt: now + FNG_TTL_MS };
	return value;
}

async function fetchFearGreedLive() {
	const raw = await fetchUpstreamJson('https://api.alternative.me/fng/?limit=1', {
		headers: { 'user-agent': 'three.ws/1.0' },
	}, { name: 'alternative-me-fng', timeoutMs: 6000, attempts: 2 });
	const d = raw?.data?.[0];
	const value = d ? { value: Number(d.value), label: d.value_classification || null } : null;
	if (!value || !Number.isFinite(value.value)) throw new Error('fng payload');
	return value;
}

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 [globalResult, fngResult] = await Promise.allSettled([
		fetchGlobalMarket(),
		fetchFearGreed(),
	]);

	const market = globalResult.status === 'fulfilled' ? globalResult.value : null;
	const fear_greed = fngResult.status === 'fulfilled' ? fngResult.value : null;

	if (!market && !fear_greed) {
		return error(res, 502, 'upstream_error', 'global market data is unavailable right now');
	}
	return json(res, 200, { market, fear_greed }, {
		'cache-control': 'public, max-age=60, s-maxage=120, stale-while-revalidate=600',
	});
});