Skip to content

Commit 6bf2f29

Browse files
committed
fix: address code review — dead import, error handling, tests, docs
- Remove dead adjustedRewardsRoutes import/registration from main.ts (file was deleted, server would crash on startup) - Replace stale "Adjusted Rewards" swagger tag with "Provider Rewards" - Gracefully handle CoinGecko failure: price degrades to null instead of crashing the entire /provider-rewards request - Add 8 validation/edge-case tests: bad date format, invalid date, 6-month range limit, date swap, bad spec format, CoinGecko failure, zero QoS weight - Update CLAUDE.md: remove stale /lava/price and ?timestamp= docs, add /provider-rewards endpoint reference
1 parent 1788e0a commit 6bf2f29

5 files changed

Lines changed: 97 additions & 7 deletions

File tree

‎CLAUDE.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ packages/
176176

177177
| Endpoint | Cache | Source | Description |
178178
|----------|-------|--------|-------------|
179-
| `GET /supply/total` | 300s | Chain RPC | Total supply in LAVA (plain text). `fetchTotalSupply() / 1_000_000`. Optional `?timestamp=` (unix seconds or ISO-8601) for historical supply — resolves timestamp to block height via binary search on Tendermint RPC, then queries supply at that block via `x-cosmos-block-height` header |
179+
| `GET /supply/total` | 300s | Chain RPC | Total supply in LAVA (plain text). `fetchTotalSupply() / 1_000_000` |
180180
| `GET /supply/circulating` | 300s | Chain RPC | Circulating supply in LAVA (plain text). Formula: `total - continuousVesting - periodicVesting - rewardPools`. Paginates through all ~53K accounts to calculate locked vesting amounts. 5 reward pools subtracted: validators_rewards_distribution_pool, validators_rewards_allocation_pool, providers_rewards_distribution_pool, providers_rewards_allocation_pool, iprpc_pool |
181181

182182
### Other (root-level)
@@ -190,7 +190,7 @@ packages/
190190
| `GET /apr` | 1800s | Chain RPC + CoinGecko | Per-entity APR percentiles matching jsinfo. Queries `estimated_{provider,validator}_rewards` for all providers/validators with 10k LAVA benchmark, converts multi-denom rewards to USD, compounds monthly, takes 80th percentile, caps at 30%. Returns `{ restaking_apr_percentile, staking_apr_percentile }`. Uses 7-day weighted Redis history when available |
191191
| `GET /all_providers_apr` | 1800s | Chain RPC + CoinGecko + Indexer MV | Per-provider APR data matching jsinfo. Returns array of providers with: APR, commission, 30d CU/relays, 10k LAVA reward breakdown, per-spec rewards (rewards_last_month), specs, avatar |
192192
| `GET /lava/specs` | 300s | Chain RPC | All chain specs (raw, used by frontend `useChainNames` hook) |
193-
| `GET /lava/price` | 300s | CoinGecko | LAVA USD price. Optional `?date=` (YYYY-MM-DD or unix seconds) for historical price via CoinGecko `/coins/{id}/history`. Returns `{ price }` (current) or `{ price, date }` (historical) |
193+
| `GET /provider-rewards?from=&to=` | 300s | Indexer MV + CoinGecko | Per-provider QoS-adjusted reward distribution. Required: `from`, `to` (YYYY-MM-DD, max 6 months). Optional: `specs` (chain filter). Returns `{ meta: { from, to, lavaUsdPrice, totalAdjustedRewards }, data: [{ provider, moniker, relays, cus, adjustedRewards, rewardShare, estimatedRewardsUsd }] }`. Uses historical LAVA price when date range is in the past. Price gracefully degrades to null on CoinGecko failure |
194194

195195
## Pagination Convention
196196

‎apps/api/src/__tests__/routes-provider-rewards.test.ts‎

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,4 +146,95 @@ describe("GET /provider-rewards", () => {
146146
const res = await app.inject({ method: "GET", url: "/provider-rewards" });
147147
expect(res.statusCode).toBe(400);
148148
});
149+
150+
it("rejects bad date format", async () => {
151+
const app = await buildApp();
152+
const res = await app.inject({
153+
method: "GET",
154+
url: "/provider-rewards?from=2025-1-1&to=2025-04-01",
155+
});
156+
expect(res.statusCode).toBe(400);
157+
});
158+
159+
it("rejects invalid date value", async () => {
160+
const app = await buildApp();
161+
const res = await app.inject({
162+
method: "GET",
163+
url: "/provider-rewards?from=2025-99-99&to=2025-04-01",
164+
});
165+
expect(res.statusCode).toBe(400);
166+
});
167+
168+
it("rejects date range exceeding 6 months", async () => {
169+
const app = await buildApp();
170+
const res = await app.inject({
171+
method: "GET",
172+
url: "/provider-rewards?from=2024-01-01&to=2025-01-01",
173+
});
174+
expect(res.statusCode).toBe(400);
175+
expect(JSON.parse(res.body).error).toMatch(/6 months/);
176+
});
177+
178+
it("swaps dates when to < from", async () => {
179+
const app = await buildApp();
180+
const res = await app.inject({
181+
method: "GET",
182+
url: "/provider-rewards?from=2025-04-01&to=2025-01-01",
183+
});
184+
expect(res.statusCode).toBe(200);
185+
const body = JSON.parse(res.body);
186+
expect(body.meta.from).toBe("2025-01-01");
187+
expect(body.meta.to).toBe("2025-04-01");
188+
});
189+
190+
it("rejects bad spec format", async () => {
191+
const app = await buildApp();
192+
const res = await app.inject({
193+
method: "GET",
194+
url: "/provider-rewards?from=2025-01-01&to=2025-04-01&specs=a",
195+
});
196+
expect(res.statusCode).toBe(400);
197+
expect(JSON.parse(res.body).error).toMatch(/bad spec format/);
198+
});
199+
200+
it("returns null lavaUsdPrice when CoinGecko fails", async () => {
201+
(fetchLavaUsdPrice as ReturnType<typeof vi.fn>).mockRejectedValue(
202+
new Error("CoinGecko down"),
203+
);
204+
(fetchLavaUsdPriceAt as ReturnType<typeof vi.fn>).mockRejectedValue(
205+
new Error("CoinGecko down"),
206+
);
207+
const app = await buildApp();
208+
const res = await app.inject({
209+
method: "GET",
210+
url: "/provider-rewards?from=2025-01-01&to=2025-04-01",
211+
});
212+
expect(res.statusCode).toBe(200);
213+
const body = JSON.parse(res.body);
214+
expect(body.meta.lavaUsdPrice).toBeNull();
215+
expect(body.data).toHaveLength(2);
216+
expect(body.data[0].estimatedRewardsUsd).toBeNull();
217+
});
218+
219+
it("returns zero adjusted rewards when qosWeight is zero", async () => {
220+
const zeroWeightMv = {
221+
mvRelayDailies: {
222+
groupedAggregates: [{
223+
keys: ["ETH1", "lava@1abc"],
224+
sum: {
225+
cu: "1000", relays: "500",
226+
qosSyncW: null, qosAvailW: null, qosLatencyW: null, qosWeight: "0",
227+
},
228+
}],
229+
},
230+
};
231+
(gqlSafe as ReturnType<typeof vi.fn>).mockResolvedValue(zeroWeightMv);
232+
const app = await buildApp();
233+
const res = await app.inject({
234+
method: "GET",
235+
url: "/provider-rewards?from=2025-01-01&to=2025-04-01",
236+
});
237+
const body = JSON.parse(res.body);
238+
expect(body.data[0].adjustedRewards).toBe(0);
239+
});
149240
});

‎apps/api/src/main.ts‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import { lavaRoutes } from "./routes/lava.js";
2020
import { allProvidersAprRoutes } from "./routes/all-providers-apr.js";
2121
import { relaysDbPlugin } from "./plugins/relays-db.js";
2222
import { optimizerMetricsRoutes } from "./routes/optimizer-metrics.js";
23-
import { adjustedRewardsRoutes } from "./routes/adjusted-rewards.js";
2423
import { providerRewardsRoutes } from "./routes/provider-rewards.js";
2524

2625
const PORT = parseInt(process.env.API_PORT ?? "8080", 10);
@@ -64,7 +63,6 @@ async function main() {
6463
await app.register(searchRoutes);
6564
await app.register(lavaRoutes, { prefix: "/lava" });
6665
await app.register(optimizerMetricsRoutes);
67-
await app.register(adjustedRewardsRoutes);
6866
await app.register(providerRewardsRoutes);
6967

7068
await app.register(healthProbePlugin);

‎apps/api/src/plugins/swagger.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export const swaggerPlugin = fp(async (app: FastifyInstance) => {
2222
{ name: "Search", description: "Search providers and specs" },
2323
{ name: "Lava", description: "Raw chain data for frontend consumption" },
2424
{ name: "Health", description: "Service health checks" },
25-
{ name: "Adjusted Rewards", description: "Per-(spec, provider) QoS-adjusted reward metrics" },
25+
{ name: "Provider Rewards", description: "Per-provider QoS-adjusted reward distribution with USD values" },
2626
],
2727
},
2828
});

‎apps/api/src/routes/provider-rewards.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,8 @@ export async function providerRewardsRoutes(app: FastifyInstance) {
154154
return map;
155155
})(),
156156

157-
isHistorical ? fetchLavaUsdPriceAt(dateTo) : fetchLavaUsdPrice(),
157+
(isHistorical ? fetchLavaUsdPriceAt(dateTo) : fetchLavaUsdPrice())
158+
.catch(() => null as number | null),
158159
]);
159160

160161
// ── Compute adjusted rewards per provider ─────────────────────
@@ -198,7 +199,7 @@ export async function providerRewardsRoutes(app: FastifyInstance) {
198199
cus: p.cus,
199200
adjustedRewards: p.adjustedRewards,
200201
rewardShare: share,
201-
estimatedRewardsUsd: share * lavaPrice,
202+
estimatedRewardsUsd: lavaPrice != null ? share * lavaPrice : null,
202203
};
203204
})
204205
.sort((a, b) => b.adjustedRewards - a.adjustedRewards);

0 commit comments

Comments
 (0)