Skip to content

Commit b74c5ad

Browse files
committed
fix: only require missing token amount for transactions
1 parent ffb7b64 commit b74c5ad

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

packages/thirdweb/src/react/core/hooks/useTransactionDetails.ts

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import type { CompilerMetadata } from "../../../contract/actions/compiler-metada
88
import { getCompilerMetadata } from "../../../contract/actions/get-compiler-metadata.js";
99
import { getContract } from "../../../contract/contract.js";
1010
import { decimals } from "../../../extensions/erc20/read/decimals.js";
11+
import { getBalance } from "../../../extensions/erc20/read/getBalance.js";
1112
import { getToken } from "../../../pay/convert/get-token.js";
1213
import type { SupportedFiatCurrency } from "../../../pay/convert/type.js";
1314
import { encode } from "../../../transaction/actions/encode.js";
@@ -17,6 +18,7 @@ import { resolvePromisedValue } from "../../../utils/promise/resolve-promised-va
1718
import { toTokens } from "../../../utils/units.js";
1819
import type { Wallet } from "../../../wallets/interfaces/wallet.js";
1920
import { hasSponsoredTransactionsEnabled } from "../../../wallets/smart/is-smart-wallet.js";
21+
import { getWalletBalance } from "../../../wallets/utils/getWalletBalance.js";
2022
import {
2123
formatCurrencyAmount,
2224
formatTokenAmount,
@@ -30,6 +32,8 @@ interface TransactionDetails {
3032
selector: string;
3133
description?: string;
3234
};
35+
userBalance: string;
36+
userBalanceWei: bigint;
3337
usdValueDisplay: string | null;
3438
txCostDisplay: string;
3539
gasCostDisplay: string | null;
@@ -78,12 +82,33 @@ export function useTransactionDetails({
7882
encode(transaction).catch(() => "0x"),
7983
]);
8084

81-
const [tokenInfo, gasCostWei] = await Promise.all([
85+
const account = wallet?.getAccount();
86+
if (!account) {
87+
throw new Error("No active account");
88+
}
89+
90+
const [tokenInfo, userBalance, gasCostWei] = await Promise.all([
8291
getToken(
8392
client,
8493
erc20Value ? erc20Value.tokenAddress : NATIVE_TOKEN_ADDRESS,
8594
transaction.chain.id,
8695
).catch(() => null),
96+
(async () =>
97+
erc20Value &&
98+
erc20Value.tokenAddress.toLowerCase() !== NATIVE_TOKEN_ADDRESS
99+
? getBalance({
100+
contract: getContract({
101+
address: erc20Value.tokenAddress,
102+
chain: transaction.chain,
103+
client,
104+
}),
105+
address: account.address,
106+
})
107+
: getWalletBalance({
108+
address: account.address,
109+
chain: transaction.chain,
110+
client,
111+
}))().then((result) => result?.value || 0n),
87112
hasSponsoredTransactions
88113
? 0n
89114
: getTransactionGasCost(transaction).catch(() => null),
@@ -151,9 +176,11 @@ export function useTransactionDetails({
151176
chainMetadata.data?.nativeCurrency?.symbol || "ETH";
152177
const tokenSymbol = tokenInfo?.symbol || nativeTokenSymbol;
153178

154-
const totalCostWei = erc20Value
155-
? erc20Value.amountWei
156-
: (value || 0n) + (gasCostWei || 0n);
179+
const totalCostWei =
180+
erc20Value &&
181+
erc20Value.tokenAddress.toLowerCase() !== NATIVE_TOKEN_ADDRESS
182+
? erc20Value.amountWei
183+
: (value || 0n) + (gasCostWei || 0n);
157184
const totalCost = toTokens(totalCostWei, decimal);
158185

159186
const price = tokenInfo?.prices[currency || "USD"] || 0;
@@ -163,6 +190,8 @@ export function useTransactionDetails({
163190
contractMetadata,
164191
costWei,
165192
functionInfo,
193+
userBalance: toTokens(userBalance, decimal),
194+
userBalanceWei: userBalance,
166195
gasCostDisplay: gasCostWei
167196
? `${formatTokenAmount(gasCostWei, 18)} ${nativeTokenSymbol}`
168197
: null,

packages/thirdweb/src/react/web/ui/Bridge/TransactionPayment.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,10 @@ export function TransactionPayment({
327327
onClick={() => {
328328
if (transactionDataQuery.data?.tokenInfo) {
329329
onContinue(
330-
transactionDataQuery.data.totalCost,
330+
(Math.max(0,
331+
Number(transactionDataQuery.data.totalCost) -
332+
Number(transactionDataQuery.data.userBalance)
333+
)).toString(),
331334
transactionDataQuery.data.tokenInfo,
332335
getAddress(activeAccount.address),
333336
);

0 commit comments

Comments
 (0)