// Shared functionality for Crypto Oracle Supreme // API Configuration const API_CONFIG = { COINGECKO: 'https://api.coingecko.com/api/v3', DEFI_LLAMA: 'https://api.llama.fi', DEX_SCREENER: 'https://api.dexscreener.com/latest/dex', WHALE_ALERT: 'https://api.whale-alert.io/v1', ETHERSCAN: 'https://api.etherscan.io/api', MORALIS: 'https://deep-index.moralis.io/api/v2' }; // API Keys (should be handled via environment variables in production) const API_KEYS = { WHALE_ALERT: 'your-whale-alert-key', // Register at whale-alert.io ETHERSCAN: 'your-etherscan-key', // Get from etherscan.io MORALIS: 'your-moralis-key' // Get from moralis.io }; // Cache for API responses const API_CACHE = { coingecko: {}, defillama: {}, dexscreener: {}, whale: {} }; // Global fetch with error handling async function fetchAPI(url, options = {}) { try { const response = await fetch(url, options); if (!response.ok) throw new Error(`API Error: ${response.status}`); return await response.json(); } catch (error) { console.error('API Fetch Error:', error); showToast('Failed to fetch data. Please try again.', 'error'); return null; } } // Crypto Data Functions async function getMarketData(coinIds = 'bitcoin,ethereum,solana') { const cacheKey = `market-${coinIds}`; if (API_CACHE.coingecko[cacheKey]) return API_CACHE.coingecko[cacheKey]; const url = `${API_CONFIG.COINGECKO}/coins/markets?vs_currency=usd&ids=${coinIds}&order=market_cap_desc`; const data = await fetchAPI(url); if (data) API_CACHE.coingecko[cacheKey] = data; return data; } async function getDefiProtocols() { if (API_CACHE.defillama.protocols) return API_CACHE.defillama.protocols; const url = `${API_CONFIG.DEFI_LLAMA}/protocols`; const data = await fetchAPI(url); if (data) API_CACHE.defillama.protocols = data; return data; } async function getWhaleTransactions(minValue = 1000000, limit = 10) { const cacheKey = `whale-${minValue}-${limit}`; if (API_CACHE.whale[cacheKey]) return API_CACHE.whale[cacheKey]; const url = `${API_CONFIG.WHALE_ALERT}/transactions?api_key=${API_KEYS.WHALE_ALERT}&min_value=${minValue}&limit=${limit}`; const data = await fetchAPI(url); if (data) API_CACHE.whale[cacheKey] = data.transactions; return data?.transactions; } async function getDexPair(pairAddress, chain = 'ethereum') { const cacheKey = `pair-${pairAddress}-${chain}`; if (API_CACHE.dexscreener[cacheKey]) return API_CACHE.dexscreener[cacheKey]; const url = `${API_CONFIG.DEX_SCREENER}/${chain}/pairs/${pairAddress}`; const data = await fetchAPI(url); if (data) API_CACHE.dexscreener[cacheKey] = data.pair; return data?.pair; } // Initialize tooltips document.addEventListener('DOMContentLoaded', function() { const tooltipElements = document.querySelectorAll('[data-tooltip]'); tooltipElements.forEach(el => { el.addEventListener('mouseenter', showTooltip); el.addEventListener('mouseleave', hideTooltip); }); function showTooltip(e) { const tooltipText = this.getAttribute('data-tooltip'); const tooltip = document.createElement('div'); tooltip.className = 'tooltip absolute bg-gray-800 text-white px-2 py-1 rounded text-sm z-50'; tooltip.textContent = tooltipText; document.body.appendChild(tooltip); const rect = this.getBoundingClientRect(); tooltip.style.top = (rect.top - tooltip.offsetHeight - 10) + 'px'; tooltip.style.left = (rect.left + rect.width / 2 - tooltip.offsetWidth / 2) + 'px'; this._tooltip = tooltip; } function hideTooltip() { if (this._tooltip) { document.body.removeChild(this._tooltip); delete this._tooltip; } } }); // Smooth scroll for anchor links document.querySelectorAll('a[href^="#"]').forEach(anchor => { anchor.addEventListener('click', function(e) { e.preventDefault(); document.querySelector(this.getAttribute('href')).scrollIntoView({ behavior: 'smooth' }); }); }); // Toast notification function function showToast(message, type = 'info') { const toast = document.createElement('div'); toast.className = `fixed bottom-4 right-4 px-4 py-2 rounded-md shadow-lg text-white ${ type === 'success' ? 'bg-green-500' : type === 'error' ? 'bg-red-500' : 'bg-indigo-500' }`; toast.textContent = message; document.body.appendChild(toast); setTimeout(() => { toast.classList.add('opacity-0', 'transition-opacity', 'duration-300'); setTimeout(() => toast.remove(), 300); }, 3000); }