Skip to content

Commit f33e31a

Browse files
authored
[SDK] Fix: only require missing token amount for transaction widget (#7591)
1 parent 4111d0e commit f33e31a

File tree

4 files changed

+54
-8
lines changed

4 files changed

+54
-8
lines changed

.changeset/few-moments-add.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+
Fixed a regression that prompted the user to pay the full amount in the TransactionWidget, rather than the difference from their current balance

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ export function useTransactionDetails({
8181
const [tokenInfo, gasCostWei] = await Promise.all([
8282
getToken(
8383
client,
84-
erc20Value ? erc20Value.tokenAddress : NATIVE_TOKEN_ADDRESS,
84+
erc20Value?.tokenAddress || NATIVE_TOKEN_ADDRESS,
8585
transaction.chain.id,
8686
).catch(() => null),
8787
hasSponsoredTransactions
@@ -151,9 +151,11 @@ export function useTransactionDetails({
151151
chainMetadata.data?.nativeCurrency?.symbol || "ETH";
152152
const tokenSymbol = tokenInfo?.symbol || nativeTokenSymbol;
153153

154-
const totalCostWei = erc20Value
155-
? erc20Value.amountWei
156-
: (value || 0n) + (gasCostWei || 0n);
154+
const totalCostWei =
155+
erc20Value &&
156+
erc20Value.tokenAddress.toLowerCase() !== NATIVE_TOKEN_ADDRESS
157+
? erc20Value.amountWei
158+
: (value || 0n) + (gasCostWei || 0n);
157159
const totalCost = toTokens(totalCostWei, decimal);
158160

159161
const price = tokenInfo?.prices[currency || "USD"] || 0;

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

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
"use client";
2+
import { useQuery } from "@tanstack/react-query";
23
import type { Token } from "../../../../bridge/index.js";
34
import type { ThirdwebClient } from "../../../../client/client.js";
5+
import { NATIVE_TOKEN_ADDRESS } from "../../../../constants/addresses.js";
46
import {
57
type Address,
68
getAddress,
79
shortenAddress,
810
} from "../../../../utils/address.js";
11+
import { resolvePromisedValue } from "../../../../utils/promise/resolve-promised-value.js";
12+
import { getWalletBalance } from "../../../../wallets/utils/getWalletBalance.js";
913
import { useCustomTheme } from "../../../core/design-system/CustomThemeProvider.js";
1014
import {
1115
fontSize,
@@ -77,6 +81,31 @@ export function TransactionPayment({
7781
wallet,
7882
});
7983

84+
// We can't use useWalletBalance here because erc20Value is a possibly async value
85+
const { data: userBalance } = useQuery({
86+
enabled: !!activeAccount?.address,
87+
queryFn: async (): Promise<string> => {
88+
if (!activeAccount?.address) {
89+
return "0";
90+
}
91+
const erc20Value = await resolvePromisedValue(
92+
uiOptions.transaction.erc20Value,
93+
);
94+
const walletBalance = await getWalletBalance({
95+
address: activeAccount?.address,
96+
chain: uiOptions.transaction.chain,
97+
tokenAddress:
98+
erc20Value?.tokenAddress.toLowerCase() !== NATIVE_TOKEN_ADDRESS
99+
? erc20Value?.tokenAddress
100+
: undefined,
101+
client,
102+
});
103+
104+
return walletBalance.displayValue;
105+
},
106+
queryKey: ["user-balance", activeAccount?.address],
107+
});
108+
80109
const contractName =
81110
transactionDataQuery.data?.contractMetadata?.name || "Unknown Contract";
82111
const functionName =
@@ -327,7 +356,11 @@ export function TransactionPayment({
327356
onClick={() => {
328357
if (transactionDataQuery.data?.tokenInfo) {
329358
onContinue(
330-
transactionDataQuery.data.totalCost,
359+
Math.max(
360+
0,
361+
Number(transactionDataQuery.data.totalCost) -
362+
Number(userBalance ?? "0"),
363+
).toString(),
331364
transactionDataQuery.data.tokenInfo,
332365
getAddress(activeAccount.address),
333366
);

packages/thirdweb/src/react/web/ui/Bridge/payment-selection/FiatProviderSelection.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { useMemo } from "react";
33
import type { ThirdwebClient } from "../../../../../client/client.js";
44
import type { SupportedFiatCurrency } from "../../../../../pay/convert/type.js";
55
import { checksumAddress } from "../../../../../utils/address.js";
6+
import { formatNumber } from "../../../../../utils/formatNumber.js";
67
import { toTokens } from "../../../../../utils/units.js";
78
import { useCustomTheme } from "../../../../core/design-system/CustomThemeProvider.js";
89
import {
@@ -171,9 +172,14 @@ export function FiatProviderSelection({
171172
{quote.currency}
172173
</Text>
173174
<Text color="secondaryText" size="xs">
174-
{toTokens(
175-
quote.destinationAmount,
176-
quote.destinationToken.decimals,
175+
{formatNumber(
176+
Number(
177+
toTokens(
178+
quote.destinationAmount,
179+
quote.destinationToken.decimals,
180+
),
181+
),
182+
4,
177183
)}{" "}
178184
{quote.destinationToken.symbol}
179185
</Text>

0 commit comments

Comments
 (0)