Skip to content

Commit ce099a3

Browse files
committed
resolve todo
1 parent 751bd0a commit ce099a3

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

packages/thirdweb/src/react/web/ui/Bridge/swap-widget/SwapWidget.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ type SwapWidgetScreen =
8989
quote: Buy.quote.Result | Sell.quote.Result;
9090
buyToken: TokenWithPrices;
9191
sellToken: TokenWithPrices;
92+
sellTokenBalance: bigint;
9293
}
9394
| {
9495
id: "3:preview";
@@ -97,6 +98,7 @@ type SwapWidgetScreen =
9798
quote: Buy.quote.Result | Sell.quote.Result;
9899
buyToken: TokenWithPrices;
99100
sellToken: TokenWithPrices;
101+
sellTokenBalance: bigint;
100102
}
101103
| {
102104
id: "4:execute";
@@ -105,6 +107,7 @@ type SwapWidgetScreen =
105107
preparedQuote: BridgePrepareResult;
106108
buyToken: TokenWithPrices;
107109
sellToken: TokenWithPrices;
110+
sellTokenBalance: bigint;
108111
}
109112
| {
110113
id: "5:success";
@@ -186,7 +189,7 @@ function SwapWidgetContent(props: SwapWidgetProps) {
186189
quote: screen.quote,
187190
type: "wallet",
188191
payerWallet: activeWalletInfo.activeWallet,
189-
balance: 0n, // TODO - what is this?
192+
balance: screen.sellTokenBalance,
190193
originToken: screen.sellToken,
191194
}}
192195
/>
@@ -211,7 +214,7 @@ function SwapWidgetContent(props: SwapWidgetProps) {
211214
quote: screen.quote,
212215
type: "wallet",
213216
payerWallet: activeWalletInfo.activeWallet,
214-
balance: 0n, // TODO - what is this?
217+
balance: screen.sellTokenBalance,
215218
originToken: screen.sellToken,
216219
}}
217220
preparedQuote={screen.preparedQuote}
@@ -233,6 +236,7 @@ function SwapWidgetContent(props: SwapWidgetProps) {
233236
setScreen({
234237
...screen,
235238
id: "3:preview",
239+
sellTokenBalance: screen.sellTokenBalance,
236240
});
237241
}}
238242
onCancel={props.onCancel}

packages/thirdweb/src/react/web/ui/Bridge/swap-widget/swap-ui.tsx

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@ import { useState } from "react";
55
import { Buy, Sell } from "../../../../../bridge/index.js";
66
import type { BridgeChain } from "../../../../../bridge/types/Chain.js";
77
import type { TokenWithPrices } from "../../../../../bridge/types/Token.js";
8+
import { defineChain } from "../../../../../chains/utils.js";
89
import type { ThirdwebClient } from "../../../../../client/client.js";
10+
import { NATIVE_TOKEN_ADDRESS } from "../../../../../constants/addresses.js";
911
import {
1012
getFiatSymbol,
1113
type SupportedFiatCurrency,
1214
} from "../../../../../pay/convert/type.js";
15+
import { getAddress } from "../../../../../utils/address.js";
1316
import { toTokens, toUnits } from "../../../../../utils/units.js";
1417
import { useCustomTheme } from "../../../../core/design-system/CustomThemeProvider.js";
1518
import {
@@ -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";
2226
import { ConnectButton } from "../../ConnectWallet/ConnectButton.js";
2327
import { ArrowUpDownIcon } from "../../ConnectWallet/icons/ArrowUpDownIcon.js";
2428
import { 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

Comments
 (0)