11"use client" ;
2+ import { useQuery } from "@tanstack/react-query" ;
23import type { Token } from "../../../../bridge/index.js" ;
34import type { ThirdwebClient } from "../../../../client/client.js" ;
5+ import { NATIVE_TOKEN_ADDRESS } from "../../../../constants/addresses.js" ;
46import {
57 type Address ,
68 getAddress ,
79 shortenAddress ,
810} from "../../../../utils/address.js" ;
11+ import { resolvePromisedValue } from "../../../../utils/promise/resolve-promised-value.js" ;
12+ import { getWalletBalance } from "../../../../wallets/utils/getWalletBalance.js" ;
913import { useCustomTheme } from "../../../core/design-system/CustomThemeProvider.js" ;
1014import {
1115 fontSize ,
@@ -27,13 +31,6 @@ import type { PayEmbedConnectOptions } from "../PayEmbed.js";
2731import type { UIOptions } from "./BridgeOrchestrator.js" ;
2832import { ChainIcon } from "./common/TokenAndChain.js" ;
2933import { 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" ;
3734
3835export interface TransactionPaymentProps {
3936 /**
@@ -91,26 +88,22 @@ export function TransactionPayment({
9188 if ( ! activeAccount ?. address ) {
9289 return "0" ;
9390 }
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 ;
91+ const erc20Value = await resolvePromisedValue (
92+ uiOptions . transaction . erc20Value ,
93+ ) ;
10494 const walletBalance = await getWalletBalance ( {
10595 address : activeAccount ?. address ,
10696 chain : uiOptions . transaction . chain ,
107- tokenAddress : erc20Value ?. tokenAddress . toLowerCase ( ) !== NATIVE_TOKEN_ADDRESS ? erc20Value ?. tokenAddress : undefined ,
97+ tokenAddress :
98+ erc20Value ?. tokenAddress . toLowerCase ( ) !== NATIVE_TOKEN_ADDRESS
99+ ? erc20Value ?. tokenAddress
100+ : undefined ,
108101 client,
109102 } ) ;
110103
111- return toTokens ( walletBalance . value , tokenDecimals ) ;
104+ return walletBalance . displayValue ;
112105 } ,
113- queryKey : [ "active-account-address" ] ,
106+ queryKey : [ "user-balance" , activeAccount ?. address ] ,
114107 } ) ;
115108
116109 const contractName =
@@ -366,7 +359,7 @@ export function TransactionPayment({
366359 Math . max (
367360 0 ,
368361 Number ( transactionDataQuery . data . totalCost ) -
369- Number ( userBalance ?? "0" ) ,
362+ Number ( userBalance ?? "0" ) ,
370363 ) . toString ( ) ,
371364 transactionDataQuery . data . tokenInfo ,
372365 getAddress ( activeAccount . address ) ,
0 commit comments