Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
37 changes: 33 additions & 4 deletions packages/thirdweb/src/react/core/hooks/useTransactionDetails.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import { getCompilerMetadata } from "../../../contract/actions/get-compiler-metadata.js";
import { getContract } from "../../../contract/contract.js";
import { decimals } from "../../../extensions/erc20/read/decimals.js";
import { getBalance } from "../../../extensions/erc20/read/getBalance.js";
import { getToken } from "../../../pay/convert/get-token.js";
import type { SupportedFiatCurrency } from "../../../pay/convert/type.js";
import { encode } from "../../../transaction/actions/encode.js";
Expand All @@ -17,6 +18,7 @@
import { toTokens } from "../../../utils/units.js";
import type { Wallet } from "../../../wallets/interfaces/wallet.js";
import { hasSponsoredTransactionsEnabled } from "../../../wallets/smart/is-smart-wallet.js";
import { getWalletBalance } from "../../../wallets/utils/getWalletBalance.js";
import {
formatCurrencyAmount,
formatTokenAmount,
Expand All @@ -30,6 +32,8 @@
selector: string;
description?: string;
};
userBalance: string;
userBalanceWei: bigint;
usdValueDisplay: string | null;
txCostDisplay: string;
gasCostDisplay: string | null;
Expand Down Expand Up @@ -78,12 +82,33 @@
encode(transaction).catch(() => "0x"),
]);

const [tokenInfo, gasCostWei] = await Promise.all([
const account = wallet?.getAccount();
if (!account) {
throw new Error("No active account");
}

Check warning on line 88 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#L85-L88

Added lines #L85 - L88 were not covered by tests

const [tokenInfo, userBalance, gasCostWei] = await Promise.all([

Check warning on line 90 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#L90

Added line #L90 was not covered by tests
getToken(
client,
erc20Value ? erc20Value.tokenAddress : NATIVE_TOKEN_ADDRESS,
transaction.chain.id,
).catch(() => null),
(async () =>
erc20Value &&
erc20Value.tokenAddress.toLowerCase() !== NATIVE_TOKEN_ADDRESS
? getBalance({
contract: getContract({
address: erc20Value.tokenAddress,
chain: transaction.chain,
client,
}),
address: account.address,
})
: getWalletBalance({
address: account.address,
chain: transaction.chain,
client,
}))().then((result) => result?.value || 0n),

Check warning on line 111 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#L96-L111

Added lines #L96 - L111 were not covered by tests
hasSponsoredTransactions
? 0n
: getTransactionGasCost(transaction).catch(() => null),
Expand Down Expand Up @@ -151,9 +176,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 183 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#L179-L183

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

const price = tokenInfo?.prices[currency || "USD"] || 0;
Expand All @@ -163,6 +190,8 @@
contractMetadata,
costWei,
functionInfo,
userBalance: toTokens(userBalance, decimal),
userBalanceWei: userBalance,

Check warning on line 194 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#L193-L194

Added lines #L193 - L194 were not covered by tests
gasCostDisplay: gasCostWei
? `${formatTokenAmount(gasCostWei, 18)} ${nativeTokenSymbol}`
: null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,11 @@
onClick={() => {
if (transactionDataQuery.data?.tokenInfo) {
onContinue(
transactionDataQuery.data.totalCost,
Math.max(
0,
Number(transactionDataQuery.data.totalCost) -
Number(transactionDataQuery.data.userBalance),
).toString(),

Check warning on line 334 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#L330-L334

Added lines #L330 - L334 were not covered by tests
transactionDataQuery.data.tokenInfo,
getAddress(activeAccount.address),
);
Expand Down
Loading