Skip to content

Commit 968913d

Browse files
committed
[MNY-231] SDK: Fix TransactionWidget not updating on currency prop change after initial render (#8192)
<!-- ## title your PR with this format: "[SDK/Dashboard/Portal] Feature/Fix: Concise title for the changes" If you did not copy the branch name from Linear, paste the issue tag here (format is TEAM-0000): ## Notes for the reviewer Anything important to call out? Be sure to also clarify these in your comments. ## How to test Unit tests, playground, etc. --> <!-- start pr-codex --> --- ## PR-Codex overview This PR focuses on fixing the `TransactionWidget` to ensure it updates correctly when the `currency` prop changes after the initial render. It also refines how the total cost and display values are calculated based on the selected currency. ### Detailed summary - Updated `PaymentOverview` to remove the `currency` prop from `TransactionOverViewCompact`. - Enhanced `TransactionPayment` to calculate `totalFiatCost` and `costToDisplay` based on the selected currency. - Adjusted the display of USD value to use `costToDisplay`. - Removed the `currency` type from `UseTransactionDetailsOptions`. - Simplified `useTransactionDetails` by removing currency-related calculations. > ✨ Ask PR-Codex anything about this PR by commenting with `/codex {your question}` <!-- end pr-codex --> <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Transaction widget now updates correctly when the selected currency changes after initial render. * Payment and transaction details display accurate fiat totals, with graceful fallback when pricing data is unavailable. * **Improvements** * Streamlined pricing display to focus on the selected currency, improving clarity and consistency across payment views. * **Chores** * Added a patch-level changeset entry documenting the bug fix. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent bd70db4 commit 968913d

File tree

4 files changed

+21
-20
lines changed

4 files changed

+21
-20
lines changed

.changeset/rich-pens-hug.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+
Fix TransactionWidget not updating when `currency` prop is changed after initial render

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

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import { getCompilerMetadata } from "../../../contract/actions/get-compiler-meta
99
import { getContract } from "../../../contract/contract.js";
1010
import { decimals } from "../../../extensions/erc20/read/decimals.js";
1111
import { getToken } from "../../../pay/convert/get-token.js";
12-
import type { SupportedFiatCurrency } from "../../../pay/convert/type.js";
1312
import { encode } from "../../../transaction/actions/encode.js";
1413
import type { PreparedTransaction } from "../../../transaction/prepare-transaction.js";
1514
import { getTransactionGasCost } from "../../../transaction/utils.js";
@@ -18,10 +17,7 @@ import { resolvePromisedValue } from "../../../utils/promise/resolve-promised-va
1817
import { toTokens } from "../../../utils/units.js";
1918
import type { Wallet } from "../../../wallets/interfaces/wallet.js";
2019
import { hasSponsoredTransactionsEnabled } from "../../../wallets/smart/is-smart-wallet.js";
21-
import {
22-
formatCurrencyAmount,
23-
formatTokenAmount,
24-
} from "../../web/ui/ConnectWallet/screens/formatTokenBalance.js";
20+
import { formatTokenAmount } from "../../web/ui/ConnectWallet/screens/formatTokenBalance.js";
2521
import { useChainMetadata } from "./others/useChainQuery.js";
2622

2723
interface TransactionDetails {
@@ -31,7 +27,6 @@ interface TransactionDetails {
3127
selector: string;
3228
description?: string;
3329
};
34-
usdValueDisplay: string | null;
3530
txCostDisplay: string;
3631
gasCostDisplay: string | null;
3732
tokenInfo: TokenWithPrices | null;
@@ -45,7 +40,6 @@ interface UseTransactionDetailsOptions {
4540
transaction: PreparedTransaction;
4641
client: ThirdwebClient;
4742
wallet: Wallet | undefined;
48-
currency: SupportedFiatCurrency | undefined;
4943
}
5044

5145
/**
@@ -54,7 +48,6 @@ interface UseTransactionDetailsOptions {
5448
*/
5549
export function useTransactionDetails({
5650
transaction,
57-
currency,
5851
client,
5952
wallet,
6053
}: UseTransactionDetailsOptions) {
@@ -158,9 +151,6 @@ export function useTransactionDetails({
158151
: (value || 0n) + (gasCostWei || 0n);
159152
const totalCost = toTokens(totalCostWei, decimal);
160153

161-
const price = tokenInfo?.prices[currency || "USD"] || 0;
162-
const usdValue = price ? Number(totalCost) * price : null;
163-
164154
return {
165155
contractMetadata,
166156
costWei,
@@ -173,9 +163,6 @@ export function useTransactionDetails({
173163
totalCost,
174164
totalCostWei,
175165
txCostDisplay: `${formatTokenAmount(costWei, decimal)} ${tokenSymbol}`,
176-
usdValueDisplay: usdValue
177-
? formatCurrencyAmount(currency || "USD", usdValue)
178-
: null,
179166
};
180167
},
181168
queryKey: [

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { useActiveAccount } from "../../../core/hooks/wallets/useActiveAccount.j
2424
import { useActiveWallet } from "../../../core/hooks/wallets/useActiveWallet.js";
2525
import { ConnectButton } from "../ConnectWallet/ConnectButton.js";
2626
import { PoweredByThirdweb } from "../ConnectWallet/PoweredByTW.js";
27+
import { formatCurrencyAmount } from "../ConnectWallet/screens/formatTokenBalance.js";
2728
import { Container, Line } from "../components/basic.js";
2829
import { Button } from "../components/buttons.js";
2930
import { ChainName } from "../components/ChainName.js";
@@ -100,7 +101,6 @@ export function TransactionPayment({
100101
client,
101102
transaction: transaction,
102103
wallet,
103-
currency: currency,
104104
});
105105

106106
// We can't use useWalletBalance here because erc20Value is a possibly async value
@@ -134,6 +134,19 @@ export function TransactionPayment({
134134

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

137+
const tokenFiatPricePerToken =
138+
transactionDataQuery.data?.tokenInfo?.prices[currency] || undefined;
139+
140+
const totalFiatCost =
141+
tokenFiatPricePerToken && transactionDataQuery.data
142+
? tokenFiatPricePerToken * Number(transactionDataQuery.data.totalCost)
143+
: undefined;
144+
145+
const costToDisplay =
146+
totalFiatCost !== undefined
147+
? formatCurrencyAmount(currency, totalFiatCost)
148+
: transactionDataQuery.data?.txCostDisplay;
149+
137150
if (isLoading) {
138151
return (
139152
<WithHeader
@@ -207,8 +220,7 @@ export function TransactionPayment({
207220
>
208221
{/* USD Value */}
209222
<Text color="primaryText" size="xl" weight={600}>
210-
{transactionDataQuery.data?.usdValueDisplay ||
211-
transactionDataQuery.data?.txCostDisplay}
223+
{costToDisplay}
212224
</Text>
213225

214226
{/* Function Name */}

packages/thirdweb/src/react/web/ui/Bridge/payment-details/PaymentOverview.tsx

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,6 @@ export function PaymentOverview(props: {
199199
<TransactionOverViewCompact
200200
client={props.client}
201201
paymentMethod={props.paymentMethod}
202-
currency={props.currency}
203202
transaction={props.modeInfo.transaction}
204203
metadata={props.metadata}
205204
/>
@@ -211,7 +210,6 @@ export function PaymentOverview(props: {
211210

212211
const TransactionOverViewCompact = (props: {
213212
transaction: PreparedTransaction;
214-
currency: SupportedFiatCurrency;
215213
paymentMethod: PaymentMethod;
216214
client: ThirdwebClient;
217215
metadata: {
@@ -224,7 +222,6 @@ const TransactionOverViewCompact = (props: {
224222
client: props.client,
225223
transaction: props.transaction,
226224
wallet: props.paymentMethod.payerWallet,
227-
currency: props.currency,
228225
});
229226

230227
if (!txInfo.data) {

0 commit comments

Comments
 (0)