Skip to content

Commit e0738d6

Browse files
authored
SDK, Dashboard: Fix SwapWidget setting same token for buy and sell in chain page (#8067)
1 parent e160deb commit e0738d6

File tree

3 files changed

+45
-26
lines changed

3 files changed

+45
-26
lines changed

.changeset/cuddly-steaks-bathe.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+
Fix SwapWidget setting same token for buy and sell in some cases when using last used token from storage

apps/dashboard/src/@/components/blocks/BuyAndSwapEmbed.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,12 @@ export function BuyAndSwapEmbed(props: {
9797
chainId: props.chain.id,
9898
tokenAddress: props.tokenAddress,
9999
},
100-
buyToken: {
101-
chainId: props.chain.id,
102-
},
100+
// only set `buyToken` as "Native token" if `sellToken` is not a "native token" already
101+
buyToken: props.tokenAddress
102+
? {
103+
chainId: props.chain.id,
104+
}
105+
: undefined,
103106
}}
104107
onError={(error, quote) => {
105108
const errorMessage = parseError(error);

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

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -323,33 +323,30 @@ function SwapWidgetContent(props: SwapWidgetProps) {
323323
chainId: props.prefill.buyToken.chainId,
324324
};
325325
}
326-
const last = getLastUsedTokens()?.buyToken;
327-
if (last) {
328-
return {
329-
tokenAddress: getAddress(last.tokenAddress),
330-
chainId: last.chainId,
331-
};
326+
const lastUsedBuyToken = getLastUsedTokens()?.buyToken;
327+
328+
// the token that will be set as initial value of sell token
329+
const sellToken = getInitialSellToken(
330+
props.prefill,
331+
getLastUsedTokens()?.sellToken,
332+
);
333+
334+
// if both tokens are same, ignore "buyToken", keep "sellToken"
335+
if (
336+
lastUsedBuyToken &&
337+
sellToken &&
338+
lastUsedBuyToken.tokenAddress.toLowerCase() ===
339+
sellToken.tokenAddress.toLowerCase() &&
340+
lastUsedBuyToken.chainId === sellToken.chainId
341+
) {
342+
return undefined;
332343
}
333-
return undefined;
344+
345+
return lastUsedBuyToken;
334346
});
335347

336348
const [sellToken, setSellToken] = useState<TokenSelection | undefined>(() => {
337-
if (props.prefill?.sellToken) {
338-
return {
339-
tokenAddress:
340-
props.prefill.sellToken.tokenAddress ||
341-
getAddress(NATIVE_TOKEN_ADDRESS),
342-
chainId: props.prefill.sellToken.chainId,
343-
};
344-
}
345-
const last = getLastUsedTokens()?.sellToken;
346-
if (last) {
347-
return {
348-
tokenAddress: getAddress(last.tokenAddress),
349-
chainId: last.chainId,
350-
};
351-
}
352-
return undefined;
349+
return getInitialSellToken(props.prefill, getLastUsedTokens()?.sellToken);
353350
});
354351

355352
// persist selections to localStorage whenever they change
@@ -515,3 +512,17 @@ function SwapWidgetContent(props: SwapWidgetProps) {
515512

516513
return null;
517514
}
515+
516+
function getInitialSellToken(
517+
prefill: SwapWidgetProps["prefill"],
518+
lastUsedSellToken: TokenSelection | undefined,
519+
) {
520+
if (prefill?.sellToken) {
521+
return {
522+
tokenAddress:
523+
prefill.sellToken.tokenAddress || getAddress(NATIVE_TOKEN_ADDRESS),
524+
chainId: prefill.sellToken.chainId,
525+
};
526+
}
527+
return lastUsedSellToken;
528+
}

0 commit comments

Comments
 (0)