fix: switch SOL price API to Binance (CORS reliable) with CoinGecko fallback
This commit is contained in:
parent
55c8f499f1
commit
02ad42cc5d
1 changed files with 21 additions and 4 deletions
25
js/nav.js
25
js/nav.js
|
|
@ -186,11 +186,12 @@
|
||||||
if (!priceEl || !changeEl) return;
|
if (!priceEl || !changeEl) return;
|
||||||
|
|
||||||
try {
|
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);
|
if (!res.ok) throw new Error(res.status);
|
||||||
const data = await res.json();
|
const data = await res.json();
|
||||||
const price = data.solana.usd;
|
const price = parseFloat(data.lastPrice);
|
||||||
const change = data.solana.usd_24h_change;
|
const change = parseFloat(data.priceChangePercent);
|
||||||
|
|
||||||
priceEl.textContent = '$' + price.toLocaleString('en-US', { minimumFractionDigits: 2, maximumFractionDigits: 2 });
|
priceEl.textContent = '$' + price.toLocaleString('en-US', { minimumFractionDigits: 2, maximumFractionDigits: 2 });
|
||||||
|
|
||||||
|
|
@ -199,7 +200,23 @@
|
||||||
changeEl.textContent = `${arrow} ${sign}${change.toFixed(1)}%`;
|
changeEl.textContent = `${arrow} ${sign}${change.toFixed(1)}%`;
|
||||||
changeEl.className = 'sol-price-change ' + (change >= 0 ? 'up' : 'down');
|
changeEl.className = 'sol-price-change ' + (change >= 0 ? 'up' : 'down');
|
||||||
} catch (e) {
|
} 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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue