Repository navigation
feat(api): CoinGecko retry-on-throttle + fail-loud + 1-year cache for past-block rewards - #40
Merged
Merged
Conversation
Hardens historical rewards pricing so wrong numbers never land in the
block-keyed cache for a year.
Three changes layered together:
1. Retry on CoinGecko 429 with backoff
----------------------------------
Free tier throttles ~10-30/min. New coingeckoFetch helper wraps each
CoinGecko request with:
• up to 5 attempts
• honors Retry-After header when present
• exponential backoff (2^attempt seconds, capped at 60s) otherwise
Applies to both current-price (fetchLavaUsdPrice) and historical-price
(fetchTokenUsdPriceAt) paths.
2. Fail loud on rate-limit exhaustion
----------------------------------
Before: fetchTokenUsdPriceAt caught all errors and returned 0 →
processRewardTokens fell back to current price cache → USD figure in
response was WRONG but cached as if historical → wrong for a year.
Now: fetchTokenUsdPriceAt throws on transient failures (rate limit,
network error) and only returns 0 for legitimate "no data" (unknown
denom, 404, empty market_data). Zeros cache indefinitely (permanent
facts), throws don't cache (next caller retries).
buildHistoricalPriceMap makes LAVA required (virtually all reward value
is LAVA), other denoms best-effort. If LAVA throws, the route returns
503 with a clear "please retry" message. Cache plugin skips 4xx/5xx so
no bad response gets written to Redis.
3. 1-year cache for immutable past-block snapshots
-----------------------------------------------
New CACHE_TTL.IMMUTABLE = 31_536_000. Applied to:
• /provider-estimated-rewards?block=N (response fully determined by
past chain state + block-time prices, both unchangeable)
• /provider-estimated-rewards/blocks (monthly 17th heights permanent)
• /provider-rewards?from=&to= when `to` is older than today - 1 day
Kept on HISTORICAL (1 day):
• /providers/:addr/avatar (Keybase avatars can change)
• /burn-rate (mixes immutable blocks with a "latest" field)
Tests updated: 113/113. Added a case asserting that when
buildHistoricalPriceMap throws, the route returns 503 AND does not call
fetchRewardsBySpec — i.e. we refuse to ship a response rather than
silently shipping one with the wrong price.
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
Three layered fixes so a historical rewards query never silently bakes a wrong USD price into the response cache:
Retry-AfterCACHE_TTL.IMMUTABLE = 31_536_000) since past-block data is fully deterministicThe bug we're fixing
Today (post-PR #39):
buildHistoricalPriceMapfires 22 parallel CoinGecko callsprocessRewardTokensfalls back to current LAVA priceThe fix
(1) Retry. New
coingeckoFetchwraps each CoinGecko call:Retry-Afterheader when CoinGecko sends it(2) Fail loud.
fetchTokenUsdPriceAtnow distinguishes "no data" from "can't reach":market_data→ return 0, cache indefinitely (permanent fact)buildHistoricalPriceMap:Route handler catches the LAVA exception and returns 503 with a clear "please retry" message. Cache plugin already skips 4xx/5xx (
cache.ts:60), so no bad response lands in Redis.(3) 1-year cache. New
CACHE_TTL.IMMUTABLEapplied to:/provider-estimated-rewards?block=N— response fully determined by past state + block-time prices/provider-estimated-rewards/blocks— monthly 17th heights are permanent/provider-rewards?from=&to=whentois older than today - 1 dayKept on 1-day HISTORICAL:
/providers/:addr/avatar(Keybase updates)/burn-rate(mixes immutable blocks with alatestfield)Test plan
pnpm --filter @info/api typecheckcleanpnpm --filter @info/api test— 113 passing (new: 503 when historical price unavailable ANDfetchRewardsBySpecis NOT called — proves we refuse to ship wrong numbers)Known limitation
Retries can make a first-time cold request take several minutes under heavy throttling. Once the first response succeeds, it's cached for a year — so this only matters for the initial cold fetch. If the free-tier throttling becomes a real bottleneck, we should upgrade to CoinGecko's demo/pro API key (via a
x-cg-demo-api-keyheader on the existing fetch path).