@@ -3,14 +3,16 @@ import type { ThirdwebClient } from "../../../../client/client.js";
33import { getContract } from "../../../../contract/contract.js" ;
44import { resolveAddress } from "../../../../extensions/ens/resolve-address.js" ;
55import { transfer } from "../../../../extensions/erc20/write/transfer.js" ;
6- import { sendTransaction } from "../../../../transaction/actions/send-transaction.js" ;
76import { waitForReceipt } from "../../../../transaction/actions/wait-for-tx-receipt.js" ;
87import { prepareTransaction } from "../../../../transaction/prepare-transaction.js" ;
98import { isAddress } from "../../../../utils/address.js" ;
109import { isValidENSName } from "../../../../utils/ens/isValidENSName.js" ;
1110import { toWei } from "../../../../utils/units.js" ;
1211import { invalidateWalletBalance } from "../../providers/invalidateWalletBalance.js" ;
1312import { useActiveWallet } from "./useActiveWallet.js" ;
13+ import { sendTransaction } from "../../../../transaction/actions/send-transaction.js" ;
14+ import { estimateGas } from "../../../../transaction/actions/estimate-gas.js" ;
15+ import { getWalletBalance } from "../../../../wallets/utils/getWalletBalance.js" ;
1416
1517/**
1618 * Send Native or ERC20 tokens from active wallet to given address.
@@ -85,13 +87,24 @@ export function useSendToken(client: ThirdwebClient) {
8587 to,
8688 value : toWei ( amount ) ,
8789 } ) ;
90+ const gasEstimate = await estimateGas ( {
91+ transaction : sendNativeTokenTx ,
92+ account,
93+ } ) ;
94+ const balance = await getWalletBalance ( {
95+ address : account . address ,
96+ chain : activeChain ,
97+ client,
98+ } ) ;
99+ if ( toWei ( amount ) + gasEstimate > balance . value ) {
100+ throw new Error ( "Insufficient balance for transfer amount and gas" ) ;
101+ }
88102
89- return sendTransaction ( {
103+ return await sendTransaction ( {
90104 transaction : sendNativeTokenTx ,
91105 account,
92106 } ) ;
93107 }
94-
95108 // erc20 token transfer
96109 else {
97110 const contract = getContract ( {
@@ -106,7 +119,7 @@ export function useSendToken(client: ThirdwebClient) {
106119 to,
107120 } ) ;
108121
109- return sendTransaction ( {
122+ return await sendTransaction ( {
110123 transaction : tx ,
111124 account,
112125 } ) ;
@@ -121,6 +134,7 @@ export function useSendToken(client: ThirdwebClient) {
121134 transactionHash : data . transactionHash ,
122135 client,
123136 chain : data . chain ,
137+ maxBlocksWaitTime : 10_000 ,
124138 } ) ;
125139 }
126140 invalidateWalletBalance ( queryClient ) ;
0 commit comments