feat(market): add 24h crypto sparkline chart with CoinGecko history and resilient fallback

This commit is contained in:
2026-03-10 15:12:48 +01:00
parent afb000f16b
commit 4537ce8b9c
6 changed files with 217 additions and 2 deletions

View File

@@ -13,6 +13,11 @@ export interface CryptoMarketSnapshot {
updatedAt: string;
}
export interface CryptoPricePoint {
timestamp: string;
priceUsd: number;
}
const COINGECKO_BASE_URL = "https://api.coingecko.com/api/v3";
function buildCoinGeckoHeaders(): HeadersInit {
@@ -91,6 +96,43 @@ export async function fetchCryptoMarketSnapshot(
};
}
export async function fetchCryptoPriceHistory24h(
providerId: string,
): Promise<CryptoPricePoint[]> {
const url = `${COINGECKO_BASE_URL}/coins/${encodeURIComponent(
providerId,
)}/market_chart?vs_currency=usd&days=1`;
const response = await fetch(url, {
next: { revalidate: 60 },
headers: buildCoinGeckoHeaders(),
});
if (!response.ok) {
throw new Error(`Unable to load crypto price history (${response.status})`);
}
const payload = (await response.json()) as {
prices?: Array<[number, number]>;
};
const points = (payload.prices ?? [])
.filter(
(entry): entry is [number, number] =>
Array.isArray(entry) &&
entry.length >= 2 &&
Number.isFinite(entry[0]) &&
Number.isFinite(entry[1]) &&
entry[1] > 0,
)
.map(([timestamp, priceUsd]) => ({
timestamp: new Date(timestamp).toISOString(),
priceUsd,
}));
return points;
}
export async function fetchCryptoData(): Promise<CryptoRateResult> {
const ids = CRYPTO_ASSETS.map((asset) => asset.providerId).filter(
(id): id is string => Boolean(id)