@@ -33,7 +33,7 @@ import {
3333import { ContractVerificationStatus , TokenBalance } from '@0xsequence/indexer'
3434import { useState , ChangeEvent , useRef , useEffect } from 'react'
3535import { encodeFunctionData , formatUnits , parseUnits , toHex , zeroAddress , Hex } from 'viem'
36- import { useAccount , useChainId , useSwitchChain , useConfig , useSendTransaction , usePublicClient } from 'wagmi'
36+ import { useAccount , useChainId , useSwitchChain , useConfig , usePublicClient , useWalletClient } from 'wagmi'
3737
3838import { WalletSelect } from '../../components/Select/WalletSelect'
3939import { SendItemInfo } from '../../components/SendItemInfo'
@@ -68,7 +68,7 @@ export const SendCoin = ({ chainId, contractAddress }: SendCoinProps) => {
6868 const { fiatCurrency } = useSettings ( )
6969 const [ amount , setAmount ] = useState < string > ( '0' )
7070 const [ toAddress , setToAddress ] = useState < string > ( '' )
71- const { sendTransaction } = useSendTransaction ( )
71+ const { data : walletClient } = useWalletClient ( )
7272 const [ isSendTxnPending , setIsSendTxnPending ] = useState ( false )
7373 const [ showConfirmation , setShowConfirmation ] = useState ( false )
7474 const [ feeOptions , setFeeOptions ] = useState <
@@ -229,57 +229,90 @@ export const SendCoin = ({ chainId, contractAddress }: SendCoinProps) => {
229229 }
230230 } )
231231
232+ if ( ! walletClient ) {
233+ console . error ( 'Wallet client not found' )
234+ toast ( {
235+ title : 'Error' ,
236+ description : 'Wallet client not available. Please ensure your wallet is connected.' ,
237+ variant : 'error'
238+ } )
239+ setIsSendTxnPending ( false )
240+ return
241+ }
242+
232243 setIsSendTxnPending ( true )
233244
234245 const sendAmount = parseUnits ( amountToSendFormatted , decimals )
246+ let txHash : Hex | undefined
235247
236- const txOptions = {
237- onSettled : async ( hash : `0x${string } ` | undefined ) => {
238- setIsBackButtonEnabled ( true )
248+ try {
249+ if ( isNativeCoin ) {
250+ console . log ( 'Sending native coin via walletClient' )
251+ txHash = await walletClient . sendTransaction ( {
252+ account : accountAddress as `0x${string } `,
253+ to : toAddress as `0x${string } `,
254+ value : BigInt ( sendAmount . toString ( ) ) ,
255+ chain : chains . find ( c => c . id === chainId )
256+ } )
257+ } else {
258+ console . log ( 'Sending ERC20 coin via walletClient' )
259+ txHash = await walletClient . sendTransaction ( {
260+ account : accountAddress as `0x${string } `,
261+ to : tokenBalance ?. contractAddress as `0x${string } `,
262+ data : encodeFunctionData ( { abi : ERC_20_ABI , functionName : 'transfer' , args : [ toAddress , toHex ( sendAmount ) ] } ) ,
263+ chain : chains . find ( c => c . id === chainId )
264+ } )
265+ }
239266
240- if ( hash ) {
241- setNavigation ( {
242- location : 'home'
243- } )
244- setIsSendTxnPending ( false )
245- if ( publicClient ) {
246- await waitForTransactionReceipt ( {
247- indexerClient,
248- txnHash : hash as Hex ,
249- publicClient,
250- confirmations : TRANSACTION_CONFIRMATIONS_DEFAULT
251- } )
252- clearCachedBalances ( )
253- }
254- }
255- setIsSendTxnPending ( false )
267+ // Handle successful transaction submission
268+ setIsBackButtonEnabled ( true )
269+ if ( txHash ) {
270+ setNavigation ( {
271+ location : 'home'
272+ } )
273+ setIsSendTxnPending ( false ) // Set pending to false immediately after getting hash
256274
257275 toast ( {
258276 title : 'Transaction sent' ,
259277 description : `Successfully sent ${ amountToSendFormatted } ${ symbol } to ${ toAddress } ` ,
260278 variant : 'success'
261279 } )
262- }
263- }
264280
265- if ( isNativeCoin ) {
266- sendTransaction (
267- {
268- to : toAddress as `0x${string } `,
269- value : BigInt ( sendAmount . toString ( ) ) ,
270- gas : null
271- } ,
272- txOptions
273- )
274- } else {
275- sendTransaction (
276- {
277- to : tokenBalance ?. contractAddress as `0x${string } `,
278- data : encodeFunctionData ( { abi : ERC_20_ABI , functionName : 'transfer' , args : [ toAddress , toHex ( sendAmount ) ] } ) ,
279- gas : null
280- } ,
281- txOptions
282- )
281+ // Wait for receipt in the background
282+ if ( publicClient ) {
283+ waitForTransactionReceipt ( {
284+ indexerClient,
285+ txnHash : txHash ,
286+ publicClient,
287+ confirmations : TRANSACTION_CONFIRMATIONS_DEFAULT
288+ } )
289+ . then ( ( ) => {
290+ clearCachedBalances ( )
291+ console . log ( 'Transaction confirmed and balances cleared:' , txHash )
292+ } )
293+ . catch ( error => {
294+ console . error ( 'Error waiting for transaction receipt:' , error )
295+ // Optionally show another toast for confirmation failure
296+ } )
297+ }
298+ } else {
299+ // Handle case where txHash is unexpectedly undefined
300+ setIsSendTxnPending ( false )
301+ toast ( {
302+ title : 'Transaction Error' ,
303+ description : 'Transaction submitted but no hash received.' ,
304+ variant : 'error'
305+ } )
306+ }
307+ } catch ( error : any ) {
308+ console . error ( 'Transaction failed:' , error )
309+ setIsSendTxnPending ( false )
310+ setIsBackButtonEnabled ( true )
311+ toast ( {
312+ title : 'Transaction Failed' ,
313+ description : error ?. shortMessage || error ?. message || 'An unknown error occurred.' ,
314+ variant : 'error'
315+ } )
283316 }
284317 }
285318
0 commit comments