diff --git a/modules/rwallet/lib/price-feed.ts b/modules/rwallet/lib/price-feed.ts index 86b9574..10f0d29 100644 --- a/modules/rwallet/lib/price-feed.ts +++ b/modules/rwallet/lib/price-feed.ts @@ -75,7 +75,7 @@ export async function getNativePrice(chainId: string): Promise { return data?.[coinId]?.usd ?? 0; } -/** Fetch token prices for a batch of contract addresses on a chain */ +/** Fetch token prices for contract addresses on a chain */ export async function getTokenPrices( chainId: string, addresses: string[], @@ -86,6 +86,21 @@ export async function getTokenPrices( const lower = [...new Set(addresses.map((a) => a.toLowerCase()))]; const prices = new Map(); + // CoinGecko free tier limits to 1 address per request. + // For 1 token, do a single lookup. For multiple, try batch (works with Pro/Demo keys). + if (lower.length === 1) { + const addr = lower[0]; + const data = await cgFetch( + `https://api.coingecko.com/api/v3/simple/token_price/${platform}?contract_addresses=${addr}&vs_currencies=usd`, + ); + if (data && !data.error_code) { + if (data[addr]?.usd) prices.set(addr, data[addr].usd); + return { prices, available: true }; + } + return { prices, available: false }; + } + + // Multiple tokens: batch request (succeeds with CoinGecko Demo/Pro API key) const data = await cgFetch( `https://api.coingecko.com/api/v3/simple/token_price/${platform}?contract_addresses=${lower.join(",")}&vs_currencies=usd`, ); @@ -95,6 +110,7 @@ export async function getTokenPrices( } return { prices, available: true }; } + // Batch failed (free tier limit) — degrade gracefully, no spam filtering return { prices, available: false }; }