Skip to content

Commit

Permalink
yo
Browse files Browse the repository at this point in the history
  • Loading branch information
Zizzamia committed Aug 14, 2024
1 parent d5cd575 commit 3c29bde
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 34 deletions.
25 changes: 7 additions & 18 deletions src/transaction/components/TransactionProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -30,6 +26,7 @@ import type {
TransactionContextType,
TransactionProviderReact,
} from '../types';
import { isUserRejectedRequestError } from '../utils/isUserRejectedRequestError';

const emptyContext = {} as TransactionContextType;
export const TransactionContext =
Expand Down Expand Up @@ -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]);
Expand Down Expand Up @@ -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 {
Expand Down
13 changes: 5 additions & 8 deletions src/transaction/hooks/useWriteContract.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import type { Address, TransactionExecutionError } from 'viem';
import type { Address } from 'viem';
import { useWriteContract as useWriteContractWagmi } from 'wagmi';
import {
GENERIC_ERROR_MESSAGE,
UNCAUGHT_WRITE_CONTRACT_ERROR_CODE,
WRITE_CONTRACT_ERROR_CODE,
} from '../constants';
import type { UseWriteContractParams } from '../types';
import { isUserRejectedRequestError } from '../utils/isUserRejectedRequestError';

/**
* Wagmi hook for single contract transactions.
Expand All @@ -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: {
Expand Down
12 changes: 4 additions & 8 deletions src/transaction/hooks/useWriteContracts.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { TransactionExecutionError } from 'viem';
import { useWriteContracts as useWriteContractsWagmi } from 'wagmi/experimental';
import {
GENERIC_ERROR_MESSAGE,
Expand All @@ -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.
Expand All @@ -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: {
Expand Down
21 changes: 21 additions & 0 deletions src/transaction/utils/isUserRejectedRequestError.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
8 changes: 8 additions & 0 deletions src/transaction/utils/isUserRejectedRequestError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import type { TransactionExecutionError } from 'viem';

export function isUserRejectedRequestError(err: unknown) {
return (
(err as TransactionExecutionError)?.cause?.name ===
'UserRejectedRequestError'
);
}

0 comments on commit 3c29bde

Please sign in to comment.