@@ -5,11 +5,14 @@ import { useState } from "react";
55import { Buy , Sell } from "../../../../../bridge/index.js" ;
66import type { BridgeChain } from "../../../../../bridge/types/Chain.js" ;
77import type { TokenWithPrices } from "../../../../../bridge/types/Token.js" ;
8+ import { defineChain } from "../../../../../chains/utils.js" ;
89import type { ThirdwebClient } from "../../../../../client/client.js" ;
10+ import { NATIVE_TOKEN_ADDRESS } from "../../../../../constants/addresses.js" ;
911import {
1012 getFiatSymbol ,
1113 type SupportedFiatCurrency ,
1214} from "../../../../../pay/convert/type.js" ;
15+ import { getAddress } from "../../../../../utils/address.js" ;
1316import { toTokens , toUnits } from "../../../../../utils/units.js" ;
1417import { useCustomTheme } from "../../../../core/design-system/CustomThemeProvider.js" ;
1518import {
@@ -19,6 +22,7 @@ import {
1922 spacing ,
2023 type Theme ,
2124} from "../../../../core/design-system/index.js" ;
25+ import { useWalletBalance } from "../../../../core/hooks/others/useWalletBalance.js" ;
2226import { ConnectButton } from "../../ConnectWallet/ConnectButton.js" ;
2327import { ArrowUpDownIcon } from "../../ConnectWallet/icons/ArrowUpDownIcon.js" ;
2428import { Container } from "../../components/basic.js" ;
@@ -44,6 +48,7 @@ type SwapUIProps = {
4448 quote : Buy . quote . Result | Sell . quote . Result ,
4549 selection : {
4650 buyToken : TokenWithPrices ;
51+ sellTokenBalance : bigint ;
4752 sellToken : TokenWithPrices ;
4853 } ,
4954 ) => void ;
@@ -193,6 +198,13 @@ export function SwapUIBase(
193198 const isBuyAmountFetching = mode === "sell" && preparedResultQuery . isFetching ;
194199 const isSellAmountFetching = mode === "buy" && preparedResultQuery . isFetching ;
195200
201+ const sellTokenBalanceQuery = useTokenBalance ( {
202+ chainId : props . sellToken ?. chainId ,
203+ tokenAddress : props . sellToken ?. address ,
204+ client : props . client ,
205+ walletAddress : props . activeWalletInfo ?. activeAccount . address ,
206+ } ) ;
207+
196208 return (
197209 < Container p = "md" >
198210 { /* Sell */ }
@@ -261,10 +273,16 @@ export function SwapUIBase(
261273 disabled = { ! preparedResultQuery . data || preparedResultQuery . isFetching }
262274 fullWidth
263275 onClick = { ( ) => {
264- if ( preparedResultQuery . data && props . buyToken && props . sellToken ) {
276+ if (
277+ preparedResultQuery . data &&
278+ props . buyToken &&
279+ props . sellToken &&
280+ sellTokenBalanceQuery . data
281+ ) {
265282 props . onSwap ( preparedResultQuery . data , {
266283 buyToken : props . buyToken ,
267284 sellToken : props . sellToken ,
285+ sellTokenBalance : sellTokenBalanceQuery . data . value ,
268286 } ) ;
269287 }
270288 } }
@@ -561,3 +579,21 @@ const SwitchButtonInner = /* @__PURE__ */ styled(Button)(() => {
561579 border : `1px solid ${ theme . colors . borderColor } ` ,
562580 } ;
563581} ) ;
582+
583+ function useTokenBalance ( props : {
584+ chainId : number | undefined ;
585+ tokenAddress : string | undefined ;
586+ client : ThirdwebClient ;
587+ walletAddress : string | undefined ;
588+ } ) {
589+ return useWalletBalance ( {
590+ address : props . walletAddress ,
591+ chain : props . chainId ? defineChain ( props . chainId ) : undefined ,
592+ client : props . client ,
593+ tokenAddress : props . tokenAddress
594+ ? getAddress ( props . tokenAddress ) === getAddress ( NATIVE_TOKEN_ADDRESS )
595+ ? undefined
596+ : getAddress ( props . tokenAddress )
597+ : undefined ,
598+ } ) ;
599+ }
0 commit comments