Skip to content

Commit 374e9cd

Browse files
committed
chore: move user balance query out of transaction details hook
1 parent ee2f756 commit 374e9cd

File tree

2 files changed

+40
-26
lines changed

2 files changed

+40
-26
lines changed

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

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import type { CompilerMetadata } from "../../../contract/actions/compiler-metada
88
import { getCompilerMetadata } from "../../../contract/actions/get-compiler-metadata.js";
99
import { getContract } from "../../../contract/contract.js";
1010
import { decimals } from "../../../extensions/erc20/read/decimals.js";
11-
import { getBalance } from "../../../extensions/erc20/read/getBalance.js";
1211
import { getToken } from "../../../pay/convert/get-token.js";
1312
import type { SupportedFiatCurrency } from "../../../pay/convert/type.js";
1413
import { encode } from "../../../transaction/actions/encode.js";
@@ -18,7 +17,6 @@ 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 { getWalletBalance } from "../../../wallets/utils/getWalletBalance.js";
2220
import {
2321
formatCurrencyAmount,
2422
formatTokenAmount,
@@ -32,8 +30,6 @@ interface TransactionDetails {
3230
selector: string;
3331
description?: string;
3432
};
35-
userBalance: string;
36-
userBalanceWei: bigint;
3733
usdValueDisplay: string | null;
3834
txCostDisplay: string;
3935
gasCostDisplay: string | null;
@@ -87,28 +83,12 @@ export function useTransactionDetails({
8783
throw new Error("No active account");
8884
}
8985

90-
const [tokenInfo, userBalance, gasCostWei] = await Promise.all([
86+
const [tokenInfo, gasCostWei] = await Promise.all([
9187
getToken(
9288
client,
93-
erc20Value ? erc20Value.tokenAddress : NATIVE_TOKEN_ADDRESS,
89+
erc20Value?.tokenAddress || NATIVE_TOKEN_ADDRESS,
9490
transaction.chain.id,
9591
).catch(() => null),
96-
(async () =>
97-
erc20Value &&
98-
erc20Value.tokenAddress.toLowerCase() !== NATIVE_TOKEN_ADDRESS
99-
? getBalance({
100-
contract: getContract({
101-
address: erc20Value.tokenAddress,
102-
chain: transaction.chain,
103-
client,
104-
}),
105-
address: account.address,
106-
})
107-
: getWalletBalance({
108-
address: account.address,
109-
chain: transaction.chain,
110-
client,
111-
}))().then((result) => result?.value || 0n),
11292
hasSponsoredTransactions
11393
? 0n
11494
: getTransactionGasCost(transaction).catch(() => null),
@@ -178,7 +158,7 @@ export function useTransactionDetails({
178158

179159
const totalCostWei =
180160
erc20Value &&
181-
erc20Value.tokenAddress.toLowerCase() !== NATIVE_TOKEN_ADDRESS
161+
erc20Value.tokenAddress.toLowerCase() !== NATIVE_TOKEN_ADDRESS
182162
? erc20Value.amountWei
183163
: (value || 0n) + (gasCostWei || 0n);
184164
const totalCost = toTokens(totalCostWei, decimal);
@@ -190,8 +170,6 @@ export function useTransactionDetails({
190170
contractMetadata,
191171
costWei,
192172
functionInfo,
193-
userBalance: toTokens(userBalance, decimal),
194-
userBalanceWei: userBalance,
195173
gasCostDisplay: gasCostWei
196174
? `${formatTokenAmount(gasCostWei, 18)} ${nativeTokenSymbol}`
197175
: null,

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

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ import type { PayEmbedConnectOptions } from "../PayEmbed.js";
2727
import type { UIOptions } from "./BridgeOrchestrator.js";
2828
import { ChainIcon } from "./common/TokenAndChain.js";
2929
import { WithHeader } from "./common/WithHeader.js";
30+
import { useQuery } from "@tanstack/react-query";
31+
import { getWalletBalance } from "../../../../wallets/utils/getWalletBalance.js";
32+
import { resolvePromisedValue } from "../../../../utils/promise/resolve-promised-value.js";
33+
import { decimals } from "../../../../extensions/erc20/read/decimals.js";
34+
import { getContract } from "../../../../contract/contract.js";
35+
import { NATIVE_TOKEN_ADDRESS } from "../../../../constants/addresses.js";
36+
import { toTokens } from "../../../../utils/units.js";
3037

3138
export interface TransactionPaymentProps {
3239
/**
@@ -77,6 +84,35 @@ export function TransactionPayment({
7784
wallet,
7885
});
7986

87+
// We can't use useWalletBalance here because erc20Value is a possibly async value
88+
const { data: userBalance } = useQuery({
89+
enabled: !!activeAccount?.address,
90+
queryFn: async (): Promise<string> => {
91+
if (!activeAccount?.address) {
92+
return "0";
93+
}
94+
const erc20Value = await resolvePromisedValue(uiOptions.transaction.erc20Value);
95+
const tokenDecimals = erc20Value?.tokenAddress.toLowerCase() !== NATIVE_TOKEN_ADDRESS && erc20Value
96+
? await decimals({
97+
contract: getContract({
98+
address: erc20Value.tokenAddress,
99+
chain: uiOptions.transaction.chain,
100+
client,
101+
}),
102+
})
103+
: 18;
104+
const walletBalance = await getWalletBalance({
105+
address: activeAccount?.address,
106+
chain: uiOptions.transaction.chain,
107+
tokenAddress: erc20Value?.tokenAddress,
108+
client,
109+
});
110+
111+
return toTokens(walletBalance.value, tokenDecimals);
112+
},
113+
queryKey: ["active-account-address"],
114+
});
115+
80116
const contractName =
81117
transactionDataQuery.data?.contractMetadata?.name || "Unknown Contract";
82118
const functionName =
@@ -330,7 +366,7 @@ export function TransactionPayment({
330366
Math.max(
331367
0,
332368
Number(transactionDataQuery.data.totalCost) -
333-
Number(transactionDataQuery.data.userBalance),
369+
Number(userBalance),
334370
).toString(),
335371
transactionDataQuery.data.tokenInfo,
336372
getAddress(activeAccount.address),

0 commit comments

Comments
 (0)