Skip to content

Commit

Permalink
Fix/swap default to token ok-34398 (#6363)
Browse files Browse the repository at this point in the history
  • Loading branch information
ezailWang authored Dec 17, 2024
1 parent ddadbfb commit 4b79c9d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 17 deletions.
9 changes: 7 additions & 2 deletions packages/kit/src/states/jotai/contexts/swap/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1634,8 +1634,12 @@ export const useSwapActions = () => {
);
const swapLoadAllNetworkTokenList = actions.swapLoadAllNetworkTokenList.use();
const swapTypeSwitchAction = actions.swapTypeSwitchAction.use();
const { cleanQuoteInterval, cleanApprovingInterval, closeQuoteEvent } =
actions;
const {
cleanQuoteInterval,
cleanApprovingInterval,
closeQuoteEvent,
needChangeToken,
} = actions;

return useRef({
selectFromToken,
Expand All @@ -1655,5 +1659,6 @@ export const useSwapActions = () => {
swapLoadAllNetworkTokenList,
closeQuoteEvent,
swapTypeSwitchAction,
needChangeToken,
});
};
25 changes: 11 additions & 14 deletions packages/kit/src/views/Swap/hooks/useSwapTokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ import { useFuse } from '@onekeyhq/shared/src/modules3rdParty/fuse';
import { ETabRoutes } from '@onekeyhq/shared/src/routes';
import networkUtils from '@onekeyhq/shared/src/utils/networkUtils';
import { equalTokenNoCaseSensitive } from '@onekeyhq/shared/src/utils/tokenUtils';
import {
swapDefaultSetTokens,
tokenDetailSwapDefaultToTokens,
} from '@onekeyhq/shared/types/swap/SwapProvider.constants';
import { swapDefaultSetTokens } from '@onekeyhq/shared/types/swap/SwapProvider.constants';
import type {
ISwapInitParams,
ISwapNetwork,
Expand Down Expand Up @@ -50,7 +47,7 @@ export function useSwapInit(params?: ISwapInitParams) {
const [fromToken, setFromToken] = useSwapSelectFromTokenAtom();
const [toToken, setToToken] = useSwapSelectToTokenAtom();
const [, setInAppNotificationAtom] = useInAppNotificationAtom();
const { syncNetworksSort } = useSwapActions().current;
const { syncNetworksSort, needChangeToken } = useSwapActions().current;
const swapAddressInfo = useSwapAddressInfo(ESwapDirectionType.FROM);
const { updateSelectedAccountNetwork } = useAccountSelectorActions().current;
const [networkListFetching, setNetworkListFetching] = useState<boolean>(true);
Expand Down Expand Up @@ -206,20 +203,19 @@ export function useSwapInit(params?: ISwapInitParams) {
}
}
if (params?.importFromToken && !params?.importToToken) {
const fromNetworkDefault =
swapDefaultSetTokens[params?.importFromToken.networkId];

const defaultToToken = !params?.importFromToken?.isNative
? tokenDetailSwapDefaultToTokens[params?.importFromToken.networkId]
: fromNetworkDefault?.toToken;
if (defaultToToken) {
const needSetToToken = needChangeToken({
token: params.importFromToken,
swapTypeSwitchValue:
params?.swapTabSwitchType ?? ESwapTabSwitchType.SWAP,
});
if (needSetToToken) {
const defaultTokenSupportTypes =
checkSupportTokenSwapType(defaultToToken);
checkSupportTokenSwapType(needSetToToken);
if (
params?.swapTabSwitchType &&
defaultTokenSupportTypes.includes(params?.swapTabSwitchType)
) {
setToToken(defaultToToken);
setToToken(needSetToToken);
}
}
}
Expand Down Expand Up @@ -297,6 +293,7 @@ export function useSwapInit(params?: ISwapInitParams) {
checkSupportTokenSwapType,
setFromToken,
setToToken,
needChangeToken,
]);

useEffect(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ const SwapInputActions = ({
height="$5"
px="$1.5"
py="$0"
pt="$1"
pt={platformEnv.isNativeIOS ? '$1' : '$0'}
bg="$bgSubdued"
size="small"
label={
Expand Down

0 comments on commit 4b79c9d

Please sign in to comment.