Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/few-moments-add.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"thirdweb": patch
---

Fixed a regression that prompted the user to pay the full amount in the TransactionWidget, rather than the difference from their current balance
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
const [tokenInfo, gasCostWei] = await Promise.all([
getToken(
client,
erc20Value ? erc20Value.tokenAddress : NATIVE_TOKEN_ADDRESS,
erc20Value?.tokenAddress || NATIVE_TOKEN_ADDRESS,

Check warning on line 84 in packages/thirdweb/src/react/core/hooks/useTransactionDetails.ts

View check run for this annotation

Codecov / codecov/patch

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

Added line #L84 was not covered by tests
transaction.chain.id,
).catch(() => null),
hasSponsoredTransactions
Expand Down Expand Up @@ -151,9 +151,11 @@
chainMetadata.data?.nativeCurrency?.symbol || "ETH";
const tokenSymbol = tokenInfo?.symbol || nativeTokenSymbol;

const totalCostWei = erc20Value
? erc20Value.amountWei
: (value || 0n) + (gasCostWei || 0n);
const totalCostWei =
erc20Value &&
erc20Value.tokenAddress.toLowerCase() !== NATIVE_TOKEN_ADDRESS
? erc20Value.amountWei
: (value || 0n) + (gasCostWei || 0n);

Check warning on line 158 in packages/thirdweb/src/react/core/hooks/useTransactionDetails.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/react/core/hooks/useTransactionDetails.ts#L154-L158

Added lines #L154 - L158 were not covered by tests
const totalCost = toTokens(totalCostWei, decimal);

const price = tokenInfo?.prices[currency || "USD"] || 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
"use client";
import { useQuery } from "@tanstack/react-query";
import type { Token } from "../../../../bridge/index.js";
import type { ThirdwebClient } from "../../../../client/client.js";
import { NATIVE_TOKEN_ADDRESS } from "../../../../constants/addresses.js";
import {
type Address,
getAddress,
shortenAddress,
} from "../../../../utils/address.js";
import { resolvePromisedValue } from "../../../../utils/promise/resolve-promised-value.js";
import { getWalletBalance } from "../../../../wallets/utils/getWalletBalance.js";
import { useCustomTheme } from "../../../core/design-system/CustomThemeProvider.js";
import {
fontSize,
Expand Down Expand Up @@ -77,6 +81,31 @@
wallet,
});

// We can't use useWalletBalance here because erc20Value is a possibly async value
const { data: userBalance } = useQuery({
enabled: !!activeAccount?.address,
queryFn: async (): Promise<string> => {
if (!activeAccount?.address) {
return "0";
}
const erc20Value = await resolvePromisedValue(
uiOptions.transaction.erc20Value,
);
const walletBalance = await getWalletBalance({
address: activeAccount?.address,
chain: uiOptions.transaction.chain,
tokenAddress:
erc20Value?.tokenAddress.toLowerCase() !== NATIVE_TOKEN_ADDRESS
? erc20Value?.tokenAddress
: undefined,
client,
});

Check warning on line 102 in packages/thirdweb/src/react/web/ui/Bridge/TransactionPayment.tsx

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/react/web/ui/Bridge/TransactionPayment.tsx#L85-L102

Added lines #L85 - L102 were not covered by tests

return walletBalance.displayValue;
},
queryKey: ["user-balance", activeAccount?.address],
});

Check warning on line 107 in packages/thirdweb/src/react/web/ui/Bridge/TransactionPayment.tsx

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/react/web/ui/Bridge/TransactionPayment.tsx#L104-L107

Added lines #L104 - L107 were not covered by tests

const contractName =
transactionDataQuery.data?.contractMetadata?.name || "Unknown Contract";
const functionName =
Expand Down Expand Up @@ -327,7 +356,11 @@
onClick={() => {
if (transactionDataQuery.data?.tokenInfo) {
onContinue(
transactionDataQuery.data.totalCost,
Math.max(
0,
Number(transactionDataQuery.data.totalCost) -
Number(userBalance ?? "0"),
).toString(),

Check warning on line 363 in packages/thirdweb/src/react/web/ui/Bridge/TransactionPayment.tsx

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/react/web/ui/Bridge/TransactionPayment.tsx#L359-L363

Added lines #L359 - L363 were not covered by tests
transactionDataQuery.data.tokenInfo,
getAddress(activeAccount.address),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import type { ThirdwebClient } from "../../../../../client/client.js";
import type { SupportedFiatCurrency } from "../../../../../pay/convert/type.js";
import { checksumAddress } from "../../../../../utils/address.js";
import { formatNumber } from "../../../../../utils/formatNumber.js";
import { toTokens } from "../../../../../utils/units.js";
import { useCustomTheme } from "../../../../core/design-system/CustomThemeProvider.js";
import {
Expand Down Expand Up @@ -171,9 +172,14 @@
{quote.currency}
</Text>
<Text color="secondaryText" size="xs">
{toTokens(
quote.destinationAmount,
quote.destinationToken.decimals,
{formatNumber(
Number(
toTokens(
quote.destinationAmount,
quote.destinationToken.decimals,
),
),
4,

Check warning on line 182 in packages/thirdweb/src/react/web/ui/Bridge/payment-selection/FiatProviderSelection.tsx

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L175 - L182 were not covered by tests
)}{" "}
{quote.destinationToken.symbol}
</Text>
Expand Down
Loading