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/rich-pens-hug.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"thirdweb": patch
---

Fix TransactionWidget not updating when `currency` prop is changed after initial render
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { getCompilerMetadata } from "../../../contract/actions/get-compiler-meta
import { getContract } from "../../../contract/contract.js";
import { decimals } from "../../../extensions/erc20/read/decimals.js";
import { getToken } from "../../../pay/convert/get-token.js";
import type { SupportedFiatCurrency } from "../../../pay/convert/type.js";
import { encode } from "../../../transaction/actions/encode.js";
import type { PreparedTransaction } from "../../../transaction/prepare-transaction.js";
import { getTransactionGasCost } from "../../../transaction/utils.js";
Expand All @@ -18,10 +17,7 @@ import { resolvePromisedValue } from "../../../utils/promise/resolve-promised-va
import { toTokens } from "../../../utils/units.js";
import type { Wallet } from "../../../wallets/interfaces/wallet.js";
import { hasSponsoredTransactionsEnabled } from "../../../wallets/smart/is-smart-wallet.js";
import {
formatCurrencyAmount,
formatTokenAmount,
} from "../../web/ui/ConnectWallet/screens/formatTokenBalance.js";
import { formatTokenAmount } from "../../web/ui/ConnectWallet/screens/formatTokenBalance.js";
import { useChainMetadata } from "./others/useChainQuery.js";

interface TransactionDetails {
Expand All @@ -31,7 +27,6 @@ interface TransactionDetails {
selector: string;
description?: string;
};
usdValueDisplay: string | null;
txCostDisplay: string;
gasCostDisplay: string | null;
tokenInfo: TokenWithPrices | null;
Expand All @@ -45,7 +40,6 @@ interface UseTransactionDetailsOptions {
transaction: PreparedTransaction;
client: ThirdwebClient;
wallet: Wallet | undefined;
currency: SupportedFiatCurrency | undefined;
}

/**
Expand All @@ -54,7 +48,6 @@ interface UseTransactionDetailsOptions {
*/
export function useTransactionDetails({
transaction,
currency,
client,
wallet,
}: UseTransactionDetailsOptions) {
Expand Down Expand Up @@ -158,9 +151,6 @@ export function useTransactionDetails({
: (value || 0n) + (gasCostWei || 0n);
const totalCost = toTokens(totalCostWei, decimal);

const price = tokenInfo?.prices[currency || "USD"] || 0;
const usdValue = price ? Number(totalCost) * price : null;

return {
contractMetadata,
costWei,
Expand All @@ -173,9 +163,6 @@ export function useTransactionDetails({
totalCost,
totalCostWei,
txCostDisplay: `${formatTokenAmount(costWei, decimal)} ${tokenSymbol}`,
usdValueDisplay: usdValue
? formatCurrencyAmount(currency || "USD", usdValue)
: null,
};
},
queryKey: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import { useActiveWallet } from "../../../core/hooks/wallets/useActiveWallet.js";
import { ConnectButton } from "../ConnectWallet/ConnectButton.js";
import { PoweredByThirdweb } from "../ConnectWallet/PoweredByTW.js";
import { formatCurrencyAmount } from "../ConnectWallet/screens/formatTokenBalance.js";
import { Container, Line } from "../components/basic.js";
import { Button } from "../components/buttons.js";
import { ChainName } from "../components/ChainName.js";
Expand Down Expand Up @@ -100,7 +101,6 @@
client,
transaction: transaction,
wallet,
currency: currency,
});

// We can't use useWalletBalance here because erc20Value is a possibly async value
Expand Down Expand Up @@ -134,6 +134,19 @@

const buttonLabel = _buttonLabel || `Execute ${functionName}`;

const tokenFiatPricePerToken =
transactionDataQuery.data?.tokenInfo?.prices[currency] || undefined;

Check warning on line 138 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#L137-L138

Added lines #L137 - L138 were not covered by tests

const totalFiatCost =
tokenFiatPricePerToken && transactionDataQuery.data
? tokenFiatPricePerToken * Number(transactionDataQuery.data.totalCost)
: undefined;

Check warning on line 143 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#L140-L143

Added lines #L140 - L143 were not covered by tests

const costToDisplay =
totalFiatCost !== undefined
? formatCurrencyAmount(currency, totalFiatCost)
: transactionDataQuery.data?.txCostDisplay;

Check warning on line 148 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#L145-L148

Added lines #L145 - L148 were not covered by tests

if (isLoading) {
return (
<WithHeader
Expand Down Expand Up @@ -207,8 +220,7 @@
>
{/* USD Value */}
<Text color="primaryText" size="xl" weight={600}>
{transactionDataQuery.data?.usdValueDisplay ||
transactionDataQuery.data?.txCostDisplay}
{costToDisplay}

Check warning on line 223 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#L223

Added line #L223 was not covered by tests
</Text>

{/* Function Name */}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,6 @@ export function PaymentOverview(props: {
<TransactionOverViewCompact
client={props.client}
paymentMethod={props.paymentMethod}
currency={props.currency}
transaction={props.modeInfo.transaction}
metadata={props.metadata}
/>
Expand All @@ -211,7 +210,6 @@ export function PaymentOverview(props: {

const TransactionOverViewCompact = (props: {
transaction: PreparedTransaction;
currency: SupportedFiatCurrency;
paymentMethod: PaymentMethod;
client: ThirdwebClient;
metadata: {
Expand All @@ -224,7 +222,6 @@ const TransactionOverViewCompact = (props: {
client: props.client,
transaction: props.transaction,
wallet: props.paymentMethod.payerWallet,
currency: props.currency,
});

if (!txInfo.data) {
Expand Down
Loading