@@ -8,6 +8,7 @@ import type { CompilerMetadata } from "../../../contract/actions/compiler-metada
88import { getCompilerMetadata } from "../../../contract/actions/get-compiler-metadata.js" ;
99import { getContract } from "../../../contract/contract.js" ;
1010import { decimals } from "../../../extensions/erc20/read/decimals.js" ;
11+ import { getBalance } from "../../../extensions/erc20/read/getBalance.js" ;
1112import { getToken } from "../../../pay/convert/get-token.js" ;
1213import type { SupportedFiatCurrency } from "../../../pay/convert/type.js" ;
1314import { encode } from "../../../transaction/actions/encode.js" ;
@@ -17,6 +18,7 @@ import { resolvePromisedValue } from "../../../utils/promise/resolve-promised-va
1718import { toTokens } from "../../../utils/units.js" ;
1819import type { Wallet } from "../../../wallets/interfaces/wallet.js" ;
1920import { hasSponsoredTransactionsEnabled } from "../../../wallets/smart/is-smart-wallet.js" ;
21+ import { getWalletBalance } from "../../../wallets/utils/getWalletBalance.js" ;
2022import {
2123 formatCurrencyAmount ,
2224 formatTokenAmount ,
@@ -30,6 +32,8 @@ interface TransactionDetails {
3032 selector : string ;
3133 description ?: string ;
3234 } ;
35+ userBalance : string ;
36+ userBalanceWei : bigint ;
3337 usdValueDisplay : string | null ;
3438 txCostDisplay : string ;
3539 gasCostDisplay : string | null ;
@@ -78,12 +82,33 @@ export function useTransactionDetails({
7882 encode ( transaction ) . catch ( ( ) => "0x" ) ,
7983 ] ) ;
8084
81- const [ tokenInfo , gasCostWei ] = await Promise . all ( [
85+ const account = wallet ?. getAccount ( ) ;
86+ if ( ! account ) {
87+ throw new Error ( "No active account" ) ;
88+ }
89+
90+ const [ tokenInfo , userBalance , gasCostWei ] = await Promise . all ( [
8291 getToken (
8392 client ,
8493 erc20Value ? erc20Value . tokenAddress : NATIVE_TOKEN_ADDRESS ,
8594 transaction . chain . id ,
8695 ) . 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 ) ,
87112 hasSponsoredTransactions
88113 ? 0n
89114 : getTransactionGasCost ( transaction ) . catch ( ( ) => null ) ,
@@ -151,9 +176,11 @@ export function useTransactionDetails({
151176 chainMetadata . data ?. nativeCurrency ?. symbol || "ETH" ;
152177 const tokenSymbol = tokenInfo ?. symbol || nativeTokenSymbol ;
153178
154- const totalCostWei = erc20Value
155- ? erc20Value . amountWei
156- : ( value || 0n ) + ( gasCostWei || 0n ) ;
179+ const totalCostWei =
180+ erc20Value &&
181+ erc20Value . tokenAddress . toLowerCase ( ) !== NATIVE_TOKEN_ADDRESS
182+ ? erc20Value . amountWei
183+ : ( value || 0n ) + ( gasCostWei || 0n ) ;
157184 const totalCost = toTokens ( totalCostWei , decimal ) ;
158185
159186 const price = tokenInfo ?. prices [ currency || "USD" ] || 0 ;
@@ -163,6 +190,8 @@ export function useTransactionDetails({
163190 contractMetadata,
164191 costWei,
165192 functionInfo,
193+ userBalance : toTokens ( userBalance , decimal ) ,
194+ userBalanceWei : userBalance ,
166195 gasCostDisplay : gasCostWei
167196 ? `${ formatTokenAmount ( gasCostWei , 18 ) } ${ nativeTokenSymbol } `
168197 : null ,
0 commit comments