Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
cfe5f73
chore(api): register @fastify/compress for gzip+brotli
nimrod-teich Apr 16, 2026
5a01831
refactor(api): drop dead exports in rpc/lava.ts
nimrod-teich Apr 16, 2026
b3d6f49
refactor: share strict parseYMD date parser
nimrod-teich Apr 16, 2026
3e4e4b8
refactor: share weightedQos helper across index + providers routes
nimrod-teich Apr 16, 2026
8b29686
perf(api): share undici keep-alive dispatcher + add GQL timeout
nimrod-teich Apr 16, 2026
214f23f
perf(api): health-probe batches honor RPC_BATCH_SIZE
nimrod-teich Apr 16, 2026
de4650a
perf(api): cache address→moniker map across reward routes
nimrod-teich Apr 16, 2026
7241c8f
refactor: wire shared PaginatedResponse/ProviderListItem types
nimrod-teich Apr 16, 2026
dd1365f
refactor(api): split rpc/lava.ts into focused modules
nimrod-teich Apr 16, 2026
2923358
refactor(api): extract health-probe into standalone worker
nimrod-teich Apr 16, 2026
877835e
perf(api): precompute heavy endpoints out-of-process
nimrod-teich Apr 16, 2026
1f9a321
test: add missing tests for routes + web utilities
nimrod-teich Apr 16, 2026
c29c521
chore: enable noUncheckedIndexedAccess across workspace
nimrod-teich Apr 16, 2026
e057f5d
refactor: drop API pagination — providers fit in a single payload
nimrod-teich Apr 16, 2026
7fda35e
style(web): use 1_000_000n literal instead of BigInt(1e6)
nimrod-teich Apr 16, 2026
1bfc601
refactor: single source for ulava→LAVA conversion
nimrod-teich Apr 16, 2026
0212541
refactor(api): normalize error-response shape via sendApiError helper
nimrod-teich Apr 16, 2026
57a4801
refactor: move CHAIN_DISPLAY_NAMES to @info/shared
nimrod-teich Apr 16, 2026
0c2ad6b
docs(api): explain mixed snake/camel casing on AllProviderAprEntry
nimrod-teich Apr 16, 2026
308df05
refactor(api): consolidate env var defaults in config.ts
nimrod-teich Apr 16, 2026
9eaff26
security(api): validate Keybase identity format before outbound request
nimrod-teich Apr 16, 2026
05bb577
refactor(api): name cache TTLs instead of scattering magic numbers
nimrod-teich Apr 16, 2026
1d89753
docs(api): note that /validators-and-rewards serves external consumers
nimrod-teich Apr 16, 2026
93d7554
perf(web): wire global SWRConfig (dedupe + refresh defaults)
nimrod-teich Apr 16, 2026
30d28fb
perf(api): cache + coalesce fetchLavaUsdPrice
nimrod-teich Apr 16, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,6 @@ CLAUDE.local.md

# pnpm store
.pnpm-store/

# Next.js dev artifacts (old/backup directories)
.next.old/
8 changes: 7 additions & 1 deletion apps/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
25 changes: 5 additions & 20 deletions apps/api/src/__tests__/health-store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down
52 changes: 0 additions & 52 deletions apps/api/src/__tests__/plugins.test.ts
Original file line number Diff line number Diff line change
@@ -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 () => {
Expand Down
2 changes: 0 additions & 2 deletions apps/api/src/__tests__/routes-all-providers-apr.test.ts
Original file line number Diff line number Diff line change
@@ -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", () => ({
Expand All @@ -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;
}
Expand Down
2 changes: 0 additions & 2 deletions apps/api/src/__tests__/routes-apr.test.ts
Original file line number Diff line number Diff line change
@@ -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", () => ({
Expand All @@ -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;
}
Expand Down
2 changes: 0 additions & 2 deletions apps/api/src/__tests__/routes-burn-rate.test.ts
Original file line number Diff line number Diff line change
@@ -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", () => ({
Expand All @@ -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;
}
Expand Down
2 changes: 0 additions & 2 deletions apps/api/src/__tests__/routes-health.test.ts
Original file line number Diff line number Diff line change
@@ -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", () => ({
Expand All @@ -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;
}
Expand Down
80 changes: 80 additions & 0 deletions apps/api/src/__tests__/routes-index.test.ts
Original file line number Diff line number Diff line change
@@ -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<typeof vi.fn>).mockResolvedValue({
mvRelayDailies: { aggregates: { sum: { cu: "1000", relays: "50" } } },
});
(fetchLatestBlockHeight as ReturnType<typeof vi.fn>).mockResolvedValue({
height: 12345,
time: "2025-01-01T00:00:00Z",
});
(fetchAllProviders as ReturnType<typeof vi.fn>).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<typeof vi.fn>).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<typeof vi.fn>).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);
});
});
40 changes: 40 additions & 0 deletions apps/api/src/__tests__/routes-lava.test.ts
Original file line number Diff line number Diff line change
@@ -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<typeof vi.fn>).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<typeof vi.fn>).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([]);
});
});
62 changes: 62 additions & 0 deletions apps/api/src/__tests__/routes-optimizer-metrics.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
Original file line number Diff line number Diff line change
@@ -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", () => ({
Expand All @@ -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;
}
Expand Down
Loading
Loading