/* ========================================================== OPN Stake Protocol api.js Production API Layer ========================================================== */ class APIService { constructor() { this.baseURL = CONFIG.API.BASE_URL; this.cache = new Map(); this.cacheTTL = 30000; // 30 sec this.defaultHeaders = { "Content-Type": "application/json", "Accept": "application/json" }; } /* ================================================== HTTP REQUEST ================================================== */ async request( endpoint, options = {} ) { const url = `${this.baseURL}${endpoint}`; const config = { method: "GET", headers: { ...this.defaultHeaders, ...options.headers }, ...options }; try { const response = await fetch( url, config ); if (!response.ok) { throw new Error( `API Error: ${response.status}` ); } return await response.json(); } catch (error) { console.error( "API Request Failed:", error ); throw error; } } /* ================================================== CACHE ================================================== */ getCache(key) { const item = this.cache.get(key); if (!item) return null; const expired = Date.now() - item.timestamp > this.cacheTTL; if (expired) { this.cache.delete(key); return null; } return item.data; } setCache(key, data) { this.cache.set( key, { data, timestamp: Date.now() } ); } async cachedRequest( cacheKey, endpoint ) { const cached = this.getCache(cacheKey); if (cached) return cached; const data = await this.request( endpoint ); this.setCache( cacheKey, data ); return data; } /* ================================================== TVL ================================================== */ async getTVL() { try { return await this.cachedRequest( "tvl", CONFIG.API.TVL ); } catch { return { tvl: 125000000 }; } } /* ================================================== APR ================================================== */ async getAPR() { try { return await this.cachedRequest( "apr", CONFIG.API.APR ); } catch { return { apr: 7.84 }; } } /* ================================================== VALIDATORS ================================================== */ async getValidators() { try { return await this.cachedRequest( "validators", CONFIG.API.VALIDATORS ); } catch { return [ { id: 1, name: "OPN Validator #1", stake: 52000000, apr: 7.9, status: "online" }, { id: 2, name: "OPN Validator #2", stake: 41000000, apr: 7.8, status: "online" } ]; } } /* ================================================== TREASURY ================================================== */ async getTreasury() { try { return await this.cachedRequest( "treasury", CONFIG.API.TREASURY ); } catch { return { total: 12000000, stable: 5000000, reserve: 4000000, liquid: 3000000 }; } } /* ================================================== GOVERNANCE ================================================== */ async getProposals() { try { return await this.cachedRequest( "governance", CONFIG.API.GOVERNANCE ); } catch { return [ { id: 1, title: "Increase Validator Rewards", status: "active", forVotes: 850000, againstVotes: 200000 }, { id: 2, title: "Treasury Diversification", status: "pending", forVotes: 420000, againstVotes: 90000 } ]; } } /* ================================================== RESTAKING ================================================== */ async getRestakingMetrics() { try { return await this.cachedRequest( "restaking", CONFIG.API.RESTAKING ); } catch { return { totalRestaked: 65000000, apr: 9.5, avs: 12, operators: 28 }; } } /* ================================================== REWARDS ================================================== */ async getRewards(address) { if (!address) return null; try { return await this.request( `${CONFIG.API.REWARDS}/${address}` ); } catch { return { pending: 1250, claimed: 4500 }; } } /* ================================================== USER PORTFOLIO ================================================== */ async getPortfolio(address) { if (!address) return null; try { return await this.request( `/portfolio/${address}` ); } catch { return { opn: 15000, stOPN: 12500, rstOPN: 4500 }; } } /* ================================================== ANALYTICS ================================================== */ async getAnalytics() { const [ tvl, apr, validators, treasury ] = await Promise.all([ this.getTVL(), this.getAPR(), this.getValidators(), this.getTreasury() ]); return { tvl, apr, validators, treasury }; } /* ================================================== CHART DATA ================================================== */ async getTVLHistory() { return [ 82, 85, 90, 95, 102, 110, 120, 125 ]; } async getAPRHistory() { return [ 7.1, 7.2, 7.3, 7.4, 7.5, 7.7, 7.8, 7.9 ]; } async getRevenueData() { return { labels: [ "Jan", "Feb", "Mar", "Apr", "May", "Jun" ], revenue: [ 100000, 130000, 160000, 220000, 280000, 340000 ] }; } async getRevenueSources() { return { labels: [ "Staking", "Restaking", "Fees", "Treasury" ], values: [ 45, 30, 15, 10 ] }; } /* ================================================== HEALTH CHECK ================================================== */ async ping() { try { await this.request("/health"); return true; } catch { return false; } } /* ================================================== CLEAR CACHE ================================================== */ clearCache() { this.cache.clear(); console.log( "API cache cleared" ); } } /* ========================================================== GLOBAL INSTANCE ========================================================== */ window.API = new APIService(); /* ========================================================== AUTO REFRESH ========================================================== */ window.startAPIRefresh = function() { setInterval( () => { API.clearCache(); }, CONFIG.DASHBOARD .REFRESH_INTERVAL ); }; /* ========================================================== SHORTCUTS ========================================================== */ window.fetchTVL = () => API.getTVL(); window.fetchAPR = () => API.getAPR(); window.fetchValidators = () => API.getValidators(); window.fetchTreasury = () => API.getTreasury(); window.fetchProposals = () => API.getProposals(); window.fetchRestaking = () => API.getRestakingMetrics(); window.fetchAnalytics = () => API.getAnalytics(); /* ========================================================== INIT ========================================================== */ document.addEventListener( "DOMContentLoaded", () => { startAPIRefresh(); console.log( "API Service Loaded" ); } );