Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 30 additions & 18 deletions pages/api/current-round.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { getCacheControlHeader, getCurrentRound } from "@lib/api";
import { getCacheControlHeader } from "@lib/api";
import { roundsManager } from "@lib/api/abis/main/RoundsManager";
import { getContractAddress } from "@lib/api/contracts";
import { internalError, methodNotAllowed } from "@lib/api/errors";
import { CurrentRoundInfo } from "@lib/api/types/get-current-round";
import { l1PublicClient } from "@lib/chains";
import { l2PublicClient } from "@lib/chains";
import { NextApiRequest, NextApiResponse } from "next";

const handler = async (
Expand All @@ -14,28 +16,38 @@ const handler = async (
if (method === "GET") {
res.setHeader("Cache-Control", getCacheControlHeader("minute"));

const {
data: { protocol, _meta },
} = await getCurrentRound();
const currentRound = protocol?.currentRound;
const roundsManagerAddress = await getContractAddress("RoundsManager");

if (!currentRound) {
throw new Error("No current round found");
}
const contract = {
address: roundsManagerAddress,
abi: roundsManager,
} as const;

if (!_meta?.block) {
throw new Error("No block number found");
}

const { id, startBlock, initialized } = currentRound;

const currentL2Block = _meta.block.number;
const currentL1Block = await l1PublicClient.getBlockNumber();
const [id, startBlock, initialized, currentL1Block, currentL2Block] =
await Promise.all([
l2PublicClient.readContract({
...contract,
functionName: "currentRound",
}),
l2PublicClient.readContract({
...contract,
functionName: "currentRoundStartBlock",
}),
l2PublicClient.readContract({
...contract,
functionName: "currentRoundInitialized",
}),
l2PublicClient.readContract({
...contract,
functionName: "blockNum",
}),
l2PublicClient.getBlockNumber(),
]);
Comment on lines +26 to +45

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shuold be handled internally in the client call.

Comment on lines +26 to +45

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win

Guarantee the "single snapshot" claimed in the PR description.

Problem: The PR states that round data is read using a "single snapshot" to avoid inconsistencies. However, mapping multiple readContract calls into Promise.all executes separate RPC requests (unless batch: { multicall: true } is enabled globally on l2PublicClient).
Why it matters: Independent RPC requests can be routed to differently-synced load-balanced nodes or straddle a new block, resulting in torn state (e.g., startBlock from block $N$ and blockNum from block $N+1$). This reintroduces the exact timing inconsistencies the PR aims to fix.
Suggested fix: Use l2PublicClient.multicall with allowFailure: false to guarantee all contract reads evaluate atomically in a single eth_call.

🛠 Proposed fix
-      const [id, startBlock, initialized, currentL1Block, currentL2Block] =
-        await Promise.all([
-          l2PublicClient.readContract({
-            ...contract,
-            functionName: "currentRound",
-          }),
-          l2PublicClient.readContract({
-            ...contract,
-            functionName: "currentRoundStartBlock",
-          }),
-          l2PublicClient.readContract({
-            ...contract,
-            functionName: "currentRoundInitialized",
-          }),
-          l2PublicClient.readContract({
-            ...contract,
-            functionName: "blockNum",
-          }),
-          l2PublicClient.getBlockNumber(),
-        ]);
+      const [[id, startBlock, initialized, currentL1Block], currentL2Block] =
+        await Promise.all([
+          l2PublicClient.multicall({
+            contracts: [
+              { ...contract, functionName: "currentRound" },
+              { ...contract, functionName: "currentRoundStartBlock" },
+              { ...contract, functionName: "currentRoundInitialized" },
+              { ...contract, functionName: "blockNum" },
+            ],
+            allowFailure: false,
+          }),
+          l2PublicClient.getBlockNumber(),
+        ]);
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const [id, startBlock, initialized, currentL1Block, currentL2Block] =
await Promise.all([
l2PublicClient.readContract({
...contract,
functionName: "currentRound",
}),
l2PublicClient.readContract({
...contract,
functionName: "currentRoundStartBlock",
}),
l2PublicClient.readContract({
...contract,
functionName: "currentRoundInitialized",
}),
l2PublicClient.readContract({
...contract,
functionName: "blockNum",
}),
l2PublicClient.getBlockNumber(),
]);
const [[id, startBlock, initialized, currentL1Block], currentL2Block] =
await Promise.all([
l2PublicClient.multicall({
contracts: [
{ ...contract, functionName: "currentRound" },
{ ...contract, functionName: "currentRoundStartBlock" },
{ ...contract, functionName: "currentRoundInitialized" },
{ ...contract, functionName: "blockNum" },
],
allowFailure: false,
}),
l2PublicClient.getBlockNumber(),
]);
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@pages/api/current-round.tsx` around lines 29 - 48, Replace the parallel
readContract calls in the current-round data-loading flow with
l2PublicClient.multicall, preserving the same five contract function reads and
result ordering. Configure the multicall with allowFailure: false so all values
are evaluated from one atomic snapshot, then continue destructuring the returned
results into id, startBlock, initialized, currentL1Block, and currentL2Block.


const roundInfo: CurrentRoundInfo = {
id: Number(id),
startBlock: Number(startBlock),
initialized,
initialized: Boolean(initialized),
currentL1Block: Number(currentL1Block),
currentL2Block: Number(currentL2Block),
};
Expand Down
Loading