Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 51 additions & 30 deletions apps/api/src/__tests__/routes-provider-estimated-rewards.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { errorHandlerPlugin } from "../plugins/error-handler.js";
vi.mock("../rpc/lava.js", () => ({
RPC_BATCH_SIZE: 5,
prewarmPriceCache: vi.fn(),
fetchProvidersWithSpecs: vi.fn(),
fetchAllSpecs: vi.fn(),
fetchAllProviderMonikers: vi.fn(),
fetchRawProviderRewards: vi.fn(),
extractBaseDenoms: vi.fn(),
processRawProviderRewards: vi.fn(),
Expand All @@ -15,9 +16,14 @@ vi.mock("../rpc/lava.js", () => ({
buildHistoricalPriceMap: vi.fn(),
}));

vi.mock("../graphql/client.js", () => ({
gqlSafe: vi.fn(),
}));

const {
prewarmPriceCache,
fetchProvidersWithSpecs,
fetchAllSpecs,
fetchAllProviderMonikers,
fetchRawProviderRewards,
extractBaseDenoms,
processRawProviderRewards,
Expand All @@ -26,6 +32,7 @@ const {
fetchLavaUsdPrice,
buildHistoricalPriceMap,
} = await import("../rpc/lava.js");
const { gqlSafe } = await import("../graphql/client.js");
const { providerEstimatedRewardsRoutes } = await import("../routes/provider-estimated-rewards.js");

async function buildApp() {
Expand All @@ -35,12 +42,16 @@ async function buildApp() {
return app;
}

const MOCK_PROVIDERS_WITH_SPECS = {
providers: new Map([
["lava@1abc", { moniker: "AlphaProvider", identity: "", commission: "50", specs: [] }],
["lava@2def", { moniker: "BetaProvider", identity: "", commission: "30", specs: [] }],
]),
specNames: new Map([["ETH1", "Ethereum Mainnet"], ["NEAR", "Near"]]),
const MOCK_SPECS = [
{ index: "ETH1", name: "Ethereum Mainnet" },
{ index: "NEAR", name: "Near" },
];
const MOCK_MONIKERS = new Map([
["lava@1abc", "AlphaProvider"],
["lava@2def", "BetaProvider"],
]);
const MOCK_INDEXER_PROVIDERS = {
allProviders: { nodes: [{ addr: "lava@1abc" }, { addr: "lava@2def" }] },
};

const TOK = (amount: string, usd: string) => ({
Expand Down Expand Up @@ -90,7 +101,9 @@ beforeEach(() => {
vi.resetAllMocks();
(prewarmPriceCache as ReturnType<typeof vi.fn>).mockResolvedValue(undefined);
(fetchLavaUsdPrice as ReturnType<typeof vi.fn>).mockResolvedValue(0.12);
(fetchProvidersWithSpecs as ReturnType<typeof vi.fn>).mockResolvedValue(MOCK_PROVIDERS_WITH_SPECS);
(fetchAllSpecs as ReturnType<typeof vi.fn>).mockResolvedValue(MOCK_SPECS);
(fetchAllProviderMonikers as ReturnType<typeof vi.fn>).mockResolvedValue(MOCK_MONIKERS);
(gqlSafe as ReturnType<typeof vi.fn>).mockResolvedValue(MOCK_INDEXER_PROVIDERS);
(fetchRawProviderRewards as ReturnType<typeof vi.fn>).mockResolvedValue(STUB_RAW);
(extractBaseDenoms as ReturnType<typeof vi.fn>).mockResolvedValue(new Set(["lava"]));
(processRawProviderRewards as ReturnType<typeof vi.fn>).mockImplementation(
Expand Down Expand Up @@ -179,13 +192,9 @@ describe("GET /provider-estimated-rewards", () => {
});

it("batches provider RPC calls 5 at a time", async () => {
const providers = new Map<string, { moniker: string; identity: string; commission: string; specs: never[] }>();
for (let i = 0; i < 12; i++) {
providers.set(`lava@p${i}`, { moniker: `P${i}`, identity: "", commission: "50", specs: [] });
}
(fetchProvidersWithSpecs as ReturnType<typeof vi.fn>).mockResolvedValue({
providers, specNames: new Map(),
});
const nodes = Array.from({ length: 12 }, (_, i) => ({ addr: `lava@p${i}` }));
(gqlSafe as ReturnType<typeof vi.fn>).mockResolvedValue({ allProviders: { nodes } });
(fetchAllProviderMonikers as ReturnType<typeof vi.fn>).mockResolvedValue(new Map());
(processRawProviderRewards as ReturnType<typeof vi.fn>).mockResolvedValue([]);
const app = await buildApp();
await app.inject({ method: "GET", url: "/provider-estimated-rewards" });
Expand All @@ -208,7 +217,11 @@ describe("GET /provider-estimated-rewards", () => {

it("for historical blocks, fetches prices only for denoms actually in rewards", async () => {
(extractBaseDenoms as ReturnType<typeof vi.fn>).mockResolvedValue(new Set(["lava", "atom"]));
(buildHistoricalPriceMap as ReturnType<typeof vi.fn>).mockResolvedValue({ lava: 0.035, atom: 8.2 });
// LAVA fetch (kicked off in parallel with fan-out) returns lava; the
// follow-up non-LAVA fetch resolves the remaining denoms.
(buildHistoricalPriceMap as ReturnType<typeof vi.fn>)
.mockResolvedValueOnce({ lava: 0.035 })
.mockResolvedValueOnce({ atom: 8.2 });

const app = await buildApp();
const res = await app.inject({
Expand All @@ -217,15 +230,15 @@ describe("GET /provider-estimated-rewards", () => {
});
expect(res.statusCode).toBe(200);

// buildHistoricalPriceMap called with ONLY the relevant denoms (not all 22)
expect(buildHistoricalPriceMap).toHaveBeenCalledWith(
expect.any(Date),
expect.arrayContaining(["lava", "atom"]),
);
const passedDenoms = (buildHistoricalPriceMap as ReturnType<typeof vi.fn>).mock.calls[0]![1] as string[];
expect(passedDenoms.length).toBe(2);
// Two calls total: ['lava'] upfront + any non-LAVA denoms after fan-out.
// Never all 22 known denoms (that used to blow the gateway timeout).
expect(buildHistoricalPriceMap).toHaveBeenCalledTimes(2);
const first = (buildHistoricalPriceMap as ReturnType<typeof vi.fn>).mock.calls[0]![1] as string[];
expect(first).toEqual(["lava"]);
const second = (buildHistoricalPriceMap as ReturnType<typeof vi.fn>).mock.calls[1]![1] as string[];
expect(second).toEqual(["atom"]);

// Price overrides passed down to processRawProviderRewards
// Merged price overrides passed down to processRawProviderRewards
for (const call of (processRawProviderRewards as ReturnType<typeof vi.fn>).mock.calls) {
expect(call[2]).toEqual({ lava: 0.035, atom: 8.2 });
}
Expand All @@ -248,20 +261,28 @@ describe("GET /provider-estimated-rewards", () => {
expect(fetchLavaUsdPrice).not.toHaveBeenCalled();
});

it("snapshots the provider set at the historical block", async () => {
it("uses the indexer's all-time provider list (includes unstaked historical providers)", async () => {
// Indexer has a provider that isn't currently staked (no moniker). Route
// should still query it at the historical block and render it with "-".
(gqlSafe as ReturnType<typeof vi.fn>).mockResolvedValue({
allProviders: { nodes: [{ addr: "lava@1abc" }, { addr: "lava@2def" }, { addr: "lava@3ghost" }] },
});
mockProcessPerProvider();
(fetchBlockTime as ReturnType<typeof vi.fn>).mockResolvedValue("2026-03-17T15:00:00Z");
const app = await buildApp();
await app.inject({ method: "GET", url: "/provider-estimated-rewards?block=4697952" });
expect(fetchProvidersWithSpecs).toHaveBeenCalledWith(4697952);
const res = await app.inject({ method: "GET", url: "/provider-estimated-rewards?block=4697952" });
expect(res.statusCode).toBe(200);
expect(fetchRawProviderRewards).toHaveBeenCalledTimes(3);
const queriedAddrs = (fetchRawProviderRewards as ReturnType<typeof vi.fn>).mock.calls.map((c) => c[0]);
expect(queriedAddrs).toEqual(expect.arrayContaining(["lava@1abc", "lava@2def", "lava@3ghost"]));
});

it("uses live prices + current provider set when ?block= is omitted", async () => {
it("uses live prices when ?block= is omitted", async () => {
(fetchLavaUsdPrice as ReturnType<typeof vi.fn>).mockResolvedValue(0.025);
const app = await buildApp();
const res = await app.inject({ method: "GET", url: "/provider-estimated-rewards" });
const body = JSON.parse(res.body);
expect(body.meta.priceLavaUsd).toBe(0.025);
expect(fetchProvidersWithSpecs).toHaveBeenCalledWith(undefined);
expect(buildHistoricalPriceMap).not.toHaveBeenCalled();
for (const call of (processRawProviderRewards as ReturnType<typeof vi.fn>).mock.calls) {
expect(call[2]).toBeUndefined();
Expand Down
94 changes: 56 additions & 38 deletions apps/api/src/routes/provider-estimated-rewards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@ import {
RPC_BATCH_SIZE,
buildHistoricalPriceMap,
extractBaseDenoms,
fetchAllProviderMonikers,
fetchAllSpecs,
fetchBlockAtTimestamp,
fetchBlockTime,
fetchLavaUsdPrice,
fetchProvidersWithSpecs,
fetchRawProviderRewards,
prewarmPriceCache,
processRawProviderRewards,
type EstimatedRewardsResponse,
type RewardsBySpecEntry,
} from "../rpc/lava.js";
import { gqlSafe } from "../graphql/client.js";
import { sendApiError } from "../plugins/error-handler.js";

// ── Helpers ──────────────────────────────────────────────────────────────────
Expand Down Expand Up @@ -138,9 +140,6 @@ export async function providerEstimatedRewardsRoutes(app: FastifyInstance) {

await prewarmPriceCache();

// Compute block metadata upfront (needed for both path branches) and
// snapshot the provider set AT the block (when set) so providers who have
// since unstaked / deregistered still appear in historical responses.
let priceTimestamp: string;
let priceLavaUsd: number | undefined;
const blockTimeIso = block ? await fetchBlockTime(block) : null;
Expand All @@ -151,52 +150,71 @@ export async function providerEstimatedRewardsRoutes(app: FastifyInstance) {
priceLavaUsd = await fetchLavaUsdPrice();
}

const { providers: providerMap, specNames } = await fetchProvidersWithSpecs(block);
const addresses = Array.from(providerMap.keys());
// ── Resolve addresses ────────────────────────────────────────────────
// Source of truth is the indexer's `app.providers` table — every provider
// that's ever been paid a relay, which is a superset of anyone who can
// have estimated rewards at a historical block. Monikers come from the
// current chain stake list (fast, cached); providers who've since unstaked
// won't have a moniker and render as "-" in the FE.
const [specs, indexerProviders, monikerMap] = await Promise.all([
fetchAllSpecs(),
gqlSafe<{ allProviders: { nodes: Array<{ addr: string }> } } | null>(
`query { allProviders { nodes { addr } } }`,
undefined,
null,
),
fetchAllProviderMonikers(),
]);
const specNames = new Map(specs.map((s) => [s.index, s.name]));
const providerMap = new Map<string, { moniker: string }>();
const addresses: string[] = [];
for (const node of indexerProviders?.allProviders.nodes ?? []) {
addresses.push(node.addr);
providerMap.set(node.addr, { moniker: monikerMap.get(node.addr) ?? "" });
}

// ── LAVA price: kicked off here so it overlaps with the chain fan-out.
// ~99% of reward denoms are ulava; non-LAVA denoms that actually appear
// are fetched after fan-out inside the rare-path below. On live (non-block)
// queries we use the live cache, no override needed.
let priceOverrides: Record<string, number> | undefined;
const lavaPricePromise: Promise<Record<string, number>> | null =
block && blockTimeIso
? buildHistoricalPriceMap(new Date(blockTimeIso), ["lava"])
: null;

// ── Pass 1: fetch raw rewards for every provider ─────────────────────
// Keeps the chain response so we can (a) inspect which denoms actually
// appear and (b) format the final output with the right prices, without
// paying for a second chain-RPC round.
// ── Chain fan-out: per-provider estimated_provider_rewards ──────────
const rawByAddr = new Map<string, EstimatedRewardsResponse>();
for (let i = 0; i < addresses.length; i += RPC_BATCH_SIZE) {
const batch = addresses.slice(i, i + RPC_BATCH_SIZE);
const raws = await Promise.all(batch.map((addr) => fetchRawProviderRewards(addr, block)));
batch.forEach((addr, j) => rawByAddr.set(addr, raws[j]!));
}

// ── Price overrides ──────────────────────────────────────────────────
// For historical blocks, fetch prices ONLY for denoms that appear in the
// rewards (typically just LAVA + 0-2 IBC denoms). Fetching all 22 known
// denoms sequentially with retry backoff would exceed the gateway timeout
// under any CoinGecko throttling.
let priceOverrides: Record<string, number> | undefined;
if (block && blockTimeIso) {
if (lavaPricePromise) {
try {
priceOverrides = await lavaPricePromise;
} catch (err) {
// Cache plugin skips 4xx/5xx — refusing to ship avoids poisoning the
// block-keyed cache with a wrong price for a year.
return sendApiError(
reply, 503,
`historical LAVA price unavailable for block ${block} (${blockTimeIso}); please retry: ${(err as Error).message}`,
);
}

// Rare non-LAVA denoms that actually appeared in this block. IBC
// resolution hits chain denom_trace (cached), so this is cheap.
const relevantDenoms = await extractBaseDenoms([...rawByAddr.values()]);
if (relevantDenoms.size === 0) {
// No priceable rewards in this block — nothing to override.
priceOverrides = {};
priceLavaUsd = 0;
} else {
try {
priceOverrides = await buildHistoricalPriceMap(
new Date(blockTimeIso),
[...relevantDenoms],
);
} catch (err) {
// LAVA historical price unavailable. Refuse to ship a response —
// the cache plugin skips 4xx/5xx so we don't poison the block-keyed
// cache with a wrong price for a year.
return sendApiError(
reply, 503,
`historical LAVA price unavailable for block ${block} (${blockTimeIso}); please retry: ${(err as Error).message}`,
);
}
priceLavaUsd = priceOverrides.lava ?? await fetchLavaUsdPrice();
const missing = [...relevantDenoms].filter((d) => priceOverrides![d] === undefined);
if (missing.length > 0) {
const extra = await buildHistoricalPriceMap(new Date(blockTimeIso!), missing);
priceOverrides = { ...priceOverrides, ...extra };
}
priceLavaUsd = priceOverrides.lava ?? priceLavaUsd ?? await fetchLavaUsdPrice();
}

// ── Pass 2: format each provider's raw response with the right prices ─
// ── Format each provider's raw response ─────────────────────────────
const results: Array<{
provider: string;
moniker: string;
Expand Down
18 changes: 9 additions & 9 deletions apps/api/src/rpc/block.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { config } from "../config.js";
import { fetchRest } from "./rest.js";

// Lava mainnet genesis timestamp (block 1) — used to narrow binary search range.
const LAVA_GENESIS_UNIX = 1_713_350_000; // ~2024-04-17
Expand All @@ -9,16 +10,15 @@ const BLOCK_AT_TS_CACHE_MAX = 200;

/** Fetch the timestamp of a given block height (ISO 8601 string).
* Used for historical pricing — lets callers query CoinGecko for LAVA
* price at the block's date instead of using the current price. */
* price at the block's date instead of using the current price.
* Routes through fetchRest so retries + the archive header kick in for
* old blocks; otherwise the tendermint-RPC proxy gateway-times-out on
* pruned replicas and leaves callers with a malformed response. */
export async function fetchBlockTime(height: number): Promise<string> {
const res = await fetch(`${config.lava.rpcUrl}/block?height=${height}`, {
signal: AbortSignal.timeout(10_000),
});
if (!res.ok) throw new Error(`RPC ${res.status}`);
const data = (await res.json()) as {
result: { block: { header: { time: string } } };
};
return data.result.block.header.time;
const data = await fetchRest<{ block: { header: { time: string } } }>(
`/cosmos/base/tendermint/v1beta1/blocks/${height}`,
);
return data.block.header.time;
}

export async function fetchLatestBlockHeight(): Promise<{
Expand Down
13 changes: 9 additions & 4 deletions apps/api/src/rpc/providers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,13 @@ export async function fetchProvidersWithSpecs(

for (let i = 0; i < allSpecs.length; i += RPC_BATCH_SIZE) {
const batch = allSpecs.slice(i, i + RPC_BATCH_SIZE);
// No .catch() — fetchRest now retries transient failures (5xx, 429,
// network) up to 3 times. If it still throws, we'd rather 500 loudly
// than silently drop providers and cache an incomplete response. The
// cache plugin skips 4xx/5xx so a failed request never poisons Redis.
const results = await Promise.all(
batch.map((s) => fetchProvidersForSpec(s.index, blockHeight)
.then((ps) => ps.map((p) => ({ ...p, specId: s.index, specName: s.name })))
.catch(() => [] as Array<ProviderForSpec & { specId: string; specName: string }>)),
.then((ps) => ps.map((p) => ({ ...p, specId: s.index, specName: s.name })))),
);

for (const providers of results) {
Expand Down Expand Up @@ -222,6 +225,9 @@ async function fetchAllProvidersImpl(): Promise<AllProvidersResult> {
for (let i = 0; i < specs.length; i += RPC_BATCH_SIZE) {
const batch = specs.slice(i, i + RPC_BATCH_SIZE);
const results = await Promise.all(
// Same rationale as fetchProvidersWithSpecs: no .catch(). fetchRest
// retries transient failures; if it still throws, fail loud rather
// than silently drop providers from /providers or /all_providers_apr.
batch.map((s) =>
fetchProvidersForSpec(s.index)
.then((providers) => providers.map((p): SpecEntry => ({
Expand All @@ -232,8 +238,7 @@ async function fetchAllProvidersImpl(): Promise<AllProvidersResult> {
delegate_total: p.delegate_total,
delegate_commission: p.delegate_commission,
specId: s.index,
})))
.catch(() => [] as SpecEntry[]),
}))),
),
);
specProviders.push(...results);
Expand Down
Loading
Loading