|
| 1 | +import { NextApiRequest, NextApiResponse } from 'next'; |
| 2 | + |
| 3 | +const CG_ENDPOINT = 'https://pro-api.coingecko.com/api/v3/coins/markets'; |
| 4 | +const HEADERS: HeadersInit = { |
| 5 | + accept: 'application/json', |
| 6 | + 'x-cg-pro-api-key': process.env.COINGECKO_API_KEY ?? '', |
| 7 | +}; |
| 8 | +interface CoinGeckoCoin { |
| 9 | + id: string; |
| 10 | + symbol: string; |
| 11 | +} |
| 12 | + |
| 13 | +export default async function handler(req: NextApiRequest, res: NextApiResponse) { |
| 14 | + const allowedOrigins = ['https://app.aave.com', 'https://aave.com']; |
| 15 | + const origin = req.headers.origin; |
| 16 | + |
| 17 | + const isOriginAllowed = (origin: string | undefined): boolean => { |
| 18 | + if (!origin) return false; |
| 19 | + |
| 20 | + if (allowedOrigins.includes(origin)) return true; |
| 21 | + |
| 22 | + // Match any subdomain ending with avaraxyz.vercel.app for deployment urls |
| 23 | + const allowedPatterns = [/^https:\/\/.*avaraxyz\.vercel\.app$/]; |
| 24 | + |
| 25 | + return allowedPatterns.some((pattern) => pattern.test(origin)); |
| 26 | + }; |
| 27 | + |
| 28 | + if (process.env.CORS_DOMAINS_ALLOWED === 'true') { |
| 29 | + res.setHeader('Access-Control-Allow-Origin', '*'); |
| 30 | + } else if (origin && isOriginAllowed(origin)) { |
| 31 | + res.setHeader('Access-Control-Allow-Origin', origin); |
| 32 | + } |
| 33 | + |
| 34 | + res.setHeader('Access-Control-Allow-Methods', 'GET, OPTIONS'); |
| 35 | + res.setHeader('Access-Control-Allow-Headers', 'Content-Type'); |
| 36 | + |
| 37 | + if (req.method === 'OPTIONS') { |
| 38 | + return res.status(200).end(); |
| 39 | + } |
| 40 | + |
| 41 | + if (req.method !== 'GET') { |
| 42 | + return res.status(405).json({ error: 'Method not allowed' }); |
| 43 | + } |
| 44 | + |
| 45 | + try { |
| 46 | + const coingeckoApiKey = process.env.COINGECKO_API_KEY; |
| 47 | + if (!coingeckoApiKey) { |
| 48 | + return res.status(500).json({ error: 'CoinGecko API key is not configured' }); |
| 49 | + } |
| 50 | + |
| 51 | + // Fetch for Stablecoins Category and Eth Correlated Categories |
| 52 | + const [resStable1, resStable2, resEth1, resEth2, resEth3] = await Promise.all([ |
| 53 | + fetch(`${CG_ENDPOINT}?vs_currency=usd&category=stablecoins&per_page=250&page=1`, { |
| 54 | + method: 'GET', |
| 55 | + headers: HEADERS, |
| 56 | + }), |
| 57 | + fetch(`${CG_ENDPOINT}?vs_currency=usd&category=stablecoins&per_page=250&page=2`, { |
| 58 | + method: 'GET', |
| 59 | + headers: HEADERS, |
| 60 | + }), |
| 61 | + fetch(`${CG_ENDPOINT}?vs_currency=usd&category=liquid-staked-eth&per_page=250&page=1`, { |
| 62 | + method: 'GET', |
| 63 | + headers: HEADERS, |
| 64 | + }), |
| 65 | + fetch(`${CG_ENDPOINT}?vs_currency=usd&category=ether-fi-ecosystem&per_page=250&page=1`, { |
| 66 | + method: 'GET', |
| 67 | + headers: HEADERS, |
| 68 | + }), |
| 69 | + fetch(`${CG_ENDPOINT}?vs_currency=usd&category=liquid-staking-tokens&per_page=250&page=1`, { |
| 70 | + method: 'GET', |
| 71 | + headers: HEADERS, |
| 72 | + }), |
| 73 | + ]); |
| 74 | + |
| 75 | + if (!resStable1.ok) { |
| 76 | + return res.status(resStable1.status).json({ |
| 77 | + error: `Error fetching stablecoins page 1`, |
| 78 | + details: resStable1.statusText, |
| 79 | + }); |
| 80 | + } |
| 81 | + if (!resStable2.ok) { |
| 82 | + return res.status(resStable2.status).json({ |
| 83 | + error: `Error fetching stablecoins page 2`, |
| 84 | + details: resStable2.statusText, |
| 85 | + }); |
| 86 | + } |
| 87 | + if (!resEth1.ok) { |
| 88 | + return res.status(resEth1.status).json({ |
| 89 | + error: `Error fetching liquid-staked-eth`, |
| 90 | + details: resEth1.statusText, |
| 91 | + }); |
| 92 | + } |
| 93 | + if (!resEth2.ok) { |
| 94 | + return res.status(resEth2.status).json({ |
| 95 | + error: `Error fetching ether-fi-ecosystem`, |
| 96 | + details: resEth2.statusText, |
| 97 | + }); |
| 98 | + } |
| 99 | + if (!resEth3.ok) { |
| 100 | + return res.status(resEth3.status).json({ |
| 101 | + error: `Error fetching liquid-staking-tokens`, |
| 102 | + details: resEth3.statusText, |
| 103 | + }); |
| 104 | + } |
| 105 | + |
| 106 | + const [dataStable1, dataStable2, dataEth1, dataEth2, dataEth3] = await Promise.all([ |
| 107 | + resStable1.json(), |
| 108 | + resStable2.json(), |
| 109 | + resEth1.json(), |
| 110 | + resEth2.json(), |
| 111 | + resEth3.json(), |
| 112 | + ]); |
| 113 | + const combinedData = [...dataStable1, ...dataStable2]; |
| 114 | + |
| 115 | + const processedSymbols = combinedData |
| 116 | + .map((coin: CoinGeckoCoin) => coin.symbol?.toUpperCase()) |
| 117 | + .filter((symbol: string) => symbol); |
| 118 | + |
| 119 | + const uniqueSymbolsStablecoins = [...new Set([...processedSymbols])]; |
| 120 | + |
| 121 | + // Filter category 'liquid-staking-tokens' to only include coins correlated to ETH |
| 122 | + const filteredData3 = dataEth3.filter((coin: CoinGeckoCoin) => { |
| 123 | + const symbol = coin.symbol?.toUpperCase(); |
| 124 | + return symbol?.includes('ETH'); |
| 125 | + }); |
| 126 | + |
| 127 | + const combinedDataEth = [...dataEth1, ...dataEth2, ...filteredData3]; |
| 128 | + |
| 129 | + const symbols = combinedDataEth |
| 130 | + .map((coin: CoinGeckoCoin) => coin.symbol?.toUpperCase()) |
| 131 | + .filter((symbol: string) => symbol); |
| 132 | + |
| 133 | + const uniqueSymbolsEth = [...new Set([...symbols, 'WETH'])]; |
| 134 | + |
| 135 | + return res.status(200).json({ uniqueSymbolsStablecoins, uniqueSymbolsEth }); |
| 136 | + } catch (error) { |
| 137 | + console.error('Coingecko categories proxy error:', error); |
| 138 | + return res.status(500).json({ error: 'Internal server error', details: String(error) }); |
| 139 | + } |
| 140 | +} |
0 commit comments