Skip to content

Commit feffe06

Browse files
fix: add tryWait to withdrawals (#37)
1 parent c881f68 commit feffe06

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

src/adapters/ethers/resources/withdrawals/index.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,15 @@ export interface WithdrawalsResource {
8484
opts: { for: 'l2' | 'ready' | 'finalized'; pollMs?: number; timeoutMs?: number },
8585
): Promise<TransactionReceiptZKsyncOS | TransactionReceipt | null>;
8686

87+
// Try to wait for a withdraw to be completed
88+
tryWait(
89+
h: WithdrawalWaitable | Hex,
90+
opts: { for: 'l2' | 'ready' | 'finalized'; pollMs?: number; timeoutMs?: number },
91+
): Promise<
92+
| { ok: true; value: TransactionReceiptZKsyncOS | TransactionReceipt }
93+
| { ok: false; error: unknown }
94+
>;
95+
8796
// Finalize a withdrawal operation on L1 (if not already finalized)
8897
// Returns the updated status and, if we sent the finalization tx, the L1 receipt
8998
// May throw if the withdrawal is not yet ready to finalize or if the finalization tx fails
@@ -509,6 +518,36 @@ export function createWithdrawalsResource(
509518
ctx: { l2TxHash, where: 'withdrawals.tryFinalize' },
510519
});
511520

521+
// tryWait is like wait, but returns a TryResult instead of throwing
522+
const tryWait = (
523+
h: WithdrawalWaitable | Hex,
524+
opts: { for: 'l2' | 'ready' | 'finalized'; pollMs?: number; timeoutMs?: number },
525+
) =>
526+
toResult<TransactionReceiptZKsyncOS | TransactionReceipt>(
527+
OP_WITHDRAWALS.tryWait,
528+
async () => {
529+
const v = await wait(h, opts);
530+
if (v) return v;
531+
throw createError('STATE', {
532+
resource: 'withdrawals',
533+
operation: 'withdrawals.tryWait',
534+
message:
535+
opts.for === 'l2'
536+
? 'No L2 receipt yet; the withdrawal has not executed on L2.'
537+
: 'No L1 receipt yet; the withdrawal has not been included on L1.',
538+
context: {
539+
for: opts.for,
540+
l2TxHash: typeof h === 'string' ? h : 'l2TxHash' in h ? (h.l2TxHash as Hex) : undefined,
541+
where: 'withdrawals.tryWait',
542+
},
543+
});
544+
},
545+
{
546+
message: 'Internal error while waiting for withdrawal.',
547+
ctx: { input: h, for: opts?.for, where: 'withdrawals.tryWait' },
548+
},
549+
);
550+
512551
return {
513552
quote,
514553
tryQuote,
@@ -520,6 +559,7 @@ export function createWithdrawalsResource(
520559
wait,
521560
finalize,
522561
tryFinalize,
562+
tryWait,
523563
};
524564
}
525565

src/adapters/viem/resources/withdrawals/index.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,15 @@ export interface WithdrawalsResource {
9595
opts: { for: 'l2' | 'ready' | 'finalized'; pollMs?: number; timeoutMs?: number },
9696
): Promise<TransactionReceiptZKsyncOS | TransactionReceipt | null>;
9797

98+
// Try to wait for a withdraw to be completed
99+
tryWait(
100+
h: WithdrawalWaitable | Hex,
101+
opts: { for: 'l2' | 'ready' | 'finalized'; pollMs?: number; timeoutMs?: number },
102+
): Promise<
103+
| { ok: true; value: TransactionReceiptZKsyncOS | TransactionReceipt }
104+
| { ok: false; error: unknown }
105+
>;
106+
98107
// Finalize a withdrawal operation on L1 (if not already finalized)
99108
// Returns the updated status and, if we sent the finalization tx, the L1 receipt
100109
// May throw if the withdrawal is not yet ready to finalize or if the finalization tx fails
@@ -553,6 +562,36 @@ export function createWithdrawalsResource(
553562
ctx: { l2TxHash, where: 'withdrawals.tryFinalize' },
554563
});
555564

565+
// tryWait is like wait, but returns a TryResult instead of throwing
566+
const tryWait = (
567+
h: WithdrawalWaitable | Hex,
568+
opts: { for: 'l2' | 'ready' | 'finalized'; pollMs?: number; timeoutMs?: number },
569+
) =>
570+
toResult<TransactionReceiptZKsyncOS | TransactionReceipt>(
571+
OP_WITHDRAWALS.tryWait,
572+
async () => {
573+
const v = await wait(h, opts);
574+
if (v) return v;
575+
throw createError('STATE', {
576+
resource: 'withdrawals',
577+
operation: 'withdrawals.tryWait',
578+
message:
579+
opts.for === 'l2'
580+
? 'No L2 receipt yet; the withdrawal has not executed on L2.'
581+
: 'No L1 receipt yet; the withdrawal has not been included on L1.',
582+
context: {
583+
for: opts.for,
584+
l2TxHash: typeof h === 'string' ? h : 'l2TxHash' in h ? (h.l2TxHash as Hex) : undefined,
585+
where: 'withdrawals.tryWait',
586+
},
587+
});
588+
},
589+
{
590+
message: 'Internal error while waiting for withdrawal.',
591+
ctx: { input: h, for: opts?.for, where: 'withdrawals.tryWait' },
592+
},
593+
);
594+
556595
return {
557596
quote,
558597
tryQuote,
@@ -564,6 +603,7 @@ export function createWithdrawalsResource(
564603
wait,
565604
finalize,
566605
tryFinalize,
606+
tryWait,
567607
};
568608
}
569609

src/core/types/__tests__/errors.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,5 +107,6 @@ describe('types/errors — operation constants', () => {
107107
'withdrawals.finalize.readiness:isWithdrawalFinalized',
108108
);
109109
expect(OP_WITHDRAWALS.finalize.wait).toBe('withdrawals.finalize.finalizeDeposit:wait');
110+
expect(OP_WITHDRAWALS.tryWait).toBe('withdrawals.tryWait');
110111
});
111112
});

0 commit comments

Comments
 (0)