// 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) => ` `).join(''); } function renderStockCards() { const grid = document.getElementById('stocks-grid'); if (!grid) return; grid.innerHTML = state.stockData.map(stock => ` `).join(''); } function renderNews() { const container = document.getElementById('news-feed'); if (!container) return; container.innerHTML = state.newsData.map((news, index) => `
${news.source} • ${new Date(news.published_on * 1000).toLocaleDateString()}

${news.title}

${news.body?.substring(0, 120) || 'No description available'}...

`).join(''); } function renderCryptoSkeleton() { const grid = document.getElementById('crypto-grid'); if (!grid) return; grid.innerHTML = Array(6).fill(0).map(() => `
`).join(''); } function renderNewsSkeleton() { const container = document.getElementById('news-feed'); if (!container) return; container.innerHTML = Array(4).fill(0).map(() => `
`).join(''); } function updateGlobalStats() { const totalMcap = state.cryptoData.reduce((sum, coin) => sum + (coin.market_cap || 0), 0); const totalVolume = state.cryptoData.reduce((sum, coin) => sum + (coin.total_volume || 0), 0); const btcDominance = state.cryptoData[0] ? (state.cryptoData[0].market_cap / totalMcap * 100) : 0; document.getElementById('global-mcap').textContent = formatCurrency(totalMcap, 1); document.getElementById('global-volume').textContent = formatCurrency(totalVolume, 1); document.getElementById('btc-dominance').textContent = btcDominance.toFixed(1) + '%'; // Simulate fear & greed const fearGreed = Math.floor(Math.random() * 40) + 30; const fearGreedEl = document.getElementById('fear-greed'); fearGreedEl.textContent = fearGreed; fearGreedEl.className = `text-2xl font-bold ${fearGreed > 50 ? 'text-accent-lime' : 'text-accent-amber'}`; } function renderMarketChart() { const container = document.getElementById('market-chart'); if (!container) return; const bars = 50; const heights = Array(bars).fill(0).map(() => Math.random() * 80 + 20); container.innerHTML = heights.map((h, i) => `
`).join(''); } // Live Price Updates function startLiveUpdates() { setInterval(() => { // Simulate price updates state.cryptoData.forEach(coin => { const change = (Math.random() - 0.5) * 0.002; coin.current_price *= (1 + change); }); // Update displayed prices with flash effect document.querySelectorAll('.price-value').forEach(el => { const newPrice = parseFloat(el.dataset.price) * (1 + (Math.random() - 0.5) * 0.001); el.dataset.price = newPrice; el.textContent = formatCurrency(newPrice); const flashClass = Math.random() > 0.5 ? 'price-flash-up' : 'price-flash-down'; el.classList.remove('price-flash-up', 'price-flash-down'); void el.offsetWidth; // Trigger reflow el.classList.add(flashClass); }); }, 5000); } // Initialize document.addEventListener('DOMContentLoaded', () => { fetchCryptoData(); fetchStockData(); fetchNews(); renderMarketChart(); startLiveUpdates(); // Update every 60 seconds setInterval(() => { fetchCryptoData(); fetchStockData(); }, 60000); // Update news every 5 minutes setInterval(fetchNews, 300000); });