Spaces:
Running
Running
File size: 4,820 Bytes
8b78902 4346918 8b78902 4346918 68cc3ce | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
// 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);
}
|