diff --git a/js/nav.js b/js/nav.js index a402ab4..1ceb2c0 100644 --- a/js/nav.js +++ b/js/nav.js @@ -186,11 +186,12 @@ if (!priceEl || !changeEl) return; try { - const res = await fetch('https://api.coingecko.com/api/v3/simple/price?ids=solana&vs_currencies=usd&include_24hr_change=true'); + // Try Binance first (reliable CORS) + const res = await fetch('https://api.binance.com/api/v3/ticker/24hr?symbol=SOLUSDT'); if (!res.ok) throw new Error(res.status); const data = await res.json(); - const price = data.solana.usd; - const change = data.solana.usd_24h_change; + const price = parseFloat(data.lastPrice); + const change = parseFloat(data.priceChangePercent); priceEl.textContent = '$' + price.toLocaleString('en-US', { minimumFractionDigits: 2, maximumFractionDigits: 2 }); @@ -199,7 +200,23 @@ changeEl.textContent = `${arrow} ${sign}${change.toFixed(1)}%`; changeEl.className = 'sol-price-change ' + (change >= 0 ? 'up' : 'down'); } catch (e) { - console.warn('SOL price fetch failed:', e); + // Fallback to CoinGecko + try { + const res2 = await fetch('https://api.coingecko.com/api/v3/simple/price?ids=solana&vs_currencies=usd&include_24hr_change=true'); + if (!res2.ok) throw new Error(res2.status); + const data2 = await res2.json(); + const price = data2.solana.usd; + const change = data2.solana.usd_24h_change; + + priceEl.textContent = '$' + price.toLocaleString('en-US', { minimumFractionDigits: 2, maximumFractionDigits: 2 }); + + const sign = change >= 0 ? '+' : ''; + const arrow = change >= 0 ? '▲' : '▼'; + changeEl.textContent = `${arrow} ${sign}${change.toFixed(1)}%`; + changeEl.className = 'sol-price-change ' + (change >= 0 ? 'up' : 'down'); + } catch (e2) { + console.warn('SOL price fetch failed:', e, e2); + } } }