diff --git a/src/contractBatch/aggregate.ts b/src/contractBatch/aggregate.ts index d4aa2f0..80ce020 100644 --- a/src/contractBatch/aggregate.ts +++ b/src/contractBatch/aggregate.ts @@ -21,12 +21,12 @@ const partitionResponses = (responses: string[]): string[][] => { return [response, ...partitionResponses(remainingResponses)] } -const extractErrorCallIndex = (e: Error) => { +export const extractErrorCallIndex = (e: Error) => { try { const errorCallText = e.toString() const sequencerErrorIndex = errorCallText.match( - /Error message: multicall (\d+) failed/, + /Error message: multicall (\d+).*failed/, )?.[1] if (sequencerErrorIndex) { return Number(sequencerErrorIndex) diff --git a/src/contractBatch/unit.test.ts b/src/contractBatch/unit.test.ts index 0c09760..1f2311b 100644 --- a/src/contractBatch/unit.test.ts +++ b/src/contractBatch/unit.test.ts @@ -1,6 +1,7 @@ import { describe, expect, mock, test } from "bun:test" import { ContractBatchProvider } from "./ContractBatchProvider" import { MinimalProviderInterface } from "../types" +import { extractErrorCallIndex } from "./aggregate.ts" function getMockProvider(responses: string[][]): MinimalProviderInterface { const blockNumber = "0x1" @@ -56,3 +57,27 @@ describe("ContractBatchProvider", () => { expect(provider.callContract).toHaveBeenCalledTimes(1) }) }) + +describe("extractErrorCallIndex", () => { + test("should extract call index from error message", () => { + const error = new Error("Error message: multicall 5 failed") + const result = extractErrorCallIndex(error) + expect(result).toBe(5) + }) + + test("should extract call index from error message with colon ", () => { + const error = new Error("Error message: multicall 12:300 failed") + const result = extractErrorCallIndex(error) + expect(result).toBe(12) + }) + + test("should throw error when pattern doesn't match", () => { + const error = new Error("Some other error message") + expect(() => extractErrorCallIndex(error)).toThrow() + }) + + test("should throw error when no index is present", () => { + const error = new Error("Error message: multicall failed") + expect(() => extractErrorCallIndex(error)).toThrow() + }) +})