diff --git a/.gitignore b/.gitignore index b2b303a..1da6bd2 100644 --- a/.gitignore +++ b/.gitignore @@ -36,3 +36,6 @@ CLAUDE.local.md # pnpm store .pnpm-store/ + +# Next.js dev artifacts (old/backup directories) +.next.old/ diff --git a/apps/api/package.json b/apps/api/package.json index 003b5d5..eb13aaa 100644 --- a/apps/api/package.json +++ b/apps/api/package.json @@ -7,10 +7,15 @@ "build": "tsc", "dev": "tsx watch src/main.ts", "start": "node dist/main.js", + "probe:dev": "tsx watch src/bin/health-probe.ts", + "probe:start": "node dist/bin/health-probe.js", + "precompute:dev": "tsx watch src/bin/precompute.ts", + "precompute:start": "node dist/bin/precompute.js", "typecheck": "tsc --noEmit", "test": "vitest run" }, "dependencies": { + "@fastify/compress": "^8.3.1", "@fastify/cors": "^11.0.0", "@fastify/helmet": "^13.0.2", "@fastify/rate-limit": "^10.3.0", @@ -23,7 +28,8 @@ "fastify-plugin": "^5.1.0", "ioredis": "^5.4.0", "pino": "^10.0.0", - "postgres": "^3.4.9" + "postgres": "^3.4.9", + "undici": "^6.25.0" }, "devDependencies": { "@types/node": "^25.5.0", diff --git a/apps/api/src/__tests__/health-store.test.ts b/apps/api/src/__tests__/health-store.test.ts index 8f728a3..9ae31c2 100644 --- a/apps/api/src/__tests__/health-store.test.ts +++ b/apps/api/src/__tests__/health-store.test.ts @@ -71,34 +71,19 @@ describe("health-store", () => { describe("readHealthForProvider", () => { it("returns empty for unknown provider", async () => { - const result = await readHealthForProvider(redis as any, "lava@unknown", 1, 20); - expect(result.data).toEqual([]); - expect(result.total).toBe(0); + const result = await readHealthForProvider(redis as any, "lava@unknown"); + expect(result).toEqual([]); }); it("returns all health records for a provider", async () => { await writeHealthStatus(redis as any, "lava@test", "ETH1", "jsonrpc", "2", "healthy", { latency: 10 }); await writeHealthStatus(redis as any, "lava@test", "LAVA", "grpc", "1", "unhealthy", { message: "timeout" }); - const result = await readHealthForProvider(redis as any, "lava@test", 1, 20); - expect(result.total).toBe(2); - expect(result.data).toHaveLength(2); - const specs = result.data.map((r) => r.spec).sort(); + const result = await readHealthForProvider(redis as any, "lava@test"); + expect(result).toHaveLength(2); + const specs = result.map((r) => r.spec).sort(); expect(specs).toEqual(["ETH1", "LAVA"]); }); - - it("paginates correctly", async () => { - await writeHealthStatus(redis as any, "lava@test", "ETH1", "jsonrpc", "2", "healthy", {}); - await writeHealthStatus(redis as any, "lava@test", "LAVA", "grpc", "1", "healthy", {}); - await writeHealthStatus(redis as any, "lava@test", "BSC", "jsonrpc", "2", "healthy", {}); - - const page1 = await readHealthForProvider(redis as any, "lava@test", 1, 2); - expect(page1.data).toHaveLength(2); - expect(page1.total).toBe(3); - - const page2 = await readHealthForProvider(redis as any, "lava@test", 2, 2); - expect(page2.data).toHaveLength(1); - }); }); describe("readHealthSummaryForSpec", () => { diff --git a/apps/api/src/__tests__/plugins.test.ts b/apps/api/src/__tests__/plugins.test.ts index 9d91742..68cb036 100644 --- a/apps/api/src/__tests__/plugins.test.ts +++ b/apps/api/src/__tests__/plugins.test.ts @@ -1,57 +1,5 @@ import { describe, it, expect } from "vitest"; import { buildTestApp } from "./test-helpers.js"; -import { parsePagination } from "../plugins/pagination.js"; - -describe("pagination plugin", () => { - it("parses default values", () => { - const p = parsePagination({}); - expect(p.page).toBe(1); - expect(p.limit).toBe(20); - expect(p.offset).toBe(0); - expect(p.sort).toBeNull(); - expect(p.order).toBe("asc"); - }); - - it("parses custom values", () => { - const p = parsePagination({ page: "3", limit: "50", sort: "name", order: "desc" }); - expect(p.page).toBe(3); - expect(p.limit).toBe(50); - expect(p.offset).toBe(100); - expect(p.sort).toBe("name"); - expect(p.order).toBe("desc"); - }); - - it("clamps page to 1-4000", () => { - expect(parsePagination({ page: "0" }).page).toBe(1); - expect(parsePagination({ page: "-5" }).page).toBe(1); - expect(parsePagination({ page: "5000" }).page).toBe(4000); - }); - - it("clamps limit to 1-100", () => { - expect(parsePagination({ limit: "0" }).limit).toBe(20); // 0 is falsy → falls to default 20 - expect(parsePagination({ limit: "200" }).limit).toBe(100); - expect(parsePagination({ limit: "-1" }).limit).toBe(1); - expect(parsePagination({ limit: "1" }).limit).toBe(1); - expect(parsePagination({ limit: "100" }).limit).toBe(100); - }); - - it("defaults order to asc for invalid values", () => { - expect(parsePagination({ order: "invalid" }).order).toBe("asc"); - expect(parsePagination({ order: "" }).order).toBe("asc"); - }); - - it("handles NaN gracefully", () => { - const p = parsePagination({ page: "abc", limit: "xyz" }); - expect(p.page).toBe(1); - expect(p.limit).toBe(20); - }); - - it("calculates offset correctly", () => { - expect(parsePagination({ page: "1", limit: "10" }).offset).toBe(0); - expect(parsePagination({ page: "2", limit: "10" }).offset).toBe(10); - expect(parsePagination({ page: "5", limit: "25" }).offset).toBe(100); - }); -}); describe("error handler plugin", () => { it("returns 404 for unknown routes", async () => { diff --git a/apps/api/src/__tests__/routes-all-providers-apr.test.ts b/apps/api/src/__tests__/routes-all-providers-apr.test.ts index e685cdb..37e96e1 100644 --- a/apps/api/src/__tests__/routes-all-providers-apr.test.ts +++ b/apps/api/src/__tests__/routes-all-providers-apr.test.ts @@ -1,6 +1,5 @@ import { describe, it, expect, vi } from "vitest"; import Fastify from "fastify"; -import { paginationPlugin } from "../plugins/pagination.js"; import { errorHandlerPlugin } from "../plugins/error-handler.js"; vi.mock("../rpc/lava.js", () => ({ @@ -17,7 +16,6 @@ const { allProvidersAprRoutes } = await import("../routes/all-providers-apr.js") async function buildApp() { const app = Fastify({ logger: false }); await app.register(errorHandlerPlugin); - await app.register(paginationPlugin); await app.register(allProvidersAprRoutes, { prefix: "/all_providers_apr" }); return app; } diff --git a/apps/api/src/__tests__/routes-apr.test.ts b/apps/api/src/__tests__/routes-apr.test.ts index 47af496..ad8c30f 100644 --- a/apps/api/src/__tests__/routes-apr.test.ts +++ b/apps/api/src/__tests__/routes-apr.test.ts @@ -1,6 +1,5 @@ import { describe, it, expect, vi } from "vitest"; import Fastify from "fastify"; -import { paginationPlugin } from "../plugins/pagination.js"; import { errorHandlerPlugin } from "../plugins/error-handler.js"; vi.mock("../rpc/lava.js", () => ({ @@ -13,7 +12,6 @@ const { aprRoutes } = await import("../routes/apr.js"); async function buildApp() { const app = Fastify({ logger: false }); await app.register(errorHandlerPlugin); - await app.register(paginationPlugin); await app.register(aprRoutes); return app; } diff --git a/apps/api/src/__tests__/routes-burn-rate.test.ts b/apps/api/src/__tests__/routes-burn-rate.test.ts index 60f11a7..10ae761 100644 --- a/apps/api/src/__tests__/routes-burn-rate.test.ts +++ b/apps/api/src/__tests__/routes-burn-rate.test.ts @@ -1,6 +1,5 @@ import { describe, it, expect, vi, beforeEach } from "vitest"; import Fastify from "fastify"; -import { paginationPlugin } from "../plugins/pagination.js"; import { errorHandlerPlugin } from "../plugins/error-handler.js"; vi.mock("../rpc/lava.js", () => ({ @@ -19,7 +18,6 @@ const { burnRateRoutes } = await import("../routes/burn-rate.js"); async function buildApp() { const app = Fastify({ logger: false }); await app.register(errorHandlerPlugin); - await app.register(paginationPlugin); await app.register(burnRateRoutes); return app; } diff --git a/apps/api/src/__tests__/routes-health.test.ts b/apps/api/src/__tests__/routes-health.test.ts index 0cef145..d35e8b8 100644 --- a/apps/api/src/__tests__/routes-health.test.ts +++ b/apps/api/src/__tests__/routes-health.test.ts @@ -1,6 +1,5 @@ import { describe, it, expect, vi } from "vitest"; import Fastify from "fastify"; -import { paginationPlugin } from "../plugins/pagination.js"; import { errorHandlerPlugin } from "../plugins/error-handler.js"; vi.mock("../rpc/lava.js", () => ({ @@ -13,7 +12,6 @@ const { healthRoutes } = await import("../routes/health.js"); async function buildApp() { const app = Fastify({ logger: false }); await app.register(errorHandlerPlugin); - await app.register(paginationPlugin); await app.register(healthRoutes); return app; } diff --git a/apps/api/src/__tests__/routes-index.test.ts b/apps/api/src/__tests__/routes-index.test.ts new file mode 100644 index 0000000..173fe5a --- /dev/null +++ b/apps/api/src/__tests__/routes-index.test.ts @@ -0,0 +1,80 @@ +import { describe, it, expect, vi, beforeEach } from "vitest"; +import Fastify from "fastify"; +import { errorHandlerPlugin } from "../plugins/error-handler.js"; + +vi.mock("../graphql/client.js", () => ({ gqlSafe: vi.fn() })); +vi.mock("../rpc/lava.js", () => ({ + fetchLatestBlockHeight: vi.fn(), + fetchAllProviders: vi.fn(), +})); + +const { gqlSafe } = await import("../graphql/client.js"); +const { fetchLatestBlockHeight, fetchAllProviders } = await import("../rpc/lava.js"); +const { indexRoutes } = await import("../routes/index.js"); + +async function buildApp() { + const app = Fastify({ logger: false }); + await app.register(errorHandlerPlugin); + await app.register(indexRoutes, { prefix: "/index" }); + return app; +} + +beforeEach(() => vi.resetAllMocks()); + +describe("GET /index/stats", () => { + it("returns aggregate stats enriched with latest block height", async () => { + (gqlSafe as ReturnType).mockResolvedValue({ + mvRelayDailies: { aggregates: { sum: { cu: "1000", relays: "50" } } }, + }); + (fetchLatestBlockHeight as ReturnType).mockResolvedValue({ + height: 12345, + time: "2025-01-01T00:00:00Z", + }); + (fetchAllProviders as ReturnType).mockResolvedValue([ + { address: "lava@a", totalStake: "1000", totalDelegation: "500" }, + ]); + + const app = await buildApp(); + const res = await app.inject({ method: "GET", url: "/index/stats" }); + expect(res.statusCode).toBe(200); + const body = JSON.parse(res.body); + expect(body.latestBlock).toBe(12345); + expect(body.activeProviderCount).toBe(1); + expect(body.totalStake).toBe("1500"); + }); +}); + +describe("GET /index/charts", () => { + it("returns empty data when indexer is down", async () => { + (gqlSafe as ReturnType).mockResolvedValue(null); + const app = await buildApp(); + const res = await app.inject({ method: "GET", url: "/index/charts" }); + expect(res.statusCode).toBe(200); + expect(JSON.parse(res.body)).toEqual({ data: [] }); + }); + + it("groups daily MV rows by (date, chainId) and computes weighted QoS", async () => { + (gqlSafe as ReturnType).mockResolvedValue({ + mvRelayDailies: { + nodes: [ + { + date: "2025-01-01", chainId: "ETH1", cu: "100", relays: "10", + qosSyncW: 8, qosAvailW: 9, qosLatencyW: 10, qosWeight: "10", + }, + { + date: "2025-01-01", chainId: "ETH1", cu: "50", relays: "5", + qosSyncW: 4, qosAvailW: 5, qosLatencyW: 5, qosWeight: "5", + }, + ], + }, + }); + const app = await buildApp(); + const res = await app.inject({ method: "GET", url: "/index/charts?from=2025-01-01&to=2025-01-31" }); + const body = JSON.parse(res.body); + expect(body.data).toHaveLength(1); + const row = body.data[0]; + expect(row.cu).toBe("150"); + expect(row.relays).toBe("15"); + expect(row.qosSync).toBeCloseTo(12 / 15); + }); +}); diff --git a/apps/api/src/__tests__/routes-lava.test.ts b/apps/api/src/__tests__/routes-lava.test.ts new file mode 100644 index 0000000..c7355f3 --- /dev/null +++ b/apps/api/src/__tests__/routes-lava.test.ts @@ -0,0 +1,40 @@ +import { describe, it, expect, vi } from "vitest"; +import Fastify from "fastify"; +import { errorHandlerPlugin } from "../plugins/error-handler.js"; + +vi.mock("../rpc/lava.js", () => ({ + fetchAllSpecs: vi.fn(), +})); + +const { fetchAllSpecs } = await import("../rpc/lava.js"); +const { lavaRoutes } = await import("../routes/lava.js"); + +async function buildApp() { + const app = Fastify({ logger: false }); + await app.register(errorHandlerPlugin); + await app.register(lavaRoutes, { prefix: "/lava" }); + return app; +} + +describe("GET /lava/specs", () => { + it("returns all chain specs from rpc", async () => { + (fetchAllSpecs as ReturnType).mockResolvedValue([ + { index: "ETH1", name: "Ethereum Mainnet" }, + { index: "LAVA", name: "Lava Mainnet" }, + ]); + const app = await buildApp(); + const res = await app.inject({ method: "GET", url: "/lava/specs" }); + expect(res.statusCode).toBe(200); + const body = JSON.parse(res.body); + expect(body.data).toHaveLength(2); + expect(body.data[0].index).toBe("ETH1"); + }); + + it("returns empty data if rpc returns nothing", async () => { + (fetchAllSpecs as ReturnType).mockResolvedValue([]); + const app = await buildApp(); + const res = await app.inject({ method: "GET", url: "/lava/specs" }); + expect(res.statusCode).toBe(200); + expect(JSON.parse(res.body).data).toEqual([]); + }); +}); diff --git a/apps/api/src/__tests__/routes-optimizer-metrics.test.ts b/apps/api/src/__tests__/routes-optimizer-metrics.test.ts new file mode 100644 index 0000000..c4f67d7 --- /dev/null +++ b/apps/api/src/__tests__/routes-optimizer-metrics.test.ts @@ -0,0 +1,62 @@ +import { describe, it, expect } from "vitest"; +import Fastify from "fastify"; +import { errorHandlerPlugin } from "../plugins/error-handler.js"; +import { optimizerMetricsRoutes } from "../routes/optimizer-metrics.js"; + +async function buildApp(withDb: boolean) { + const app = Fastify({ logger: false }); + await app.register(errorHandlerPlugin); + if (withDb) { + // Minimal tagged-template stub for routes that early-return when relaysDb is set; + // we don't actually need to execute queries for these tests. + const stub = Object.assign(() => ({}), { unsafe: () => "" }); + app.decorate("relaysDb", stub as never); + } else { + app.decorate("relaysDb", null); + } + await app.register(optimizerMetricsRoutes); + return app; +} + +describe("GET /providers/:addr/optimizer-metrics", () => { + it("returns 503 when relays DB is not configured", async () => { + const app = await buildApp(false); + const res = await app.inject({ + method: "GET", + url: "/providers/lava@abc/optimizer-metrics", + }); + expect(res.statusCode).toBe(503); + expect(JSON.parse(res.body).message).toContain("not configured"); + }); + + it("rejects invalid `from` date with 400", async () => { + const app = await buildApp(true); + const res = await app.inject({ + method: "GET", + url: "/providers/lava@abc/optimizer-metrics?from=not-a-date", + }); + expect(res.statusCode).toBe(400); + expect(JSON.parse(res.body).message).toContain("invalid from"); + }); + + it("rejects invalid `to` date with 400", async () => { + const app = await buildApp(true); + const res = await app.inject({ + method: "GET", + url: "/providers/lava@abc/optimizer-metrics?to=2025-02-30", + }); + expect(res.statusCode).toBe(400); + expect(JSON.parse(res.body).message).toContain("invalid to"); + }); +}); + +describe("GET /specs/:specId/optimizer-metrics", () => { + it("returns 503 when relays DB is not configured", async () => { + const app = await buildApp(false); + const res = await app.inject({ + method: "GET", + url: "/specs/ETH1/optimizer-metrics", + }); + expect(res.statusCode).toBe(503); + }); +}); diff --git a/apps/api/src/__tests__/routes-provider-claimable-rewards.test.ts b/apps/api/src/__tests__/routes-provider-claimable-rewards.test.ts index f20cd7e..b3301db 100644 --- a/apps/api/src/__tests__/routes-provider-claimable-rewards.test.ts +++ b/apps/api/src/__tests__/routes-provider-claimable-rewards.test.ts @@ -1,6 +1,5 @@ import { describe, it, expect, vi, beforeEach } from "vitest"; import Fastify from "fastify"; -import { paginationPlugin } from "../plugins/pagination.js"; import { errorHandlerPlugin } from "../plugins/error-handler.js"; vi.mock("../rpc/lava.js", () => ({ @@ -22,7 +21,6 @@ const { providerClaimableRewardsRoutes } = await import("../routes/provider-clai async function buildApp() { const app = Fastify({ logger: false }); await app.register(errorHandlerPlugin); - await app.register(paginationPlugin); await app.register(providerClaimableRewardsRoutes); return app; } diff --git a/apps/api/src/__tests__/routes-provider-estimated-rewards.test.ts b/apps/api/src/__tests__/routes-provider-estimated-rewards.test.ts index a0cadbe..8e0c713 100644 --- a/apps/api/src/__tests__/routes-provider-estimated-rewards.test.ts +++ b/apps/api/src/__tests__/routes-provider-estimated-rewards.test.ts @@ -1,6 +1,5 @@ import { describe, it, expect, vi, beforeEach } from "vitest"; import Fastify from "fastify"; -import { paginationPlugin } from "../plugins/pagination.js"; import { errorHandlerPlugin } from "../plugins/error-handler.js"; vi.mock("../rpc/lava.js", () => ({ @@ -24,7 +23,6 @@ const { providerEstimatedRewardsRoutes } = await import("../routes/provider-esti async function buildApp() { const app = Fastify({ logger: false }); await app.register(errorHandlerPlugin); - await app.register(paginationPlugin); await app.register(providerEstimatedRewardsRoutes); return app; } @@ -201,7 +199,7 @@ describe("GET /provider-estimated-rewards", () => { url: "/provider-estimated-rewards?spec=!", }); expect(res.statusCode).toBe(400); - expect(JSON.parse(res.body).error).toMatch(/bad spec format/); + expect(JSON.parse(res.body).message).toMatch(/bad spec format/); }); it("excludes providers whose filtered rewards become empty", async () => { diff --git a/apps/api/src/__tests__/routes-provider-rewards.test.ts b/apps/api/src/__tests__/routes-provider-rewards.test.ts index 0ea7560..4c03e0e 100644 --- a/apps/api/src/__tests__/routes-provider-rewards.test.ts +++ b/apps/api/src/__tests__/routes-provider-rewards.test.ts @@ -1,6 +1,5 @@ import { describe, it, expect, vi, beforeEach } from "vitest"; import Fastify from "fastify"; -import { paginationPlugin } from "../plugins/pagination.js"; import { errorHandlerPlugin } from "../plugins/error-handler.js"; vi.mock("../graphql/client.js", () => ({ @@ -9,17 +8,16 @@ vi.mock("../graphql/client.js", () => ({ vi.mock("../rpc/lava.js", () => ({ fetchProvidersForSpec: vi.fn(), - fetchAllProviders: vi.fn(), + fetchAllProviderMonikers: vi.fn(), })); const { gqlSafe } = await import("../graphql/client.js"); -const { fetchAllProviders } = await import("../rpc/lava.js"); +const { fetchAllProviderMonikers } = await import("../rpc/lava.js"); const { providerRewardsRoutes } = await import("../routes/provider-rewards.js"); async function buildApp() { const app = Fastify({ logger: false }); await app.register(errorHandlerPlugin); - await app.register(paginationPlugin); await app.register(providerRewardsRoutes); return app; } @@ -82,15 +80,15 @@ const MOCK_MV_SPEC = { }, }; -const MOCK_PROVIDERS = [ - { address: "lava@1abc", moniker: "AlphaProvider" }, - { address: "lava@2def", moniker: "BetaProvider" }, -]; +const MOCK_MONIKER_MAP = new Map([ + ["lava@1abc", "AlphaProvider"], + ["lava@2def", "BetaProvider"], +]); beforeEach(() => { vi.resetAllMocks(); (gqlSafe as ReturnType).mockResolvedValue(MOCK_MV_PROVIDER); - (fetchAllProviders as ReturnType).mockResolvedValue(MOCK_PROVIDERS); + (fetchAllProviderMonikers as ReturnType).mockResolvedValue(MOCK_MONIKER_MAP); }); describe("GET /provider-rewards (default groupBy=provider)", () => { @@ -211,7 +209,7 @@ describe("GET /provider-rewards (default groupBy=provider)", () => { url: "/provider-rewards?from=2025-02-30&to=2025-04-01", }); expect(res.statusCode).toBe(400); - expect(JSON.parse(res.body).error).toMatch(/bad from date/); + expect(JSON.parse(res.body).message).toMatch(/bad from date/); }); it("rejects calendar-invalid dates (Apr 31)", async () => { @@ -221,7 +219,7 @@ describe("GET /provider-rewards (default groupBy=provider)", () => { url: "/provider-rewards?from=2025-01-01&to=2025-04-31", }); expect(res.statusCode).toBe(400); - expect(JSON.parse(res.body).error).toMatch(/bad to date/); + expect(JSON.parse(res.body).message).toMatch(/bad to date/); }); it("rejects date range exceeding 6 months", async () => { @@ -231,7 +229,7 @@ describe("GET /provider-rewards (default groupBy=provider)", () => { url: "/provider-rewards?from=2024-01-01&to=2025-01-01", }); expect(res.statusCode).toBe(400); - expect(JSON.parse(res.body).error).toMatch(/6 months/); + expect(JSON.parse(res.body).message).toMatch(/6 months/); }); it("swaps dates when to < from", async () => { @@ -253,7 +251,7 @@ describe("GET /provider-rewards (default groupBy=provider)", () => { url: "/provider-rewards?from=2025-01-01&to=2025-04-01&specs=a", }); expect(res.statusCode).toBe(400); - expect(JSON.parse(res.body).error).toMatch(/bad spec format/); + expect(JSON.parse(res.body).message).toMatch(/bad spec format/); }); it("returns zero adjusted rewards when qosCu is zero", async () => { diff --git a/apps/api/src/__tests__/routes-providers.test.ts b/apps/api/src/__tests__/routes-providers.test.ts new file mode 100644 index 0000000..34ee137 --- /dev/null +++ b/apps/api/src/__tests__/routes-providers.test.ts @@ -0,0 +1,107 @@ +import { describe, it, expect, vi, beforeEach } from "vitest"; +import Fastify from "fastify"; +import { errorHandlerPlugin } from "../plugins/error-handler.js"; + +vi.mock("../graphql/client.js", () => ({ gqlSafe: vi.fn() })); +vi.mock("../rpc/lava.js", () => ({ + fetchAllProviders: vi.fn(), + fetchProvidersForSpec: vi.fn(), + fetchAllSpecs: vi.fn(), + fetchProviderAvatar: vi.fn(), + fetchDelegatorRewards: vi.fn(), +})); +vi.mock("../services/health-store.js", () => ({ + readHealthForProvider: vi.fn(), + readHealthMapForProvider: vi.fn(), +})); + +const { gqlSafe } = await import("../graphql/client.js"); +const { fetchAllProviders, fetchAllSpecs, fetchProvidersForSpec } = await import("../rpc/lava.js"); +const { providerRoutes } = await import("../routes/providers.js"); + +async function buildApp() { + const app = Fastify({ logger: false }); + await app.register(errorHandlerPlugin); + await app.register(providerRoutes, { prefix: "/providers" }); + return app; +} + +beforeEach(() => vi.resetAllMocks()); + +const MOCK_ALL_PROVIDERS = [ + { + address: "lava@alpha", moniker: "AlphaProvider", identity: "", + totalStake: "2000", totalDelegation: "100", commission: "5", + specs: ["ETH1", "LAVA"], + }, + { + address: "lava@beta", moniker: "BetaProvider", identity: "", + totalStake: "1000", totalDelegation: "50", commission: "10", + specs: ["ETH1"], + }, +]; + +describe("GET /providers", () => { + it("returns the full list sorted by totalStake desc, enriched with 30d relay data", async () => { + (fetchAllProviders as ReturnType).mockResolvedValue(MOCK_ALL_PROVIDERS); + (gqlSafe as ReturnType).mockResolvedValue({ + mvRelayDailies: { groupedAggregates: [{ keys: ["lava@alpha"], sum: { cu: "500", relays: "100" } }] }, + }); + + const app = await buildApp(); + const res = await app.inject({ method: "GET", url: "/providers" }); + expect(res.statusCode).toBe(200); + const body = JSON.parse(res.body); + expect(body.data).toHaveLength(2); + expect(body.data[0].provider).toBe("lava@alpha"); // bigger stake first + expect(body.data[0].cuSum30d).toBe("500"); + expect(body.data[1].cuSum30d).toBeNull(); + }); +}); + +describe("GET /providers/:addr — schema validation", () => { + it("rejects an invalid address pattern with 400", async () => { + (fetchAllSpecs as ReturnType).mockResolvedValue([]); + const app = await buildApp(); + const res = await app.inject({ method: "GET", url: "/providers/notlava" }); + expect(res.statusCode).toBe(400); + }); +}); + +describe("GET /providers/:addr/stakes", () => { + it("returns stakes filtered to the requested provider", async () => { + (fetchAllSpecs as ReturnType).mockResolvedValue([ + { index: "ETH1", name: "Ethereum" }, + ]); + (fetchProvidersForSpec as ReturnType).mockResolvedValue([ + { + address: "lava@aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", + moniker: "Alpha", identity: "", + stake: { amount: "1000" }, + delegate_total: { amount: "0" }, + delegate_commission: "5", + geolocation: 1, + addons: "", extensions: "", + apiInterfaces: "", + endpoints: [], + }, + { + address: "lava@bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", + moniker: "Beta", identity: "", + stake: { amount: "500" }, + delegate_total: { amount: "0" }, + delegate_commission: "10", + geolocation: 1, + addons: "", extensions: "", + apiInterfaces: "", + endpoints: [], + }, + ]); + const app = await buildApp(); + const res = await app.inject({ method: "GET", url: "/providers/lava@aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/stakes" }); + expect(res.statusCode).toBe(200); + const body = JSON.parse(res.body); + expect(body.data).toHaveLength(1); + expect(body.data[0].stake).toBe("1000"); + }); +}); diff --git a/apps/api/src/__tests__/routes-search.test.ts b/apps/api/src/__tests__/routes-search.test.ts new file mode 100644 index 0000000..d6ebbcd --- /dev/null +++ b/apps/api/src/__tests__/routes-search.test.ts @@ -0,0 +1,58 @@ +import { describe, it, expect, vi } from "vitest"; +import Fastify from "fastify"; +import { errorHandlerPlugin } from "../plugins/error-handler.js"; + +vi.mock("../rpc/lava.js", () => ({ + fetchAllProviders: vi.fn(), + fetchAllSpecs: vi.fn(), +})); + +const { fetchAllProviders, fetchAllSpecs } = await import("../rpc/lava.js"); +const { searchRoutes } = await import("../routes/search.js"); + +async function buildApp() { + const app = Fastify({ logger: false }); + await app.register(errorHandlerPlugin); + await app.register(searchRoutes); + return app; +} + +const MOCK_PROVIDERS = [ + { address: "lava@abc", moniker: "AlphaProvider", identity: "", totalStake: "1000", totalDelegation: "0", commission: "5", specs: ["ETH1"] }, + { address: "lava@xyz", moniker: "BetaProvider", identity: "", totalStake: "500", totalDelegation: "0", commission: "10", specs: ["LAVA"] }, +]; +const MOCK_SPECS = [ + { index: "ETH1", name: "Ethereum Mainnet" }, + { index: "LAVA", name: "Lava Mainnet" }, +]; + +describe("GET /search", () => { + it("returns combined provider+spec results matching case-insensitive query", async () => { + (fetchAllProviders as ReturnType).mockResolvedValue(MOCK_PROVIDERS); + (fetchAllSpecs as ReturnType).mockResolvedValue(MOCK_SPECS); + const app = await buildApp(); + const res = await app.inject({ method: "GET", url: "/search?q=alpha" }); + expect(res.statusCode).toBe(200); + const body = JSON.parse(res.body); + expect(body.data.some((r: { moniker: string }) => r.moniker === "AlphaProvider")).toBe(true); + expect(body.data.some((r: { moniker: string }) => r.moniker === "BetaProvider")).toBe(false); + }); + + it("matches on spec name", async () => { + (fetchAllProviders as ReturnType).mockResolvedValue(MOCK_PROVIDERS); + (fetchAllSpecs as ReturnType).mockResolvedValue(MOCK_SPECS); + const app = await buildApp(); + const res = await app.inject({ method: "GET", url: "/search?q=ethereum" }); + const body = JSON.parse(res.body); + expect(body.data.some((r: { type: string; name: string }) => r.type === "spec" && r.name === "ETH1")).toBe(true); + }); + + it("returns everything when no query", async () => { + (fetchAllProviders as ReturnType).mockResolvedValue(MOCK_PROVIDERS); + (fetchAllSpecs as ReturnType).mockResolvedValue(MOCK_SPECS); + const app = await buildApp(); + const res = await app.inject({ method: "GET", url: "/search" }); + const body = JSON.parse(res.body); + expect(body.data.length).toBe(MOCK_PROVIDERS.length + MOCK_SPECS.length); + }); +}); diff --git a/apps/api/src/__tests__/routes-specs.test.ts b/apps/api/src/__tests__/routes-specs.test.ts new file mode 100644 index 0000000..150ef65 --- /dev/null +++ b/apps/api/src/__tests__/routes-specs.test.ts @@ -0,0 +1,85 @@ +import { describe, it, expect, vi, beforeEach } from "vitest"; +import Fastify from "fastify"; +import { errorHandlerPlugin } from "../plugins/error-handler.js"; + +vi.mock("../graphql/client.js", () => ({ + gqlSafe: vi.fn(), +})); + +vi.mock("../rpc/lava.js", () => ({ + fetchAllSpecs: vi.fn(), + fetchProvidersForSpec: vi.fn(), +})); + +vi.mock("../services/health-store.js", () => ({ + readHealthSummaryForSpec: vi.fn(), + readHealthByProviderForSpec: vi.fn(), +})); + +const { gqlSafe } = await import("../graphql/client.js"); +const { fetchAllSpecs, fetchProvidersForSpec } = await import("../rpc/lava.js"); +const { readHealthSummaryForSpec } = await import("../services/health-store.js"); +const { specRoutes } = await import("../routes/specs.js"); + +async function buildApp() { + const app = Fastify({ logger: false }); + await app.register(errorHandlerPlugin); + await app.register(specRoutes, { prefix: "/specs" }); + return app; +} + +beforeEach(() => { + vi.resetAllMocks(); +}); + +describe("GET /specs", () => { + it("returns specs sorted by provider count desc, enriched with 30d relay data", async () => { + (fetchAllSpecs as ReturnType).mockResolvedValue([ + { index: "ETH1", name: "Ethereum Mainnet" }, + { index: "LAVA", name: "Lava Mainnet" }, + ]); + (fetchProvidersForSpec as ReturnType) + .mockResolvedValueOnce([{}, {}, {}]) // ETH1 has 3 + .mockResolvedValueOnce([{}]); // LAVA has 1 + (gqlSafe as ReturnType).mockResolvedValue({ + mvRelayDailies: { + groupedAggregates: [ + { keys: ["ETH1"], sum: { cu: "500", relays: "100" } }, + ], + }, + }); + + const app = await buildApp(); + const res = await app.inject({ method: "GET", url: "/specs" }); + expect(res.statusCode).toBe(200); + const body = JSON.parse(res.body); + expect(body.data[0].specId).toBe("ETH1"); // more providers first + expect(body.data[0].providerCount).toBe(3); + expect(body.data[0].relays30d).toBe("100"); + expect(body.data[1].relays30d).toBeNull(); + }); + + it("gracefully returns empty list when indexer is down", async () => { + (fetchAllSpecs as ReturnType).mockResolvedValue([]); + (gqlSafe as ReturnType).mockResolvedValue(null); + const app = await buildApp(); + const res = await app.inject({ method: "GET", url: "/specs" }); + expect(res.statusCode).toBe(200); + expect(JSON.parse(res.body).data).toEqual([]); + }); +}); + +describe("GET /specs/:specId/health", () => { + it("rejects invalid specId via schema validation", async () => { + const app = await buildApp(); + const res = await app.inject({ method: "GET", url: "/specs/$$$bad/health" }); + expect(res.statusCode).toBe(400); + }); + + it("returns empty data gracefully when Redis isn't decorated", async () => { + const app = await buildApp(); + const res = await app.inject({ method: "GET", url: "/specs/ETH1/health" }); + expect(res.statusCode).toBe(200); + expect(JSON.parse(res.body)).toEqual({ data: [] }); + }); +}); diff --git a/apps/api/src/__tests__/routes-supply.test.ts b/apps/api/src/__tests__/routes-supply.test.ts index 8ca7b4e..e3657b5 100644 --- a/apps/api/src/__tests__/routes-supply.test.ts +++ b/apps/api/src/__tests__/routes-supply.test.ts @@ -1,6 +1,5 @@ import { describe, it, expect, vi } from "vitest"; import Fastify from "fastify"; -import { paginationPlugin } from "../plugins/pagination.js"; import { errorHandlerPlugin } from "../plugins/error-handler.js"; vi.mock("../rpc/lava.js", () => ({ @@ -14,7 +13,6 @@ const { supplyRoutes } = await import("../routes/supply.js"); async function buildApp() { const app = Fastify({ logger: false }); await app.register(errorHandlerPlugin); - await app.register(paginationPlugin); await app.register(supplyRoutes, { prefix: "/supply" }); return app; } diff --git a/apps/api/src/__tests__/routes-tvl.test.ts b/apps/api/src/__tests__/routes-tvl.test.ts index 20910ab..3853838 100644 --- a/apps/api/src/__tests__/routes-tvl.test.ts +++ b/apps/api/src/__tests__/routes-tvl.test.ts @@ -1,6 +1,5 @@ import { describe, it, expect, vi } from "vitest"; import Fastify from "fastify"; -import { paginationPlugin } from "../plugins/pagination.js"; import { errorHandlerPlugin } from "../plugins/error-handler.js"; vi.mock("../rpc/lava.js", () => ({ @@ -13,7 +12,6 @@ const { tvlRoutes } = await import("../routes/tvl.js"); async function buildApp() { const app = Fastify({ logger: false }); await app.register(errorHandlerPlugin); - await app.register(paginationPlugin); await app.register(tvlRoutes); return app; } diff --git a/apps/api/src/__tests__/routes-validators-and-rewards.test.ts b/apps/api/src/__tests__/routes-validators-and-rewards.test.ts index 92c0774..595466a 100644 --- a/apps/api/src/__tests__/routes-validators-and-rewards.test.ts +++ b/apps/api/src/__tests__/routes-validators-and-rewards.test.ts @@ -1,6 +1,5 @@ import { describe, it, expect, vi, beforeEach } from "vitest"; import Fastify from "fastify"; -import { paginationPlugin } from "../plugins/pagination.js"; import { errorHandlerPlugin } from "../plugins/error-handler.js"; vi.mock("../rpc/lava.js", () => ({ @@ -13,7 +12,6 @@ const { validatorsAndRewardsRoutes } = await import("../routes/validators-and-re async function buildApp() { const app = Fastify({ logger: false }); await app.register(errorHandlerPlugin); - await app.register(paginationPlugin); await app.register(validatorsAndRewardsRoutes); return app; } diff --git a/apps/api/src/__tests__/test-helpers.ts b/apps/api/src/__tests__/test-helpers.ts index 9500353..36e1dda 100644 --- a/apps/api/src/__tests__/test-helpers.ts +++ b/apps/api/src/__tests__/test-helpers.ts @@ -1,5 +1,4 @@ import Fastify, { type FastifyInstance } from "fastify"; -import { paginationPlugin } from "../plugins/pagination.js"; import { errorHandlerPlugin } from "../plugins/error-handler.js"; /** @@ -8,6 +7,5 @@ import { errorHandlerPlugin } from "../plugins/error-handler.js"; export async function buildTestApp(): Promise { const app = Fastify({ logger: false }); await app.register(errorHandlerPlugin); - await app.register(paginationPlugin); return app; } diff --git a/apps/api/src/bin/health-probe.ts b/apps/api/src/bin/health-probe.ts new file mode 100644 index 0000000..f687679 --- /dev/null +++ b/apps/api/src/bin/health-probe.ts @@ -0,0 +1,49 @@ +/** + * Standalone health-probe worker. + * + * Runs the probe loop as its own process so the gRPC probe traffic doesn't + * share the Fastify API's event loop. Deploy as a separate ECS task (or any + * container/systemd service). + * + * Env: + * REDIS_URL — required; where to write probe results + * HEALTH_PROBE_INTERVAL_MS — default 30000 + * LAVA_REST_URL — passed through to the rpc layer + * + * The in-process `healthProbePlugin` is still available for local dev via + * ENABLE_HEALTH_PROBE=true on the API container. + */ +import { Redis } from "ioredis"; +import pino from "pino"; +import { config } from "../config.js"; +import { runProbeLoop } from "../services/health-probe-loop.js"; + +const log = pino({ name: "health-probe-bin" }); + +async function main() { + if (!config.redis.url) { + log.error("REDIS_URL is required"); + process.exit(1); + } + + const redis = new Redis(config.redis.url); + + let running = true; + const shutdown = (signal: string) => { + log.info({ signal }, "Shutting down health probe"); + running = false; + }; + process.on("SIGTERM", () => shutdown("SIGTERM")); + process.on("SIGINT", () => shutdown("SIGINT")); + + try { + await runProbeLoop({ redis, isRunning: () => running }); + } finally { + await redis.quit(); + } +} + +main().catch((err) => { + log.error({ err }, "Health probe worker crashed"); + process.exit(1); +}); diff --git a/apps/api/src/bin/precompute.ts b/apps/api/src/bin/precompute.ts new file mode 100644 index 0000000..07b13a0 --- /dev/null +++ b/apps/api/src/bin/precompute.ts @@ -0,0 +1,128 @@ +/** + * Standalone precompute worker. + * + * Recomputes the heaviest read-only endpoints on a fixed interval and writes + * each result to Redis so the API can serve them without ever hitting a cold + * code path. Prevents cache stampedes when TTLs expire under load. + * + * Covered endpoints: + * - /supply/circulating (paginates ~53K accounts) + * - /apr (per-entity estimated_* RPC calls) + * - /all_providers_apr (per-provider × per-spec) + * - /validators-and-rewards (5 RPC × N validators) + * + * Deploy as a separate ECS task / container. The API still falls back to + * live compute if the precompute key is missing, so this worker can be down + * without breaking the site — users just see the slower path. + * + * Env: + * REDIS_URL — required + * PRECOMPUTE_INTERVAL_MS — default 900000 (15 min) + * LAVA_REST_URL, COINGECKO_API_URL — passed through to the rpc layer + */ +import { Redis } from "ioredis"; +import pino from "pino"; +import { ulavaToLava } from "@info/shared/utils"; +import { config } from "../config.js"; +import { gqlSafe } from "../graphql/client.js"; +import { fetchCirculatingSupply } from "../rpc/supply.js"; +import { computeAPR, computeAllProvidersApr } from "../rpc/apr.js"; +import { fetchValidatorsWithRewards } from "../rpc/validators.js"; +import { writePrecomputed } from "../services/precompute-store.js"; + +const log = pino({ name: "precompute" }); + +async function recomputeCirculatingSupply(redis: Redis): Promise { + const value = ulavaToLava(await fetchCirculatingSupply()); + await writePrecomputed(redis, "supply.circulating", value); +} + +async function recomputeApr(redis: Redis): Promise { + const apr = await computeAPR(redis); + await writePrecomputed(redis, "apr", apr); +} + +async function recomputeAllProvidersApr(redis: Redis): Promise { + const since = new Date(Date.now() - 30 * 24 * 60 * 60 * 1000).toISOString().slice(0, 10); + const relayData = await gqlSafe<{ + mvRelayDailies: { + groupedAggregates: Array<{ keys: string[]; sum: { cu: string; relays: string } }>; + }; + } | null>(`query($since: Date!) { + mvRelayDailies(filter: { date: { greaterThanOrEqualTo: $since } }) { + groupedAggregates(groupBy: PROVIDER) { keys sum { cu relays } } + } + }`, { since }, null); + + const relay30d = new Map(); + if (relayData) { + for (const agg of relayData.mvRelayDailies.groupedAggregates) { + const provider = agg.keys[0]; + if (provider) relay30d.set(provider, { cu: agg.sum.cu, relays: agg.sum.relays }); + } + } + + const data = await computeAllProvidersApr(relay30d, redis); + await writePrecomputed(redis, "all_providers_apr", data); +} + +async function recomputeValidatorsAndRewards(redis: Redis): Promise { + const data = await fetchValidatorsWithRewards(); + await writePrecomputed(redis, "validators_and_rewards", data); +} + +const TASKS: Array<{ name: string; run: (redis: Redis) => Promise }> = [ + { name: "supply.circulating", run: recomputeCirculatingSupply }, + { name: "apr", run: recomputeApr }, + { name: "all_providers_apr", run: recomputeAllProvidersApr }, + { name: "validators_and_rewards", run: recomputeValidatorsAndRewards }, +]; + +async function runOnce(redis: Redis): Promise { + for (const task of TASKS) { + const started = Date.now(); + try { + await task.run(redis); + log.info({ task: task.name, ms: Date.now() - started }, "precompute done"); + } catch (err) { + log.error({ task: task.name, err }, "precompute failed"); + } + } +} + +function sleep(ms: number): Promise { + return new Promise((r) => setTimeout(r, ms)); +} + +async function main() { + if (!config.redis.url) { + log.error("REDIS_URL is required"); + process.exit(1); + } + + const intervalMs = config.precompute.intervalMs; + const redis = new Redis(config.redis.url); + + let running = true; + const shutdown = (signal: string) => { + log.info({ signal }, "Shutting down precompute worker"); + running = false; + }; + process.on("SIGTERM", () => shutdown("SIGTERM")); + process.on("SIGINT", () => shutdown("SIGINT")); + + log.info({ intervalMs }, "Precompute worker started"); + + while (running) { + await runOnce(redis); + if (!running) break; + await sleep(intervalMs); + } + + await redis.quit(); +} + +main().catch((err) => { + log.error({ err }, "Precompute worker crashed"); + process.exit(1); +}); diff --git a/apps/api/src/config.ts b/apps/api/src/config.ts new file mode 100644 index 0000000..f9a7f39 --- /dev/null +++ b/apps/api/src/config.ts @@ -0,0 +1,99 @@ +/** + * Single source of truth for env-var defaults. + * + * Import `config` (default) for plain values or pull specific slices via + * destructure. Every default is also documented in CLAUDE.md's env-var table. + * + * Parse once at startup — changes to process.env mid-process are NOT reflected. + */ + +function env(name: string): string | undefined { + return process.env[name]; +} + +function envInt(name: string, fallback: number): number { + const raw = process.env[name]; + const n = raw !== undefined ? parseInt(raw, 10) : NaN; + return Number.isFinite(n) ? n : fallback; +} + +function envBool(name: string): boolean { + return process.env[name] === "true"; +} + +export const config = { + env: env("NODE_ENV") ?? "production", + isDev: env("NODE_ENV") === "development", + isProd: env("NODE_ENV") === "production", + + server: { + port: envInt("API_PORT", 8080), + host: env("API_HOST") ?? "0.0.0.0", + /** true = reflect any origin; array = explicit allowlist. */ + corsOrigins: env("CORS_ORIGINS") + ? env("CORS_ORIGINS")!.split(",").map((o) => o.trim()) + : (true as true), + rateLimitMax: envInt("RATE_LIMIT_MAX", 100), + }, + + lava: { + restUrl: env("LAVA_REST_URL") ?? "https://lava.rest.lava.build", + rpcUrl: env("LAVA_RPC_URL") ?? "https://lava.tendermintrpc.lava.build:443", + /** Max concurrent chain-RPC calls per batch loop. Public endpoints should stay modest (~5). */ + rpcBatchSize: Math.max(1, envInt("RPC_BATCH_SIZE", 25)), + }, + + indexer: { + graphqlUrl: env("INDEXER_GRAPHQL_URL") ?? "http://localhost:3000", + timeoutMs: envInt("INDEXER_TIMEOUT_MS", 15_000), + }, + + external: { + coingeckoApiUrl: env("COINGECKO_API_URL") ?? "https://api.coingecko.com/api/v3", + keybaseApiUrl: env("KEYBASE_API_URL") ?? "https://keybase.io/_/api/1.0", + }, + + redis: { + url: env("REDIS_URL"), + }, + + relaysDb: { + url: env("RELAYS_DB_URL"), + }, + + healthProbe: { + enabled: envBool("ENABLE_HEALTH_PROBE"), + region: env("HEALTH_PROBE_REGION") ?? "Local", + intervalMs: envInt("HEALTH_PROBE_INTERVAL_MS", 30_000), + }, + + precompute: { + /** How often the standalone precompute worker recomputes heavy endpoints. */ + intervalMs: envInt("PRECOMPUTE_INTERVAL_MS", 900_000), // 15 min + }, +} as const; + +/** + * Per-route cache TTLs (seconds). Group by freshness tier so TTL tweaks are + * one edit — rather than hunting through 30 route files for magic numbers. + */ +export const CACHE_TTL = { + /** Block-height / realtime chain probes. */ + REAL_TIME: 10, + /** Per-provider health records pulled from Redis. */ + HEALTH_PROBE: 30, + /** Default list/aggregate cache window; aligns with MV refresh cadence. */ + LIST: 300, + /** Search results — slightly longer than lists since queries repeat. */ + SEARCH: 600, + /** APR computation (expensive, derived from per-entity RPC fan-out). */ + APR: 1800, + /** Slow-moving validator / claimable rewards data. */ + SLOW_MOVING: 7200, + /** Optimizer metrics — aggregated hourly scores. */ + OPTIMIZER: 21_600, + /** Historical/immutable data (past-block snapshots, avatars). */ + HISTORICAL: 86_400, +} as const; + +export default config; diff --git a/apps/api/src/graphql/client.ts b/apps/api/src/graphql/client.ts index a396dce..e94fd84 100644 --- a/apps/api/src/graphql/client.ts +++ b/apps/api/src/graphql/client.ts @@ -1,19 +1,32 @@ import pino from "pino"; +import { Agent, setGlobalDispatcher } from "undici"; +import { config } from "../config.js"; const logger = pino({ name: "graphql" }); -const INDEXER_GRAPHQL_URL = process.env.INDEXER_GRAPHQL_URL ?? "http://localhost:3000"; + +// Shared connection pool — reuses TCP/TLS across outbound requests so each +// call saves the ~50-100ms handshake cost. Applies to all fetch() in the +// process via undici's global dispatcher. +setGlobalDispatcher( + new Agent({ + connections: 32, + keepAliveTimeout: 30_000, + keepAliveMaxTimeout: 60_000, + }), +); export async function gql(query: string, variables?: Record): Promise { - const res = await fetch(INDEXER_GRAPHQL_URL, { + const res = await fetch(config.indexer.graphqlUrl, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ query, variables }), + signal: AbortSignal.timeout(config.indexer.timeoutMs), }); if (!res.ok) throw new Error(`GraphQL request failed: ${res.status}`); const json = (await res.json()) as { data?: T; errors?: Array<{ message: string }> }; - if (json.errors?.length) throw new Error(json.errors[0].message); + if (json.errors?.length) throw new Error(json.errors[0]!.message); return json.data as T; } diff --git a/apps/api/src/main.ts b/apps/api/src/main.ts index e404092..ddae55f 100644 --- a/apps/api/src/main.ts +++ b/apps/api/src/main.ts @@ -2,9 +2,10 @@ import Fastify from "fastify"; import cors from "@fastify/cors"; import helmet from "@fastify/helmet"; import rateLimit from "@fastify/rate-limit"; +import compress from "@fastify/compress"; +import { config } from "./config.js"; import { redisPlugin } from "./plugins/redis.js"; import { cachePlugin } from "./plugins/cache.js"; -import { paginationPlugin } from "./plugins/pagination.js"; import { errorHandlerPlugin } from "./plugins/error-handler.js"; import { swaggerPlugin } from "./plugins/swagger.js"; import { healthProbePlugin } from "./plugins/health-probe.js"; @@ -26,26 +27,21 @@ import { providerClaimableRewardsRoutes } from "./routes/provider-claimable-rewa import { validatorsAndRewardsRoutes } from "./routes/validators-and-rewards.js"; import { burnRateRoutes } from "./routes/burn-rate.js"; -const PORT = parseInt(process.env.API_PORT ?? "8080", 10); -const HOST = process.env.API_HOST ?? "0.0.0.0"; -const CORS_ORIGINS = process.env.CORS_ORIGINS - ? process.env.CORS_ORIGINS.split(",").map((o) => o.trim()) - : true; // default: allow all in dev; set CORS_ORIGINS in production - async function main() { const app = Fastify({ logger: { - transport: - process.env.NODE_ENV === "development" - ? { target: "pino-pretty" } - : undefined, + transport: config.isDev ? { target: "pino-pretty" } : undefined, }, }); - await app.register(cors, { origin: CORS_ORIGINS, methods: ["GET", "HEAD", "OPTIONS", "DELETE"] }); + await app.register(cors, { + origin: config.server.corsOrigins, + methods: ["GET", "HEAD", "OPTIONS", "DELETE"], + }); await app.register(helmet, { contentSecurityPolicy: false }); // CSP handled by Next.js frontend + await app.register(compress, { global: true, threshold: 1024, encodings: ["br", "gzip"] }); await app.register(rateLimit, { - max: parseInt(process.env.RATE_LIMIT_MAX ?? "100", 10), + max: config.server.rateLimitMax, timeWindow: "1 minute", allowList: ["127.0.0.1", "::1"], }); @@ -54,7 +50,6 @@ async function main() { await app.register(redisPlugin); await app.register(relaysDbPlugin); await app.register(cachePlugin); - await app.register(paginationPlugin); await app.register(healthRoutes); await app.register(indexRoutes, { prefix: "/index" }); @@ -75,8 +70,8 @@ async function main() { await app.register(healthProbePlugin); - await app.listen({ port: PORT, host: HOST }); - app.log.info(`API server listening on ${HOST}:${PORT}`); + await app.listen({ port: config.server.port, host: config.server.host }); + app.log.info(`API server listening on ${config.server.host}:${config.server.port}`); for (const signal of ["SIGTERM", "SIGINT"] as const) { process.on(signal, () => { diff --git a/apps/api/src/plugins/cache.ts b/apps/api/src/plugins/cache.ts index f4a4929..3715e68 100644 --- a/apps/api/src/plugins/cache.ts +++ b/apps/api/src/plugins/cache.ts @@ -16,7 +16,7 @@ function resolveTtl(request: FastifyRequest): number | undefined { function buildCacheKey(request: FastifyRequest): string { // Normalize: collapse double slashes, strip trailing slash, sort query params - const [path, qs] = request.url.split("?", 2); + const [path = "", qs] = request.url.split("?", 2); const normalizedPath = path.replace(/\/+/g, "/").replace(/\/$/, "") || "/"; const normalizedQs = qs ? qs.split("&").sort().join("&") diff --git a/apps/api/src/plugins/error-handler.ts b/apps/api/src/plugins/error-handler.ts index 9db3043..e19012f 100644 --- a/apps/api/src/plugins/error-handler.ts +++ b/apps/api/src/plugins/error-handler.ts @@ -1,5 +1,17 @@ import fp from "fastify-plugin"; import type { FastifyInstance, FastifyError, FastifyRequest, FastifyReply } from "fastify"; +import type { ApiError } from "@info/shared/types"; + +/** Emit the standard ApiError shape from a route handler for 4xx/5xx responses. */ +export function sendApiError(reply: FastifyReply, statusCode: number, message: string): ApiError { + reply.status(statusCode); + const error = statusCode === 400 ? "Bad Request" + : statusCode === 404 ? "Not Found" + : statusCode === 503 ? "Service Unavailable" + : "Error"; + const body: ApiError = { error, message, statusCode }; + return body; +} export const errorHandlerPlugin = fp(async (app: FastifyInstance) => { app.setErrorHandler((error: FastifyError, request: FastifyRequest, reply: FastifyReply) => { diff --git a/apps/api/src/plugins/health-probe.ts b/apps/api/src/plugins/health-probe.ts index 3c83b60..f883f7d 100644 --- a/apps/api/src/plugins/health-probe.ts +++ b/apps/api/src/plugins/health-probe.ts @@ -1,94 +1,23 @@ import fp from "fastify-plugin"; import type { FastifyInstance } from "fastify"; -import { normalizeBlock } from "@info/shared/constants"; -import { fetchAllSpecs, fetchProvidersForSpec } from "../rpc/lava.js"; -import type { ProviderEndpoint } from "../rpc/lava.js"; -import { probeProvider } from "../services/grpc-probe.js"; -import { writeHealthStatus } from "../services/health-store.js"; - -interface ProbeTarget { - address: string; - specId: string; - endpoint: ProviderEndpoint; - apiInterface: string; -} - -async function buildProbeTargets(): Promise { - const specs = await fetchAllSpecs(); - const targets: ProbeTarget[] = []; - - // Fetch in batches of 5 to avoid rate limiting - for (let i = 0; i < specs.length; i += 5) { - const batch = specs.slice(i, i + 5); - const results = await Promise.all( - batch.map((s) => - fetchProvidersForSpec(s.index) - .then((providers) => ({ specId: s.index, providers })) - .catch(() => ({ specId: s.index, providers: [] as Awaited> })), - ), - ); - - for (const { specId, providers } of results) { - for (const provider of providers) { - for (const ep of provider.endpoints) { - // Skip endpoints targeting private/internal networks - if (!isPublicEndpoint(ep.iPPORT)) continue; - - for (const apiInterface of ep.apiInterfaces) { - targets.push({ - address: provider.address, - specId, - endpoint: ep, - apiInterface, - }); - } - } - } - } - } - - return targets; -} - -/** Reject endpoints targeting private/loopback/internal networks (SSRF mitigation) */ -function isPublicEndpoint(iPPORT: string): boolean { - const host = iPPORT.split(":")[0]; - if (!host) return false; - if (host === "localhost" || host === "0.0.0.0") return false; - if (host.startsWith("127.")) return false; - if (host.startsWith("10.")) return false; - if (host.startsWith("192.168.")) return false; - if (host.startsWith("172.")) { - const second = parseInt(host.split(".")[1], 10); - if (second >= 16 && second <= 31) return false; - } - if (host.startsWith("169.254.")) return false; - return true; -} - -function shuffle(arr: T[]): T[] { - const a = [...arr]; - for (let i = a.length - 1; i > 0; i--) { - const j = Math.floor(Math.random() * (i + 1)); - [a[i], a[j]] = [a[j], a[i]]; - } - return a; -} - -function sleep(ms: number): Promise { - return new Promise((r) => setTimeout(r, ms)); -} - +import { config } from "../config.js"; +import { runProbeLoop } from "../services/health-probe-loop.js"; + +/** + * Optional in-process health-probe runner for local dev / single-node setups. + * + * Production deployments should run the probe as a separate process via + * `apps/api/src/bin/health-probe.ts` (own ECS task / container) so gRPC probe + * traffic doesn't share the API's event loop. This plugin stays opt-in for + * convenience during local dev: set `ENABLE_HEALTH_PROBE=true` to enable. + */ export const healthProbePlugin = fp(async (app: FastifyInstance) => { - if (process.env.ENABLE_HEALTH_PROBE !== "true") return; + if (!config.healthProbe.enabled) return; if (!app.redis) { app.log.warn("Health probe requires Redis — REDIS_URL not set, skipping"); return; } - const intervalMs = parseInt(process.env.HEALTH_PROBE_INTERVAL_MS ?? "30000", 10); - const region = process.env.HEALTH_PROBE_REGION ?? "Local"; - let running = true; app.addHook("onClose", () => { @@ -97,60 +26,6 @@ export const healthProbePlugin = fp(async (app: FastifyInstance) => { app.addHook("onReady", () => { const redis = app.redis!; - - void (async () => { - app.log.info("Health probe started"); - - while (running) { - try { - const targets = await buildProbeTargets(); - if (targets.length === 0) { - app.log.warn("No probe targets found, waiting"); - await sleep(60_000); - continue; - } - - const shuffled = shuffle(targets); - app.log.info({ count: shuffled.length }, "Starting health probe cycle"); - - // Probe in batches of 5 to balance throughput vs resource usage - for (let i = 0; i < shuffled.length; i += 5) { - if (!running) break; - const batch = shuffled.slice(i, i + 5); - - await Promise.allSettled(batch.map(async (target) => { - const geolocation = target.endpoint.geolocation.toString(); - try { - const result = await probeProvider( - target.endpoint.iPPORT, - target.specId, - target.apiInterface, - ); - const block = normalizeBlock(target.specId, result.latestBlock); - await writeHealthStatus( - redis, target.address, target.specId, target.apiInterface, geolocation, - "healthy", - { block, latency: result.latencyMs, lavaEpoch: result.lavaEpoch }, - ); - } catch (err) { - const message = err instanceof Error ? err.message : String(err); - await writeHealthStatus( - redis, target.address, target.specId, target.apiInterface, geolocation, - "unhealthy", - { message }, - ); - } - })); - } - - if (running) await sleep(intervalMs); - } catch (err) { - app.log.error({ err }, "Health probe loop error"); - await sleep(60_000); - } - } - - app.log.info("Health probe stopped"); - })(); + void runProbeLoop({ redis, isRunning: () => running }); }); }); diff --git a/apps/api/src/plugins/pagination.ts b/apps/api/src/plugins/pagination.ts deleted file mode 100644 index 2aee287..0000000 --- a/apps/api/src/plugins/pagination.ts +++ /dev/null @@ -1,56 +0,0 @@ -import fp from "fastify-plugin"; -import type { FastifyInstance, FastifyRequest } from "fastify"; - -/** Default max rows per page. Override per-route via config.maxLimit. */ -const DEFAULT_MAX_LIMIT = 100; - -export interface PaginationQuery { - page?: string; - limit?: string; - sort?: string; - order?: string; -} - -export interface ParsedPagination { - page: number; - limit: number; - offset: number; - sort: string | null; - order: "asc" | "desc"; -} - -/** - * Parse pagination from query params. - * maxLimit caps the upper bound (default 100, configurable per-route via config.maxLimit). - */ -export function parsePagination(query: PaginationQuery, maxLimit = DEFAULT_MAX_LIMIT): ParsedPagination { - const page = Math.max(1, Math.min(4000, parseInt(query.page ?? "1", 10) || 1)); - const limit = Math.max(1, Math.min(maxLimit, parseInt(query.limit ?? "20", 10) || 20)); - const offset = (page - 1) * limit; - const sort = query.sort ?? null; - const order = query.order === "desc" ? "desc" : "asc"; - return { page, limit, offset, sort, order }; -} - -declare module "fastify" { - interface FastifyRequest { - pagination: ParsedPagination; - } - interface FastifyContextConfig { - maxLimit?: number; - } -} - -/** - * Pagination plugin. Parses ?page, ?limit, ?sort, ?order from query string. - * Per-route upper bound via config.maxLimit (default 100). - */ -export const paginationPlugin = fp(async (app: FastifyInstance) => { - app.decorateRequest("pagination", { - getter() { - const req = this as FastifyRequest; - const maxLimit = req.routeOptions.config?.maxLimit ?? DEFAULT_MAX_LIMIT; - return parsePagination(req.query as PaginationQuery, maxLimit); - }, - }); -}); diff --git a/apps/api/src/plugins/redis.ts b/apps/api/src/plugins/redis.ts index fae196a..32aca09 100644 --- a/apps/api/src/plugins/redis.ts +++ b/apps/api/src/plugins/redis.ts @@ -1,6 +1,7 @@ import fp from "fastify-plugin"; import type { FastifyInstance } from "fastify"; import { Redis } from "ioredis"; +import { config } from "../config.js"; declare module "fastify" { interface FastifyInstance { @@ -9,7 +10,7 @@ declare module "fastify" { } export const redisPlugin = fp(async (app: FastifyInstance) => { - const url = process.env.REDIS_URL; + const url = config.redis.url; if (!url) { app.decorate("redis", null); return; diff --git a/apps/api/src/plugins/relays-db.ts b/apps/api/src/plugins/relays-db.ts index 6aacd99..fe45bf1 100644 --- a/apps/api/src/plugins/relays-db.ts +++ b/apps/api/src/plugins/relays-db.ts @@ -1,6 +1,7 @@ import fp from "fastify-plugin"; import type { FastifyInstance } from "fastify"; import postgres from "postgres"; +import { config } from "../config.js"; declare module "fastify" { interface FastifyInstance { @@ -9,7 +10,7 @@ declare module "fastify" { } export const relaysDbPlugin = fp(async (app: FastifyInstance) => { - const url = process.env.RELAYS_DB_URL; + const url = config.relaysDb.url; if (!url) { app.decorate("relaysDb", null); app.log.warn("RELAYS_DB_URL not set — optimizer metrics disabled"); diff --git a/apps/api/src/routes/all-providers-apr.ts b/apps/api/src/routes/all-providers-apr.ts index ee2a6f7..6034070 100644 --- a/apps/api/src/routes/all-providers-apr.ts +++ b/apps/api/src/routes/all-providers-apr.ts @@ -1,14 +1,20 @@ import type { FastifyInstance } from "fastify"; -import { computeAllProvidersApr } from "../rpc/lava.js"; +import { CACHE_TTL } from "../config.js"; +import { computeAllProvidersApr, type AllProviderAprEntry } from "../rpc/lava.js"; import { gqlSafe } from "../graphql/client.js"; +import { readPrecomputed } from "../services/precompute-store.js"; export async function allProvidersAprRoutes(app: FastifyInstance) { - // GET /all_providers_apr — per-provider APR, commission, 30d relays, reward breakdown - // Matches jsinfo /all_providers_apr response format. Cached 30 min (expensive). + // GET /all_providers_apr — per-provider APR, commission, 30d relays, reward breakdown. + // Prefers the precomputed value from bin/precompute.ts; falls back to live + // compute when the precompute worker hasn't populated the key yet. app.get("/", { schema: { tags: ["APR"], summary: "Per-provider APR with commission, 30d relays, and reward breakdown" }, - config: { cacheTTL: 1800 }, + config: { cacheTTL: CACHE_TTL.APR }, }, async (request) => { + const cached = await readPrecomputed(request.server.redis, "all_providers_apr"); + if (cached) return cached.value; + const since = new Date(Date.now() - 30 * 24 * 60 * 60 * 1000) .toISOString() .slice(0, 10); @@ -33,7 +39,8 @@ export async function allProvidersAprRoutes(app: FastifyInstance) { const relay30d = new Map(); if (relayData) { for (const agg of relayData.mvRelayDailies.groupedAggregates) { - relay30d.set(agg.keys[0], { cu: agg.sum.cu, relays: agg.sum.relays }); + const provider = agg.keys[0]; + if (provider) relay30d.set(provider, { cu: agg.sum.cu, relays: agg.sum.relays }); } } diff --git a/apps/api/src/routes/apr.ts b/apps/api/src/routes/apr.ts index f7a9c14..0cb2cc4 100644 --- a/apps/api/src/routes/apr.ts +++ b/apps/api/src/routes/apr.ts @@ -1,12 +1,23 @@ import type { FastifyInstance } from "fastify"; +import { CACHE_TTL } from "../config.js"; import { computeAPR } from "../rpc/lava.js"; +import { readPrecomputed } from "../services/precompute-store.js"; + +interface AprResult { + restaking_apr_percentile: number; + staking_apr_percentile: number; +} export async function aprRoutes(app: FastifyInstance) { - // GET /apr — per-entity estimated rewards with USD conversion, cached 30 min + // GET /apr — per-entity estimated rewards with USD conversion, cached 30 min. + // Prefers the precomputed value from bin/precompute.ts; falls back to live + // compute when the precompute worker hasn't populated the key yet. app.get("/apr", { schema: { tags: ["APR"], summary: "Restaking and staking APR percentiles (80th, capped at 30%)" }, - config: { cacheTTL: 1800 }, + config: { cacheTTL: CACHE_TTL.APR }, }, async (request) => { + const cached = await readPrecomputed(request.server.redis, "apr"); + if (cached) return cached.value; return await computeAPR(request.server.redis); }); } diff --git a/apps/api/src/routes/burn-rate.ts b/apps/api/src/routes/burn-rate.ts index 739eb04..36700ea 100644 --- a/apps/api/src/routes/burn-rate.ts +++ b/apps/api/src/routes/burn-rate.ts @@ -1,4 +1,5 @@ import type { FastifyInstance } from "fastify"; +import { CACHE_TTL } from "../config.js"; import { fetchBlockAtTimestamp, fetchLatestBlockHeight, @@ -57,7 +58,7 @@ export async function burnRateRoutes(app: FastifyInstance) { }, }, }, - config: { cacheTTL: 86_400 }, + config: { cacheTTL: CACHE_TTL.HISTORICAL }, }, async (request) => { const q = request.query as { months?: number }; const count = q.months ?? DEFAULT_MONTHS; @@ -98,10 +99,10 @@ export async function burnRateRoutes(app: FastifyInstance) { const valid = resolved.filter((b): b is NonNullable => b !== null); for (let i = 0; i < valid.length; i++) { - const b = valid[i]; + const b = valid[i]!; const prev = i === 0 ? BigInt(latestSupply.toString()) - : BigInt(valid[i - 1].supply); + : BigInt(valid[i - 1]!.supply); const supplyDiff = prev - BigInt(b.supply); blocks.push({ ...b, supply_diff: supplyDiff.toString() }); } diff --git a/apps/api/src/routes/health.ts b/apps/api/src/routes/health.ts index 6ec14c6..465acc7 100644 --- a/apps/api/src/routes/health.ts +++ b/apps/api/src/routes/health.ts @@ -1,5 +1,7 @@ import type { FastifyInstance } from "fastify"; +import { CACHE_TTL, config } from "../config.js"; import { fetchLatestBlockHeight } from "../rpc/lava.js"; +import { sendApiError } from "../plugins/error-handler.js"; export async function healthRoutes(app: FastifyInstance) { app.get("/health", { @@ -12,13 +14,10 @@ export async function healthRoutes(app: FastifyInstance) { return { health: "ok" }; }); - if (process.env.NODE_ENV !== "production") { + if (!config.isProd) { app.delete("/cache", async (_request, reply) => { const client = app.redis; - if (!client) { - reply.status(404); - return { error: "Redis not connected" }; - } + if (!client) return sendApiError(reply, 404, "Redis not connected"); await client.flushdb(); return { cleared: true }; @@ -30,7 +29,7 @@ export async function healthRoutes(app: FastifyInstance) { tags: ["Health"], summary: "Detailed health status with block staleness check", }, - config: { cacheTTL: 10 }, + config: { cacheTTL: CACHE_TTL.REAL_TIME }, }, async (_request, reply) => { try { const block = await fetchLatestBlockHeight(); diff --git a/apps/api/src/routes/index.ts b/apps/api/src/routes/index.ts index 1d80e05..80f6b86 100644 --- a/apps/api/src/routes/index.ts +++ b/apps/api/src/routes/index.ts @@ -1,4 +1,6 @@ import type { FastifyInstance } from "fastify"; +import { CACHE_TTL } from "../config.js"; +import { weightedQos } from "@info/shared/utils"; import { gqlSafe } from "../graphql/client.js"; import { fetchLatestBlockHeight, fetchAllProviders } from "../rpc/lava.js"; @@ -8,7 +10,7 @@ export async function indexRoutes(app: FastifyInstance) { tags: ["Index"], summary: "Dashboard stats — alltime totals, 30d totals, stake, provider count, latest block", }, - config: { cacheTTL: 300 }, + config: { cacheTTL: CACHE_TTL.LIST }, }, async () => { const thirtyDaysAgo = new Date(Date.now() - 30 * 24 * 60 * 60 * 1000) .toISOString() @@ -57,7 +59,7 @@ export async function indexRoutes(app: FastifyInstance) { tags: ["Index"], summary: "Top 20 chains by alltime CU", }, - config: { cacheTTL: 300 }, + config: { cacheTTL: CACHE_TTL.LIST }, }, async () => { const data = await gqlSafe<{ mvRelayDailies: { @@ -93,7 +95,7 @@ export async function indexRoutes(app: FastifyInstance) { }, }, }, - config: { cacheTTL: 300 }, + config: { cacheTTL: CACHE_TTL.LIST }, }, async (request) => { const q = request.query as Record; const to = q.to ? q.to : new Date().toISOString().slice(0, 10); @@ -155,14 +157,12 @@ export async function indexRoutes(app: FastifyInstance) { }> = []; for (const [key, v] of byDayChain) { - const [date, chainId] = key.split("|"); + const [date = "", chainId = ""] = key.split("|"); result.push({ date, chainId, cu: v.cu.toString(), relays: v.relays.toString(), - qosSync: v.qosWeight > 0 ? v.qosSyncW / v.qosWeight : null, - qosAvailability: v.qosWeight > 0 ? v.qosAvailW / v.qosWeight : null, - qosLatency: v.qosWeight > 0 ? v.qosLatW / v.qosWeight : null, + ...weightedQos(v.qosSyncW, v.qosAvailW, v.qosLatW, v.qosWeight), }); } diff --git a/apps/api/src/routes/lava.ts b/apps/api/src/routes/lava.ts index a00f7ba..6683729 100644 --- a/apps/api/src/routes/lava.ts +++ b/apps/api/src/routes/lava.ts @@ -1,11 +1,12 @@ import type { FastifyInstance } from "fastify"; +import { CACHE_TTL } from "../config.js"; import { fetchAllSpecs } from "../rpc/lava.js"; export async function lavaRoutes(app: FastifyInstance) { // GET /lava/specs — all chain specs (consumed by frontend useChainNames hook) app.get("/specs", { schema: { tags: ["Lava"], summary: "All chain specs (raw, for frontend chain name lookup)" }, - config: { cacheTTL: 300 }, + config: { cacheTTL: CACHE_TTL.LIST }, }, async () => { const specs = await fetchAllSpecs(); return { data: specs }; diff --git a/apps/api/src/routes/optimizer-metrics.ts b/apps/api/src/routes/optimizer-metrics.ts index 805078c..1f70a37 100644 --- a/apps/api/src/routes/optimizer-metrics.ts +++ b/apps/api/src/routes/optimizer-metrics.ts @@ -1,4 +1,7 @@ import type { FastifyInstance } from "fastify"; +import { CACHE_TTL } from "../config.js"; +import { parseYMD } from "@info/shared/utils"; +import { sendApiError } from "../plugins/error-handler.js"; interface MetricRow { hourly_timestamp: Date; @@ -95,7 +98,8 @@ function aggregate(rows: NormalizedMetric[], keyFn: (r: NormalizedMetric) => str const result: NormalizedMetric[] = []; for (const [, group] of groups) { - const base = { ...group[0] }; + if (group.length === 0) continue; + const base: NormalizedMetric = { ...group[0]! }; for (const k of METRIC_KEYS) { const values = group.map((r) => r[k]).filter((v): v is number => v != null); (base as Record)[k] = values.length > 0 ? avg(values) : null; @@ -151,18 +155,18 @@ export async function optimizerMetricsRoutes(app: FastifyInstance) { }, }, }, - config: { cacheTTL: 21600 }, + config: { cacheTTL: CACHE_TTL.OPTIMIZER }, }, async (request, reply) => { - if (!app.relaysDb) { - reply.status(503); - return { error: "Optimizer metrics not configured" }; - } + if (!app.relaysDb) return sendApiError(reply, 503, "Optimizer metrics not configured"); try { const { addr } = request.params as { addr: string }; const query = request.query as { from?: string; to?: string; consumer?: string; chain_id?: string }; - const from = clampFrom(query.from ? new Date(query.from) : defaultFrom()); - const to = query.to ? new Date(query.to) : new Date(); + const fromRaw = query.from ? parseYMD(query.from) : defaultFrom(); + if (!fromRaw) return sendApiError(reply, 400, "invalid from (expected YYYY-MM-DD)"); + const to = query.to ? parseYMD(query.to) : new Date(); + if (!to) return sendApiError(reply, 400, "invalid to (expected YYYY-MM-DD)"); + const from = clampFrom(fromRaw); const conditions = [ app.relaysDb`provider = ${addr}`, @@ -193,8 +197,7 @@ export async function optimizerMetricsRoutes(app: FastifyInstance) { return { metrics, possibleConsumers, possibleChainIds, filters: { provider: addr, from, to } }; } catch (err) { request.log.error({ err }, "Failed to query optimizer metrics for provider"); - reply.status(503); - return { error: "Optimizer metrics temporarily unavailable" }; + return sendApiError(reply, 503, "Optimizer metrics temporarily unavailable"); } }); @@ -217,18 +220,18 @@ export async function optimizerMetricsRoutes(app: FastifyInstance) { }, }, }, - config: { cacheTTL: 21600 }, + config: { cacheTTL: CACHE_TTL.OPTIMIZER }, }, async (request, reply) => { - if (!app.relaysDb) { - reply.status(503); - return { error: "Optimizer metrics not configured" }; - } + if (!app.relaysDb) return sendApiError(reply, 503, "Optimizer metrics not configured"); try { const { specId } = request.params as { specId: string }; const query = request.query as { from?: string; to?: string; consumer?: string }; - const from = clampFrom(query.from ? new Date(query.from) : defaultFrom()); - const to = query.to ? new Date(query.to) : new Date(); + const fromRaw = query.from ? parseYMD(query.from) : defaultFrom(); + if (!fromRaw) return sendApiError(reply, 400, "invalid from (expected YYYY-MM-DD)"); + const to = query.to ? parseYMD(query.to) : new Date(); + if (!to) return sendApiError(reply, 400, "invalid to (expected YYYY-MM-DD)"); + const from = clampFrom(fromRaw); const conditions = [ app.relaysDb`chain = ${specId}`, @@ -256,8 +259,7 @@ export async function optimizerMetricsRoutes(app: FastifyInstance) { return { metrics, possibleConsumers, providers, filters: { specId, from, to } }; } catch (err) { request.log.error({ err }, "Failed to query optimizer metrics for spec"); - reply.status(503); - return { error: "Optimizer metrics temporarily unavailable" }; + return sendApiError(reply, 503, "Optimizer metrics temporarily unavailable"); } }); } diff --git a/apps/api/src/routes/provider-claimable-rewards.ts b/apps/api/src/routes/provider-claimable-rewards.ts index f9fff2c..d3edb9f 100644 --- a/apps/api/src/routes/provider-claimable-rewards.ts +++ b/apps/api/src/routes/provider-claimable-rewards.ts @@ -1,4 +1,5 @@ import type { FastifyInstance } from "fastify"; +import { CACHE_TTL } from "../config.js"; import { RPC_BATCH_SIZE, fetchAllProviders, @@ -17,7 +18,7 @@ export async function providerClaimableRewardsRoutes(app: FastifyInstance) { tags: ["Provider Rewards"], summary: "Per-provider self-delegation claimable rewards (multi-denom + USD)", }, - config: { cacheTTL: 7200 }, + config: { cacheTTL: CACHE_TTL.SLOW_MOVING }, }, async () => { await prewarmPriceCache(); const providers = await fetchAllProviders(); @@ -30,8 +31,8 @@ export async function providerClaimableRewardsRoutes(app: FastifyInstance) { batch.map((p) => fetchDelegatorRewards(p.address)), ); for (let j = 0; j < batch.length; j++) { - const addr = batch[j].address; - const entries = await processClaimableRewards(results[j], addr); + const addr = batch[j]!.address; + const entries = await processClaimableRewards(results[j]!, addr); if (entries.length > 0) { out[addr] = { rewards: entries, timestamp: ts }; } diff --git a/apps/api/src/routes/provider-estimated-rewards.ts b/apps/api/src/routes/provider-estimated-rewards.ts index 206d7c4..89cb7f4 100644 --- a/apps/api/src/routes/provider-estimated-rewards.ts +++ b/apps/api/src/routes/provider-estimated-rewards.ts @@ -1,4 +1,5 @@ import type { FastifyInstance } from "fastify"; +import { CACHE_TTL } from "../config.js"; import { RPC_BATCH_SIZE, fetchBlockAtTimestamp, @@ -8,6 +9,7 @@ import { prewarmPriceCache, type RewardsBySpecEntry, } from "../rpc/lava.js"; +import { sendApiError } from "../plugins/error-handler.js"; // ── Helpers ────────────────────────────────────────────────────────────────── @@ -68,7 +70,7 @@ export async function providerEstimatedRewardsRoutes(app: FastifyInstance) { }, }, }, - config: { cacheTTL: 86_400 }, + config: { cacheTTL: CACHE_TTL.HISTORICAL }, }, async (request) => { const q = request.query as { count?: number }; const count = q.count ?? DEFAULT_MONTHLY_BLOCKS; @@ -112,7 +114,7 @@ export async function providerEstimatedRewardsRoutes(app: FastifyInstance) { }, }, }, - config: { cacheTTL: 1800 }, + config: { cacheTTL: CACHE_TTL.APR }, }, async (request, reply) => { const q = request.query as { block?: number; spec?: string }; const block = q.block && q.block > 0 ? q.block : undefined; @@ -120,13 +122,13 @@ export async function providerEstimatedRewardsRoutes(app: FastifyInstance) { let spec: string | undefined; if (q.spec !== undefined) { if (!validateSpecId(q.spec)) { - return reply.status(400).send({ error: `Error - bad spec format: ${q.spec}` }); + return sendApiError(reply, 400, `bad spec format: ${q.spec}`); } spec = q.spec.toUpperCase(); } // Historical blocks are immutable, so cache aggressively. - if (block) request.cacheTTL = 86_400; + if (block) request.cacheTTL = CACHE_TTL.HISTORICAL; await prewarmPriceCache(); // Tokens at block N are priced with the CURRENT LAVA price (matches lava-ops @@ -152,9 +154,9 @@ export async function providerEstimatedRewardsRoutes(app: FastifyInstance) { ); for (let j = 0; j < batch.length; j++) { - const addr = batch[j]; + const addr = batch[j]!; const provider = providerMap.get(addr)!; - let rewards = rewardResults[j]; + let rewards = rewardResults[j]!; if (spec) rewards = rewards.filter((r) => r.spec === spec); if (rewards.length === 0) continue; diff --git a/apps/api/src/routes/provider-rewards.ts b/apps/api/src/routes/provider-rewards.ts index 3ec54be..f442f17 100644 --- a/apps/api/src/routes/provider-rewards.ts +++ b/apps/api/src/routes/provider-rewards.ts @@ -1,37 +1,20 @@ import type { FastifyInstance } from "fastify"; +import { CACHE_TTL } from "../config.js"; +import { parseYMD } from "@info/shared/utils"; import { gqlSafe } from "../graphql/client.js"; -import { fetchProvidersForSpec, fetchAllProviders } from "../rpc/lava.js"; +import { fetchProvidersForSpec, fetchAllProviderMonikers } from "../rpc/lava.js"; +import { sendApiError } from "../plugins/error-handler.js"; // ── Helpers ────────────────────────────────────────────────────────────────── const SPEC_ID_RE = /^[a-zA-Z][a-zA-Z0-9_-]*$/; -const DATE_RE = /^(\d{4})-(\d{2})-(\d{2})$/; const MAX_RANGE_MS = 6 * 30 * 24 * 60 * 60 * 1000; // ~6 months const ONE_DAY_MS = 24 * 60 * 60 * 1000; -const PAST_WINDOW_TTL = 86_400; // 1 day — immutable historical window -const LIVE_WINDOW_TTL = 300; // 5 min — aligned with MV refresh cadence function validateSpecId(s: string): boolean { return s.length > 2 && s.length <= 50 && SPEC_ID_RE.test(s); } -// Parses YYYY-MM-DD strictly. Rejects calendar-invalid dates like 2025-02-30 -// (which `new Date()` would silently roll over to 2025-03-02). -function parseStrictDate(s: string): Date | null { - const m = DATE_RE.exec(s); - if (!m) return null; - const y = Number(m[1]); - const mo = Number(m[2]); - const d = Number(m[3]); - if (mo < 1 || mo > 12 || d < 1 || d > 31) return null; - const ts = Date.UTC(y, mo - 1, d); - const rt = new Date(ts); - if (rt.getUTCFullYear() !== y || rt.getUTCMonth() !== mo - 1 || rt.getUTCDate() !== d) { - return null; - } - return rt; -} - function computeAdjustedRewards( avgLat: number, avgAvail: number, avgSync: number, qosCus: number, ): number { @@ -134,7 +117,7 @@ export async function providerRewardsRoutes(app: FastifyInstance) { required: ["from", "to"] as const, }, }, - config: { cacheTTL: 300 }, + config: { cacheTTL: CACHE_TTL.LIST }, }, async (request, reply) => { const q = request.query as { specs?: string[]; @@ -148,17 +131,15 @@ export async function providerRewardsRoutes(app: FastifyInstance) { // ── Validate spec IDs ────────────────────────────────────────── if (q.specs) { for (const s of q.specs) { - if (!validateSpecId(s)) { - return reply.status(400).send({ error: `Error - bad spec format: ${s}` }); - } + if (!validateSpecId(s)) return sendApiError(reply, 400, `bad spec format: ${s}`); } } // ── Validate & normalize dates ───────────────────────────────── - let dateFrom = parseStrictDate(q.from); - if (!dateFrom) return reply.status(400).send({ error: "Error - bad from date format" }); - let dateTo = parseStrictDate(q.to); - if (!dateTo) return reply.status(400).send({ error: "Error - bad to date format" }); + let dateFrom = parseYMD(q.from); + if (!dateFrom) return sendApiError(reply, 400, "bad from date format (expected YYYY-MM-DD)"); + let dateTo = parseYMD(q.to); + if (!dateTo) return sendApiError(reply, 400, "bad to date format (expected YYYY-MM-DD)"); if (dateTo < dateFrom) { [dateFrom, dateTo] = [dateTo, dateFrom]; @@ -168,9 +149,7 @@ export async function providerRewardsRoutes(app: FastifyInstance) { const to = dateTo.toISOString().slice(0, 10); if (dateTo.getTime() - dateFrom.getTime() > MAX_RANGE_MS) { - return reply.status(400).send({ - error: "Error - date range should not exceed 6 months", - }); + return sendApiError(reply, 400, "date range should not exceed 6 months"); } // Past windows are immutable (MV refreshes catch up to yesterday within a @@ -181,8 +160,8 @@ export async function providerRewardsRoutes(app: FastifyInstance) { new Date().getUTCDate(), ); request.cacheTTL = dateTo.getTime() <= todayUtc - ONE_DAY_MS - ? PAST_WINDOW_TTL - : LIVE_WINDOW_TTL; + ? CACHE_TTL.HISTORICAL + : CACHE_TTL.LIST; // ── Resolve specs ───────────────────────────────────────────── const specs = q.specs && q.specs.length > 0 ? [...new Set(q.specs)] : null; @@ -224,19 +203,13 @@ export async function providerRewardsRoutes(app: FastifyInstance) { }`, vars, { mvRelayDailies: { groupedAggregates: [] } }), (async () => { + if (!specs) return fetchAllProviderMonikers(); + // Spec-scoped: only need monikers for providers on the filtered specs. const map = new Map(); - if (specs) { - const fetches = await Promise.all( - specs.map((s) => fetchProvidersForSpec(s)), - ); - for (const providers of fetches) { - for (const p of providers) { - if (!map.has(p.address)) map.set(p.address, p.moniker); - } - } - } else { - for (const p of await fetchAllProviders()) { - map.set(p.address, p.moniker); + const fetches = await Promise.all(specs.map((s) => fetchProvidersForSpec(s))); + for (const providers of fetches) { + for (const p of providers) { + if (!map.has(p.address)) map.set(p.address, p.moniker); } } return map; @@ -254,8 +227,8 @@ export async function providerRewardsRoutes(app: FastifyInstance) { if (groupBy === "spec") { // One row per (provider, spec). Keys: [chainId, provider]. const rows = groups.map((agg) => { - const spec = agg.keys[0]; - const provider = agg.keys[1]; + const spec = agg.keys[0] ?? ""; + const provider = agg.keys[1] ?? ""; return { provider, spec, @@ -298,7 +271,7 @@ export async function providerRewardsRoutes(app: FastifyInstance) { // Default: one row per provider. Keys: [provider]. const rows = groups.map((agg) => { - const provider = agg.keys[0]; + const provider = agg.keys[0] ?? ""; return { provider, moniker: providerMap.get(provider) ?? "", diff --git a/apps/api/src/routes/providers.ts b/apps/api/src/routes/providers.ts index 75243aa..0064ad4 100644 --- a/apps/api/src/routes/providers.ts +++ b/apps/api/src/routes/providers.ts @@ -1,4 +1,7 @@ import type { FastifyInstance } from "fastify"; +import { CACHE_TTL } from "../config.js"; +import { weightedQos } from "@info/shared/utils"; +import type { ProviderListItem } from "@info/shared/types"; import { gqlSafe } from "../graphql/client.js"; import { fetchAllProviders, @@ -18,23 +21,13 @@ const addrSchema = { }; export async function providerRoutes(app: FastifyInstance) { - // GET /providers — chain RPC + indexer relay data + // GET /providers — full provider list with 30d relay stats. + // Network-wide provider count sits in the tens, so pagination buys nothing + // and complicates client filtering; return the full array sorted by stake. app.get("/", { - schema: { - tags: ["Providers"], - summary: "Paginated provider list with 30d relay stats", - querystring: { - type: "object" as const, - properties: { - page: { type: "integer" as const, default: 1 }, - limit: { type: "integer" as const, default: 20 }, - }, - }, - }, - config: { cacheTTL: 300 }, - }, async (request) => { - const { page, limit, offset } = request.pagination; - + schema: { tags: ["Providers"], summary: "All providers with 30d relay stats, sorted by total stake desc" }, + config: { cacheTTL: CACHE_TTL.LIST }, + }, async (): Promise<{ data: ProviderListItem[] }> => { const since = new Date(Date.now() - 30 * 24 * 60 * 60 * 1000) .toISOString() .slice(0, 10); @@ -61,19 +54,18 @@ export async function providerRoutes(app: FastifyInstance) { const relayMap = new Map(); if (relayData) { for (const agg of relayData.mvRelayDailies.groupedAggregates) { - relayMap.set(agg.keys[0], { cu: agg.sum.cu, relays: agg.sum.relays }); + const provider = agg.keys[0]; + if (provider) relayMap.set(provider, { cu: agg.sum.cu, relays: agg.sum.relays }); } } - const total = providers.length; providers.sort((a, b) => { const diff = BigInt(b.totalStake) - BigInt(a.totalStake); return diff > 0n ? 1 : diff < 0n ? -1 : 0; }); - const paged = providers.slice(offset, offset + limit); return { - data: paged.map((p) => { + data: providers.map((p): ProviderListItem => { const relay = relayMap.get(p.address); return { provider: p.address, @@ -87,7 +79,6 @@ export async function providerRoutes(app: FastifyInstance) { relaySum30d: relay?.relays ?? null, }; }), - pagination: { total, page, limit, pages: Math.ceil(total / limit) }, }; }); @@ -98,7 +89,7 @@ export async function providerRoutes(app: FastifyInstance) { tags: ["Providers"], summary: "Provider detail — stakes across all specs with health data", }, - config: { cacheTTL: 300 }, + config: { cacheTTL: CACHE_TTL.LIST }, }, async (request) => { const { addr } = request.params; const specs = await fetchAllSpecs(); @@ -156,7 +147,7 @@ export async function providerRoutes(app: FastifyInstance) { tags: ["Providers"], summary: "Provider stakes per spec", }, - config: { cacheTTL: 300 }, + config: { cacheTTL: CACHE_TTL.LIST }, }, async (request) => { const { addr } = request.params; const specs = await fetchAllSpecs(); @@ -183,28 +174,23 @@ export async function providerRoutes(app: FastifyInstance) { return { data: results.filter(Boolean) }; }); - // GET /providers/:addr/health — from Redis + // GET /providers/:addr/health — from Redis (per-interface probe records) app.get<{ Params: { addr: string } }>("/:addr/health", { schema: { ...addrSchema, tags: ["Providers"], - summary: "Provider health probe results (paginated)", + summary: "Provider health probe results", }, - config: { cacheTTL: 30 }, + config: { cacheTTL: CACHE_TTL.HEALTH_PROBE }, }, async (request) => { const { addr } = request.params; - const { page, limit } = request.pagination; - - if (!app.redis) { - return { data: [], pagination: { total: 0, page, limit, pages: 0 } }; - } + if (!app.redis) return { data: [] }; try { - const result = await readHealthForProvider(app.redis, addr, page, limit); - const total = result.total; - return { data: result.data, pagination: { total, page, limit, pages: Math.ceil(total / limit) } }; + const data = await readHealthForProvider(app.redis, addr); + return { data }; } catch { - return { data: [], pagination: { total: 0, page, limit, pages: 0 } }; + return { data: [] }; } }); @@ -223,7 +209,7 @@ export async function providerRoutes(app: FastifyInstance) { }, }, }, - config: { cacheTTL: 300 }, + config: { cacheTTL: CACHE_TTL.LIST }, }, async (request) => { const { addr } = request.params; const query = request.query as Record; @@ -295,19 +281,19 @@ export async function providerRoutes(app: FastifyInstance) { return { data: data.mvRelayDailies.nodes.map((n) => { - const w = Number(n.qosWeight); - const ew = Number(n.exQosWeight); + const qos = weightedQos(n.qosSyncW, n.qosAvailW, n.qosLatencyW, Number(n.qosWeight)); + const ex = weightedQos(n.exQosSyncW, n.exQosAvailW, n.exQosLatencyW, Number(n.exQosWeight)); return { date: n.date, chainId: n.chainId, cu: n.cu, relays: n.relays, - qosSync: w > 0 ? (n.qosSyncW ?? 0) / w : null, - qosAvailability: w > 0 ? (n.qosAvailW ?? 0) / w : null, - qosLatency: w > 0 ? (n.qosLatencyW ?? 0) / w : null, - excellenceQosSync: ew > 0 ? (n.exQosSyncW ?? 0) / ew : null, - excellenceQosAvailability: ew > 0 ? (n.exQosAvailW ?? 0) / ew : null, - excellenceQosLatency: ew > 0 ? (n.exQosLatencyW ?? 0) / ew : null, + qosSync: qos.qosSync, + qosAvailability: qos.qosAvailability, + qosLatency: qos.qosLatency, + excellenceQosSync: ex.qosSync, + excellenceQosAvailability: ex.qosAvailability, + excellenceQosLatency: ex.qosLatency, }; }), }; @@ -326,7 +312,7 @@ export async function providerRoutes(app: FastifyInstance) { }, }, }, - config: { cacheTTL: 86400 }, + config: { cacheTTL: CACHE_TTL.HISTORICAL }, }, async (request) => { const { addr } = request.params; const query = request.query as Record; @@ -341,7 +327,7 @@ export async function providerRoutes(app: FastifyInstance) { tags: ["Providers"], summary: "Delegator rewards from dualstaking module", }, - config: { cacheTTL: 300 }, + config: { cacheTTL: CACHE_TTL.LIST }, }, async (request) => { const { addr } = request.params; const rewards = await fetchDelegatorRewards(addr); diff --git a/apps/api/src/routes/search.ts b/apps/api/src/routes/search.ts index c8f98c0..f7527d6 100644 --- a/apps/api/src/routes/search.ts +++ b/apps/api/src/routes/search.ts @@ -1,4 +1,5 @@ import type { FastifyInstance } from "fastify"; +import { CACHE_TTL } from "../config.js"; import { fetchAllProviders, fetchAllSpecs } from "../rpc/lava.js"; interface SearchResult { @@ -23,7 +24,7 @@ export async function searchRoutes(app: FastifyInstance) { }, }, }, - config: { cacheTTL: 600 }, + config: { cacheTTL: CACHE_TTL.SEARCH }, }, async (request) => { const query = request.query as Record; const q = query.q?.toLowerCase() ?? ""; diff --git a/apps/api/src/routes/specs.ts b/apps/api/src/routes/specs.ts index dbfc212..6985a65 100644 --- a/apps/api/src/routes/specs.ts +++ b/apps/api/src/routes/specs.ts @@ -1,4 +1,5 @@ import type { FastifyInstance } from "fastify"; +import { CACHE_TTL } from "../config.js"; import { gqlSafe } from "../graphql/client.js"; import { fetchAllSpecs, fetchProvidersForSpec } from "../rpc/lava.js"; import { readHealthSummaryForSpec, readHealthByProviderForSpec } from "../services/health-store.js"; @@ -15,7 +16,7 @@ export async function specRoutes(app: FastifyInstance) { // GET /specs — chain RPC + indexer relay data app.get("/", { schema: { tags: ["Specs"], summary: "All specs with provider counts and 30d relay data" }, - config: { cacheTTL: 300 }, + config: { cacheTTL: CACHE_TTL.LIST }, }, async () => { const since = new Date(Date.now() - 30 * 24 * 60 * 60 * 1000).toISOString().slice(0, 10); @@ -38,7 +39,8 @@ export async function specRoutes(app: FastifyInstance) { const relayMap = new Map(); if (relayData) { for (const agg of relayData.mvRelayDailies.groupedAggregates) { - relayMap.set(agg.keys[0], { cu: agg.sum.cu, relays: agg.sum.relays }); + const key = agg.keys[0]; + if (key) relayMap.set(key, { cu: agg.sum.cu, relays: agg.sum.relays }); } } @@ -65,7 +67,7 @@ export async function specRoutes(app: FastifyInstance) { // GET /specs/:specId/stakes — chain RPC + health from Redis app.get<{ Params: { specId: string } }>("/:specId/stakes", { schema: { ...specIdSchema, tags: ["Specs"], summary: "Providers staked on this spec with health" }, - config: { cacheTTL: 300 }, + config: { cacheTTL: CACHE_TTL.LIST }, }, async (request) => { const { specId } = request.params; const providers = await fetchProvidersForSpec(specId); @@ -96,7 +98,7 @@ export async function specRoutes(app: FastifyInstance) { // GET /specs/:specId/health — from Redis app.get<{ Params: { specId: string } }>("/:specId/health", { schema: { ...specIdSchema, tags: ["Specs"], summary: "Health status distribution for a spec" }, - config: { cacheTTL: 30 }, + config: { cacheTTL: CACHE_TTL.HEALTH_PROBE }, }, async (request) => { const { specId } = request.params; @@ -115,7 +117,7 @@ export async function specRoutes(app: FastifyInstance) { // GET /specs/:specId/charts — indexer GraphQL (materialized view) app.get<{ Params: { specId: string } }>("/:specId/charts", { schema: { ...specIdSchema, tags: ["Specs"], summary: "Alltime CU/relays for a chain" }, - config: { cacheTTL: 300 }, + config: { cacheTTL: CACHE_TTL.LIST }, }, async (request) => { const { specId } = request.params; diff --git a/apps/api/src/routes/supply.ts b/apps/api/src/routes/supply.ts index 63683db..76d5042 100644 --- a/apps/api/src/routes/supply.ts +++ b/apps/api/src/routes/supply.ts @@ -1,26 +1,29 @@ import type { FastifyInstance } from "fastify"; +import { CACHE_TTL } from "../config.js"; +import { ulavaToLava } from "@info/shared/utils"; import { fetchTotalSupply, fetchCirculatingSupply } from "../rpc/lava.js"; - -const ULAVA_TO_LAVA = 1_000_000n; +import { readPrecomputed } from "../services/precompute-store.js"; export async function supplyRoutes(app: FastifyInstance) { // GET /supply/total — total token supply in lava, cached 5 min app.get("/total", { schema: { tags: ["Supply"], summary: "Total LAVA supply (plain text)" }, - config: { cacheTTL: 300, rateLimit: { max: 20, timeWindow: "1 minute" } }, + config: { cacheTTL: CACHE_TTL.LIST, rateLimit: { max: 20, timeWindow: "1 minute" } }, }, async (_request, reply) => { - const total = await fetchTotalSupply(); reply.header("Content-Type", "text/plain"); - return (total / ULAVA_TO_LAVA).toString(); + return ulavaToLava(await fetchTotalSupply()); }); - // GET /supply/circulating — total - locked vesting - reward pools, in lava + // GET /supply/circulating — total - locked vesting - reward pools, in lava. + // Prefers the precomputed value written by bin/precompute.ts; falls back to + // live compute when the precompute worker is down / has no entry yet. app.get("/circulating", { schema: { tags: ["Supply"], summary: "Circulating LAVA supply (plain text)" }, - config: { cacheTTL: 300, rateLimit: { max: 10, timeWindow: "1 minute" } }, + config: { cacheTTL: CACHE_TTL.LIST, rateLimit: { max: 10, timeWindow: "1 minute" } }, }, async (_request, reply) => { - const circulating = await fetchCirculatingSupply(); reply.header("Content-Type", "text/plain"); - return (circulating / ULAVA_TO_LAVA).toString(); + const cached = await readPrecomputed(app.redis, "supply.circulating"); + if (cached) return cached.value; + return ulavaToLava(await fetchCirculatingSupply()); }); } diff --git a/apps/api/src/routes/tvl.ts b/apps/api/src/routes/tvl.ts index e6cfebb..88b3ab5 100644 --- a/apps/api/src/routes/tvl.ts +++ b/apps/api/src/routes/tvl.ts @@ -1,11 +1,12 @@ import type { FastifyInstance } from "fastify"; +import { CACHE_TTL } from "../config.js"; import { computeTVL } from "../rpc/lava.js"; export async function tvlRoutes(app: FastifyInstance) { // GET /tvl — computed from chain RPC, cached 5 min app.get("/tvl", { schema: { tags: ["TVL"], summary: "Total Value Locked in USD" }, - config: { cacheTTL: 300 }, + config: { cacheTTL: CACHE_TTL.LIST }, }, async () => { return await computeTVL(); }); diff --git a/apps/api/src/routes/validators-and-rewards.ts b/apps/api/src/routes/validators-and-rewards.ts index c68de9e..8c40785 100644 --- a/apps/api/src/routes/validators-and-rewards.ts +++ b/apps/api/src/routes/validators-and-rewards.ts @@ -1,17 +1,34 @@ import type { FastifyInstance } from "fastify"; -import { fetchValidatorsWithRewards } from "../rpc/lava.js"; +import { CACHE_TTL } from "../config.js"; +import { fetchValidatorsWithRewards, type ValidatorWithRewards } from "../rpc/lava.js"; +import { readPrecomputed } from "../services/precompute-store.js"; + +interface ValidatorsAndRewardsData { + height: number; + datetime: number; + validators: ValidatorWithRewards[]; +} export async function validatorsAndRewardsRoutes(app: FastifyInstance) { // Replaces jsinfo's /lava_mainnet_validators_and_rewards. For each bonded // validator: fetches distribution rewards, outstanding rewards, estimated // rewards, delegations, and unbonding delegations — all converted to USD. + // + // Consumers: external (burn-ui dashboards, lava-ops pipelines). NOT called + // from this repo's web frontend. Don't delete it thinking it's dead — the + // /lava_mainnet_validators_and_rewards shape is the public contract. + // + // Prefers the precomputed value from bin/precompute.ts; falls back to live + // compute when the precompute worker hasn't populated the key yet. app.get("/validators-and-rewards", { schema: { tags: ["Validators"], summary: "All bonded validators with rewards, delegations, and unbonding — matches jsinfo shape", }, - config: { cacheTTL: 7200 }, - }, async () => { + config: { cacheTTL: CACHE_TTL.SLOW_MOVING }, + }, async (request) => { + const cached = await readPrecomputed(request.server.redis, "validators_and_rewards"); + if (cached) return { data: cached.value }; const data = await fetchValidatorsWithRewards(); return { data }; }); diff --git a/apps/api/src/rpc/apr.ts b/apps/api/src/rpc/apr.ts new file mode 100644 index 0000000..cdcfa5f --- /dev/null +++ b/apps/api/src/rpc/apr.ts @@ -0,0 +1,302 @@ +import type { Redis } from "ioredis"; +import { RPC_BATCH_SIZE } from "./rest.js"; +import { prewarmPriceCache, fetchTokenUsdPrice } from "./pricing.js"; +import { + APR_BENCHMARK_LAVA, + type RewardToken, + type RewardsBySpecEntry, + fetchEstimatedRewards, + fetchRewardsBySpec, +} from "./rewards.js"; +import { + type ProviderSpecEntry, + fetchAllProviders, + fetchProvidersWithSpecs, +} from "./providers.js"; +import { fetchBondedValidators } from "./validators.js"; +import { fetchProviderAvatar } from "./keybase.js"; + +/** 80th percentile, capped at 30% — same thresholds as jsinfo */ +const APR_PERCENTILE = 0.8; +const APR_MAX_PERCENTILE_CAP = 0.3; +const APR_MAX_INDIVIDUAL = 0.8; +const APR_MIN = 1e-11; + +/** Percentile calculation matching jsinfo `CalculatePercentile` */ +function calculatePercentile(values: number[], rank: number): number { + if (values.length === 0 || rank < 0 || rank > 1) return 0; + if (values.length === 1) return values[0]!; + + const sorted = [...values].sort((a, b) => a - b); + const pos = Math.floor((sorted.length - 1) * rank); + + if (sorted.length % 2 === 0) { + return sorted[pos]! + (sorted[pos + 1]! - sorted[pos]!) * rank; + } + return sorted[pos]!; +} + +/** APR from monthly reward: (1 + rewardUsd/investedUsd)^12 - 1 */ +function calculateApr(rewardUsd: number, investedUsd: number): number { + if (investedUsd <= 0 || rewardUsd <= 0) return 0; + const rate = rewardUsd / investedUsd; + const apr = Math.pow(1 + rate, 12) - 1; + if (!isFinite(apr) || apr < APR_MIN || apr > 100) return 0; + return apr; +} + +// --- Weighted APR history (Redis, matches jsinfo AprWeighted) --- + +const APR_WEIGHTS = [0.4, 0.25, 0.15, 0.1, 0.05, 0.03, 0.02]; +const APR_DAYS_TO_KEEP = 7; +const APR_HISTORY_TTL = 30 * 24 * 60 * 60; // 30 days + +interface AprRecord { date: string; aprSum: number; count: number } +interface AprHistory { records: AprRecord[]; lastUpdated: string } + +function aprHistoryKey(type: string, address: string): string { + return `apr_history:${type}:${address}`; +} + +function todayStr(): string { + const d = new Date(); + return `${d.getFullYear()}/${String(d.getMonth() + 1).padStart(2, "0")}/${String(d.getDate()).padStart(2, "0")}`; +} + +async function storeApr(redis: Redis, type: string, address: string, apr: number): Promise { + if (apr === 0) return; + const key = aprHistoryKey(type, address); + const today = todayStr(); + try { + const raw = await redis.get(key); + const history: AprHistory = raw ? JSON.parse(raw) : { records: [], lastUpdated: today }; + + const rec = history.records.find((r) => r.date === today); + if (rec) { rec.aprSum += apr; rec.count += 1; } + else { history.records.push({ date: today, aprSum: apr, count: 1 }); } + + history.records = history.records + .sort((a, b) => b.date.localeCompare(a.date)) + .slice(0, APR_DAYS_TO_KEEP); + history.lastUpdated = today; + + await redis.set(key, JSON.stringify(history), "EX", APR_HISTORY_TTL); + } catch { /* Redis failures are non-fatal */ } +} + +async function getWeightedApr(redis: Redis, type: string, address: string): Promise { + try { + const raw = await redis.get(aprHistoryKey(type, address)); + if (!raw) return null; + const history: AprHistory = JSON.parse(raw); + if (history.records.length === 0) return null; + + let weightedSum = 0; + let weightSum = 0; + for (let i = 0; i < history.records.length && i < APR_WEIGHTS.length; i++) { + const record = history.records[i]!; + const weight = APR_WEIGHTS[i]!; + const avg = record.aprSum / record.count; + weightedSum += avg * weight; + weightSum += weight; + } + return weightSum > 0 ? weightedSum / weightSum : null; + } catch { + return null; + } +} + +/** + * Compute APR percentiles matching jsinfo `/apr` response. + * + * For each active provider and bonded validator: + * 1. Query `estimated_{type}_rewards` with a 10 000 LAVA benchmark + * 2. Convert multi-denom rewards to USD via CoinGecko + * 3. Compute APR via monthly compounding: (1 + monthlyRate)^12 - 1 + * 4. Store in 7-day weighted history (Redis, if available) + * 5. Take 80th percentile, cap at 30% + */ +export async function computeAPR(redis?: Redis | null): Promise<{ + restaking_apr_percentile: number; + staking_apr_percentile: number; +}> { + // Pre-warm all token prices in a single CoinGecko call + await prewarmPriceCache(); + + const lavaPrice = await fetchTokenUsdPrice("lava"); + const investedUsd = APR_BENCHMARK_LAVA * lavaPrice; + + const [providers, validators] = await Promise.all([ + fetchAllProviders(), + fetchBondedValidators(), + ]); + + const providerAddresses = providers.map((p) => p.address); + + // Collect per-entity APRs (batched by RPC_BATCH_SIZE) + const [providerAprs, validatorAprs] = await Promise.all([ + collectEntityAprs("provider", providerAddresses, investedUsd, redis), + collectEntityAprs("validator", validators, investedUsd, redis), + ]); + + return { + restaking_apr_percentile: Math.min( + calculatePercentile(providerAprs, APR_PERCENTILE), + APR_MAX_PERCENTILE_CAP, + ), + staking_apr_percentile: Math.min( + calculatePercentile(validatorAprs, APR_PERCENTILE), + APR_MAX_PERCENTILE_CAP, + ), + }; +} + +/** Batch-fetch estimated rewards, compute APR per entity, apply weighted history */ +async function collectEntityAprs( + type: "provider" | "validator", + addresses: string[], + investedUsd: number, + redis?: Redis | null, +): Promise { + const aprs: number[] = []; + + for (let i = 0; i < addresses.length; i += RPC_BATCH_SIZE) { + const batch = addresses.slice(i, i + RPC_BATCH_SIZE); + const rewards = await Promise.all( + batch.map((addr) => fetchEstimatedRewards(type, addr)), + ); + + for (let j = 0; j < batch.length; j++) { + const addr = batch[j]!; + const currentApr = calculateApr(rewards[j]!.totalUsd, investedUsd); + if (currentApr <= 0) continue; + + let finalApr = currentApr; + if (redis) { + await storeApr(redis, type, addr, currentApr); + const weighted = await getWeightedApr(redis, type, addr); + if (weighted !== null) finalApr = weighted; + } + + if (finalApr > 0 && finalApr < APR_MAX_INDIVIDUAL) { + aprs.push(finalApr); + } + } + } + + return aprs; +} + +/** Max display APR for individual providers (matching jsinfo) */ +const MAX_DISPLAY_APR = 0.9; + +/** Format APR as percentage string matching jsinfo format */ +function formatAprPercent(apr: number): string { + if (apr <= 0) return "-"; + if (apr > MAX_DISPLAY_APR) return "90.0%"; + return `${(apr * 100).toFixed(4)}%`; +} + +/** Format commission as percentage string */ +function formatCommission(commission: string): string { + if (!commission) return "-"; + const n = parseFloat(commission); + if (!isFinite(n)) return "-"; + return `${n.toFixed(1)}%`; +} + +// Field naming is intentionally mixed on this endpoint: the four snake_case +// keys below (`30_days_cu_served`, `30_days_relays_served`, +// `rewards_10k_lava_delegation`, `rewards_last_month`) match jsinfo's +// `/all_providers_apr` shape 1:1. External consumers (burn-ui / lava-ops) +// depend on that exact contract — renaming them to camelCase would be a +// breaking change. The remaining fields were camelCase in jsinfo too. +export interface AllProviderAprEntry { + address: string; + moniker: string; + apr: string; + commission: string; + "30_days_cu_served": string; + "30_days_relays_served": string; + rewards_10k_lava_delegation: RewardToken[]; + rewards_last_month: RewardsBySpecEntry[]; + specs: ProviderSpecEntry[]; + stake: string; + stakestatus: string; + addons: string; + extensions: string; + delegateTotal: string; + avatar: string | null; +} + +/** + * Compute per-provider APR data matching jsinfo `/all_providers_apr`. + * + * Returns array of provider objects with APR, commission, 30d relay data, + * per-token reward breakdown, specs, and avatar. + */ +export async function computeAllProvidersApr( + relay30d: Map, + redis?: Redis | null, +): Promise { + // Pre-warm all token prices in a single CoinGecko call + await prewarmPriceCache(); + + const lavaPrice = await fetchTokenUsdPrice("lava"); + const investedUsd = APR_BENCHMARK_LAVA * lavaPrice; + + const { providers: providerMap, specNames } = await fetchProvidersWithSpecs(); + const addresses = Array.from(providerMap.keys()); + const results: AllProviderAprEntry[] = []; + + for (let i = 0; i < addresses.length; i += RPC_BATCH_SIZE) { + const batch = addresses.slice(i, i + RPC_BATCH_SIZE); + const [rewardResults, rewardsLastMonthResults, avatarResults] = await Promise.all([ + Promise.all(batch.map((addr) => fetchEstimatedRewards("provider", addr))), + Promise.all(batch.map((addr) => fetchRewardsBySpec(addr, specNames))), + Promise.all(batch.map((addr) => { + const p = providerMap.get(addr)!; + return fetchProviderAvatar(addr, p.identity || undefined).catch(() => null); + })), + ]); + + for (let j = 0; j < batch.length; j++) { + const addr = batch[j]!; + const provider = providerMap.get(addr)!; + const rewards = rewardResults[j]!; + const rewardsLastMonth = rewardsLastMonthResults[j]!; + const avatar = avatarResults[j] ?? null; + const currentApr = calculateApr(rewards.totalUsd, investedUsd); + + let finalApr = currentApr; + if (redis && currentApr > 0) { + await storeApr(redis, "provider", addr, currentApr); + const weighted = await getWeightedApr(redis, "provider", addr); + if (weighted !== null) finalApr = weighted; + } + + const relay = relay30d.get(addr); + const firstSpec = provider.specs[0]; + + results.push({ + address: addr, + moniker: provider.moniker || "-", + apr: formatAprPercent(finalApr), + commission: formatCommission(provider.commission), + "30_days_cu_served": relay?.cu ?? "-", + "30_days_relays_served": relay?.relays ?? "-", + rewards_10k_lava_delegation: rewards.tokens, + rewards_last_month: rewardsLastMonth, + specs: provider.specs, + stake: firstSpec?.stake ?? "", + stakestatus: firstSpec ? "Active" : "", + addons: firstSpec?.addons ?? "", + extensions: firstSpec?.extensions ?? "", + delegateTotal: firstSpec?.delegateTotal ?? "", + avatar, + }); + } + } + + return results; +} diff --git a/apps/api/src/rpc/block.ts b/apps/api/src/rpc/block.ts new file mode 100644 index 0000000..ea7c2d3 --- /dev/null +++ b/apps/api/src/rpc/block.ts @@ -0,0 +1,81 @@ +import { config } from "../config.js"; + +// Lava mainnet genesis timestamp (block 1) — used to narrow binary search range. +const LAVA_GENESIS_UNIX = 1_713_350_000; // ~2024-04-17 + +// LRU-ish cache for timestamp → block height (avoids repeated binary searches). +const blockAtTsCache = new Map(); +const BLOCK_AT_TS_CACHE_MAX = 200; + +export async function fetchLatestBlockHeight(): Promise<{ + height: number; + time: string; +}> { + const res = await fetch(`${config.lava.rpcUrl}/status`, { + signal: AbortSignal.timeout(10_000), + }); + if (!res.ok) throw new Error(`RPC ${res.status}`); + const data = (await res.json()) as { + result: { sync_info: { latest_block_height: string; latest_block_time: string } }; + }; + return { + height: parseInt(data.result.sync_info.latest_block_height, 10), + time: data.result.sync_info.latest_block_time, + }; +} + +// Binary-search Tendermint blocks to find the block closest to a target unix timestamp. +// Uses linear interpolation to estimate the starting range, reducing iterations from ~23 to ~10. +export async function fetchBlockAtTimestamp(targetUnix: number): Promise { + const cached = blockAtTsCache.get(targetUnix); + if (cached !== undefined) return cached; + + async function blockTime(height: number): Promise { + 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 Math.floor(new Date(data.result.block.header.time).getTime() / 1000); + } + + const { height: latestHeight, time: latestTime } = await fetchLatestBlockHeight(); + const latestUnix = Math.floor(new Date(latestTime).getTime() / 1000); + + if (targetUnix >= latestUnix) return latestHeight; + + // Linear interpolation to narrow the search range + const genesisUnix = LAVA_GENESIS_UNIX; + const estimatedHeight = Math.max(1, Math.min( + latestHeight, + Math.floor(latestHeight * (targetUnix - genesisUnix) / (latestUnix - genesisUnix)), + )); + const margin = 2000; + let lo = Math.max(1, estimatedHeight - margin); + let hi = Math.min(latestHeight, estimatedHeight + margin); + + // Verify bounds contain the target; widen if not + if (await blockTime(lo) > targetUnix) lo = 1; + if (await blockTime(hi) < targetUnix) hi = latestHeight; + + while (lo < hi) { + const mid = Math.floor((lo + hi) / 2); + const t = await blockTime(mid); + if (t < targetUnix) { + lo = mid + 1; + } else { + hi = mid; + } + } + + // Evict oldest entry if cache is full + if (blockAtTsCache.size >= BLOCK_AT_TS_CACHE_MAX) { + const firstKey = blockAtTsCache.keys().next().value; + if (firstKey !== undefined) blockAtTsCache.delete(firstKey); + } + blockAtTsCache.set(targetUnix, lo); + + return lo; +} diff --git a/apps/api/src/rpc/keybase.ts b/apps/api/src/rpc/keybase.ts new file mode 100644 index 0000000..89f0f0f --- /dev/null +++ b/apps/api/src/rpc/keybase.ts @@ -0,0 +1,61 @@ +import { config } from "../config.js"; +import { fetchRest } from "./rest.js"; +import { fetchAllSpecs } from "./specs.js"; + +async function fetchProviderMetadata(provider: string): Promise<{ + description?: { identity?: string; moniker?: string }; +} | null> { + try { + const specs = await fetchAllSpecs(); + for (const spec of specs) { + try { + const data = await fetchRest<{ + stakeEntry: Array<{ + address: string; + description?: { identity?: string; moniker?: string }; + }>; + }>(`/lavanet/lava/pairing/providers/${spec.index}`); + const match = data.stakeEntry?.find((p) => p.address === provider); + if (match?.description?.identity) return match; + } catch { + continue; + } + } + return null; + } catch { + return null; + } +} + +/** + * Keybase identity fingerprints are hex strings up to 32 chars. Anything + * that doesn't match is either malformed chain data or an attempt to slip + * other characters into the outbound URL. Reject early; encodeURIComponent + * still runs below as defense-in-depth so any future loosening here + * doesn't reopen an SSRF vector. + */ +const KEYBASE_IDENTITY_RE = /^[A-Fa-f0-9]{1,64}$/; + +export async function fetchProviderAvatar(provider: string, identityHint?: string): Promise { + try { + let identity = identityHint; + if (!identity) { + const meta = await fetchProviderMetadata(provider); + identity = meta?.description?.identity ?? undefined; + } + if (!identity) return null; + if (!KEYBASE_IDENTITY_RE.test(identity)) return null; + + const res = await fetch( + `${config.external.keybaseApiUrl}/user/lookup.json?key_suffix=${encodeURIComponent(identity)}&fields=pictures`, + { signal: AbortSignal.timeout(10_000) }, + ); + if (!res.ok) return null; + const data = (await res.json()) as { + them?: Array<{ pictures?: { primary?: { url?: string } } }>; + }; + return data.them?.[0]?.pictures?.primary?.url ?? null; + } catch { + return null; + } +} diff --git a/apps/api/src/rpc/lava.ts b/apps/api/src/rpc/lava.ts index 3f7ac65..3fa686a 100644 --- a/apps/api/src/rpc/lava.ts +++ b/apps/api/src/rpc/lava.ts @@ -1,1673 +1,51 @@ -import pino from "pino"; -import type { Redis } from "ioredis"; - -const logger = pino({ name: "rpc" }); - -const LAVA_REST_URL = process.env.LAVA_REST_URL ?? "https://lava.rest.lava.build"; - -// Concurrent in-flight RPC calls per batch loop. Operators running dedicated -// RPC endpoints can raise this (env: RPC_BATCH_SIZE). Public endpoints should -// keep a conservative value (~5) to avoid rate limiting. -export const RPC_BATCH_SIZE = Math.max(1, parseInt(process.env.RPC_BATCH_SIZE ?? "25", 10)); - -// Request coalescing: concurrent fetches for the same path share one in-flight request -const inflightRpc = new Map>(); - -async function fetchRest(path: string, blockHeight?: number): Promise { - const cacheKey = blockHeight ? `${path}@${blockHeight}` : path; - const existing = inflightRpc.get(cacheKey); - if (existing) return existing as Promise; - - const promise = (async () => { - const headers: Record = {}; - if (blockHeight) headers["x-cosmos-block-height"] = String(blockHeight); - - const res = await fetch(`${LAVA_REST_URL}${path}`, { - headers, - signal: AbortSignal.timeout(15_000), - }); - if (!res.ok) throw new Error(`RPC ${res.status}: ${res.statusText}`); - return (await res.json()) as T; - })(); - - inflightRpc.set(cacheKey, promise); - const cleanup = () => { inflightRpc.delete(cacheKey); }; - promise.then(cleanup, cleanup); - return promise; -} - -export async function fetchTotalSupply(blockHeight?: number): Promise { - const data = await fetchRest<{ supply: Array<{ denom: string; amount: string }> }>( - "/cosmos/bank/v1beta1/supply", - blockHeight, - ); - const lava = data.supply?.find((c) => c.denom === "ulava"); - return BigInt(lava?.amount ?? "0"); -} - -// Lava mainnet genesis timestamp (block 1) — used to narrow binary search range. -const LAVA_GENESIS_UNIX = 1_713_350_000; // ~2024-04-17 - -// LRU-ish cache for timestamp → block height (avoids repeated binary searches). -const blockAtTsCache = new Map(); -const BLOCK_AT_TS_CACHE_MAX = 200; - -// Binary-search Tendermint blocks to find the block closest to a target unix timestamp. -// Uses linear interpolation to estimate the starting range, reducing iterations from ~23 to ~10. -export async function fetchBlockAtTimestamp(targetUnix: number): Promise { - const cached = blockAtTsCache.get(targetUnix); - if (cached !== undefined) return cached; - - const LAVA_RPC_URL = process.env.LAVA_RPC_URL ?? "https://lava.tendermintrpc.lava.build:443"; - - async function blockTime(height: number): Promise { - const res = await fetch(`${LAVA_RPC_URL}/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 Math.floor(new Date(data.result.block.header.time).getTime() / 1000); - } - - const { height: latestHeight, time: latestTime } = await fetchLatestBlockHeight(); - const latestUnix = Math.floor(new Date(latestTime).getTime() / 1000); - - if (targetUnix >= latestUnix) return latestHeight; - - // Linear interpolation to narrow the search range - const genesisUnix = LAVA_GENESIS_UNIX; - const estimatedHeight = Math.max(1, Math.min( - latestHeight, - Math.floor(latestHeight * (targetUnix - genesisUnix) / (latestUnix - genesisUnix)), - )); - const margin = 2000; - let lo = Math.max(1, estimatedHeight - margin); - let hi = Math.min(latestHeight, estimatedHeight + margin); - - // Verify bounds contain the target; widen if not - if (await blockTime(lo) > targetUnix) lo = 1; - if (await blockTime(hi) < targetUnix) hi = latestHeight; - - while (lo < hi) { - const mid = Math.floor((lo + hi) / 2); - const t = await blockTime(mid); - if (t < targetUnix) { - lo = mid + 1; - } else { - hi = mid; - } - } - - // Evict oldest entry if cache is full - if (blockAtTsCache.size >= BLOCK_AT_TS_CACHE_MAX) { - const firstKey = blockAtTsCache.keys().next().value; - if (firstKey !== undefined) blockAtTsCache.delete(firstKey); - } - blockAtTsCache.set(targetUnix, lo); - - return lo; -} - -interface RewardPool { - name: string; - balance: Array<{ denom: string; amount: string }>; -} - -/** Shared fetch for reward pools — both consumers filter by pool name. */ -async function fetchRewardPools(): Promise { - const data = await fetchRest<{ pools: RewardPool[] }>("/lavanet/lava/rewards/pools"); - return data.pools ?? []; -} - -function sumPoolsUlava(pools: RewardPool[], names: string[]): bigint { - const nameSet = new Set(names); - let total = 0n; - for (const pool of pools) { - if (nameSet.has(pool.name)) { - for (const coin of pool.balance ?? []) { - if (coin.denom === "ulava") total += BigInt(coin.amount); - } - } - } - return total; -} - -const ALL_REWARD_POOLS = [ - "validators_rewards_distribution_pool", - "validators_rewards_allocation_pool", - "providers_rewards_distribution_pool", - "providers_rewards_allocation_pool", - "iprpc_pool", -]; - -async function fetchRewardPoolsAmount(): Promise { - return sumPoolsUlava(await fetchRewardPools(), ALL_REWARD_POOLS); -} - -const PROVIDER_REWARD_POOLS = [ - "providers_rewards_distribution_pool", - "providers_rewards_allocation_pool", - "iprpc_pool", -]; - -/** Fetch total ulava in provider-side reward pools (excludes validator pools). */ -export async function fetchProviderRewardPoolsAmount(): Promise { - return sumPoolsUlava(await fetchRewardPools(), PROVIDER_REWARD_POOLS); -} - -interface VestingStats { - continuousVesting: bigint; - periodicVesting: bigint; -} - -async function fetchLockedVestingTokens(): Promise { - const nowSeconds = Math.floor(Date.now() / 1000); - const stats: VestingStats = { continuousVesting: 0n, periodicVesting: 0n }; - let nextKey: string | null = null; - - do { - const params = new URLSearchParams({ "pagination.limit": "1000" }); - if (nextKey) params.set("pagination.key", nextKey); - - const data = await fetchRest<{ - accounts: Array<{ - "@type": string; - start_time?: string; - base_vesting_account?: { - original_vesting?: Array<{ denom: string; amount: string }>; - end_time?: string; - }; - vesting_periods?: Array<{ - length: string; - amount: Array<{ denom: string; amount: string }>; - }>; - }>; - pagination?: { next_key: string | null }; - }>(`/cosmos/auth/v1beta1/accounts?${params}`); - - for (const account of data.accounts ?? []) { - const type = account["@type"]; - - if (type === "/cosmos.vesting.v1beta1.ContinuousVestingAccount") { - const totalAmount = BigInt( - account.base_vesting_account?.original_vesting?.[0]?.amount ?? "0", - ); - const startTime = BigInt(account.start_time ?? "0"); - const endTime = BigInt(account.base_vesting_account?.end_time ?? "0"); - const now = BigInt(nowSeconds); - - if (now < startTime) { - stats.continuousVesting += totalAmount; - } else if (now < endTime && endTime > startTime) { - stats.continuousVesting += ((endTime - now) * totalAmount) / (endTime - startTime); - } - } else if (type === "/cosmos.vesting.v1beta1.PeriodicVestingAccount") { - let currentTime = parseInt(account.start_time ?? "0"); - for (const period of account.vesting_periods ?? []) { - currentTime += parseInt(period.length); - if (currentTime >= nowSeconds) { - stats.periodicVesting += BigInt(period.amount?.[0]?.amount ?? "0"); - } - } - } - } - - nextKey = data.pagination?.next_key ?? null; - } while (nextKey); - - return stats; -} - -export async function fetchCirculatingSupply(): Promise { - const [totalSupply, pools, vesting] = await Promise.all([ - fetchTotalSupply(), - fetchRewardPoolsAmount(), - fetchLockedVestingTokens(), - ]); - - const circulating = totalSupply - vesting.continuousVesting - vesting.periodicVesting - pools; - if (circulating < 0n) { - logger.warn( - `Negative circulating supply: total=${totalSupply} continuous=${vesting.continuousVesting} periodic=${vesting.periodicVesting} pools=${pools}`, - ); - return 0n; - } - return circulating; -} - -export interface ProviderEndpoint { - iPPORT: string; - geolocation: number; - apiInterfaces: string[]; - addons: string[]; - extensions: string[]; -} - -export interface ProviderForSpec { - address: string; - moniker: string; - identity: string; - stake: { amount: string }; - delegate_total: { amount: string }; - delegate_commission: string; - geolocation: number; - addons: string; - extensions: string; - apiInterfaces: string; - endpoints: ProviderEndpoint[]; -} - -export async function fetchProvidersForSpec(specId: string): Promise { - const data = await fetchRest<{ - stakeEntry: Array<{ - address: string; - moniker: string; - description?: { identity?: string }; - stake: { amount: string }; - delegate_total: { amount: string }; - delegate_commission: string; - geolocation: number; - endpoints?: Array<{ - iPPORT?: string; - geolocation?: number; - addons?: string[]; - extensions?: string[]; - api_interfaces?: string[]; - }>; - }>; - }>(`/lavanet/lava/pairing/providers/${specId}`); - - return (data.stakeEntry ?? []).map((entry) => { - const allAddons = new Set(); - const allExtensions = new Set(); - const allApiInterfaces = new Set(); - const endpoints: ProviderEndpoint[] = []; - - for (const ep of entry.endpoints ?? []) { - for (const a of ep.addons ?? []) if (a) allAddons.add(a); - for (const e of ep.extensions ?? []) if (e) allExtensions.add(e); - for (const i of ep.api_interfaces ?? []) if (i) allApiInterfaces.add(i); - - if (ep.iPPORT) { - endpoints.push({ - iPPORT: ep.iPPORT, - geolocation: ep.geolocation ?? entry.geolocation, - apiInterfaces: (ep.api_interfaces ?? []).filter(Boolean), - addons: (ep.addons ?? []).filter(Boolean), - extensions: (ep.extensions ?? []).filter(Boolean), - }); - } - } - - return { - address: entry.address, - moniker: entry.moniker, - identity: entry.description?.identity ?? "", - stake: entry.stake, - delegate_total: entry.delegate_total, - delegate_commission: entry.delegate_commission, - geolocation: entry.geolocation, - addons: Array.from(allAddons).join(","), - extensions: Array.from(allExtensions).join(","), - apiInterfaces: Array.from(allApiInterfaces).join(","), - endpoints, - }; - }); -} - -function titleCase(s: string): string { - return s.replace(/\b\w/g, (c) => c.toUpperCase()); -} - -/** Display name overrides — keys are specIDs (uppercase) */ -const CHAIN_DISPLAY_NAMES: Record = { - BSC: "BNB Chain Mainnet", - BSCT: "BNB Chain Testnet", - COSMOSHUB: "Cosmos Hub Mainnet", - COSMOSHUBT: "Cosmos Hub Testnet", - ETH1: "Ethereum Mainnet", - FTM4002: "Fantom Testnet", - FVMT: "Filecoin Testnet", - HEDERA: "Hedera Hashgraph Mainnet", - BTC: "Bitcoin Mainnet", - HOL1: "Ethereum Holesky Testnet", - LAVA: "Lava Mainnet", - LAV1: "Lava Testnet", - MOVEMENTT: "Movement Testnet", - OPTMS: "Optimism Sepolia Testnet", - POLYGONA: "Polygon Amoy Testnet", - SEP1: "Ethereum Sepolia Testnet", - SOLANAT: "Solana Testnet", - SONICT: "Sonic Blaze Testnet", - SPARK: "Fuse Testnet", - STRKS: "Starknet Sepolia Testnet", - TRX: "Tron Mainnet", - TRXT: "Tron Shasta Testnet", -}; - -function chainDisplayName(chainID: string, chainName: string): string { - if (CHAIN_DISPLAY_NAMES[chainID]) return CHAIN_DISPLAY_NAMES[chainID]; - return titleCase(chainName); -} - -import { BASE_SPECS } from "@info/shared/constants"; - -export async function fetchAllSpecs(): Promise> { - const data = await fetchRest<{ - chainInfoList: Array<{ chainName: string; chainID: string }>; - }>("/lavanet/lava/spec/show_all_chains"); - return (data.chainInfoList ?? []) - .filter((c) => !BASE_SPECS.has(c.chainID)) - .map((c) => ({ index: c.chainID, name: chainDisplayName(c.chainID, c.chainName) })); -} - -export async function fetchStakingPool(): Promise<{ bonded_tokens: string; not_bonded_tokens: string }> { - const data = await fetchRest<{ pool: { bonded_tokens: string; not_bonded_tokens: string } }>( - "/cosmos/staking/v1beta1/pool", - ); - return data.pool; -} - - -// Reward pools that count toward TVL (iprpc_pool excluded) -const TVL_REWARD_POOLS = [ - "validators_rewards_distribution_pool", - "validators_rewards_allocation_pool", - "providers_rewards_distribution_pool", - "providers_rewards_allocation_pool", -]; - -const COINGECKO_API_URL = process.env.COINGECKO_API_URL ?? "https://api.coingecko.com/api/v3"; - -export async function fetchLavaUsdPrice(): Promise { - const res = await fetch( - `${COINGECKO_API_URL}/simple/price?ids=lava-network&vs_currencies=usd`, - { signal: AbortSignal.timeout(15_000) }, - ); - if (!res.ok) throw new Error(`CoinGecko ${res.status}`); - const data = (await res.json()) as { "lava-network"?: { usd?: number } }; - const price = data["lava-network"]?.usd; - if (!price || price <= 0) throw new Error("Invalid LAVA price from CoinGecko"); - return price; -} - -// Fetch LAVA USD price at a specific date via CoinGecko /coins/{id}/history. -// date is a Date object; CoinGecko expects dd-mm-yyyy format. -export async function fetchLavaUsdPriceAt(date: Date): Promise { - const dd = String(date.getUTCDate()).padStart(2, "0"); - const mm = String(date.getUTCMonth() + 1).padStart(2, "0"); - const yyyy = date.getUTCFullYear(); - const res = await fetch( - `${COINGECKO_API_URL}/coins/lava-network/history?date=${dd}-${mm}-${yyyy}&localization=false`, - { signal: AbortSignal.timeout(15_000) }, - ); - if (!res.ok) throw new Error(`CoinGecko ${res.status}`); - const data = (await res.json()) as { - market_data?: { current_price?: { usd?: number } }; - }; - const price = data.market_data?.current_price?.usd; - if (!price || price <= 0) throw new Error(`No LAVA price for ${yyyy}-${mm}-${dd}`); - return price; -} - -async function fetchTvlRewardPoolsUlava(): Promise { - const data = await fetchRest<{ - pools: Array<{ name: string; balance: Array<{ denom: string; amount: string }> }>; - }>("/lavanet/lava/rewards/pools"); - let total = 0n; - for (const pool of data.pools ?? []) { - if (TVL_REWARD_POOLS.includes(pool.name)) { - for (const coin of pool.balance ?? []) { - if (coin.denom === "ulava") total += BigInt(coin.amount); - } - } - } - return total; -} - -async function fetchSubscriptionCreditsUlava(): Promise { - try { - const data = await fetchRest<{ - subs_info: Array<{ credit: { amount: string; denom: string } }>; - }>("/lavanet/lava/subscription/list"); - let total = 0n; - for (const sub of data.subs_info ?? []) { - if (sub.credit?.denom === "ulava") { - const amount = BigInt(sub.credit.amount || "0"); - if (amount > 0n) total += amount; - } - } - return total; - } catch { - return 0n; - } -} - -async function fetchOsmosisLavaUlava(): Promise { - try { - const input = JSON.stringify({ - json: { - limit: 10, search: null, denoms: ["LAVA"], - types: ["weighted", "stable", "concentrated", "cosmwasm-transmuter", - "cosmwasm", "cosmwasm-astroport-pcl", "cosmwasm-whitewhale"], - incentiveTypes: ["superfluid", "osmosis", "boost", "none"], - sort: { keyPath: "market.volume24hUsd", direction: "desc" }, - minLiquidityUsd: 1000, cursor: 0, - }, - meta: { values: { search: ["undefined"] } }, - }); - const res = await fetch( - `https://app.osmosis.zone/api/edge-trpc-pools/pools.getPools?input=${encodeURIComponent(input)}`, - { signal: AbortSignal.timeout(15_000) }, - ); - if (!res.ok) return 0n; - const data = (await res.json()) as { - result: { data: { json: { items: Array<{ - reserveCoins: Array<{ currency: { coinDenom: string }; amount: string }>; - }> } } }; - }; - let total = 0n; - for (const pool of data.result?.data?.json?.items ?? []) { - for (const coin of pool.reserveCoins ?? []) { - if (coin.currency?.coinDenom?.toLowerCase() === "lava") { - total += BigInt(Math.round(parseFloat(coin.amount || "0"))); - } - } - } - return total; - } catch { - return 0n; - } -} - -async function fetchBaseDexUsd(): Promise { - try { - const res = await fetch( - "https://api.geckoterminal.com/api/v2/search/pools?query=lava", - { signal: AbortSignal.timeout(15_000) }, - ); - if (!res.ok) return 0; - const data = (await res.json()) as { - data: Array<{ id: string; attributes: { reserve_in_usd: string } }>; - }; - const basePool = data.data?.find((p: { id: string }) => p.id.startsWith("base_")); - return basePool ? parseFloat(basePool.attributes.reserve_in_usd || "0") : 0; - } catch { - return 0; - } -} - -async function fetchArbitrumDexUsd(): Promise { - try { - const res = await fetch("https://interface.gateway.uniswap.org/v1/graphql", { - method: "POST", - signal: AbortSignal.timeout(15_000), - headers: { - "Content-Type": "application/json", - Origin: "https://app.uniswap.org", - }, - body: JSON.stringify({ - query: `query($chain: Chain!, $address: String) { - token(chain: $chain, address: $address) { - market(currency: USD) { totalValueLocked { value } } - } - }`, - variables: { - address: "0x11e969e9b3f89cb16d686a03cd8508c9fc0361af", - chain: "ARBITRUM", - }, - }), - }); - if (!res.ok) return 0; - const data = (await res.json()) as { - data?: { token?: { market?: { totalValueLocked?: { value?: number } } } }; - }; - return data.data?.token?.market?.totalValueLocked?.value ?? 0; - } catch { - return 0; - } -} - -/** - * TVL (USD) = bonded tokens + 4 reward pools + subscriptions + DEX liquidity - * Matches jsinfo formula: all components converted to USD via CoinGecko LAVA price. - */ -export async function computeTVL(): Promise<{ tvl: string }> { - const [lavaPrice, pool, rewardPools, subscriptions, osmosis, baseUsd, arbitrumUsd] = - await Promise.all([ - fetchLavaUsdPrice(), - fetchStakingPool(), - fetchTvlRewardPoolsUlava(), - fetchSubscriptionCreditsUlava(), - fetchOsmosisLavaUlava(), - fetchBaseDexUsd(), - fetchArbitrumDexUsd(), - ]); - - const bondedTokens = BigInt(pool.bonded_tokens); - - // Convert DEX USD values to ulava to match jsinfo round-trip - const baseUlava = lavaPrice > 0 - ? BigInt(Math.round((baseUsd / lavaPrice) * 1_000_000)) - : 0n; - const arbitrumUlava = lavaPrice > 0 - ? BigInt(Math.round((arbitrumUsd / lavaPrice) * 1_000_000)) - : 0n; - - // Sum all components in ulava, then convert to USD - // Use BigInt division to avoid Number precision loss for large ulava sums - const totalUlava = bondedTokens + rewardPools + subscriptions - + osmosis + baseUlava + arbitrumUlava; - const totalLava = Number(totalUlava / 1_000_000n) + Number(totalUlava % 1_000_000n) / 1_000_000; - const tvlUsd = totalLava * lavaPrice; - - return { tvl: tvlUsd.toFixed(4) }; -} - -// --- APR calculation (matches jsinfo) --- - -/** Benchmark: 10,000 LAVA in ulava / base units */ -const APR_BENCHMARK_ULAVA = 10_000_000_000; -const APR_BENCHMARK_LAVA = 10_000; -const APR_BENCHMARK_DENOM = "ulava"; -/** 80th percentile, capped at 30% — same thresholds as jsinfo */ -const APR_PERCENTILE = 0.8; -const APR_MAX_PERCENTILE_CAP = 0.3; -const APR_MAX_INDIVIDUAL = 0.8; -const APR_MIN = 1e-11; - -/** Denom → base unit conversion (matching jsinfo DENOM_CONVERSIONS) */ -const DENOM_CONVERSIONS: Record = { - ulava: { baseDenom: "lava", factor: 1_000_000 }, - uatom: { baseDenom: "atom", factor: 1_000_000 }, - uosmo: { baseDenom: "osmo", factor: 1_000_000 }, - ujuno: { baseDenom: "juno", factor: 1_000_000 }, - ustars: { baseDenom: "stars", factor: 1_000_000 }, - uakt: { baseDenom: "akt", factor: 1_000_000 }, - uhuahua: { baseDenom: "huahua", factor: 1_000_000 }, - uevmos: { baseDenom: "evmos", factor: 1e18 }, - inj: { baseDenom: "inj", factor: 1e18 }, - aevmos: { baseDenom: "evmos", factor: 1e18 }, - basecro: { baseDenom: "cro", factor: 1e8 }, - uscrt: { baseDenom: "scrt", factor: 1_000_000 }, - uiris: { baseDenom: "iris", factor: 1_000_000 }, - uregen: { baseDenom: "regen", factor: 1_000_000 }, - uion: { baseDenom: "ion", factor: 1_000_000 }, - nanolike: { baseDenom: "like", factor: 1e9 }, - uaxl: { baseDenom: "axl", factor: 1_000_000 }, - uband: { baseDenom: "band", factor: 1_000_000 }, - ubld: { baseDenom: "bld", factor: 1_000_000 }, - ucmdx: { baseDenom: "cmdx", factor: 1_000_000 }, - ucre: { baseDenom: "cre", factor: 1_000_000 }, - uxprt: { baseDenom: "xprt", factor: 1_000_000 }, - uusdc: { baseDenom: "usdc", factor: 1_000_000 }, - "unit-move": { baseDenom: "move", factor: 1e7 }, -}; - -/** Base denom → CoinGecko coin ID */ -const DENOM_COINGECKO_ID: Record = { - lava: "lava-network", atom: "cosmos", osmo: "osmosis", - juno: "juno-network", stars: "stargaze", akt: "akash-network", - huahua: "chihuahua-token", evmos: "evmos", inj: "injective-protocol", - cro: "crypto-com-chain", scrt: "secret", iris: "iris-network", - regen: "regen", ion: "ion", like: "likecoin", axl: "axelar", - band: "band-protocol", bld: "agoric", cmdx: "comdex", - cre: "crescent-network", xprt: "persistence", usdc: "usd-coin", - move: "movement", -}; - -/** In-memory price cache (5 min TTL) */ -const priceCache = new Map(); -const PRICE_CACHE_MS = 300_000; - -/** - * Pre-warm the price cache with a single batch CoinGecko call for all known denoms. - * CoinGecko supports comma-separated IDs in one request. - */ -export async function prewarmPriceCache(): Promise { - const now = Date.now(); - // Skip if all entries are still fresh - const allFresh = Object.keys(DENOM_COINGECKO_ID).every((d) => { - const c = priceCache.get(d); - return c && now - c.ts < PRICE_CACHE_MS; - }); - if (allFresh) return; - - const ids = [...new Set(Object.values(DENOM_COINGECKO_ID))].join(","); - try { - const res = await fetch( - `https://api.coingecko.com/api/v3/simple/price?ids=${ids}&vs_currencies=usd`, - { signal: AbortSignal.timeout(15_000) }, - ); - if (!res.ok) return; - const data = (await res.json()) as Record; - const ts = Date.now(); - for (const [denom, cgId] of Object.entries(DENOM_COINGECKO_ID)) { - const price = data[cgId]?.usd ?? 0; - if (price > 0) priceCache.set(denom, { price, ts }); - } - } catch { - // Keep stale cache on failure - } -} - -/** Fetch USD price for a base denom — reads from cache (pre-warmed by prewarmPriceCache) */ -async function fetchTokenUsdPrice(baseDenom: string): Promise { - const cached = priceCache.get(baseDenom); - if (cached && Date.now() - cached.ts < PRICE_CACHE_MS) return cached.price; - - // Fallback: single-denom fetch if cache miss (shouldn't happen after prewarm) - const id = DENOM_COINGECKO_ID[baseDenom]; - if (!id) return 0; - - try { - const res = await fetch( - `https://api.coingecko.com/api/v3/simple/price?ids=${id}&vs_currencies=usd`, - { signal: AbortSignal.timeout(15_000) }, - ); - if (!res.ok) return cached?.price ?? 0; - const data = (await res.json()) as Record; - const price = data[id]?.usd ?? 0; - if (price > 0) priceCache.set(baseDenom, { price, ts: Date.now() }); - return price; - } catch { - return cached?.price ?? 0; - } -} - -/** Resolve an IBC hash to its base denom via chain RPC */ -async function fetchDenomTrace(ibcHash: string): Promise { - try { - const data = await fetchRest<{ - denom_trace: { base_denom: string }; - }>(`/ibc/apps/transfer/v1/denom_traces/${ibcHash}`); - return data.denom_trace?.base_denom ?? null; - } catch { - return null; - } -} - -interface EstimatedRewardsResponse { - info: Array<{ - source: string; - amount: { denom: string; amount: string } | Array<{ denom: string; amount: string }>; - }>; - total: Array<{ denom: string; amount: string }>; - recommended_block?: string; -} - -/** IBC test denoms to skip (matching jsinfo) */ -const TEST_DENOMS = new Set([ - "ibc/E3FCBEDDBAC500B1BAB90395C7D1E4F33D9B9ECFE82A16ED7D7D141A0152323F", -]); - -/** Token breakdown matching jsinfo RewardAmount */ -export interface RewardToken { - source_denom: string; - resolved_amount: string; - resolved_denom: string; - display_denom: string; - display_amount: string; - value_usd: string; -} - -export interface ProcessedRewards { - totalUsd: number; - tokens: RewardToken[]; -} - -/** Convert multi-denom reward array to USD total + per-token breakdown */ -async function processRewardTokens( - rewards: Array<{ denom: string; amount: string }>, -): Promise { - const tokens: RewardToken[] = []; - let totalUsd = 0; - - for (const { denom, amount } of rewards) { - if (TEST_DENOMS.has(denom)) continue; - - // Resolve IBC denoms to their base denom - let rawDenom = denom; - let resolvedDenom = denom; - if (denom.startsWith("ibc/")) { - const resolved = await fetchDenomTrace(denom.slice(4)); - if (!resolved) continue; - rawDenom = resolved; - resolvedDenom = resolved; - } - - const conversion = DENOM_CONVERSIONS[rawDenom]; - if (!conversion) continue; - - const displayAmount = divideByFactor(amount, conversion.factor); - const baseAmount = parseFloat(displayAmount); - if (!isFinite(baseAmount) || baseAmount <= 0) continue; - - const price = await fetchTokenUsdPrice(conversion.baseDenom); - const usd = price > 0 ? baseAmount * price : 0; - totalUsd += usd; - - tokens.push({ - source_denom: denom, - resolved_amount: formatTokenStr(amount), - resolved_denom: resolvedDenom, - display_denom: conversion.baseDenom, - display_amount: displayAmount, - value_usd: `$${formatTokenStr(usd.toFixed(14))}`, - }); - } - - return { totalUsd, tokens }; -} - -/** Strip trailing zeros from a decimal string (matching jsinfo FormatTokenAmount) */ -function formatTokenStr(s: string): string { - const [whole, frac] = s.split("."); - if (!frac) return whole; - const trimmed = frac.replace(/0+$/, ""); - return trimmed ? `${whole}.${trimmed}` : whole; -} - -/** String-based division to avoid floating-point noise for token amounts. - * Shifts the decimal point left by the number of digits in `divisor`. - * e.g. divideStr("23584370", 1_000_000) → "23.584370" → "23.58437" */ -function divideByFactor(raw: string, factor: number): string { - // Strip any existing decimal (RPC may return "123.000000000000000000") - const cleaned = formatTokenStr(raw); - const digits = Math.round(Math.log10(factor)); - const [intPart, decPart = ""] = cleaned.split("."); - const combined = intPart + decPart; - const shiftedDecPos = combined.length - digits - decPart.length; - - // Pad with leading zeros if the number is smaller than the factor - const padded = shiftedDecPos <= 0 - ? "0." + "0".repeat(-shiftedDecPos) + combined - : combined.slice(0, shiftedDecPos) + "." + combined.slice(shiftedDecPos); - - return formatTokenStr(padded || "0"); -} - -/** Fetch estimated rewards for a provider/validator — returns USD total + token breakdown */ -async function fetchEstimatedRewards( - type: "provider" | "validator", - address: string, -): Promise { - try { - const data = await fetchRest( - `/lavanet/lava/subscription/estimated_${type}_rewards/${address}/${APR_BENCHMARK_ULAVA}${APR_BENCHMARK_DENOM}`, - ); - return await processRewardTokens(data.total ?? []); - } catch { - return { totalUsd: 0, tokens: [] }; - } -} - -/** - * Fetch a provider's actual earned rewards (no benchmark amount) and group by spec. - * Matches jsinfo's rewards_last_month: calls estimated_provider_rewards/{addr}/ - * then splits "Boost: ETH1", "Pools: ETH1", "Subscription: ETH1" into per-spec groups. - * - * Pass blockHeight to query historical chain state (archive node required). - * Note: USD values still use current CoinGecko prices even for historical blocks — - * matches the old Job 2 behavior where CoinGecko was called at pipeline-run time. - */ -export async function fetchRewardsBySpec( - address: string, - specNameMap: Map, - blockHeight?: number, -): Promise { - try { - const data = await fetchRest( - `/lavanet/lava/subscription/estimated_provider_rewards/${address}/`, - blockHeight, - ); - - // Group info entries by spec (collapse Boost/Pools/Subscription sources) - const bySpec = new Map; totalUsd: number }>(); - - for (const entry of data.info ?? []) { - const parts = (entry.source as string).split(": "); - const spec = parts.length > 1 ? parts[1] : parts[0]; - if (!spec) continue; - - const key = spec.toLowerCase(); - const group = bySpec.get(key) ?? { tokens: new Map(), totalUsd: 0 }; - - const amounts = Array.isArray(entry.amount) ? entry.amount : [entry.amount]; - for (const coin of amounts) { - if (TEST_DENOMS.has(coin.denom)) continue; - const existing = group.tokens.get(coin.denom); - const amt = parseFloat(coin.amount) || 0; - if (existing) { - existing.amount += amt; - } else { - group.tokens.set(coin.denom, { amount: amt, denom: coin.denom }); - } - } - - bySpec.set(key, group); - } - - // Convert to output format with USD values - const results: RewardsBySpecEntry[] = []; - for (const [specKey, group] of bySpec) { - const tokens: RewardToken[] = []; - let specUsd = 0; - - for (const [, coin] of group.tokens) { - const processed = await processRewardTokens([ - { denom: coin.denom, amount: coin.amount.toString() }, - ]); - tokens.push(...processed.tokens); - specUsd += processed.totalUsd; - } - - results.push({ - chain: specNameMap.get(specKey.toUpperCase()) ?? specKey.toUpperCase(), - spec: specKey.toUpperCase(), - tokens, - total_usd: specUsd, - }); - } - - return results; - } catch { - return []; - } -} - -// ── Claimable rewards (jsinfo-shape) ───────────────────────────────────────── - -export interface ClaimableRewardEntry { - amount: string; // display amount in base units (e.g. "1.23" lava) - denom: string; // base denom (e.g. "lava") - usdcValue: string; // numeric USD string (no $) - provider: string; -} - -/** Convert multi-denom claimable rewards to jsinfo-shape entries. */ -export async function processClaimableRewards( - rewards: Array<{ denom: string; amount: string }>, - provider: string, -): Promise { - const processed = await processRewardTokens(rewards); - return processed.tokens.map((t) => ({ - amount: t.display_amount, - denom: t.display_denom, - usdcValue: t.value_usd.startsWith("$") ? t.value_usd.slice(1) : t.value_usd, - provider, - })); -} - -// ── Validators & rewards (jsinfo-shape) ────────────────────────────────────── - -export interface ValidatorCommissionRates { - rate: string; - max_rate: string; - max_change_rate: string; -} - -export interface ValidatorCommission { - commission_rates: ValidatorCommissionRates; - update_time: string; -} - -export interface ValidatorBase { - operator_address: string; - description?: { moniker?: string }; - jailed: boolean; - tokens: string; - commission: ValidatorCommission; -} - -interface RewardCoin { denom: string; amount: string } - -export interface TokenBreakdown { - tokens: RewardToken[]; - total_usd: number; -} - -export interface ValidatorDistributionRewards { - self_bond_rewards: TokenBreakdown; - commission: TokenBreakdown; - operator_address: string; -} - -export interface ValidatorDelegationResponse { - delegation: { - delegator_address: string; - validator_address: string; - shares: string; - }; - balance: RewardCoin; -} - -export interface ValidatorUnbondingEntry { - creation_height: string; - completion_time: string; - initial_balance: string; - balance: string; - unbonding_id: string; - unbonding_on_hold_ref_count: string; -} - -export interface ValidatorUnbondingResponse { - delegator_address: string; - validator_address: string; - entries: ValidatorUnbondingEntry[]; -} - -export interface ValidatorWithRewards { - address: string; - moniker: string; - jailed: boolean; - tokens: string; - commission: ValidatorCommission; - apr?: number | null; - distribution: ValidatorDistributionRewards; - outstanding_rewards: TokenBreakdown; - estimated_rewards: TokenBreakdown; - delegations: { - delegation_responses: ValidatorDelegationResponse[]; - pagination: { next_key: string | null; total: string }; - }; - unbonding_delegations: { - unbonding_responses: ValidatorUnbondingResponse[]; - pagination: { next_key: string | null; total: string }; - }; -} - -const EMPTY_BREAKDOWN: TokenBreakdown = { tokens: [], total_usd: 0 }; - -async function rewardsToBreakdown(rewards: RewardCoin[] | undefined): Promise { - if (!rewards || rewards.length === 0) return EMPTY_BREAKDOWN; - const processed = await processRewardTokens(rewards); - return { tokens: processed.tokens, total_usd: processed.totalUsd }; -} - -async function fetchAllValidatorObjects(): Promise { - const validators: ValidatorBase[] = []; - let nextKey: string | null = null; - do { - const params = new URLSearchParams({ - status: "BOND_STATUS_BONDED", - "pagination.limit": "200", - }); - if (nextKey) params.set("pagination.key", nextKey); - const data = await fetchRest<{ - validators: ValidatorBase[]; - pagination: { next_key: string | null }; - }>(`/cosmos/staking/v1beta1/validators?${params}`); - for (const v of data.validators ?? []) validators.push(v); - nextKey = data.pagination?.next_key ?? null; - } while (nextKey); - return validators; -} - -interface ValidatorDistributionRewardsResponse { - operator_address: string; - self_bond_rewards: RewardCoin[]; - commission: RewardCoin[]; -} - -async function fetchValidatorDistribution( - addr: string, -): Promise { - try { - return await fetchRest( - `/cosmos/distribution/v1beta1/validators/${addr}`, - ); - } catch { return null; } -} - -async function fetchValidatorOutstanding(addr: string): Promise { - try { - const data = await fetchRest<{ rewards: { rewards: RewardCoin[] } }>( - `/cosmos/distribution/v1beta1/validators/${addr}/outstanding_rewards`, - ); - return data.rewards?.rewards ?? []; - } catch { return []; } -} - -async function fetchValidatorEstimatedTotal(addr: string): Promise { - try { - const data = await fetchRest( - `/lavanet/lava/subscription/estimated_validator_rewards/${addr}/`, - ); - return data.total ?? []; - } catch { return []; } -} - -async function fetchValidatorDelegationsPage( - addr: string, -): Promise<{ delegation_responses: ValidatorDelegationResponse[]; pagination: { next_key: string | null; total: string } }> { - try { - return await fetchRest(`/cosmos/staking/v1beta1/validators/${addr}/delegations?pagination.limit=1000&pagination.count_total=true`); - } catch { - return { delegation_responses: [], pagination: { next_key: null, total: "0" } }; - } -} - -async function fetchValidatorUnbondingPage( - addr: string, -): Promise<{ unbonding_responses: ValidatorUnbondingResponse[]; pagination: { next_key: string | null; total: string } }> { - try { - return await fetchRest(`/cosmos/staking/v1beta1/validators/${addr}/unbonding_delegations?pagination.limit=1000&pagination.count_total=true`); - } catch { - return { unbonding_responses: [], pagination: { next_key: null, total: "0" } }; - } -} - -/** - * Build the full validator-with-rewards list matching jsinfo's - * /lava_mainnet_validators_and_rewards response shape. - * - * Each validator requires 5 chain RPC calls; processes 3 validators at a time - * (15 concurrent requests) to respect rate limits. With ~50 validators this - * completes in ~20-30s cold, well within the 6h cache window. - */ -export async function fetchValidatorsWithRewards(): Promise<{ - height: number; - datetime: number; - validators: ValidatorWithRewards[]; -}> { - await prewarmPriceCache(); - - const [validators, latestBlock] = await Promise.all([ - fetchAllValidatorObjects(), - fetchLatestBlockHeight(), - ]); - - const enriched: ValidatorWithRewards[] = []; - - for (let i = 0; i < validators.length; i += RPC_BATCH_SIZE) { - const batch = validators.slice(i, i + RPC_BATCH_SIZE); - const results = await Promise.all(batch.map(async (v) => { - const addr = v.operator_address; - const [dist, outstanding, estimatedTotal, delegations, unbonding] = await Promise.all([ - fetchValidatorDistribution(addr), - fetchValidatorOutstanding(addr), - fetchValidatorEstimatedTotal(addr), - fetchValidatorDelegationsPage(addr), - fetchValidatorUnbondingPage(addr), - ]); - - const [selfBond, commissionBreakdown, outstandingBreakdown, estimatedBreakdown] = await Promise.all([ - rewardsToBreakdown(dist?.self_bond_rewards), - rewardsToBreakdown(dist?.commission), - rewardsToBreakdown(outstanding), - rewardsToBreakdown(estimatedTotal), - ]); - - return { - address: addr, - moniker: v.description?.moniker ?? "", - jailed: v.jailed, - tokens: v.tokens, - commission: v.commission, - distribution: { - self_bond_rewards: selfBond, - commission: commissionBreakdown, - operator_address: dist?.operator_address ?? addr, - }, - outstanding_rewards: outstandingBreakdown, - estimated_rewards: estimatedBreakdown, - delegations, - unbonding_delegations: unbonding, - } satisfies ValidatorWithRewards; - })); - enriched.push(...results); - } - - return { - height: latestBlock.height, - datetime: Math.floor(new Date(latestBlock.time).getTime() / 1000), - validators: enriched, - }; -} - -/** Fetch all bonded validator operator addresses */ -export async function fetchBondedValidators(): Promise { - const validators: string[] = []; - let nextKey: string | null = null; - do { - const params = new URLSearchParams({ - status: "BOND_STATUS_BONDED", - "pagination.limit": "200", - }); - if (nextKey) params.set("pagination.key", nextKey); - const data = await fetchRest<{ - validators: Array<{ operator_address: string }>; - pagination: { next_key: string | null }; - }>(`/cosmos/staking/v1beta1/validators?${params}`); - for (const v of data.validators ?? []) validators.push(v.operator_address); - nextKey = data.pagination?.next_key ?? null; - } while (nextKey); - return validators; -} - -/** Percentile calculation matching jsinfo `CalculatePercentile` */ -function calculatePercentile(values: number[], rank: number): number { - if (values.length === 0 || rank < 0 || rank > 1) return 0; - if (values.length === 1) return values[0]; - - const sorted = [...values].sort((a, b) => a - b); - const pos = Math.floor((sorted.length - 1) * rank); - - if (sorted.length % 2 === 0) { - return sorted[pos] + (sorted[pos + 1] - sorted[pos]) * rank; - } - return sorted[pos]; -} - -/** APR from monthly reward: (1 + rewardUsd/investedUsd)^12 - 1 */ -function calculateApr(rewardUsd: number, investedUsd: number): number { - if (investedUsd <= 0 || rewardUsd <= 0) return 0; - const rate = rewardUsd / investedUsd; - const apr = Math.pow(1 + rate, 12) - 1; - if (!isFinite(apr) || apr < APR_MIN || apr > 100) return 0; - return apr; -} - -// --- Weighted APR history (Redis, matches jsinfo AprWeighted) --- - -const APR_WEIGHTS = [0.4, 0.25, 0.15, 0.1, 0.05, 0.03, 0.02]; -const APR_DAYS_TO_KEEP = 7; -const APR_HISTORY_TTL = 30 * 24 * 60 * 60; // 30 days - -interface AprRecord { date: string; aprSum: number; count: number } -interface AprHistory { records: AprRecord[]; lastUpdated: string } - -function aprHistoryKey(type: string, address: string): string { - return `apr_history:${type}:${address}`; -} - -function todayStr(): string { - const d = new Date(); - return `${d.getFullYear()}/${String(d.getMonth() + 1).padStart(2, "0")}/${String(d.getDate()).padStart(2, "0")}`; -} - -async function storeApr(redis: Redis, type: string, address: string, apr: number): Promise { - if (apr === 0) return; - const key = aprHistoryKey(type, address); - const today = todayStr(); - try { - const raw = await redis.get(key); - const history: AprHistory = raw ? JSON.parse(raw) : { records: [], lastUpdated: today }; - - const rec = history.records.find((r) => r.date === today); - if (rec) { rec.aprSum += apr; rec.count += 1; } - else { history.records.push({ date: today, aprSum: apr, count: 1 }); } - - history.records = history.records - .sort((a, b) => b.date.localeCompare(a.date)) - .slice(0, APR_DAYS_TO_KEEP); - history.lastUpdated = today; - - await redis.set(key, JSON.stringify(history), "EX", APR_HISTORY_TTL); - } catch { /* Redis failures are non-fatal */ } -} - -async function getWeightedApr(redis: Redis, type: string, address: string): Promise { - try { - const raw = await redis.get(aprHistoryKey(type, address)); - if (!raw) return null; - const history: AprHistory = JSON.parse(raw); - if (history.records.length === 0) return null; - - let weightedSum = 0; - let weightSum = 0; - for (let i = 0; i < history.records.length && i < APR_WEIGHTS.length; i++) { - const avg = history.records[i].aprSum / history.records[i].count; - weightedSum += avg * APR_WEIGHTS[i]; - weightSum += APR_WEIGHTS[i]; - } - return weightSum > 0 ? weightedSum / weightSum : null; - } catch { - return null; - } -} - -/** - * Compute APR percentiles matching jsinfo `/apr` response. - * - * For each active provider and bonded validator: - * 1. Query `estimated_{type}_rewards` with a 10 000 LAVA benchmark - * 2. Convert multi-denom rewards to USD via CoinGecko - * 3. Compute APR via monthly compounding: (1 + monthlyRate)^12 - 1 - * 4. Store in 7-day weighted history (Redis, if available) - * 5. Take 80th percentile, cap at 30% - */ -export async function computeAPR(redis?: Redis | null): Promise<{ - restaking_apr_percentile: number; - staking_apr_percentile: number; -}> { - // Pre-warm all token prices in a single CoinGecko call - await prewarmPriceCache(); - - const lavaPrice = await fetchTokenUsdPrice("lava"); - const investedUsd = APR_BENCHMARK_LAVA * lavaPrice; - - const [providers, validators] = await Promise.all([ - fetchAllProviders(), - fetchBondedValidators(), - ]); - - const providerAddresses = providers.map((p) => p.address); - - // Collect per-entity APRs (batched by RPC_BATCH_SIZE) - const [providerAprs, validatorAprs] = await Promise.all([ - collectEntityAprs("provider", providerAddresses, investedUsd, redis), - collectEntityAprs("validator", validators, investedUsd, redis), - ]); - - return { - restaking_apr_percentile: Math.min( - calculatePercentile(providerAprs, APR_PERCENTILE), - APR_MAX_PERCENTILE_CAP, - ), - staking_apr_percentile: Math.min( - calculatePercentile(validatorAprs, APR_PERCENTILE), - APR_MAX_PERCENTILE_CAP, - ), - }; -} - -/** Batch-fetch estimated rewards, compute APR per entity, apply weighted history */ -async function collectEntityAprs( - type: "provider" | "validator", - addresses: string[], - investedUsd: number, - redis?: Redis | null, -): Promise { - const aprs: number[] = []; - - for (let i = 0; i < addresses.length; i += RPC_BATCH_SIZE) { - const batch = addresses.slice(i, i + RPC_BATCH_SIZE); - const rewards = await Promise.all( - batch.map((addr) => fetchEstimatedRewards(type, addr)), - ); - - for (let j = 0; j < batch.length; j++) { - const currentApr = calculateApr(rewards[j].totalUsd, investedUsd); - if (currentApr <= 0) continue; - - let finalApr = currentApr; - if (redis) { - await storeApr(redis, type, batch[j], currentApr); - const weighted = await getWeightedApr(redis, type, batch[j]); - if (weighted !== null) finalApr = weighted; - } - - if (finalApr > 0 && finalApr < APR_MAX_INDIVIDUAL) { - aprs.push(finalApr); - } - } - } - - return aprs; -} - -/** Max display APR for individual providers (matching jsinfo) */ -const MAX_DISPLAY_APR = 0.9; - -/** Format APR as percentage string matching jsinfo format */ -function formatAprPercent(apr: number): string { - if (apr <= 0) return "-"; - if (apr > MAX_DISPLAY_APR) return "90.0%"; - return `${(apr * 100).toFixed(4)}%`; -} - -/** Format commission as percentage string */ -function formatCommission(commission: string): string { - if (!commission) return "-"; - const n = parseFloat(commission); - if (!isFinite(n)) return "-"; - return `${n.toFixed(1)}%`; -} - -interface ProviderSpecEntry { - chain: string; - spec: string; - stakestatus: string; - stake: string; - addons: string; - extensions: string; - delegateCommission: string; - delegateTotal: string; - moniker: string; -} - -export interface RewardsBySpecEntry { - chain: string; - spec: string; - tokens: RewardToken[]; - total_usd: number; -} - -export interface AllProviderAprEntry { - address: string; - moniker: string; - apr: string; - commission: string; - "30_days_cu_served": string; - "30_days_relays_served": string; - rewards_10k_lava_delegation: RewardToken[]; - rewards_last_month: RewardsBySpecEntry[]; - specs: ProviderSpecEntry[]; - stake: string; - stakestatus: string; - addons: string; - extensions: string; - delegateTotal: string; - avatar: string | null; -} - -/** Build per-provider spec data + address list + spec name map from chain RPC */ -export async function fetchProvidersWithSpecs(): Promise<{ - providers: Map; - specNames: Map; -}> { - const allSpecs = await fetchAllSpecs(); - const specNames = new Map(allSpecs.map((s) => [s.index, s.name])); - const byAddress = new Map< - string, - { moniker: string; identity: string; commission: string; specs: ProviderSpecEntry[] } - >(); - - for (let i = 0; i < allSpecs.length; i += RPC_BATCH_SIZE) { - const batch = allSpecs.slice(i, i + RPC_BATCH_SIZE); - const results = await Promise.all( - batch.map((s) => fetchProvidersForSpec(s.index) - .then((ps) => ps.map((p) => ({ ...p, specId: s.index, specName: s.name }))) - .catch(() => [] as Array)), - ); - - for (const providers of results) { - for (const p of providers) { - const existing = byAddress.get(p.address); - const specEntry: ProviderSpecEntry = { - chain: p.specName, - spec: p.specId, - stakestatus: "Active", - stake: p.stake?.amount ?? "0", - addons: p.addons, - extensions: p.extensions, - delegateCommission: p.delegate_commission, - delegateTotal: p.delegate_total?.amount ?? "0", - moniker: p.moniker, - }; - - if (existing) { - existing.specs.push(specEntry); - if (!existing.commission && p.delegate_commission) existing.commission = p.delegate_commission; - if (!existing.identity && p.identity) existing.identity = p.identity; - } else { - byAddress.set(p.address, { - moniker: p.moniker ?? "", - identity: p.identity ?? "", - commission: p.delegate_commission ?? "", - specs: [specEntry], - }); - } - } - } - } - - return { providers: byAddress, specNames }; -} - -/** - * Compute per-provider APR data matching jsinfo `/all_providers_apr`. - * - * Returns array of provider objects with APR, commission, 30d relay data, - * per-token reward breakdown, specs, and avatar. - */ -export async function computeAllProvidersApr( - relay30d: Map, - redis?: Redis | null, -): Promise { - // Pre-warm all token prices in a single CoinGecko call - await prewarmPriceCache(); - - const lavaPrice = await fetchTokenUsdPrice("lava"); - const investedUsd = APR_BENCHMARK_LAVA * lavaPrice; - - const { providers: providerMap, specNames } = await fetchProvidersWithSpecs(); - const addresses = Array.from(providerMap.keys()); - const results: AllProviderAprEntry[] = []; - - for (let i = 0; i < addresses.length; i += RPC_BATCH_SIZE) { - const batch = addresses.slice(i, i + RPC_BATCH_SIZE); - const [rewardResults, rewardsLastMonthResults, avatarResults] = await Promise.all([ - Promise.all(batch.map((addr) => fetchEstimatedRewards("provider", addr))), - Promise.all(batch.map((addr) => fetchRewardsBySpec(addr, specNames))), - Promise.all(batch.map((addr) => { - const p = providerMap.get(addr)!; - return fetchProviderAvatar(addr, p.identity || undefined).catch(() => null); - })), - ]); - - for (let j = 0; j < batch.length; j++) { - const addr = batch[j]; - const provider = providerMap.get(addr)!; - const rewards = rewardResults[j]; - const rewardsLastMonth = rewardsLastMonthResults[j]; - const avatar = avatarResults[j]; - const currentApr = calculateApr(rewards.totalUsd, investedUsd); - - let finalApr = currentApr; - if (redis && currentApr > 0) { - await storeApr(redis, "provider", addr, currentApr); - const weighted = await getWeightedApr(redis, "provider", addr); - if (weighted !== null) finalApr = weighted; - } - - const relay = relay30d.get(addr); - const firstSpec = provider.specs[0]; - - results.push({ - address: addr, - moniker: provider.moniker || "-", - apr: formatAprPercent(finalApr), - commission: formatCommission(provider.commission), - "30_days_cu_served": relay?.cu ?? "-", - "30_days_relays_served": relay?.relays ?? "-", - rewards_10k_lava_delegation: rewards.tokens, - rewards_last_month: rewardsLastMonth, - specs: provider.specs, - stake: firstSpec?.stake ?? "", - stakestatus: firstSpec ? "Active" : "", - addons: firstSpec?.addons ?? "", - extensions: firstSpec?.extensions ?? "", - delegateTotal: firstSpec?.delegateTotal ?? "", - avatar, - }); - } - } - - return results; -} - -export async function fetchLatestBlockHeight(): Promise<{ - height: number; - time: string; -}> { - const LAVA_RPC_URL = process.env.LAVA_RPC_URL ?? "https://lava.tendermintrpc.lava.build:443"; - const res = await fetch(`${LAVA_RPC_URL}/status`, { - signal: AbortSignal.timeout(10_000), - }); - if (!res.ok) throw new Error(`RPC ${res.status}`); - const data = (await res.json()) as { - result: { sync_info: { latest_block_height: string; latest_block_time: string } }; - }; - return { - height: parseInt(data.result.sync_info.latest_block_height, 10), - time: data.result.sync_info.latest_block_time, - }; -} - -// Request coalescing: concurrent callers share one in-flight fetchAllProviders -let pendingProviders: Promise | null = null; - -type AllProvidersResult = Array<{ - address: string; - moniker: string; - identity: string; - totalStake: string; - totalDelegation: string; - commission: string; - specs: string[]; -}>; - -/** Fetch all providers across all specs, with dedup by address. - * Concurrent callers share one in-flight request (coalesced). */ -export function fetchAllProviders(): Promise { - if (pendingProviders) return pendingProviders; - pendingProviders = fetchAllProvidersImpl().finally(() => { pendingProviders = null; }); - return pendingProviders; -} - -async function fetchAllProvidersImpl(): Promise { - const specs = await fetchAllSpecs(); - - interface SpecEntry { - address: string; - moniker: string; - identity: string; - stake?: { amount: string }; - delegate_total?: { amount: string }; - delegate_commission: string; - specId: string; - } - - // Fetch in batches of RPC_BATCH_SIZE — raise via env for dedicated RPC endpoints. - const specProviders: Array> = []; - 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( - batch.map((s) => - fetchProvidersForSpec(s.index) - .then((providers) => providers.map((p): SpecEntry => ({ - address: p.address, - moniker: p.moniker, - identity: p.identity, - stake: p.stake, - delegate_total: p.delegate_total, - delegate_commission: p.delegate_commission, - specId: s.index, - }))) - .catch(() => [] as SpecEntry[]), - ), - ); - specProviders.push(...results); - } - - const byAddress = new Map< - string, - { moniker: string; identity: string; totalStake: bigint; totalDelegation: bigint; commission: string; specs: string[] } - >(); - - for (const providers of specProviders) { - for (const p of providers) { - const existing = byAddress.get(p.address); - const stake = BigInt(p.stake?.amount ?? "0"); - const delegation = BigInt(p.delegate_total?.amount ?? "0"); - if (existing) { - existing.totalStake += stake; - existing.totalDelegation += delegation; - existing.specs.push(p.specId); - if (!existing.commission && p.delegate_commission) { - existing.commission = p.delegate_commission; - } - if (!existing.identity && p.identity) { - existing.identity = p.identity; - } - } else { - byAddress.set(p.address, { - moniker: p.moniker ?? "", - identity: p.identity ?? "", - totalStake: stake, - totalDelegation: delegation, - commission: p.delegate_commission ?? "", - specs: [p.specId], - }); - } - } - } - - return Array.from(byAddress.entries()).map(([address, data]) => ({ - address, - moniker: data.moniker, - identity: data.identity, - totalStake: data.totalStake.toString(), - totalDelegation: data.totalDelegation.toString(), - commission: data.commission, - specs: data.specs, - })); -} - -export async function fetchDelegatorRewards(provider: string): Promise< - Array<{ denom: string; amount: string }> -> { - try { - const data = await fetchRest<{ - rewards: Array<{ provider: string; amount: Array<{ denom: string; amount: string }> }>; - }>(`/lavanet/lava/dualstaking/delegator_rewards/${provider}`); - const entry = data.rewards?.find((r) => r.provider === provider); - return entry?.amount ?? []; - } catch { - return []; - } -} - -async function fetchProviderMetadata(provider: string): Promise<{ - description?: { identity?: string; moniker?: string }; -} | null> { - try { - const specs = await fetchAllSpecs(); - for (const spec of specs) { - try { - const data = await fetchRest<{ - stakeEntry: Array<{ - address: string; - description?: { identity?: string; moniker?: string }; - }>; - }>(`/lavanet/lava/pairing/providers/${spec.index}`); - const match = data.stakeEntry?.find((p) => p.address === provider); - if (match?.description?.identity) return match; - } catch { - continue; - } - } - return null; - } catch { - return null; - } -} - -export async function fetchProviderAvatar(provider: string, identityHint?: string): Promise { - try { - let identity = identityHint; - if (!identity) { - const meta = await fetchProviderMetadata(provider); - identity = meta?.description?.identity ?? undefined; - } - if (!identity) return null; - - const KEYBASE_API_URL = process.env.KEYBASE_API_URL ?? "https://keybase.io/_/api/1.0"; - const res = await fetch( - `${KEYBASE_API_URL}/user/lookup.json?key_suffix=${encodeURIComponent(identity)}&fields=pictures`, - { signal: AbortSignal.timeout(10_000) }, - ); - if (!res.ok) return null; - const data = (await res.json()) as { - them?: Array<{ pictures?: { primary?: { url?: string } } }>; - }; - return data.them?.[0]?.pictures?.primary?.url ?? null; - } catch { - return null; - } -} +// Thin barrel: lava.ts was a 1,673-line god-module covering 10 distinct +// concerns. It's now split into focused modules below; this barrel preserves +// the original import surface so existing callers don't need to change. +// New code should import directly from the focused modules. + +export { RPC_BATCH_SIZE } from "./rest.js"; +export { fetchBlockAtTimestamp, fetchLatestBlockHeight } from "./block.js"; +export { fetchAllSpecs } from "./specs.js"; +export { fetchTotalSupply, fetchCirculatingSupply } from "./supply.js"; +export { + type ProviderEndpoint, + type ProviderForSpec, + type AllProvidersResult, + type ProviderSpecEntry, + fetchProvidersForSpec, + fetchProvidersWithSpecs, + fetchAllProviders, + fetchAllProviderMonikers, + fetchDelegatorRewards, +} from "./providers.js"; +export { + fetchLavaUsdPrice, + prewarmPriceCache, + computeTVL, +} from "./pricing.js"; +export { + type RewardToken, + type ProcessedRewards, + type RewardsBySpecEntry, + type ClaimableRewardEntry, + type TokenBreakdown, + fetchRewardsBySpec, + processClaimableRewards, +} from "./rewards.js"; +export { + type ValidatorCommissionRates, + type ValidatorCommission, + type ValidatorBase, + type ValidatorDistributionRewards, + type ValidatorDelegationResponse, + type ValidatorUnbondingEntry, + type ValidatorUnbondingResponse, + type ValidatorWithRewards, + fetchValidatorsWithRewards, +} from "./validators.js"; +export { + type AllProviderAprEntry, + computeAPR, + computeAllProvidersApr, +} from "./apr.js"; +export { fetchProviderAvatar } from "./keybase.js"; diff --git a/apps/api/src/rpc/pricing.ts b/apps/api/src/rpc/pricing.ts new file mode 100644 index 0000000..1497316 --- /dev/null +++ b/apps/api/src/rpc/pricing.ts @@ -0,0 +1,303 @@ +import { ulavaToLavaNumber } from "@info/shared/utils"; +import { config } from "../config.js"; +import { fetchRest } from "./rest.js"; +import { fetchStakingPool } from "./supply.js"; + +/** In-memory price cache (5 min TTL) — shared across fetchLavaUsdPrice + fetchTokenUsdPrice + prewarmPriceCache */ +const priceCache = new Map(); +const PRICE_CACHE_MS = 300_000; + +// Coalesce concurrent fetchLavaUsdPrice() callers on a cache miss so +// multiple routes starting in parallel share a single CoinGecko request. +let pendingLavaPrice: Promise | null = null; + +export async function fetchLavaUsdPrice(): Promise { + const cached = priceCache.get("lava"); + if (cached && Date.now() - cached.ts < PRICE_CACHE_MS) return cached.price; + if (pendingLavaPrice) return pendingLavaPrice; + + pendingLavaPrice = (async () => { + const res = await fetch( + `${config.external.coingeckoApiUrl}/simple/price?ids=lava-network&vs_currencies=usd`, + { signal: AbortSignal.timeout(15_000) }, + ); + if (!res.ok) throw new Error(`CoinGecko ${res.status}`); + const data = (await res.json()) as { "lava-network"?: { usd?: number } }; + const price = data["lava-network"]?.usd; + if (!price || price <= 0) throw new Error("Invalid LAVA price from CoinGecko"); + priceCache.set("lava", { price, ts: Date.now() }); + return price; + })().finally(() => { pendingLavaPrice = null; }); + + return pendingLavaPrice; +} + +/** Denom → base unit conversion (matching jsinfo DENOM_CONVERSIONS) */ +export const DENOM_CONVERSIONS: Record = { + ulava: { baseDenom: "lava", factor: 1_000_000 }, + uatom: { baseDenom: "atom", factor: 1_000_000 }, + uosmo: { baseDenom: "osmo", factor: 1_000_000 }, + ujuno: { baseDenom: "juno", factor: 1_000_000 }, + ustars: { baseDenom: "stars", factor: 1_000_000 }, + uakt: { baseDenom: "akt", factor: 1_000_000 }, + uhuahua: { baseDenom: "huahua", factor: 1_000_000 }, + uevmos: { baseDenom: "evmos", factor: 1e18 }, + inj: { baseDenom: "inj", factor: 1e18 }, + aevmos: { baseDenom: "evmos", factor: 1e18 }, + basecro: { baseDenom: "cro", factor: 1e8 }, + uscrt: { baseDenom: "scrt", factor: 1_000_000 }, + uiris: { baseDenom: "iris", factor: 1_000_000 }, + uregen: { baseDenom: "regen", factor: 1_000_000 }, + uion: { baseDenom: "ion", factor: 1_000_000 }, + nanolike: { baseDenom: "like", factor: 1e9 }, + uaxl: { baseDenom: "axl", factor: 1_000_000 }, + uband: { baseDenom: "band", factor: 1_000_000 }, + ubld: { baseDenom: "bld", factor: 1_000_000 }, + ucmdx: { baseDenom: "cmdx", factor: 1_000_000 }, + ucre: { baseDenom: "cre", factor: 1_000_000 }, + uxprt: { baseDenom: "xprt", factor: 1_000_000 }, + uusdc: { baseDenom: "usdc", factor: 1_000_000 }, + "unit-move": { baseDenom: "move", factor: 1e7 }, +}; + +/** Base denom → CoinGecko coin ID */ +export const DENOM_COINGECKO_ID: Record = { + lava: "lava-network", atom: "cosmos", osmo: "osmosis", + juno: "juno-network", stars: "stargaze", akt: "akash-network", + huahua: "chihuahua-token", evmos: "evmos", inj: "injective-protocol", + cro: "crypto-com-chain", scrt: "secret", iris: "iris-network", + regen: "regen", ion: "ion", like: "likecoin", axl: "axelar", + band: "band-protocol", bld: "agoric", cmdx: "comdex", + cre: "crescent-network", xprt: "persistence", usdc: "usd-coin", + move: "movement", +}; + +/** + * Pre-warm the price cache with a single batch CoinGecko call for all known denoms. + * CoinGecko supports comma-separated IDs in one request. + */ +export async function prewarmPriceCache(): Promise { + const now = Date.now(); + // Skip if all entries are still fresh + const allFresh = Object.keys(DENOM_COINGECKO_ID).every((d) => { + const c = priceCache.get(d); + return c && now - c.ts < PRICE_CACHE_MS; + }); + if (allFresh) return; + + const ids = [...new Set(Object.values(DENOM_COINGECKO_ID))].join(","); + try { + const res = await fetch( + `${config.external.coingeckoApiUrl}/simple/price?ids=${ids}&vs_currencies=usd`, + { signal: AbortSignal.timeout(15_000) }, + ); + if (!res.ok) return; + const data = (await res.json()) as Record; + const ts = Date.now(); + for (const [denom, cgId] of Object.entries(DENOM_COINGECKO_ID)) { + const price = data[cgId]?.usd ?? 0; + if (price > 0) priceCache.set(denom, { price, ts }); + } + } catch { + // Keep stale cache on failure + } +} + +/** Fetch USD price for a base denom — reads from cache (pre-warmed by prewarmPriceCache) */ +export async function fetchTokenUsdPrice(baseDenom: string): Promise { + const cached = priceCache.get(baseDenom); + if (cached && Date.now() - cached.ts < PRICE_CACHE_MS) return cached.price; + + // Fallback: single-denom fetch if cache miss (shouldn't happen after prewarm) + const id = DENOM_COINGECKO_ID[baseDenom]; + if (!id) return 0; + + try { + const res = await fetch( + `${config.external.coingeckoApiUrl}/simple/price?ids=${id}&vs_currencies=usd`, + { signal: AbortSignal.timeout(15_000) }, + ); + if (!res.ok) return cached?.price ?? 0; + const data = (await res.json()) as Record; + const price = data[id]?.usd ?? 0; + if (price > 0) priceCache.set(baseDenom, { price, ts: Date.now() }); + return price; + } catch { + return cached?.price ?? 0; + } +} + +/** Resolve an IBC hash to its base denom via chain RPC */ +export async function fetchDenomTrace(ibcHash: string): Promise { + try { + const data = await fetchRest<{ + denom_trace: { base_denom: string }; + }>(`/ibc/apps/transfer/v1/denom_traces/${ibcHash}`); + return data.denom_trace?.base_denom ?? null; + } catch { + return null; + } +} + +// ── TVL computation (jsinfo-shape) ─────────────────────────────────────────── + +// Reward pools that count toward TVL (iprpc_pool excluded) +const TVL_REWARD_POOLS = [ + "validators_rewards_distribution_pool", + "validators_rewards_allocation_pool", + "providers_rewards_distribution_pool", + "providers_rewards_allocation_pool", +]; + +async function fetchTvlRewardPoolsUlava(): Promise { + const data = await fetchRest<{ + pools: Array<{ name: string; balance: Array<{ denom: string; amount: string }> }>; + }>("/lavanet/lava/rewards/pools"); + let total = 0n; + for (const pool of data.pools ?? []) { + if (TVL_REWARD_POOLS.includes(pool.name)) { + for (const coin of pool.balance ?? []) { + if (coin.denom === "ulava") total += BigInt(coin.amount); + } + } + } + return total; +} + +async function fetchSubscriptionCreditsUlava(): Promise { + try { + const data = await fetchRest<{ + subs_info: Array<{ credit: { amount: string; denom: string } }>; + }>("/lavanet/lava/subscription/list"); + let total = 0n; + for (const sub of data.subs_info ?? []) { + if (sub.credit?.denom === "ulava") { + const amount = BigInt(sub.credit.amount || "0"); + if (amount > 0n) total += amount; + } + } + return total; + } catch { + return 0n; + } +} + +async function fetchOsmosisLavaUlava(): Promise { + try { + const input = JSON.stringify({ + json: { + limit: 10, search: null, denoms: ["LAVA"], + types: ["weighted", "stable", "concentrated", "cosmwasm-transmuter", + "cosmwasm", "cosmwasm-astroport-pcl", "cosmwasm-whitewhale"], + incentiveTypes: ["superfluid", "osmosis", "boost", "none"], + sort: { keyPath: "market.volume24hUsd", direction: "desc" }, + minLiquidityUsd: 1000, cursor: 0, + }, + meta: { values: { search: ["undefined"] } }, + }); + const res = await fetch( + `https://app.osmosis.zone/api/edge-trpc-pools/pools.getPools?input=${encodeURIComponent(input)}`, + { signal: AbortSignal.timeout(15_000) }, + ); + if (!res.ok) return 0n; + const data = (await res.json()) as { + result: { data: { json: { items: Array<{ + reserveCoins: Array<{ currency: { coinDenom: string }; amount: string }>; + }> } } }; + }; + let total = 0n; + for (const pool of data.result?.data?.json?.items ?? []) { + for (const coin of pool.reserveCoins ?? []) { + if (coin.currency?.coinDenom?.toLowerCase() === "lava") { + total += BigInt(Math.round(parseFloat(coin.amount || "0"))); + } + } + } + return total; + } catch { + return 0n; + } +} + +async function fetchBaseDexUsd(): Promise { + try { + const res = await fetch( + "https://api.geckoterminal.com/api/v2/search/pools?query=lava", + { signal: AbortSignal.timeout(15_000) }, + ); + if (!res.ok) return 0; + const data = (await res.json()) as { + data: Array<{ id: string; attributes: { reserve_in_usd: string } }>; + }; + const basePool = data.data?.find((p: { id: string }) => p.id.startsWith("base_")); + return basePool ? parseFloat(basePool.attributes.reserve_in_usd || "0") : 0; + } catch { + return 0; + } +} + +async function fetchArbitrumDexUsd(): Promise { + try { + const res = await fetch("https://interface.gateway.uniswap.org/v1/graphql", { + method: "POST", + signal: AbortSignal.timeout(15_000), + headers: { + "Content-Type": "application/json", + Origin: "https://app.uniswap.org", + }, + body: JSON.stringify({ + query: `query($chain: Chain!, $address: String) { + token(chain: $chain, address: $address) { + market(currency: USD) { totalValueLocked { value } } + } + }`, + variables: { + address: "0x11e969e9b3f89cb16d686a03cd8508c9fc0361af", + chain: "ARBITRUM", + }, + }), + }); + if (!res.ok) return 0; + const data = (await res.json()) as { + data?: { token?: { market?: { totalValueLocked?: { value?: number } } } }; + }; + return data.data?.token?.market?.totalValueLocked?.value ?? 0; + } catch { + return 0; + } +} + +/** + * TVL (USD) = bonded tokens + 4 reward pools + subscriptions + DEX liquidity + * Matches jsinfo formula: all components converted to USD via CoinGecko LAVA price. + */ +export async function computeTVL(): Promise<{ tvl: string }> { + const [lavaPrice, pool, rewardPools, subscriptions, osmosis, baseUsd, arbitrumUsd] = + await Promise.all([ + fetchLavaUsdPrice(), + fetchStakingPool(), + fetchTvlRewardPoolsUlava(), + fetchSubscriptionCreditsUlava(), + fetchOsmosisLavaUlava(), + fetchBaseDexUsd(), + fetchArbitrumDexUsd(), + ]); + + const bondedTokens = BigInt(pool.bonded_tokens); + + // Convert DEX USD values to ulava to match jsinfo round-trip + const baseUlava = lavaPrice > 0 + ? BigInt(Math.round((baseUsd / lavaPrice) * 1_000_000)) + : 0n; + const arbitrumUlava = lavaPrice > 0 + ? BigInt(Math.round((arbitrumUsd / lavaPrice) * 1_000_000)) + : 0n; + + // Sum all components in ulava, then convert to USD via shared helper + // (full precision: integer-divide + remainder, avoiding Number overflow) + const totalUlava = bondedTokens + rewardPools + subscriptions + + osmosis + baseUlava + arbitrumUlava; + const tvlUsd = ulavaToLavaNumber(totalUlava) * lavaPrice; + + return { tvl: tvlUsd.toFixed(4) }; +} diff --git a/apps/api/src/rpc/providers.ts b/apps/api/src/rpc/providers.ts new file mode 100644 index 0000000..462ba8a --- /dev/null +++ b/apps/api/src/rpc/providers.ts @@ -0,0 +1,286 @@ +import { RPC_BATCH_SIZE, fetchRest } from "./rest.js"; +import { fetchAllSpecs } from "./specs.js"; + +export interface ProviderEndpoint { + iPPORT: string; + geolocation: number; + apiInterfaces: string[]; + addons: string[]; + extensions: string[]; +} + +export interface ProviderForSpec { + address: string; + moniker: string; + identity: string; + stake: { amount: string }; + delegate_total: { amount: string }; + delegate_commission: string; + geolocation: number; + addons: string; + extensions: string; + apiInterfaces: string; + endpoints: ProviderEndpoint[]; +} + +export async function fetchProvidersForSpec(specId: string): Promise { + const data = await fetchRest<{ + stakeEntry: Array<{ + address: string; + moniker: string; + description?: { identity?: string }; + stake: { amount: string }; + delegate_total: { amount: string }; + delegate_commission: string; + geolocation: number; + endpoints?: Array<{ + iPPORT?: string; + geolocation?: number; + addons?: string[]; + extensions?: string[]; + api_interfaces?: string[]; + }>; + }>; + }>(`/lavanet/lava/pairing/providers/${specId}`); + + return (data.stakeEntry ?? []).map((entry) => { + const allAddons = new Set(); + const allExtensions = new Set(); + const allApiInterfaces = new Set(); + const endpoints: ProviderEndpoint[] = []; + + for (const ep of entry.endpoints ?? []) { + for (const a of ep.addons ?? []) if (a) allAddons.add(a); + for (const e of ep.extensions ?? []) if (e) allExtensions.add(e); + for (const i of ep.api_interfaces ?? []) if (i) allApiInterfaces.add(i); + + if (ep.iPPORT) { + endpoints.push({ + iPPORT: ep.iPPORT, + geolocation: ep.geolocation ?? entry.geolocation, + apiInterfaces: (ep.api_interfaces ?? []).filter(Boolean), + addons: (ep.addons ?? []).filter(Boolean), + extensions: (ep.extensions ?? []).filter(Boolean), + }); + } + } + + return { + address: entry.address, + moniker: entry.moniker, + identity: entry.description?.identity ?? "", + stake: entry.stake, + delegate_total: entry.delegate_total, + delegate_commission: entry.delegate_commission, + geolocation: entry.geolocation, + addons: Array.from(allAddons).join(","), + extensions: Array.from(allExtensions).join(","), + apiInterfaces: Array.from(allApiInterfaces).join(","), + endpoints, + }; + }); +} + +export interface ProviderSpecEntry { + chain: string; + spec: string; + stakestatus: string; + stake: string; + addons: string; + extensions: string; + delegateCommission: string; + delegateTotal: string; + moniker: string; +} + +/** Build per-provider spec data + address list + spec name map from chain RPC */ +export async function fetchProvidersWithSpecs(): Promise<{ + providers: Map; + specNames: Map; +}> { + const allSpecs = await fetchAllSpecs(); + const specNames = new Map(allSpecs.map((s) => [s.index, s.name])); + const byAddress = new Map< + string, + { moniker: string; identity: string; commission: string; specs: ProviderSpecEntry[] } + >(); + + for (let i = 0; i < allSpecs.length; i += RPC_BATCH_SIZE) { + const batch = allSpecs.slice(i, i + RPC_BATCH_SIZE); + const results = await Promise.all( + batch.map((s) => fetchProvidersForSpec(s.index) + .then((ps) => ps.map((p) => ({ ...p, specId: s.index, specName: s.name }))) + .catch(() => [] as Array)), + ); + + for (const providers of results) { + for (const p of providers) { + const existing = byAddress.get(p.address); + const specEntry: ProviderSpecEntry = { + chain: p.specName, + spec: p.specId, + stakestatus: "Active", + stake: p.stake?.amount ?? "0", + addons: p.addons, + extensions: p.extensions, + delegateCommission: p.delegate_commission, + delegateTotal: p.delegate_total?.amount ?? "0", + moniker: p.moniker, + }; + + if (existing) { + existing.specs.push(specEntry); + if (!existing.commission && p.delegate_commission) existing.commission = p.delegate_commission; + if (!existing.identity && p.identity) existing.identity = p.identity; + } else { + byAddress.set(p.address, { + moniker: p.moniker ?? "", + identity: p.identity ?? "", + commission: p.delegate_commission ?? "", + specs: [specEntry], + }); + } + } + } + } + + return { providers: byAddress, specNames }; +} + +export type AllProvidersResult = Array<{ + address: string; + moniker: string; + identity: string; + totalStake: string; + totalDelegation: string; + commission: string; + specs: string[]; +}>; + +// Request coalescing: concurrent callers share one in-flight fetchAllProviders +let pendingProviders: Promise | null = null; + +/** Fetch all providers across all specs, with dedup by address. + * Concurrent callers share one in-flight request (coalesced). */ +export function fetchAllProviders(): Promise { + if (pendingProviders) return pendingProviders; + pendingProviders = fetchAllProvidersImpl().finally(() => { pendingProviders = null; }); + return pendingProviders; +} + +// Moniker-only cache. Routes that need just address→moniker shouldn't pay +// the full fetchAllProviders cost (stake/delegation/commission/specs) on +// cache misses. 60s TTL is short enough to stay fresh, long enough to +// deduplicate the 3+ reward routes that all call it in the same minute. +const MONIKER_CACHE_TTL_MS = 60_000; +let monikerCache: { at: number; map: Map } | null = null; +let pendingMonikers: Promise> | null = null; + +/** address → moniker map. 60-second in-process cache shared across routes. */ +export function fetchAllProviderMonikers(): Promise> { + if (monikerCache && Date.now() - monikerCache.at < MONIKER_CACHE_TTL_MS) { + return Promise.resolve(monikerCache.map); + } + if (pendingMonikers) return pendingMonikers; + pendingMonikers = (async () => { + const providers = await fetchAllProviders(); + const map = new Map(); + for (const p of providers) map.set(p.address, p.moniker); + monikerCache = { at: Date.now(), map }; + return map; + })().finally(() => { pendingMonikers = null; }); + return pendingMonikers; +} + +async function fetchAllProvidersImpl(): Promise { + const specs = await fetchAllSpecs(); + + interface SpecEntry { + address: string; + moniker: string; + identity: string; + stake?: { amount: string }; + delegate_total?: { amount: string }; + delegate_commission: string; + specId: string; + } + + // Fetch in batches of RPC_BATCH_SIZE — raise via env for dedicated RPC endpoints. + const specProviders: Array> = []; + 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( + batch.map((s) => + fetchProvidersForSpec(s.index) + .then((providers) => providers.map((p): SpecEntry => ({ + address: p.address, + moniker: p.moniker, + identity: p.identity, + stake: p.stake, + delegate_total: p.delegate_total, + delegate_commission: p.delegate_commission, + specId: s.index, + }))) + .catch(() => [] as SpecEntry[]), + ), + ); + specProviders.push(...results); + } + + const byAddress = new Map< + string, + { moniker: string; identity: string; totalStake: bigint; totalDelegation: bigint; commission: string; specs: string[] } + >(); + + for (const providers of specProviders) { + for (const p of providers) { + const existing = byAddress.get(p.address); + const stake = BigInt(p.stake?.amount ?? "0"); + const delegation = BigInt(p.delegate_total?.amount ?? "0"); + if (existing) { + existing.totalStake += stake; + existing.totalDelegation += delegation; + existing.specs.push(p.specId); + if (!existing.commission && p.delegate_commission) { + existing.commission = p.delegate_commission; + } + if (!existing.identity && p.identity) { + existing.identity = p.identity; + } + } else { + byAddress.set(p.address, { + moniker: p.moniker ?? "", + identity: p.identity ?? "", + totalStake: stake, + totalDelegation: delegation, + commission: p.delegate_commission ?? "", + specs: [p.specId], + }); + } + } + } + + return Array.from(byAddress.entries()).map(([address, data]) => ({ + address, + moniker: data.moniker, + identity: data.identity, + totalStake: data.totalStake.toString(), + totalDelegation: data.totalDelegation.toString(), + commission: data.commission, + specs: data.specs, + })); +} + +export async function fetchDelegatorRewards(provider: string): Promise< + Array<{ denom: string; amount: string }> +> { + try { + const data = await fetchRest<{ + rewards: Array<{ provider: string; amount: Array<{ denom: string; amount: string }> }>; + }>(`/lavanet/lava/dualstaking/delegator_rewards/${provider}`); + const entry = data.rewards?.find((r) => r.provider === provider); + return entry?.amount ?? []; + } catch { + return []; + } +} diff --git a/apps/api/src/rpc/rest.ts b/apps/api/src/rpc/rest.ts new file mode 100644 index 0000000..47e275f --- /dev/null +++ b/apps/api/src/rpc/rest.ts @@ -0,0 +1,37 @@ +import pino from "pino"; +import { config } from "../config.js"; + +export const logger = pino({ name: "rpc" }); + +/** + * Concurrent in-flight RPC calls per batch loop. Operators running dedicated + * RPC endpoints can raise this (env: RPC_BATCH_SIZE). Public endpoints should + * keep a conservative value (~5) to avoid rate limiting. + */ +export const RPC_BATCH_SIZE = config.lava.rpcBatchSize; + +// Request coalescing: concurrent fetches for the same path share one in-flight request +const inflightRpc = new Map>(); + +export async function fetchRest(path: string, blockHeight?: number): Promise { + const cacheKey = blockHeight ? `${path}@${blockHeight}` : path; + const existing = inflightRpc.get(cacheKey); + if (existing) return existing as Promise; + + const promise = (async () => { + const headers: Record = {}; + if (blockHeight) headers["x-cosmos-block-height"] = String(blockHeight); + + const res = await fetch(`${config.lava.restUrl}${path}`, { + headers, + signal: AbortSignal.timeout(15_000), + }); + if (!res.ok) throw new Error(`RPC ${res.status}: ${res.statusText}`); + return (await res.json()) as T; + })(); + + inflightRpc.set(cacheKey, promise); + const cleanup = () => { inflightRpc.delete(cacheKey); }; + promise.then(cleanup, cleanup); + return promise; +} diff --git a/apps/api/src/rpc/rewards.ts b/apps/api/src/rpc/rewards.ts new file mode 100644 index 0000000..a7b422c --- /dev/null +++ b/apps/api/src/rpc/rewards.ts @@ -0,0 +1,245 @@ +import { fetchRest } from "./rest.js"; +import { + DENOM_CONVERSIONS, + fetchDenomTrace, + fetchTokenUsdPrice, +} from "./pricing.js"; + +/** Benchmark: 10,000 LAVA in ulava / base units */ +export const APR_BENCHMARK_ULAVA = 10_000_000_000; +export const APR_BENCHMARK_LAVA = 10_000; +const APR_BENCHMARK_DENOM = "ulava"; + +export interface EstimatedRewardsResponse { + info: Array<{ + source: string; + amount: { denom: string; amount: string } | Array<{ denom: string; amount: string }>; + }>; + total: Array<{ denom: string; amount: string }>; + recommended_block?: string; +} + +/** IBC test denoms to skip (matching jsinfo) */ +const TEST_DENOMS = new Set([ + "ibc/E3FCBEDDBAC500B1BAB90395C7D1E4F33D9B9ECFE82A16ED7D7D141A0152323F", +]); + +/** Token breakdown matching jsinfo RewardAmount */ +export interface RewardToken { + source_denom: string; + resolved_amount: string; + resolved_denom: string; + display_denom: string; + display_amount: string; + value_usd: string; +} + +export interface ProcessedRewards { + totalUsd: number; + tokens: RewardToken[]; +} + +/** Convert multi-denom reward array to USD total + per-token breakdown */ +async function processRewardTokens( + rewards: Array<{ denom: string; amount: string }>, +): Promise { + const tokens: RewardToken[] = []; + let totalUsd = 0; + + for (const { denom, amount } of rewards) { + if (TEST_DENOMS.has(denom)) continue; + + // Resolve IBC denoms to their base denom + let rawDenom = denom; + let resolvedDenom = denom; + if (denom.startsWith("ibc/")) { + const resolved = await fetchDenomTrace(denom.slice(4)); + if (!resolved) continue; + rawDenom = resolved; + resolvedDenom = resolved; + } + + const conversion = DENOM_CONVERSIONS[rawDenom]; + if (!conversion) continue; + + const displayAmount = divideByFactor(amount, conversion.factor); + const baseAmount = parseFloat(displayAmount); + if (!isFinite(baseAmount) || baseAmount <= 0) continue; + + const price = await fetchTokenUsdPrice(conversion.baseDenom); + const usd = price > 0 ? baseAmount * price : 0; + totalUsd += usd; + + tokens.push({ + source_denom: denom, + resolved_amount: formatTokenStr(amount), + resolved_denom: resolvedDenom, + display_denom: conversion.baseDenom, + display_amount: displayAmount, + value_usd: `$${formatTokenStr(usd.toFixed(14))}`, + }); + } + + return { totalUsd, tokens }; +} + +/** Strip trailing zeros from a decimal string (matching jsinfo FormatTokenAmount) */ +function formatTokenStr(s: string): string { + const [whole = "", frac] = s.split("."); + if (!frac) return whole; + const trimmed = frac.replace(/0+$/, ""); + return trimmed ? `${whole}.${trimmed}` : whole; +} + +/** String-based division to avoid floating-point noise for token amounts. + * Shifts the decimal point left by the number of digits in `divisor`. + * e.g. divideStr("23584370", 1_000_000) → "23.584370" → "23.58437" */ +function divideByFactor(raw: string, factor: number): string { + // Strip any existing decimal (RPC may return "123.000000000000000000") + const cleaned = formatTokenStr(raw); + const digits = Math.round(Math.log10(factor)); + const [intPart, decPart = ""] = cleaned.split("."); + const combined = intPart + decPart; + const shiftedDecPos = combined.length - digits - decPart.length; + + // Pad with leading zeros if the number is smaller than the factor + const padded = shiftedDecPos <= 0 + ? "0." + "0".repeat(-shiftedDecPos) + combined + : combined.slice(0, shiftedDecPos) + "." + combined.slice(shiftedDecPos); + + return formatTokenStr(padded || "0"); +} + +/** Fetch estimated rewards for a provider/validator — returns USD total + token breakdown */ +export async function fetchEstimatedRewards( + type: "provider" | "validator", + address: string, +): Promise { + try { + const data = await fetchRest( + `/lavanet/lava/subscription/estimated_${type}_rewards/${address}/${APR_BENCHMARK_ULAVA}${APR_BENCHMARK_DENOM}`, + ); + return await processRewardTokens(data.total ?? []); + } catch { + return { totalUsd: 0, tokens: [] }; + } +} + +export interface RewardsBySpecEntry { + chain: string; + spec: string; + tokens: RewardToken[]; + total_usd: number; +} + +/** + * Fetch a provider's actual earned rewards (no benchmark amount) and group by spec. + * Matches jsinfo's rewards_last_month: calls estimated_provider_rewards/{addr}/ + * then splits "Boost: ETH1", "Pools: ETH1", "Subscription: ETH1" into per-spec groups. + * + * Pass blockHeight to query historical chain state (archive node required). + * Note: USD values still use current CoinGecko prices even for historical blocks — + * matches the old Job 2 behavior where CoinGecko was called at pipeline-run time. + */ +export async function fetchRewardsBySpec( + address: string, + specNameMap: Map, + blockHeight?: number, +): Promise { + try { + const data = await fetchRest( + `/lavanet/lava/subscription/estimated_provider_rewards/${address}/`, + blockHeight, + ); + + // Group info entries by spec (collapse Boost/Pools/Subscription sources) + const bySpec = new Map; totalUsd: number }>(); + + for (const entry of data.info ?? []) { + const parts = (entry.source as string).split(": "); + const spec = parts.length > 1 ? parts[1] : parts[0]; + if (!spec) continue; + + const key = spec.toLowerCase(); + const group = bySpec.get(key) ?? { tokens: new Map(), totalUsd: 0 }; + + const amounts = Array.isArray(entry.amount) ? entry.amount : [entry.amount]; + for (const coin of amounts) { + if (TEST_DENOMS.has(coin.denom)) continue; + const existing = group.tokens.get(coin.denom); + const amt = parseFloat(coin.amount) || 0; + if (existing) { + existing.amount += amt; + } else { + group.tokens.set(coin.denom, { amount: amt, denom: coin.denom }); + } + } + + bySpec.set(key, group); + } + + // Convert to output format with USD values + const results: RewardsBySpecEntry[] = []; + for (const [specKey, group] of bySpec) { + const tokens: RewardToken[] = []; + let specUsd = 0; + + for (const [, coin] of group.tokens) { + const processed = await processRewardTokens([ + { denom: coin.denom, amount: coin.amount.toString() }, + ]); + tokens.push(...processed.tokens); + specUsd += processed.totalUsd; + } + + results.push({ + chain: specNameMap.get(specKey.toUpperCase()) ?? specKey.toUpperCase(), + spec: specKey.toUpperCase(), + tokens, + total_usd: specUsd, + }); + } + + return results; + } catch { + return []; + } +} + +export interface ClaimableRewardEntry { + amount: string; // display amount in base units (e.g. "1.23" lava) + denom: string; // base denom (e.g. "lava") + usdcValue: string; // numeric USD string (no $) + provider: string; +} + +/** Convert multi-denom claimable rewards to jsinfo-shape entries. */ +export async function processClaimableRewards( + rewards: Array<{ denom: string; amount: string }>, + provider: string, +): Promise { + const processed = await processRewardTokens(rewards); + return processed.tokens.map((t) => ({ + amount: t.display_amount, + denom: t.display_denom, + usdcValue: t.value_usd.startsWith("$") ? t.value_usd.slice(1) : t.value_usd, + provider, + })); +} + +// ── Validator reward breakdown helper (shared between validators.ts and rewards) ── + +interface RewardCoin { denom: string; amount: string } + +export interface TokenBreakdown { + tokens: RewardToken[]; + total_usd: number; +} + +export const EMPTY_BREAKDOWN: TokenBreakdown = { tokens: [], total_usd: 0 }; + +export async function rewardsToBreakdown(rewards: RewardCoin[] | undefined): Promise { + if (!rewards || rewards.length === 0) return EMPTY_BREAKDOWN; + const processed = await processRewardTokens(rewards); + return { tokens: processed.tokens, total_usd: processed.totalUsd }; +} diff --git a/apps/api/src/rpc/specs.ts b/apps/api/src/rpc/specs.ts new file mode 100644 index 0000000..7eac17d --- /dev/null +++ b/apps/api/src/rpc/specs.ts @@ -0,0 +1,11 @@ +import { BASE_SPECS, chainDisplayName } from "@info/shared/constants"; +import { fetchRest } from "./rest.js"; + +export async function fetchAllSpecs(): Promise> { + const data = await fetchRest<{ + chainInfoList: Array<{ chainName: string; chainID: string }>; + }>("/lavanet/lava/spec/show_all_chains"); + return (data.chainInfoList ?? []) + .filter((c) => !BASE_SPECS.has(c.chainID)) + .map((c) => ({ index: c.chainID, name: chainDisplayName(c.chainID, c.chainName) })); +} diff --git a/apps/api/src/rpc/supply.ts b/apps/api/src/rpc/supply.ts new file mode 100644 index 0000000..f01418b --- /dev/null +++ b/apps/api/src/rpc/supply.ts @@ -0,0 +1,133 @@ +import { fetchRest, logger } from "./rest.js"; + +export async function fetchTotalSupply(blockHeight?: number): Promise { + const data = await fetchRest<{ supply: Array<{ denom: string; amount: string }> }>( + "/cosmos/bank/v1beta1/supply", + blockHeight, + ); + const lava = data.supply?.find((c) => c.denom === "ulava"); + return BigInt(lava?.amount ?? "0"); +} + +export async function fetchStakingPool(): Promise<{ bonded_tokens: string; not_bonded_tokens: string }> { + const data = await fetchRest<{ pool: { bonded_tokens: string; not_bonded_tokens: string } }>( + "/cosmos/staking/v1beta1/pool", + ); + return data.pool; +} + +interface RewardPool { + name: string; + balance: Array<{ denom: string; amount: string }>; +} + +/** Shared fetch for reward pools — both consumers filter by pool name. */ +export async function fetchRewardPools(): Promise { + const data = await fetchRest<{ pools: RewardPool[] }>("/lavanet/lava/rewards/pools"); + return data.pools ?? []; +} + +function sumPoolsUlava(pools: RewardPool[], names: string[]): bigint { + const nameSet = new Set(names); + let total = 0n; + for (const pool of pools) { + if (nameSet.has(pool.name)) { + for (const coin of pool.balance ?? []) { + if (coin.denom === "ulava") total += BigInt(coin.amount); + } + } + } + return total; +} + +const ALL_REWARD_POOLS = [ + "validators_rewards_distribution_pool", + "validators_rewards_allocation_pool", + "providers_rewards_distribution_pool", + "providers_rewards_allocation_pool", + "iprpc_pool", +]; + +async function fetchRewardPoolsAmount(): Promise { + return sumPoolsUlava(await fetchRewardPools(), ALL_REWARD_POOLS); +} + +interface VestingStats { + continuousVesting: bigint; + periodicVesting: bigint; +} + +async function fetchLockedVestingTokens(): Promise { + const nowSeconds = Math.floor(Date.now() / 1000); + const stats: VestingStats = { continuousVesting: 0n, periodicVesting: 0n }; + let nextKey: string | null = null; + + do { + const params = new URLSearchParams({ "pagination.limit": "1000" }); + if (nextKey) params.set("pagination.key", nextKey); + + const data = await fetchRest<{ + accounts: Array<{ + "@type": string; + start_time?: string; + base_vesting_account?: { + original_vesting?: Array<{ denom: string; amount: string }>; + end_time?: string; + }; + vesting_periods?: Array<{ + length: string; + amount: Array<{ denom: string; amount: string }>; + }>; + }>; + pagination?: { next_key: string | null }; + }>(`/cosmos/auth/v1beta1/accounts?${params}`); + + for (const account of data.accounts ?? []) { + const type = account["@type"]; + + if (type === "/cosmos.vesting.v1beta1.ContinuousVestingAccount") { + const totalAmount = BigInt( + account.base_vesting_account?.original_vesting?.[0]?.amount ?? "0", + ); + const startTime = BigInt(account.start_time ?? "0"); + const endTime = BigInt(account.base_vesting_account?.end_time ?? "0"); + const now = BigInt(nowSeconds); + + if (now < startTime) { + stats.continuousVesting += totalAmount; + } else if (now < endTime && endTime > startTime) { + stats.continuousVesting += ((endTime - now) * totalAmount) / (endTime - startTime); + } + } else if (type === "/cosmos.vesting.v1beta1.PeriodicVestingAccount") { + let currentTime = parseInt(account.start_time ?? "0"); + for (const period of account.vesting_periods ?? []) { + currentTime += parseInt(period.length); + if (currentTime >= nowSeconds) { + stats.periodicVesting += BigInt(period.amount?.[0]?.amount ?? "0"); + } + } + } + } + + nextKey = data.pagination?.next_key ?? null; + } while (nextKey); + + return stats; +} + +export async function fetchCirculatingSupply(): Promise { + const [totalSupply, pools, vesting] = await Promise.all([ + fetchTotalSupply(), + fetchRewardPoolsAmount(), + fetchLockedVestingTokens(), + ]); + + const circulating = totalSupply - vesting.continuousVesting - vesting.periodicVesting - pools; + if (circulating < 0n) { + logger.warn( + `Negative circulating supply: total=${totalSupply} continuous=${vesting.continuousVesting} periodic=${vesting.periodicVesting} pools=${pools}`, + ); + return 0n; + } + return circulating; +} diff --git a/apps/api/src/rpc/validators.ts b/apps/api/src/rpc/validators.ts new file mode 100644 index 0000000..a3e5662 --- /dev/null +++ b/apps/api/src/rpc/validators.ts @@ -0,0 +1,244 @@ +import { RPC_BATCH_SIZE, fetchRest } from "./rest.js"; +import { fetchLatestBlockHeight } from "./block.js"; +import { prewarmPriceCache } from "./pricing.js"; +import { + type EstimatedRewardsResponse, + type RewardToken, + type TokenBreakdown, + rewardsToBreakdown, +} from "./rewards.js"; + +export interface ValidatorCommissionRates { + rate: string; + max_rate: string; + max_change_rate: string; +} + +export interface ValidatorCommission { + commission_rates: ValidatorCommissionRates; + update_time: string; +} + +export interface ValidatorBase { + operator_address: string; + description?: { moniker?: string }; + jailed: boolean; + tokens: string; + commission: ValidatorCommission; +} + +interface RewardCoin { denom: string; amount: string } + +export interface ValidatorDistributionRewards { + self_bond_rewards: TokenBreakdown; + commission: TokenBreakdown; + operator_address: string; +} + +export interface ValidatorDelegationResponse { + delegation: { + delegator_address: string; + validator_address: string; + shares: string; + }; + balance: RewardCoin; +} + +export interface ValidatorUnbondingEntry { + creation_height: string; + completion_time: string; + initial_balance: string; + balance: string; + unbonding_id: string; + unbonding_on_hold_ref_count: string; +} + +export interface ValidatorUnbondingResponse { + delegator_address: string; + validator_address: string; + entries: ValidatorUnbondingEntry[]; +} + +export interface ValidatorWithRewards { + address: string; + moniker: string; + jailed: boolean; + tokens: string; + commission: ValidatorCommission; + apr?: number | null; + distribution: ValidatorDistributionRewards; + outstanding_rewards: TokenBreakdown; + estimated_rewards: TokenBreakdown; + delegations: { + delegation_responses: ValidatorDelegationResponse[]; + pagination: { next_key: string | null; total: string }; + }; + unbonding_delegations: { + unbonding_responses: ValidatorUnbondingResponse[]; + pagination: { next_key: string | null; total: string }; + }; +} + +async function fetchAllValidatorObjects(): Promise { + const validators: ValidatorBase[] = []; + let nextKey: string | null = null; + do { + const params = new URLSearchParams({ + status: "BOND_STATUS_BONDED", + "pagination.limit": "200", + }); + if (nextKey) params.set("pagination.key", nextKey); + const data = await fetchRest<{ + validators: ValidatorBase[]; + pagination: { next_key: string | null }; + }>(`/cosmos/staking/v1beta1/validators?${params}`); + for (const v of data.validators ?? []) validators.push(v); + nextKey = data.pagination?.next_key ?? null; + } while (nextKey); + return validators; +} + +interface ValidatorDistributionRewardsResponse { + operator_address: string; + self_bond_rewards: RewardCoin[]; + commission: RewardCoin[]; +} + +async function fetchValidatorDistribution( + addr: string, +): Promise { + try { + return await fetchRest( + `/cosmos/distribution/v1beta1/validators/${addr}`, + ); + } catch { return null; } +} + +async function fetchValidatorOutstanding(addr: string): Promise { + try { + const data = await fetchRest<{ rewards: { rewards: RewardCoin[] } }>( + `/cosmos/distribution/v1beta1/validators/${addr}/outstanding_rewards`, + ); + return data.rewards?.rewards ?? []; + } catch { return []; } +} + +async function fetchValidatorEstimatedTotal(addr: string): Promise { + try { + const data = await fetchRest( + `/lavanet/lava/subscription/estimated_validator_rewards/${addr}/`, + ); + return data.total ?? []; + } catch { return []; } +} + +async function fetchValidatorDelegationsPage( + addr: string, +): Promise<{ delegation_responses: ValidatorDelegationResponse[]; pagination: { next_key: string | null; total: string } }> { + try { + return await fetchRest(`/cosmos/staking/v1beta1/validators/${addr}/delegations?pagination.limit=1000&pagination.count_total=true`); + } catch { + return { delegation_responses: [], pagination: { next_key: null, total: "0" } }; + } +} + +async function fetchValidatorUnbondingPage( + addr: string, +): Promise<{ unbonding_responses: ValidatorUnbondingResponse[]; pagination: { next_key: string | null; total: string } }> { + try { + return await fetchRest(`/cosmos/staking/v1beta1/validators/${addr}/unbonding_delegations?pagination.limit=1000&pagination.count_total=true`); + } catch { + return { unbonding_responses: [], pagination: { next_key: null, total: "0" } }; + } +} + +/** + * Build the full validator-with-rewards list matching jsinfo's + * /lava_mainnet_validators_and_rewards response shape. + * + * Each validator requires 5 chain RPC calls; processes RPC_BATCH_SIZE at a time + * to respect rate limits. With ~50 validators this completes in ~20-30s cold, + * well within the 6h cache window. + */ +export async function fetchValidatorsWithRewards(): Promise<{ + height: number; + datetime: number; + validators: ValidatorWithRewards[]; +}> { + await prewarmPriceCache(); + + const [validators, latestBlock] = await Promise.all([ + fetchAllValidatorObjects(), + fetchLatestBlockHeight(), + ]); + + const enriched: ValidatorWithRewards[] = []; + + for (let i = 0; i < validators.length; i += RPC_BATCH_SIZE) { + const batch = validators.slice(i, i + RPC_BATCH_SIZE); + const results = await Promise.all(batch.map(async (v) => { + const addr = v.operator_address; + const [dist, outstanding, estimatedTotal, delegations, unbonding] = await Promise.all([ + fetchValidatorDistribution(addr), + fetchValidatorOutstanding(addr), + fetchValidatorEstimatedTotal(addr), + fetchValidatorDelegationsPage(addr), + fetchValidatorUnbondingPage(addr), + ]); + + const [selfBond, commissionBreakdown, outstandingBreakdown, estimatedBreakdown] = await Promise.all([ + rewardsToBreakdown(dist?.self_bond_rewards), + rewardsToBreakdown(dist?.commission), + rewardsToBreakdown(outstanding), + rewardsToBreakdown(estimatedTotal), + ]); + + return { + address: addr, + moniker: v.description?.moniker ?? "", + jailed: v.jailed, + tokens: v.tokens, + commission: v.commission, + distribution: { + self_bond_rewards: selfBond, + commission: commissionBreakdown, + operator_address: dist?.operator_address ?? addr, + }, + outstanding_rewards: outstandingBreakdown, + estimated_rewards: estimatedBreakdown, + delegations, + unbonding_delegations: unbonding, + } satisfies ValidatorWithRewards; + })); + enriched.push(...results); + } + + return { + height: latestBlock.height, + datetime: Math.floor(new Date(latestBlock.time).getTime() / 1000), + validators: enriched, + }; +} + +/** Fetch all bonded validator operator addresses */ +export async function fetchBondedValidators(): Promise { + const validators: string[] = []; + let nextKey: string | null = null; + do { + const params = new URLSearchParams({ + status: "BOND_STATUS_BONDED", + "pagination.limit": "200", + }); + if (nextKey) params.set("pagination.key", nextKey); + const data = await fetchRest<{ + validators: Array<{ operator_address: string }>; + pagination: { next_key: string | null }; + }>(`/cosmos/staking/v1beta1/validators?${params}`); + for (const v of data.validators ?? []) validators.push(v.operator_address); + nextKey = data.pagination?.next_key ?? null; + } while (nextKey); + return validators; +} + +// Re-export RewardToken so callers consuming ValidatorWithRewards can type tokens. +export type { RewardToken }; diff --git a/apps/api/src/services/grpc-probe.ts b/apps/api/src/services/grpc-probe.ts index 0ae96d3..34b6f9d 100644 --- a/apps/api/src/services/grpc-probe.ts +++ b/apps/api/src/services/grpc-probe.ts @@ -63,7 +63,12 @@ export async function probeProvider( lavaLatestBlock: number; }>((resolve, reject) => { const deadline = new Date(Date.now() + PROBE_TIMEOUT_MS); - client.probe( + const probe = client.probe as ( + req: unknown, + opts: unknown, + cb: (err: grpc.ServiceError | null, response: unknown) => void, + ) => void; + probe( { guid, specId, apiInterface, withVerifications: false }, { deadline }, (err: grpc.ServiceError | null, response: unknown) => { diff --git a/apps/api/src/services/health-probe-loop.ts b/apps/api/src/services/health-probe-loop.ts new file mode 100644 index 0000000..1266699 --- /dev/null +++ b/apps/api/src/services/health-probe-loop.ts @@ -0,0 +1,155 @@ +import type { Redis } from "ioredis"; +import pino from "pino"; +import { normalizeBlock } from "@info/shared/constants"; +import { config } from "../config.js"; +import { fetchAllSpecs, fetchProvidersForSpec, RPC_BATCH_SIZE } from "../rpc/lava.js"; +import type { ProviderEndpoint } from "../rpc/lava.js"; +import { probeProvider } from "./grpc-probe.js"; +import { writeHealthStatus } from "./health-store.js"; + +const logger = pino({ name: "health-probe" }); + +interface ProbeTarget { + address: string; + specId: string; + endpoint: ProviderEndpoint; + apiInterface: string; +} + +async function buildProbeTargets(): Promise { + const specs = await fetchAllSpecs(); + const targets: ProbeTarget[] = []; + + // Fetch in batches — raise RPC_BATCH_SIZE via env for dedicated RPC endpoints. + 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( + batch.map((s) => + fetchProvidersForSpec(s.index) + .then((providers) => ({ specId: s.index, providers })) + .catch(() => ({ specId: s.index, providers: [] as Awaited> })), + ), + ); + + for (const { specId, providers } of results) { + for (const provider of providers) { + for (const ep of provider.endpoints) { + // Skip endpoints targeting private/internal networks + if (!isPublicEndpoint(ep.iPPORT)) continue; + + for (const apiInterface of ep.apiInterfaces) { + targets.push({ + address: provider.address, + specId, + endpoint: ep, + apiInterface, + }); + } + } + } + } + } + + return targets; +} + +/** Reject endpoints targeting private/loopback/internal networks (SSRF mitigation) */ +function isPublicEndpoint(iPPORT: string): boolean { + const host = iPPORT.split(":")[0]; + if (!host) return false; + if (host === "localhost" || host === "0.0.0.0") return false; + if (host.startsWith("127.")) return false; + if (host.startsWith("10.")) return false; + if (host.startsWith("192.168.")) return false; + if (host.startsWith("172.")) { + const second = parseInt(host.split(".")[1] ?? "", 10); + if (second >= 16 && second <= 31) return false; + } + if (host.startsWith("169.254.")) return false; + return true; +} + +function shuffle(arr: T[]): T[] { + const a = [...arr]; + for (let i = a.length - 1; i > 0; i--) { + const j = Math.floor(Math.random() * (i + 1)); + [a[i], a[j]] = [a[j]!, a[i]!]; + } + return a; +} + +function sleep(ms: number): Promise { + return new Promise((r) => setTimeout(r, ms)); +} + +export interface ProbeLoopOptions { + redis: Redis; + intervalMs?: number; + /** Returns true while the loop should keep running — called between iterations. */ + isRunning: () => boolean; +} + +/** + * Run one provider-health probe cycle followed by sleep, repeatedly. + * + * This loop is the core of the health-probe subsystem. It's extracted so it + * can be invoked from either an in-process Fastify plugin (dev / single-node) + * or a standalone worker process (production) — see `bin/health-probe.ts`. + */ +export async function runProbeLoop(opts: ProbeLoopOptions): Promise { + const { redis, isRunning } = opts; + const intervalMs = opts.intervalMs ?? config.healthProbe.intervalMs; + + logger.info("Health probe started"); + + while (isRunning()) { + try { + const targets = await buildProbeTargets(); + if (targets.length === 0) { + logger.warn("No probe targets found, waiting"); + await sleep(60_000); + continue; + } + + const shuffled = shuffle(targets); + logger.info({ count: shuffled.length }, "Starting health probe cycle"); + + // Probe in batches — raise RPC_BATCH_SIZE via env for dedicated endpoints. + for (let i = 0; i < shuffled.length; i += RPC_BATCH_SIZE) { + if (!isRunning()) break; + const batch = shuffled.slice(i, i + RPC_BATCH_SIZE); + + await Promise.allSettled(batch.map(async (target) => { + const geolocation = target.endpoint.geolocation.toString(); + try { + const result = await probeProvider( + target.endpoint.iPPORT, + target.specId, + target.apiInterface, + ); + const block = normalizeBlock(target.specId, result.latestBlock); + await writeHealthStatus( + redis, target.address, target.specId, target.apiInterface, geolocation, + "healthy", + { block, latency: result.latencyMs, lavaEpoch: result.lavaEpoch }, + ); + } catch (err) { + const message = err instanceof Error ? err.message : String(err); + await writeHealthStatus( + redis, target.address, target.specId, target.apiInterface, geolocation, + "unhealthy", + { message }, + ); + } + })); + } + + if (isRunning()) await sleep(intervalMs); + } catch (err) { + logger.error({ err }, "Health probe loop error"); + await sleep(60_000); + } + } + + logger.info("Health probe stopped"); +} diff --git a/apps/api/src/services/health-store.ts b/apps/api/src/services/health-store.ts index fb2deb0..e9625c6 100644 --- a/apps/api/src/services/health-store.ts +++ b/apps/api/src/services/health-store.ts @@ -76,7 +76,7 @@ async function readFromIndex( if (val) { records.push(JSON.parse(val) as HealthRecord); } else { - staleKeys.push(batch[j]); + staleKeys.push(batch[j]!); } } } @@ -92,15 +92,10 @@ async function readFromIndex( export async function readHealthForProvider( redis: Redis, provider: string, - page: number, - limit: number, -): Promise<{ data: HealthRecord[]; total: number }> { +): Promise { const records = await readFromIndex(redis, providerIndexKey(provider)); records.sort((a, b) => b.timestamp.localeCompare(a.timestamp)); - - const total = records.length; - const offset = (page - 1) * limit; - return { data: records.slice(offset, offset + limit), total }; + return records; } export interface InterfaceHealth { @@ -149,7 +144,7 @@ export async function readHealthMapForProvider( const unhealthyCount = interfaces.filter((i) => i.status !== "healthy").length; const oldestTimestamp = recs.reduce( (oldest, r) => (r.timestamp < oldest ? r.timestamp : oldest), - recs[0].timestamp, + recs[0]!.timestamp, ); result.set(spec, { @@ -192,7 +187,7 @@ export async function readHealthByProviderForSpec( const unhealthyCount = interfaces.filter((i) => i.status !== "healthy").length; const oldestTimestamp = recs.reduce( (oldest, r) => (r.timestamp < oldest ? r.timestamp : oldest), - recs[0].timestamp, + recs[0]!.timestamp, ); result.set(provider, { diff --git a/apps/api/src/services/precompute-store.ts b/apps/api/src/services/precompute-store.ts new file mode 100644 index 0000000..271b57f --- /dev/null +++ b/apps/api/src/services/precompute-store.ts @@ -0,0 +1,37 @@ +import type { Redis } from "ioredis"; + +/** + * Stale-while-revalidate store for heavy endpoints. + * + * A separate worker process (see bin/precompute.ts) periodically runs the + * expensive computations and writes results here. API route handlers read + * from here first — on a hit they skip the cold-path compute entirely, + * which means no cache stampede when TTLs expire under load. + * + * Values never expire in Redis; the worker overwrites them on each cycle. + * If the worker is down, routes fall back to live compute. + */ + +const KEY_PREFIX = "precompute:"; + +export interface PrecomputedValue { + /** Unix millis when the worker last wrote this value. */ + at: number; + value: T; +} + +export async function readPrecomputed(redis: Redis | null, name: string): Promise | null> { + if (!redis) return null; + try { + const raw = await redis.get(`${KEY_PREFIX}${name}`); + if (!raw) return null; + return JSON.parse(raw) as PrecomputedValue; + } catch { + return null; + } +} + +export async function writePrecomputed(redis: Redis, name: string, value: T): Promise { + const payload: PrecomputedValue = { at: Date.now(), value }; + await redis.set(`${KEY_PREFIX}${name}`, JSON.stringify(payload)); +} diff --git a/apps/web/package.json b/apps/web/package.json index b5be62e..7f95b30 100644 --- a/apps/web/package.json +++ b/apps/web/package.json @@ -8,7 +8,8 @@ "dev": "next dev", "start": "next start", "lint": "next lint", - "typecheck": "tsc --noEmit" + "typecheck": "tsc --noEmit", + "test": "vitest run" }, "dependencies": { "@info/shared": "workspace:*", @@ -31,6 +32,7 @@ "devDependencies": { "@types/react": "^19.0.0", "@types/react-dom": "^19.0.0", - "typescript": "^6.0.0" + "typescript": "^6.0.0", + "vitest": "^4.1.3" } } diff --git a/apps/web/src/app/layout.tsx b/apps/web/src/app/layout.tsx index 5ca4941..f728c42 100644 --- a/apps/web/src/app/layout.tsx +++ b/apps/web/src/app/layout.tsx @@ -3,6 +3,7 @@ import { Inter } from "next/font/google"; import { Header } from "@/components/layout/Header"; import { Footer } from "@/components/layout/Footer"; import { ErrorBoundary } from "@/components/data/ErrorBoundary"; +import { SwrProvider } from "./swr-provider"; import "@/styles/globals.css"; @@ -56,19 +57,21 @@ export default function RootLayout({ children }: { children: React.ReactNode }) return ( -
-
- -
-
-
- {children} + +
+
+ +
+
+
+ {children} +
-
-
-
-
-
+ + +