Repository navigation
feat(api): fetch historical prices only for denoms present in the block - #41
Merged
Merged
Conversation
Previously /provider-estimated-rewards?block=N called buildHistoricalPriceMap
with all 22 known denoms. Under CoinGecko throttling (sequential + retry
backoff), this blew past the 120s gateway timeout and 504'd — so cold
historical queries effectively never succeeded.
Now the route introspects each block's actual rewards first, extracts the
set of base denoms that appear (after IBC resolution), and fetches
historical prices only for those — typically just LAVA + 0-2 IBC denoms.
First cold response returns in seconds; subsequent requests come from the
1-year IMMUTABLE cache.
Implementation:
- rpc/rewards.ts: split fetchRewardsBySpec into three composable pieces:
fetchRawProviderRewards (one chain RPC per provider), extractBaseDenoms
(post-IBC-resolution denom set across N raw responses), and
processRawProviderRewards (raw → spec-grouped with source breakdown).
fetchRewardsBySpec is kept as a convenience wrapper for APR callers.
- rpc/pricing.ts:
• coingeckoFetch helper: honors Retry-After on 429, exponential backoff
up to 5 attempts capped at 60s per try. Used by both current-price
(fetchLavaUsdPrice) and historical-price paths.
• fetchTokenUsdPriceAt: throws on transient failures (rate-limit
exhaustion, network error) instead of silently returning 0. Zero is
reserved for legitimate "no data" (unknown denom, 404, empty
market_data) and cached indefinitely. Throws don't cache — next
caller retries from scratch.
• buildHistoricalPriceMap: sequential (not parallel), LAVA first, LAVA
failure propagates. Other denoms best-effort; on failure we log and
continue, falling back to the live cache for that denom.
• fetchDenomTrace gains an in-memory cache since IBC-hash → base-denom
is immutable per-channel.
- routes/provider-estimated-rewards.ts: two-pass flow
1. Fetch raw rewards for every provider (with blockHeight).
2. Extract base denoms across all raws, build historical price map for
only those, then process each raw with overrides.
If LAVA historical price fetch throws, return 503 (cache plugin skips
4xx/5xx so a poisoned wrong-price response can never enter the 1-year
IMMUTABLE cache).
- config.ts: new CACHE_TTL.IMMUTABLE (365 days). Applied to past-block
endpoints: /provider-estimated-rewards?block=N,
/provider-estimated-rewards/blocks, and /provider-rewards with past
date ranges. Avatars + burn-rate stay on HISTORICAL (1 day) since they
can legitimately change.
114/114 tests passing. New assertions cover:
• buildHistoricalPriceMap called with only the denoms actually in the block
• 503 when LAVA historical price unavailable + processRawProviderRewards
is NOT called (so no wrong cached response)
• live-mode doesn't invoke any historical code path
nimrod-teich
force-pushed
the
feat/rewards-prices-relevant-denoms
branch
from
April 20, 2026 11:43
fcbeb00 to
6681672
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fully accurate historical rewards pricing without blowing the gateway timeout. The previous approach fetched all 22 known CoinGecko denoms for every historical-block query; with sequential-with-retry-backoff that ran into 504s on cold requests.
Key insight: only a handful of denoms actually appear in any given block's rewards — typically just LAVA + 0-2 IBC denoms. Introspect the rewards response first, then fetch prices only for what's there.
New flow
estimated_provider_rewardsfor every provider (withx-cosmos-block-height)Implementation notes
rpc/rewards.ts: splitfetchRewardsBySpecinto composable pieces —fetchRawProviderRewards,extractBaseDenoms,processRawProviderRewards.fetchRewardsBySpeckept as a convenience wrapper (APR route still uses it).rpc/pricing.ts:coingeckoFetchhelper: honorsRetry-Afteron 429, exponential backoff, throws after 5 attemptsfetchTokenUsdPriceAt: throws on transient failures (no more silent fallback to current price). Returns 0 only for legitimate "no data" cases (unknown denom, 404, emptymarket_data), and those cache indefinitelybuildHistoricalPriceMap: sequential, LAVA first, LAVA failure propagates so the route can 503 rather than cache wrong datafetchDenomTracegains a persistent in-memory cache (IBC hash → base denom is immutable per channel)routes/provider-estimated-rewards.ts: new two-pass flow as above. When LAVA historical price fails, returns 503 — the cache plugin skips 4xx/5xx so a wrong-price response can't poison the 1-year cache.config.ts: newCACHE_TTL.IMMUTABLE = 31_536_000(365 days) for past-block endpoints. Past-window/provider-rewards, block-scoped/provider-estimated-rewards?block=, and/provider-estimated-rewards/blocksall graduate fromHISTORICAL(1 day) toIMMUTABLE. Avatars +/burn-ratestay onHISTORICALsince they can change.Test plan
pnpm --filter @info/api typecheckcleanpnpm --filter @info/api test— 114 passing. New assertions:buildHistoricalPriceMapcalled with only the denoms actually in the block's rewards (not all 22)processRawProviderRewardsis NOT called (refuses to ship wrong numbers)?block=omitted) doesn't invoke any historical code pathKnown behavior
If a block's rewards contain a denom we've never seen before AND CoinGecko rate-limits the first fetch for that denom for longer than 5 retries (~2 min), the request 503s. The next caller retries from scratch — no bad response gets cached. LAVA never hits this because its price is in the live-cache pre-warm.