Skip to content

Commit 3e016c0

Browse files
committed
perf(api): cache + coalesce fetchLavaUsdPrice
fetchLavaUsdPrice hit CoinGecko every call while fetchTokenUsdPrice for the same 'lava' denom already cached through priceCache for 5 min. Route handlers calling fetchLavaUsdPrice directly (e.g. /provider-estimated- rewards, /validators-and-rewards, /all_providers_apr, TVL) each paid the ~150ms handshake instead of sharing. Use the existing priceCache (key 'lava') for reads and writes, and add an in-flight promise to coalesce concurrent callers on a miss so ten parallel routes become one CoinGecko request.
1 parent adcf8ad commit 3e016c0

1 file changed

Lines changed: 26 additions & 13 deletions

File tree

‎apps/api/src/rpc/pricing.ts‎

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,33 @@ import { config } from "../config.js";
33
import { fetchRest } from "./rest.js";
44
import { fetchStakingPool } from "./supply.js";
55

6+
/** In-memory price cache (5 min TTL) — shared across fetchLavaUsdPrice + fetchTokenUsdPrice + prewarmPriceCache */
7+
const priceCache = new Map<string, { price: number; ts: number }>();
8+
const PRICE_CACHE_MS = 300_000;
9+
10+
// Coalesce concurrent fetchLavaUsdPrice() callers on a cache miss so
11+
// multiple routes starting in parallel share a single CoinGecko request.
12+
let pendingLavaPrice: Promise<number> | null = null;
13+
614
export async function fetchLavaUsdPrice(): Promise<number> {
7-
const res = await fetch(
8-
`${config.external.coingeckoApiUrl}/simple/price?ids=lava-network&vs_currencies=usd`,
9-
{ signal: AbortSignal.timeout(15_000) },
10-
);
11-
if (!res.ok) throw new Error(`CoinGecko ${res.status}`);
12-
const data = (await res.json()) as { "lava-network"?: { usd?: number } };
13-
const price = data["lava-network"]?.usd;
14-
if (!price || price <= 0) throw new Error("Invalid LAVA price from CoinGecko");
15-
return price;
15+
const cached = priceCache.get("lava");
16+
if (cached && Date.now() - cached.ts < PRICE_CACHE_MS) return cached.price;
17+
if (pendingLavaPrice) return pendingLavaPrice;
18+
19+
pendingLavaPrice = (async () => {
20+
const res = await fetch(
21+
`${config.external.coingeckoApiUrl}/simple/price?ids=lava-network&vs_currencies=usd`,
22+
{ signal: AbortSignal.timeout(15_000) },
23+
);
24+
if (!res.ok) throw new Error(`CoinGecko ${res.status}`);
25+
const data = (await res.json()) as { "lava-network"?: { usd?: number } };
26+
const price = data["lava-network"]?.usd;
27+
if (!price || price <= 0) throw new Error("Invalid LAVA price from CoinGecko");
28+
priceCache.set("lava", { price, ts: Date.now() });
29+
return price;
30+
})().finally(() => { pendingLavaPrice = null; });
31+
32+
return pendingLavaPrice;
1633
}
1734

1835
/** Denom → base unit conversion (matching jsinfo DENOM_CONVERSIONS) */
@@ -55,10 +72,6 @@ export const DENOM_COINGECKO_ID: Record<string, string> = {
5572
move: "movement",
5673
};
5774

58-
/** In-memory price cache (5 min TTL) */
59-
const priceCache = new Map<string, { price: number; ts: number }>();
60-
const PRICE_CACHE_MS = 300_000;
61-
6275
/**
6376
* Pre-warm the price cache with a single batch CoinGecko call for all known denoms.
6477
* CoinGecko supports comma-separated IDs in one request.

0 commit comments

Comments
 (0)