Skip to content

Commit 4d025d4

Browse files
committed
fix: throw error if amount and gas is more than balance
1 parent 85b21ec commit 4d025d4

File tree

3 files changed

+18
-176
lines changed

3 files changed

+18
-176
lines changed

packages/thirdweb/src/react/core/hooks/useBridgeError.test.ts

Lines changed: 0 additions & 172 deletions
This file was deleted.

packages/thirdweb/src/react/core/hooks/wallets/useSendToken.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ import type { ThirdwebClient } from "../../../../client/client.js";
33
import { getContract } from "../../../../contract/contract.js";
44
import { resolveAddress } from "../../../../extensions/ens/resolve-address.js";
55
import { transfer } from "../../../../extensions/erc20/write/transfer.js";
6+
import { estimateGas } from "../../../../transaction/actions/estimate-gas.js";
67
import { sendTransaction } from "../../../../transaction/actions/send-transaction.js";
78
import { waitForReceipt } from "../../../../transaction/actions/wait-for-tx-receipt.js";
89
import { prepareTransaction } from "../../../../transaction/prepare-transaction.js";
910
import { isAddress } from "../../../../utils/address.js";
1011
import { isValidENSName } from "../../../../utils/ens/isValidENSName.js";
1112
import { toWei } from "../../../../utils/units.js";
13+
import { getWalletBalance } from "../../../../wallets/utils/getWalletBalance.js";
1214
import { invalidateWalletBalance } from "../../providers/invalidateWalletBalance.js";
1315
import { useActiveWallet } from "./useActiveWallet.js";
1416

@@ -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);

packages/thirdweb/src/wallets/utils/getWalletBalance.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export type GetWalletBalanceResult = GetBalanceResult;
4141
*/
4242
export async function getWalletBalance(
4343
options: GetWalletBalanceOptions,
44-
): Promise<GetWalletBalanceResult> {
44+
): Promise<GetBalanceResult> {
4545
const { address, client, chain, tokenAddress } = options;
4646
// erc20 case
4747
if (tokenAddress) {

0 commit comments

Comments
 (0)