Files
worldmonitor/scripts/seed-crypto-quotes.mjs
Nicolas Dos Santos 7b9426299d fix: Tech Readiness toggle, Crypto top 10, FIRMS API key check (#1132, #979, #997) (#1135)
* fix: three panel issues — Tech Readiness toggle, Crypto top 10, FIRMS key check

1. #1132 — Add tech-readiness to FULL_PANELS so it appears in the
   Settings toggle list for Full/Geopolitical variant users.

2. #979 — Expand crypto panel from 4 coins to top 10 by market cap
   (BTC, ETH, USDT, BNB, SOL, XRP, USDC, ADA, DOGE, TRX) across
   client config, server metadata, CoinPaprika fallback map, and
   seed script.

3. #997 — Check isFeatureAvailable('nasaFirms') before loading FIRMS
   data. When the API key is missing, show a clear "not configured"
   message instead of the generic "No fire data available".

Closes #1132, closes #979, closes #997

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* fix: replace stablecoins with AVAX/LINK, remove duplicate key, revert FIRMS change

- Replace USDT/USDC (stablecoins pegged ~$1) with AVAX and LINK
- Remove duplicate 'usd-coin' key in COINPAPRIKA_ID_MAP
- Add CoinPaprika fallback IDs for avalanche-2 and chainlink
- Revert FIRMS API key gating (handled differently now)
- Add sync comments across the 3 crypto config locations

* fix: update AIS relay + seed CoinPaprika fallback for all 10 coins

The AIS relay (primary seeder) still had the old 4-coin list.
The seed script's CoinPaprika fallback map was also missing the
new coins. Both now have all 10 entries.

* refactor: DRY crypto config into shared/crypto.json

Single source of truth for crypto IDs, metadata, and CoinPaprika
fallback mappings. All 4 consumers now import from shared/crypto.json:
- src/config/markets.ts (client)
- server/worldmonitor/market/v1/_shared.ts (server)
- scripts/seed-crypto-quotes.mjs (seed script)
- scripts/ais-relay.cjs (primary relay seeder)

Adding a new coin now requires editing only shared/crypto.json.

* chore: fix pre-existing markdown lint errors in README.md

Add blank lines between headings and lists per MD022/MD032 rules.

* fix: correct CoinPaprika XRP mapping and add crypto config test

- Fix xrp-ripple → xrp-xrp (current CoinPaprika id)
- Add tests/crypto-config.test.mjs: validates every coin has meta,
  coinpaprika mapping, unique symbols, no stablecoins, and valid
  id format — bad fallback ids now fail fast

* test: validate CoinPaprika ids against live API

The regex-only check wouldn't have caught the xrp-ripple typo.
New test fetches /v1/coins from CoinPaprika and asserts every
configured id exists. Gracefully skips if API is unreachable.

* fix(test): handle network failures in CoinPaprika API validation

Wrap fetch in try-catch so DNS failures, timeouts, and rate limits
skip gracefully instead of failing the test suite.

---------

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
Co-authored-by: Elie Habib <elie.habib@gmail.com>
2026-03-07 18:23:32 +04:00

128 lines
4.0 KiB
JavaScript

#!/usr/bin/env node
import { createRequire } from 'module';
import { loadEnvFile, CHROME_UA, runSeed, sleep } from './_seed-utils.mjs';
const require = createRequire(import.meta.url);
const cryptoConfig = require('../shared/crypto.json');
loadEnvFile(import.meta.url);
const CANONICAL_KEY = 'market:crypto:v1';
const CACHE_TTL = 3600; // 1 hour
const CRYPTO_IDS = cryptoConfig.ids;
const CRYPTO_META = cryptoConfig.meta;
async function fetchWithRateLimitRetry(url, maxAttempts = 5, headers = { Accept: 'application/json', 'User-Agent': CHROME_UA }) {
for (let i = 0; i < maxAttempts; i++) {
const resp = await fetch(url, {
headers,
signal: AbortSignal.timeout(15_000),
});
if (resp.status === 429) {
const wait = Math.min(10_000 * (i + 1), 60_000);
console.warn(` CoinGecko 429 — waiting ${wait / 1000}s (attempt ${i + 1}/${maxAttempts})`);
await sleep(wait);
continue;
}
if (!resp.ok) throw new Error(`CoinGecko HTTP ${resp.status}`);
return resp;
}
throw new Error('CoinGecko rate limit exceeded after retries');
}
const COINPAPRIKA_ID_MAP = cryptoConfig.coinpaprika;
async function fetchFromCoinGecko() {
const ids = CRYPTO_IDS.join(',');
const apiKey = process.env.COINGECKO_API_KEY;
const baseUrl = apiKey
? 'https://pro-api.coingecko.com/api/v3'
: 'https://api.coingecko.com/api/v3';
const url = `${baseUrl}/coins/markets?vs_currency=usd&ids=${ids}&order=market_cap_desc&sparkline=true&price_change_percentage=24h`;
const headers = { Accept: 'application/json', 'User-Agent': CHROME_UA };
if (apiKey) headers['x-cg-pro-api-key'] = apiKey;
const resp = await fetchWithRateLimitRetry(url, 5, headers);
const data = await resp.json();
if (!Array.isArray(data) || data.length === 0) {
throw new Error('CoinGecko returned no data');
}
return data;
}
async function fetchFromCoinPaprika() {
console.log(' [CoinPaprika] Falling back to CoinPaprika...');
const resp = await fetch('https://api.coinpaprika.com/v1/tickers?quotes=USD', {
headers: { Accept: 'application/json', 'User-Agent': CHROME_UA },
signal: AbortSignal.timeout(15_000),
});
if (!resp.ok) throw new Error(`CoinPaprika HTTP ${resp.status}`);
const allTickers = await resp.json();
const paprikaIds = new Set(CRYPTO_IDS.map((id) => COINPAPRIKA_ID_MAP[id]).filter(Boolean));
const reverseMap = new Map(Object.entries(COINPAPRIKA_ID_MAP).map(([g, p]) => [p, g]));
return allTickers
.filter((t) => paprikaIds.has(t.id))
.map((t) => ({
id: reverseMap.get(t.id) || t.id,
current_price: t.quotes.USD.price,
price_change_percentage_24h: t.quotes.USD.percent_change_24h,
sparkline_in_7d: undefined,
symbol: t.symbol.toLowerCase(),
name: t.name,
}));
}
async function fetchCryptoQuotes() {
let data;
try {
data = await fetchFromCoinGecko();
} catch (err) {
console.warn(` [CoinGecko] Failed: ${err.message}`);
data = await fetchFromCoinPaprika();
}
const byId = new Map(data.map((c) => [c.id, c]));
const quotes = [];
for (const id of CRYPTO_IDS) {
const coin = byId.get(id);
if (!coin) continue;
const meta = CRYPTO_META[id];
const prices = coin.sparkline_in_7d?.price;
const sparkline = prices && prices.length > 24 ? prices.slice(-48) : (prices || []);
quotes.push({
name: meta?.name || id,
symbol: meta?.symbol || id.toUpperCase(),
price: coin.current_price ?? 0,
change: coin.price_change_percentage_24h ?? 0,
sparkline,
});
}
if (quotes.every((q) => q.price === 0)) {
throw new Error('All sources returned all-zero prices');
}
return { quotes };
}
function validate(data) {
return (
Array.isArray(data?.quotes) &&
data.quotes.length >= 1 &&
data.quotes.some((q) => q.price > 0)
);
}
runSeed('market', 'crypto', CANONICAL_KEY, fetchCryptoQuotes, {
validateFn: validate,
ttlSeconds: CACHE_TTL,
sourceVersion: 'coingecko-markets',
}).catch((err) => {
console.error('FATAL:', err.message || err);
process.exit(1);
});