Skip to content
Open
Show file tree
Hide file tree
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: 41 additions & 7 deletions packages/sdk/src/actions/getFillByDepositTx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
TransactionReceipt,
} from "viem";
import { MAINNET_INDEXER_API } from "../constants/index.js";
import { NoFillLogError } from "../errors/index.js";
import { NoFillLogError, WaitForFillTimeoutError } from "../errors/index.js";
import { FillEventLog, IndexerStatusResponse } from "../types/index.js";
import { parseFillLogs } from "./waitForFillTx.js";

Expand Down Expand Up @@ -127,26 +127,60 @@ export type FillStatus = {
parsedFillEvent: FillEventLog;
};

// Default max wait for a fill before rejecting (5 minutes).
export const DEFAULT_WAIT_FOR_FILL_TIMEOUT_MS = 300_000;

export async function waitForFillByDepositTx(
params: GetFillByDepositTxParams & {
pollingInterval?: number;
timeout?: number;
},
): ReturnType<typeof getFillByDepositTx> {
const interval =
params?.pollingInterval ?? params.destinationChainClient.pollingInterval;
params.pollingInterval ?? params.destinationChainClient.pollingInterval;
const timeoutMs = params.timeout ?? DEFAULT_WAIT_FOR_FILL_TIMEOUT_MS;
const deadline = Date.now() + timeoutMs;

return new Promise((res) => {
return new Promise((resolve, reject) => {
const poll = () => {
getFillByDepositTx(params)
.then((response) => {
if (response.fillTxReceipt) {
res(response);
} else {
setTimeout(poll, interval);
resolve(response);
return;
}

if (Date.now() >= deadline) {
reject(
new WaitForFillTimeoutError(
BigInt(params.deposit.depositId),
params.deposit.destinationChainId,
timeoutMs,
params.deposit.depositTxHash,
),
);
return;
}

setTimeout(poll, interval);
})
.catch((error) => {
params?.logger ? params.logger.error(error) : console.log(error);
params.logger ? params.logger.error(error) : console.log(error);

if (Date.now() >= deadline) {
reject(
error instanceof NoFillLogError
? new WaitForFillTimeoutError(
BigInt(params.deposit.depositId),
params.deposit.destinationChainId,
timeoutMs,
params.deposit.depositTxHash,
)
: error,
);
return;
}

setTimeout(poll, interval);
});
};
Expand Down
15 changes: 15 additions & 0 deletions packages/sdk/src/errors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,18 @@ export class NoFillLogError extends Error {
this.name = "Fill Log Not Found";
}
}

export class WaitForFillTimeoutError extends Error {
constructor(
depositId: bigint,
chainId: number,
timeoutMs: number,
depositTxHash?: Hash,
) {
super(
`Timed out after ${timeoutMs}ms waiting for fill on chain ${chainId} for deposit id #${depositId.toString()}${depositTxHash ? ` with depositTxHash ${depositTxHash}` : "."}`,
);
this.name = "WaitForFillTimeout";
}
}

57 changes: 57 additions & 0 deletions packages/sdk/test/unit/actions/waitForFillByDepositTx.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
import {
waitForFillByDepositTx,
DEFAULT_WAIT_FOR_FILL_TIMEOUT_MS,
} from "../../../src/actions/getFillByDepositTx.js";
import { WaitForFillTimeoutError } from "../../../src/errors/index.js";
import type { PublicClient } from "viem";

describe("waitForFillByDepositTx", () => {
beforeEach(() => {
vi.stubGlobal(
"fetch",
vi.fn().mockRejectedValue(new Error("indexer unavailable")),
);
});

afterEach(() => {
vi.unstubAllGlobals();
});

test("rejects with WaitForFillTimeoutError when no fill appears before timeout", async () => {
const destinationChainClient = {
pollingInterval: 20,
getLogs: vi.fn().mockResolvedValue([]),
getTransactionReceipt: vi.fn(),
getBlock: vi.fn(),
} as unknown as PublicClient;

await expect(
waitForFillByDepositTx({
deposit: {
depositId: 42n,
originChainId: 1,
destinationChainId: 10,
destinationSpokePoolAddress:
"0x0000000000000000000000000000000000000001",
message: "0x",
},
destinationChainClient,
indexerUrl: "http://127.0.0.1:9",
timeout: 80,
pollingInterval: 20,
logger: {
error: vi.fn(),
warn: vi.fn(),
debug: vi.fn(),
info: vi.fn(),
},
}),
).rejects.toBeInstanceOf(WaitForFillTimeoutError);
});

test("exports a finite default timeout so callers are not left polling forever", () => {
expect(DEFAULT_WAIT_FOR_FILL_TIMEOUT_MS).toBeGreaterThan(0);
expect(DEFAULT_WAIT_FOR_FILL_TIMEOUT_MS).toBe(300_000);
});
});