From 587d9f6b52510f80b61c18609a42fa60f402ca48 Mon Sep 17 00:00:00 2001 From: Toni Tabak Date: Mon, 25 Mar 2024 14:19:36 +0100 Subject: [PATCH] feat: add type guard to receipt response status methods --- __tests__/utils/ethSigner.test.ts | 13 +++++++++++-- src/provider/errors.ts | 10 ++++++++-- src/utils/transactionReceipt.ts | 6 +++--- 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/__tests__/utils/ethSigner.test.ts b/__tests__/utils/ethSigner.test.ts index 30c62903d..21b420bcc 100644 --- a/__tests__/utils/ethSigner.test.ts +++ b/__tests__/utils/ethSigner.test.ts @@ -159,7 +159,12 @@ describe('Ethereum signer', () => { { maxFee: 1 * 10 ** 16 } ); const txR = await provider.waitForTransaction(respTransfer.transaction_hash); - expect(txR.execution_status).toBe('SUCCEEDED'); + + if (txR.isSuccess()) { + expect(txR.execution_status).toBe('SUCCEEDED'); + } else { + fail('txR not success'); + } }); test('ETH account declaration V2', async () => { @@ -279,7 +284,11 @@ describe('Ethereum signer', () => { }); const txR = await provider.waitForTransaction(respTransfer.transaction_hash); - expect(txR.execution_status).toBe('SUCCEEDED'); + if (txR.isSuccess()) { + expect(txR.execution_status).toBe('SUCCEEDED'); + } else { + fail('txR not success'); + } }); test('ETH account declaration V3', async () => { diff --git a/src/provider/errors.ts b/src/provider/errors.ts index 2824dec64..1e2a01b4b 100644 --- a/src/provider/errors.ts +++ b/src/provider/errors.ts @@ -37,13 +37,19 @@ export class CustomError extends Error { export class LibraryError extends CustomError {} export class GatewayError extends LibraryError { - constructor(message: string, public errorCode: string) { + constructor( + message: string, + public errorCode: string + ) { super(message); } } export class HttpError extends LibraryError { - constructor(message: string, public errorCode: number) { + constructor( + message: string, + public errorCode: number + ) { super(message); } } diff --git a/src/utils/transactionReceipt.ts b/src/utils/transactionReceipt.ts index ffc353985..9cbf09909 100644 --- a/src/utils/transactionReceipt.ts +++ b/src/utils/transactionReceipt.ts @@ -67,15 +67,15 @@ export class ReceiptTx implements TransactionReceiptUtilityInterface { return (callbacks as TransactionReceiptCallbacksDefault)._(); } - isSuccess() { + isSuccess(): this is SuccessfulTransactionReceiptResponse { return this.statusReceipt === 'success'; } - isReverted() { + isReverted(): this is RevertedTransactionReceiptResponse { return this.statusReceipt === 'reverted'; } - isRejected() { + isRejected(): this is RejectedTransactionReceiptResponse { return this.statusReceipt === 'rejected'; }