Skip to content

Commit 2cf3a47

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

File tree

5 files changed

+24
-177
lines changed

5 files changed

+24
-177
lines changed

.changeset/weak-papayas-occur.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"thirdweb": patch
3+
---
4+
5+
Fix etherlink transfers when too little funds

packages/thirdweb/src/bridge/Status.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ describe.runIf(process.env.TW_SECRET_KEY)("Bridge.status", () => {
4444
it("should handle successful status with chain", async () => {
4545
const result = await status({
4646
transactionHash:
47-
"0x7bedc4693e899fe81a22dac11301e77a12a6e772834bba5b698baf3ebcf86f7a",
47+
"0x06ac91479b3ea4c6507f9b7bff1f2d5f553253fa79af9a7db3755563b60f7dfb",
4848
chain: defineChain(8453),
4949
client: TEST_CLIENT,
5050
});

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)