Skip to content

Commit b31ac81

Browse files
authored
Merge pull request #214 from r4topunk/chore/mor-note-diag
chore: _morNote diagnostic
2 parents 7bf924b + 93f7fbe commit b31ac81

2 files changed

Lines changed: 11 additions & 7 deletions

File tree

src/app/api/stake-graph/route.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { NextResponse } from "next/server";
2-
import { getStakeGraph } from "@/services/stake-graph";
2+
import { getStakeGraph, lastMorNote } from "@/services/stake-graph";
33

44
// Recompute at most once a minute; serve stale for 5 more while revalidating.
55
export const revalidate = 60;
@@ -9,7 +9,7 @@ export async function GET() {
99
const graph = await getStakeGraph();
1010
// Diagnostic: is the Etherscan key present in this deploy's env? (temporary)
1111
const _etherscanKey = Boolean(process.env.ETHERSCAN_API_KEY || process.env.BASESCAN_API_KEY);
12-
return NextResponse.json({ ...graph, _etherscanKey }, {
12+
return NextResponse.json({ ...graph, _etherscanKey, _morNote: lastMorNote() }, {
1313
headers: { "Cache-Control": "public, s-maxage=60, stale-while-revalidate=300" },
1414
});
1515
} catch {

src/services/stake-graph.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,11 @@ const ETHERSCAN_KEY = process.env.ETHERSCAN_API_KEY || process.env.BASESCAN_API_
143143
const USER_REFERRED_SIG = keccak256(toHex("UserReferred(uint256,address,address,uint256)"));
144144
const pad32 = (a: Address) => `0x000000000000000000000000${a.slice(2).toLowerCase()}`;
145145

146+
// Temporary: capture the last Etherscan response so /api/stake-graph can expose
147+
// why the MOR scan came back empty (rate limit vs bad key vs no records).
148+
let _morNote = "init";
149+
export function lastMorNote() { return _morNote; }
150+
146151
/** A rider's referred stakes on a pool, straight from the UserReferred events. */
147152
async function etherscanReferred(pool: Address, referrer: Address, key: string): Promise<Array<{ user: Address; amount: bigint }>> {
148153
const url =
@@ -151,16 +156,15 @@ async function etherscanReferred(pool: Address, referrer: Address, key: string):
151156
`&fromBlock=0&toBlock=latest&page=1&offset=1000&apikey=${key}`;
152157
for (let i = 0; i < 3; i++) {
153158
try {
154-
// no-store so each retry is a real call — the route's own cache (60s)
155-
// handles caching, and a transient rate-limit won't stick in the data cache.
156159
const res = await fetch(url, { cache: "no-store" });
157-
const j = (await res.json()) as { status?: string; message?: string; result?: Array<{ topics: string[]; data: string }> };
160+
const j = (await res.json()) as { status?: string; message?: string; result?: unknown };
161+
_morNote = `${j.status ?? "?"}|${String(j.message ?? "?").slice(0, 40)}|${Array.isArray(j.result) ? j.result.length : typeof j.result}`;
158162
if (j.status === "1" && Array.isArray(j.result)) {
159-
return j.result.map((l) => ({ user: getAddress(`0x${l.topics[2].slice(26)}`), amount: BigInt(l.data) }));
163+
return (j.result as Array<{ topics: string[]; data: string }>).map((l) => ({ user: getAddress(`0x${l.topics[2].slice(26)}`), amount: BigInt(l.data) }));
160164
}
161165
if (typeof j.message === "string" && /rate limit/i.test(j.message)) { await sleep(500); continue; }
162166
return []; // "No records found"
163-
} catch { await sleep(300); }
167+
} catch (e) { _morNote = `throw|${String(e).slice(0, 40)}`; await sleep(300); }
164168
}
165169
return [];
166170
}

0 commit comments

Comments
 (0)