11import { erc20Token , nativeToken } from '@cardinal-cryptography/shielder-sdk' ;
22import { useMutation , useQueryClient } from '@tanstack/react-query' ;
3- import { useRef } from 'react' ;
4- import { erc20Abi , TransactionExecutionError , UserRejectedRequestError } from 'viem' ;
3+ import { erc20Abi } from 'viem' ;
54import { useAccount , usePublicClient , useSendTransaction , useWalletClient } from 'wagmi' ;
65
76import { Token } from 'src/domains/chains/types/misc' ;
87import useChain from 'src/domains/chains/utils/useChain' ;
98import { useToast } from 'src/domains/misc/components/Toast' ;
109import getQueryKey from 'src/domains/misc/utils/getQueryKey' ;
10+ import { getWalletErrorName , handleWalletError } from 'src/domains/shielder/utils/walletErrors' ;
1111
1212import useShielderClient from './useShielderClient' ;
1313
@@ -20,7 +20,6 @@ export const useShield = () => {
2020 const queryClient = useQueryClient ( ) ;
2121 const chainConfig = useChain ( ) ;
2222 const { showToast } = useToast ( ) ;
23- const rejectedTxRef = useRef ( false ) ;
2423
2524 const sendTransactionWithToast = async ( params : Parameters < typeof sendTransactionAsync > [ 0 ] ) => {
2625 const toast = showToast ( {
@@ -34,36 +33,20 @@ export const useShield = () => {
3433 toast . updateToast ( {
3534 subtitle : 'Still waiting? Make sure you signed the transaction from your wallet.' ,
3635 } ) ;
37- } , 30_000 ) ;
36+ } , 10_000 ) ;
3837
3938 try {
40- const tx = await sendTransactionAsync ( params ) ;
41- clearTimeout ( timeoutId ) ;
42- toast . dismissToast ( ) ;
43- return tx ;
39+ return await sendTransactionAsync ( params ) ;
4440 } catch ( error ) {
41+ return handleWalletError ( error ) ;
42+ } finally {
4543 clearTimeout ( timeoutId ) ;
4644 toast . dismissToast ( ) ;
47-
48- const isRejected = error instanceof TransactionExecutionError && error . cause instanceof UserRejectedRequestError ;
49-
50- if ( isRejected ) {
51- rejectedTxRef . current = true ;
52- showToast ( {
53- status : 'error' ,
54- title : 'Transaction rejected' ,
55- subtitle : 'Transaction has been rejected in the wallet' ,
56- } ) ;
57- }
58-
59- throw error ;
6045 }
6146 } ;
6247
6348 const { mutateAsync : shield , isPending : isShielding , ...meta } = useMutation ( {
6449 mutationFn : async ( { token, amount } : { token : Token , amount : bigint , onSuccess ?: ( ) => void } ) => {
65- rejectedTxRef . current = false ;
66-
6750 if ( ! shielderClient ) throw new Error ( 'Shielder is not ready' ) ;
6851 if ( ! walletAddress ) throw new Error ( 'Address is not available' ) ;
6952
@@ -120,11 +103,30 @@ export const useShield = () => {
120103 queryKey : getQueryKey . tokenPublicBalance ( 'native' , chainId . toString ( ) , walletAddress ) ,
121104 } ) ;
122105
123- if ( ! rejectedTxRef . current ) {
124- showToast ( {
125- status : 'error' ,
126- title : 'Shielding failed' ,
127- } ) ;
106+ const knowErrorName = getWalletErrorName ( error ) ;
107+
108+ switch ( knowErrorName ) {
109+ case 'USER_REJECTED_REQUEST' :
110+ showToast ( {
111+ status : 'error' ,
112+ title : 'Transaction rejected' ,
113+ subtitle : 'Transaction has been rejected in the wallet' ,
114+ } ) ;
115+ break ;
116+
117+ case 'LOCKED_OR_UNAUTHORIZED' :
118+ showToast ( {
119+ status : 'error' ,
120+ title : 'Transaction not initiated' ,
121+ subtitle : 'Make sure your wallet is unlocked and your account is authorized.' ,
122+ } ) ;
123+ break ;
124+
125+ default :
126+ showToast ( {
127+ status : 'error' ,
128+ title : 'Shielding failed' ,
129+ } ) ;
128130 }
129131 } ,
130132 } ) ;
0 commit comments