@@ -3,12 +3,14 @@ import type { ThirdwebClient } from "../../../../client/client.js";
3
3
import { getContract } from "../../../../contract/contract.js" ;
4
4
import { resolveAddress } from "../../../../extensions/ens/resolve-address.js" ;
5
5
import { transfer } from "../../../../extensions/erc20/write/transfer.js" ;
6
+ import { estimateGas } from "../../../../transaction/actions/estimate-gas.js" ;
6
7
import { sendTransaction } from "../../../../transaction/actions/send-transaction.js" ;
7
8
import { waitForReceipt } from "../../../../transaction/actions/wait-for-tx-receipt.js" ;
8
9
import { prepareTransaction } from "../../../../transaction/prepare-transaction.js" ;
9
10
import { isAddress } from "../../../../utils/address.js" ;
10
11
import { isValidENSName } from "../../../../utils/ens/isValidENSName.js" ;
11
12
import { toWei } from "../../../../utils/units.js" ;
13
+ import { getWalletBalance } from "../../../../wallets/utils/getWalletBalance.js" ;
12
14
import { invalidateWalletBalance } from "../../providers/invalidateWalletBalance.js" ;
13
15
import { useActiveWallet } from "./useActiveWallet.js" ;
14
16
@@ -85,13 +87,24 @@ export function useSendToken(client: ThirdwebClient) {
85
87
to,
86
88
value : toWei ( amount ) ,
87
89
} ) ;
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
+ }
88
102
89
- return sendTransaction ( {
103
+ return await sendTransaction ( {
90
104
transaction : sendNativeTokenTx ,
91
105
account,
92
106
} ) ;
93
107
}
94
-
95
108
// erc20 token transfer
96
109
else {
97
110
const contract = getContract ( {
@@ -106,7 +119,7 @@ export function useSendToken(client: ThirdwebClient) {
106
119
to,
107
120
} ) ;
108
121
109
- return sendTransaction ( {
122
+ return await sendTransaction ( {
110
123
transaction : tx ,
111
124
account,
112
125
} ) ;
@@ -121,6 +134,7 @@ export function useSendToken(client: ThirdwebClient) {
121
134
transactionHash : data . transactionHash ,
122
135
client,
123
136
chain : data . chain ,
137
+ maxBlocksWaitTime : 10_000 ,
124
138
} ) ;
125
139
}
126
140
invalidateWalletBalance ( queryClient ) ;
0 commit comments