diff --git a/src/transaction/components/TransactionProvider.tsx b/src/transaction/components/TransactionProvider.tsx index 1fa05400b2..086b1fd6e0 100644 --- a/src/transaction/components/TransactionProvider.tsx +++ b/src/transaction/components/TransactionProvider.tsx @@ -5,11 +5,7 @@ import { useEffect, useState, } from 'react'; -import type { - Address, - TransactionExecutionError, - TransactionReceipt, -} from 'viem'; +import type { Address, TransactionReceipt } from 'viem'; import { useAccount, useConfig, @@ -30,6 +26,7 @@ import type { TransactionContextType, TransactionProviderReact, } from '../types'; +import { isUserRejectedRequestError } from '../utils/isUserRejectedRequestError'; const emptyContext = {} as TransactionContextType; export const TransactionContext = @@ -144,15 +141,10 @@ export function TransactionProvider({ try { await writeContractAsync?.(contract); } catch (err) { - // if user rejected request - if ( - (err as TransactionExecutionError)?.cause?.name === - 'UserRejectedRequestError' - ) { - setErrorMessage('Request denied.'); - } else { - setErrorMessage(GENERIC_ERROR_MESSAGE); - } + const errorMessage = isUserRejectedRequestError(err) + ? 'Request denied.' + : GENERIC_ERROR_MESSAGE; + setErrorMessage(errorMessage); } } }, [contracts, writeContractAsync]); @@ -187,10 +179,7 @@ export function TransactionProvider({ setErrorMessage(GENERIC_ERROR_MESSAGE); } // handles user rejected request error - } else if ( - (err as TransactionExecutionError)?.cause?.name === - 'UserRejectedRequestError' - ) { + } else if (isUserRejectedRequestError(err)) { setErrorMessage('Request denied.'); // handles generic error } else { diff --git a/src/transaction/hooks/useWriteContract.ts b/src/transaction/hooks/useWriteContract.ts index 744f8fab6b..07918f268b 100644 --- a/src/transaction/hooks/useWriteContract.ts +++ b/src/transaction/hooks/useWriteContract.ts @@ -1,4 +1,4 @@ -import type { Address, TransactionExecutionError } from 'viem'; +import type { Address } from 'viem'; import { useWriteContract as useWriteContractWagmi } from 'wagmi'; import { GENERIC_ERROR_MESSAGE, @@ -6,6 +6,7 @@ import { WRITE_CONTRACT_ERROR_CODE, } from '../constants'; import type { UseWriteContractParams } from '../types'; +import { isUserRejectedRequestError } from '../utils/isUserRejectedRequestError'; /** * Wagmi hook for single contract transactions. @@ -21,13 +22,9 @@ export function useWriteContract({ const { status, writeContractAsync, data } = useWriteContractWagmi({ mutation: { onError: (e) => { - let errorMessage = GENERIC_ERROR_MESSAGE; - if ( - (e as TransactionExecutionError)?.cause?.name === - 'UserRejectedRequestError' - ) { - errorMessage = 'Request denied.'; - } + const errorMessage = isUserRejectedRequestError(e) + ? 'Request denied.' + : GENERIC_ERROR_MESSAGE; setLifeCycleStatus({ statusName: 'error', statusData: { diff --git a/src/transaction/hooks/useWriteContracts.ts b/src/transaction/hooks/useWriteContracts.ts index 158a40dfc0..bd7f55e5df 100644 --- a/src/transaction/hooks/useWriteContracts.ts +++ b/src/transaction/hooks/useWriteContracts.ts @@ -1,4 +1,3 @@ -import type { TransactionExecutionError } from 'viem'; import { useWriteContracts as useWriteContractsWagmi } from 'wagmi/experimental'; import { GENERIC_ERROR_MESSAGE, @@ -7,6 +6,7 @@ import { WRITE_CONTRACTS_ERROR_CODE, } from '../constants'; import type { UseWriteContractsParams } from '../types'; +import { isUserRejectedRequestError } from '../utils/isUserRejectedRequestError'; /** * useWriteContracts: Experimental Wagmi hook for batching transactions. @@ -29,13 +29,9 @@ export function useWriteContracts({ if (e.message.includes(METHOD_NOT_SUPPORTED_ERROR_SUBSTRING)) { return; } - let errorMessage = GENERIC_ERROR_MESSAGE; - if ( - (e as TransactionExecutionError)?.cause?.name === - 'UserRejectedRequestError' - ) { - errorMessage = 'Request denied.'; - } + const errorMessage = isUserRejectedRequestError(e) + ? 'Request denied.' + : GENERIC_ERROR_MESSAGE; setLifeCycleStatus({ statusName: 'error', statusData: { diff --git a/src/transaction/utils/isUserRejectedRequestError.test.ts b/src/transaction/utils/isUserRejectedRequestError.test.ts new file mode 100644 index 0000000000..ce05b640bf --- /dev/null +++ b/src/transaction/utils/isUserRejectedRequestError.test.ts @@ -0,0 +1,21 @@ +import { isUserRejectedRequestError } from './isUserRejectedRequestError'; + +describe('isUserRejectedRequestError', () => { + it('should return true if error is UserRejectedRequestError', () => { + const error = { + cause: { + name: 'UserRejectedRequestError', + }, + }; + expect(isUserRejectedRequestError(error)).toBe(true); + }); + + it('should return false if error is not UserRejectedRequestError', () => { + const error = { + cause: { + name: 'Error', + }, + }; + expect(isUserRejectedRequestError(error)).toBe(false); + }); +}); diff --git a/src/transaction/utils/isUserRejectedRequestError.ts b/src/transaction/utils/isUserRejectedRequestError.ts new file mode 100644 index 0000000000..1fbc185484 --- /dev/null +++ b/src/transaction/utils/isUserRejectedRequestError.ts @@ -0,0 +1,8 @@ +import type { TransactionExecutionError } from 'viem'; + +export function isUserRejectedRequestError(err: unknown) { + return ( + (err as TransactionExecutionError)?.cause?.name === + 'UserRejectedRequestError' + ); +}