${news.title}
${news.body?.substring(0, 120) || 'No description available'}...
// CryptoVoid Terminal - Main Script
// API Endpoints
const APIS = {
crypto: 'https://api.coingecko.com/api/v3',
stocks: 'https://query1.finance.yahoo.com/v8/finance/chart',
news: 'https://min-api.cryptocompare.com/data/v2/news'
};
// State Management
const state = {
cryptoData: [],
stockData: [],
newsData: [],
lastUpdate: null,
selectedTimeframe: '24h'
};
// Utility Functions
const formatCurrency = (value, decimals = 2) => {
if (value >= 1e12) return `$${(value / 1e12).toFixed(decimals)}T`;
if (value >= 1e9) return `$${(value / 1e9).toFixed(decimals)}B`;
if (value >= 1e6) return `$${(value / 1e6).toFixed(decimals)}M`;
if (value >= 1e3) return `$${(value / 1e3).toFixed(decimals)}K`;
return `$${value.toFixed(decimals)}`;
};
const formatPercentage = (value) => {
const sign = value >= 0 ? '+' : '';
return `${sign}${value.toFixed(2)}%`;
};
const getChangeColor = (value) => {
return value >= 0 ? 'text-accent-lime' : 'text-accent-magenta';
};
const getChangeBg = (value) => {
return value >= 0 ? 'bg-accent-lime/10' : 'bg-accent-magenta/10';
};
// Fetch Crypto Data
async function fetchCryptoData() {
try {
const response = await fetch(
`${APIS.crypto}/coins/markets?vs_currency=usd&order=market_cap_desc&per_page=12&page=1&sparkline=true&price_change_percentage=24h,7d`
);
const data = await response.json();
state.cryptoData = data;
renderCryptoCards();
updateGlobalStats();
} catch (error) {
console.error('Crypto fetch error:', error);
renderCryptoSkeleton();
}
}
// Fetch Stock Data (Simulated with popular tickers)
async function fetchStockData() {
// Simulated stock data since Yahoo Finance has CORS restrictions
const mockStocks = [
{ symbol: 'SPY', name: 'S&P 500 ETF', price: 445.23, change: 1.25, changePercent: 0.28, volume: '52.3M' },
{ symbol: 'QQQ', name: 'Nasdaq-100 ETF', price: 378.45, change: -2.15, changePercent: -0.57, volume: '38.7M' },
{ symbol: 'AAPL', name: 'Apple Inc.', price: 178.35, change: 2.45, changePercent: 1.39, volume: '45.2M' },
{ symbol: 'MSFT', name: 'Microsoft Corp.', price: 332.18, change: -1.82, changePercent: -0.54, volume: '22.1M' },
{ symbol: 'GOOGL', name: 'Alphabet Inc.', price: 136.42, change: 0.95, changePercent: 0.70, volume: '18.9M' },
{ symbol: 'AMZN', name: 'Amazon.com', price: 127.83, change: -0.67, changePercent: -0.52, volume: '31.4M' },
{ symbol: 'TSLA', name: 'Tesla Inc.', price: 242.65, change: 5.23, changePercent: 2.20, volume: '98.5M' },
{ symbol: 'NVDA', name: 'NVIDIA Corp.', price: 460.18, change: 8.45, changePercent: 1.87, volume: '42.7M' }
];
// Add some randomization to simulate live data
state.stockData = mockStocks.map(stock => ({
...stock,
price: stock.price * (1 + (Math.random() - 0.5) * 0.02),
change: stock.change + (Math.random() - 0.5) * 0.5
}));
renderStockCards();
}
// Fetch News
async function fetchNews() {
try {
const response = await fetch(
`https://min-api.cryptocompare.com/data/v2/news/?lang=EN&limit=6`
);
const data = await response.json();
state.newsData = data.Data || [];
renderNews();
} catch (error) {
console.error('News fetch error:', error);
renderNewsSkeleton();
}
}
// Render Functions
function renderCryptoCards() {
const grid = document.getElementById('crypto-grid');
if (!grid) return;
grid.innerHTML = state.cryptoData.map((coin, index) => `
${news.body?.substring(0, 120) || 'No description available'}...
${news.title}