Skip to content

Commit d9fc667

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

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

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

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@ 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 { sendTransaction } from "../../../../transaction/actions/send-transaction.js";
76
import { waitForReceipt } from "../../../../transaction/actions/wait-for-tx-receipt.js";
87
import { prepareTransaction } from "../../../../transaction/prepare-transaction.js";
98
import { isAddress } from "../../../../utils/address.js";
109
import { isValidENSName } from "../../../../utils/ens/isValidENSName.js";
1110
import { toWei } from "../../../../utils/units.js";
1211
import { invalidateWalletBalance } from "../../providers/invalidateWalletBalance.js";
1312
import { 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);

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ export type GetWalletBalanceOptions = {
2222
tokenAddress?: string;
2323
};
2424

25-
export type GetWalletBalanceResult = GetBalanceResult;
26-
2725
/**
2826
* Retrieves the balance of a token or native currency for a given wallet.
2927
* @param options - The options for retrieving the token balance.
@@ -41,7 +39,7 @@ export type GetWalletBalanceResult = GetBalanceResult;
4139
*/
4240
export async function getWalletBalance(
4341
options: GetWalletBalanceOptions,
44-
): Promise<GetWalletBalanceResult> {
42+
): Promise<GetBalanceResult> {
4543
const { address, client, chain, tokenAddress } = options;
4644
// erc20 case
4745
if (tokenAddress) {

0 commit comments

Comments
 (0)