Skip to content

Commit 5ec84bd

Browse files
committed
[MNY-314] SDK: Token Selection UX improvements in SwapWidget (#8552)
<!-- ## title your PR with this format: "[SDK/Dashboard/Portal] Feature/Fix: Concise title for the changes" If you did not copy the branch name from Linear, paste the issue tag here (format is TEAM-0000): ## Notes for the reviewer Anything important to call out? Be sure to also clarify these in your comments. ## How to test Unit tests, playground, etc. --> <!-- start pr-codex --> --- ## PR-Codex overview This PR focuses on improving the user experience in the `SwapWidget` by automatically selecting the native token when no sell or buy token is selected. ### Detailed summary - Added logic to set the `sellToken` as the native token of the buy token's chain if no sell token is selected. - Added logic to set the `buyToken` as the native token of the sell token's chain if no buy token is selected. > ✨ Ask PR-Codex anything about this PR by commenting with `/codex {your question}` <!-- end pr-codex -->
1 parent 38f4a89 commit 5ec84bd

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

.changeset/eager-loops-obey.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"thirdweb": patch
3+
---
4+
5+
Token Selection UX improvements in SwapWidget

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ import { Buy, Sell } from "../../../../../bridge/index.js";
66
import type { prepare as SellPrepare } from "../../../../../bridge/Sell.js";
77
import type { TokenWithPrices } from "../../../../../bridge/types/Token.js";
88
import type { ThirdwebClient } from "../../../../../client/client.js";
9+
import { NATIVE_TOKEN_ADDRESS } from "../../../../../constants/addresses.js";
910
import { getToken } from "../../../../../pay/convert/get-token.js";
1011
import type { SupportedFiatCurrency } from "../../../../../pay/convert/type.js";
12+
import { getAddress } from "../../../../../utils/address.js";
1113
import { toTokens, toUnits } from "../../../../../utils/units.js";
1214
import { useCustomTheme } from "../../../../core/design-system/CustomThemeProvider.js";
1315
import {
@@ -245,6 +247,18 @@ export function SwapUI(props: SwapUIProps) {
245247
) {
246248
props.setSellToken(undefined);
247249
}
250+
251+
// if sell token is not selected, set it as native token of the buy token's chain if buy token is not a native token itself
252+
if (
253+
!props.sellToken &&
254+
token.tokenAddress.toLowerCase() !==
255+
NATIVE_TOKEN_ADDRESS.toLowerCase()
256+
) {
257+
props.setSellToken({
258+
tokenAddress: getAddress(NATIVE_TOKEN_ADDRESS),
259+
chainId: token.chainId,
260+
});
261+
}
248262
}}
249263
/>
250264
)}
@@ -270,6 +284,18 @@ export function SwapUI(props: SwapUIProps) {
270284
) {
271285
props.setBuyToken(undefined);
272286
}
287+
288+
// if buy token is not selected, set it as native token of the sell token's chain if sell token is not a native token itself
289+
if (
290+
!props.buyToken &&
291+
token.tokenAddress.toLowerCase() !==
292+
NATIVE_TOKEN_ADDRESS.toLowerCase()
293+
) {
294+
props.setBuyToken({
295+
tokenAddress: getAddress(NATIVE_TOKEN_ADDRESS),
296+
chainId: token.chainId,
297+
});
298+
}
273299
}}
274300
activeWalletInfo={props.activeWalletInfo}
275301
/>

0 commit comments

Comments
 (0)