fix(rwallet): handle CoinGecko free tier 1-address limit gracefully

Single-token wallets get CoinGecko verification + spam filtering.
Multi-token wallets attempt batch (works with Pro/Demo keys), degrade
gracefully on free tier — Safe API trusted+exclude_spam handles most spam.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Jeff Emmett 2026-03-25 16:31:44 -07:00
parent 110b733f94
commit 76a846cc36
1 changed files with 17 additions and 1 deletions

View File

@ -75,7 +75,7 @@ export async function getNativePrice(chainId: string): Promise<number> {
return data?.[coinId]?.usd ?? 0; 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( export async function getTokenPrices(
chainId: string, chainId: string,
addresses: string[], addresses: string[],
@ -86,6 +86,21 @@ export async function getTokenPrices(
const lower = [...new Set(addresses.map((a) => a.toLowerCase()))]; const lower = [...new Set(addresses.map((a) => a.toLowerCase()))];
const prices = new Map<string, number>(); const prices = new Map<string, number>();
// 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( const data = await cgFetch(
`https://api.coingecko.com/api/v3/simple/token_price/${platform}?contract_addresses=${lower.join(",")}&vs_currencies=usd`, `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 }; return { prices, available: true };
} }
// Batch failed (free tier limit) — degrade gracefully, no spam filtering
return { prices, available: false }; return { prices, available: false };
} }