From e99c2ec5cf7e5fa8295d04257ea97f2127f902d0 Mon Sep 17 00:00:00 2001 From: JellyWang <38491708+ezailWang@users.noreply.github.com> Date: Wed, 9 Oct 2024 10:58:56 +0800 Subject: [PATCH] fix: swap bridge switch (#5943) --- packages/kit-bg/src/services/ServiceSwap.ts | 2 + .../src/states/jotai/contexts/swap/actions.ts | 160 ++++- .../src/states/jotai/contexts/swap/atoms.ts | 21 +- .../components/SwapNetworkToggleGroup.tsx | 19 +- .../kit/src/views/Swap/hooks/useSwapState.ts | 16 +- .../kit/src/views/Swap/hooks/useSwapTokens.ts | 17 +- .../SwapAccountAddressContainer.tsx | 616 ++++++++++-------- .../pages/components/SwapAlertContainer.tsx | 93 ++- .../pages/components/SwapHeaderContainer.tsx | 111 +++- .../pages/components/SwapInputContainer.tsx | 13 +- .../Swap/pages/components/SwapMainLand.tsx | 8 +- .../Swap/pages/components/SwapQuoteResult.tsx | 1 - .../Swap/pages/modal/SwapTokenSelectModal.tsx | 50 +- packages/shared/types/swap/types.ts | 36 +- 14 files changed, 814 insertions(+), 349 deletions(-) diff --git a/packages/kit-bg/src/services/ServiceSwap.ts b/packages/kit-bg/src/services/ServiceSwap.ts index 14e609e89c8..a750f4118b9 100644 --- a/packages/kit-bg/src/services/ServiceSwap.ts +++ b/packages/kit-bg/src/services/ServiceSwap.ts @@ -160,6 +160,8 @@ export default class ServiceSwap extends ServiceBase { logoURI: clientNetwork.logoURI, networkId: network.networkId, defaultSelectToken: network.defaultSelectToken, + supportCrossChainSwap: network.supportCrossChainSwap, + supportSingleSwap: network.supportSingleSwap, }; } return null; diff --git a/packages/kit/src/states/jotai/contexts/swap/actions.ts b/packages/kit/src/states/jotai/contexts/swap/actions.ts index e4e3e696d84..5f85f4d486b 100644 --- a/packages/kit/src/states/jotai/contexts/swap/actions.ts +++ b/packages/kit/src/states/jotai/contexts/swap/actions.ts @@ -15,6 +15,7 @@ import { numberFormat } from '@onekeyhq/shared/src/utils/numberUtils'; import { equalTokenNoCaseSensitive } from '@onekeyhq/shared/src/utils/tokenUtils'; import { swapApprovingStateFetchInterval, + swapDefaultSetTokens, swapHistoryStateFetchRiceIntervalCount, swapQuoteFetchInterval, swapQuoteIntervalMaxCount, @@ -25,8 +26,10 @@ import { import type { IFetchQuotesParams, IFetchTokensParams, + ISwapAlertActionData, ISwapAlertState, ISwapApproveTransaction, + ISwapNetwork, ISwapQuoteEvent, ISwapQuoteEventAutoSlippage, ISwapQuoteEventData, @@ -35,12 +38,14 @@ import type { ISwapToken, } from '@onekeyhq/shared/types/swap/types'; import { + ESwapAlertActionType, ESwapAlertLevel, ESwapApproveTransactionStatus, ESwapDirectionType, ESwapFetchCancelCause, ESwapRateDifferenceUnit, ESwapSlippageSegmentKey, + ESwapTabSwitchType, ESwapTxHistoryStatus, } from '@onekeyhq/shared/types/swap/types'; @@ -57,6 +62,7 @@ import { swapFromTokenAmountAtom, swapManualSelectQuoteProvidersAtom, swapNetworks, + swapNetworksIncludeAllNetworkAtom, swapQuoteActionLockAtom, swapQuoteCurrentSelectAtom, swapQuoteEventTotalCountAtom, @@ -75,6 +81,7 @@ import { swapTokenFetchingAtom, swapTokenMapAtom, swapTokenMetadataAtom, + swapTypeSwitchAtom, } from './atoms'; class ContentJotaiActionsSwap extends ContextJotaiActionsBase { @@ -156,6 +163,8 @@ class ContentJotaiActionsSwap extends ContextJotaiActionsBase { selectFromToken = contextAtomMethod(async (get, set, token: ISwapToken) => { const fromToken = get(swapSelectFromTokenAtom()); + const toToken = get(swapSelectToTokenAtom()); + const swapTypeSwitchValue = get(swapTypeSwitchAtom()); if ( fromToken?.networkId !== token.networkId || fromToken?.contractAddress !== token.contractAddress @@ -163,6 +172,19 @@ class ContentJotaiActionsSwap extends ContextJotaiActionsBase { this.cleanManualSelectQuoteProviders.call(set); this.resetSwapSlippage.call(set); await this.syncNetworksSort.call(set, token.networkId); + if ( + token.networkId !== toToken?.networkId && + swapTypeSwitchValue === ESwapTabSwitchType.SWAP + ) { + if (token.isNative) { + set(swapSelectToTokenAtom(), undefined); + } else { + const defaultTokenSet = swapDefaultSetTokens[token.networkId]; + if (defaultTokenSet.fromToken) { + set(swapSelectToTokenAtom(), defaultTokenSet.fromToken); + } + } + } } set(swapSelectFromTokenAtom(), token); }); @@ -690,15 +712,54 @@ class ContentJotaiActionsSwap extends ContextJotaiActionsBase { } }; + checkAddressNeedCreate = ( + swapSupportAllNetworks: ISwapNetwork[], + fromToken: ISwapToken, + addressInfo: ReturnType, + ) => { + const netInfo = swapSupportAllNetworks.find( + (net) => net.networkId === fromToken.networkId, + ); + const networkId = addressInfo.accountInfo?.network?.id; + const walletId = addressInfo.accountInfo?.wallet?.id; + const indexedAccountId = addressInfo.accountInfo?.indexedAccount?.id; + const deriveType = addressInfo.accountInfo?.deriveType; + const account = { + walletId, + indexedAccountId, + deriveType, + networkId, + }; + const key = + networkId && walletId && (deriveType || indexedAccountId) + ? [networkId, deriveType, walletId, indexedAccountId].join('-') + : Math.random().toString(); + return { + message: `No ${netInfo?.name ?? ''} address`, + alertLevel: ESwapAlertLevel.INFO, + action: { + actionType: ESwapAlertActionType.CREATE_ADDRESS, + actionLabel: 'Create', + actionData: { + num: 0, + key, + account, + } as ISwapAlertActionData, + }, + }; + }; + checkSwapWarning = contextAtomMethod( async ( get, set, swapFromAddressInfo: ReturnType, + swapToAddressInfo: ReturnType, ) => { const fromToken = get(swapSelectFromTokenAtom()); const toToken = get(swapSelectToTokenAtom()); const networks = get(swapNetworks()); + const swapSupportAllNetworks = get(swapNetworksIncludeAllNetworkAtom()); const quoteResult = get(swapQuoteCurrentSelectAtom()); const tokenMetadata = get(swapTokenMetadataAtom()); const quoteResultList = get(swapQuoteListAtom()); @@ -774,8 +835,51 @@ class ContentJotaiActionsSwap extends ContextJotaiActionsBase { ]; } - // provider best check + // check from address + if ( + fromToken && + !swapFromAddressInfo.address && + (accountUtils.isHdWallet({ + walletId: swapFromAddressInfo.accountInfo?.wallet?.id, + }) || + accountUtils.isHwWallet({ + walletId: swapFromAddressInfo.accountInfo?.wallet?.id, + }) || + accountUtils.isQrWallet({ + walletId: swapFromAddressInfo.accountInfo?.wallet?.id, + })) + ) { + const alertAction = this.checkAddressNeedCreate( + swapSupportAllNetworks, + fromToken, + swapFromAddressInfo, + ); + alertsRes = [...alertsRes, alertAction]; + } + // check to address + if ( + toToken && + !swapToAddressInfo.address && + (accountUtils.isHdWallet({ + walletId: swapToAddressInfo.accountInfo?.wallet?.id, + }) || + accountUtils.isHwWallet({ + walletId: swapToAddressInfo.accountInfo?.wallet?.id, + }) || + accountUtils.isQrWallet({ + walletId: swapToAddressInfo.accountInfo?.wallet?.id, + })) + ) { + const alertAction = this.checkAddressNeedCreate( + swapSupportAllNetworks, + toToken, + swapToAddressInfo, + ); + alertsRes = [...alertsRes, alertAction]; + } + if (quoteResult?.toAmount && !quoteResult.isBest) { + // provider best check alertsRes = [ ...alertsRes, { @@ -825,6 +929,9 @@ class ContentJotaiActionsSwap extends ContextJotaiActionsBase { { number: '100%' }, ), alertLevel: ESwapAlertLevel.WARNING, + action: { + actionType: ESwapAlertActionType.TOKEN_DETAIL_FETCHING, + }, }, ]; } else if (difference.lt(swapRateDifferenceMax)) { @@ -842,6 +949,9 @@ class ContentJotaiActionsSwap extends ContextJotaiActionsBase { }, ), alertLevel: ESwapAlertLevel.WARNING, + action: { + actionType: ESwapAlertActionType.TOKEN_DETAIL_FETCHING, + }, }, ]; } @@ -911,6 +1021,9 @@ class ContentJotaiActionsSwap extends ContextJotaiActionsBase { id: ETranslations.swap_page_alert_fee_exceeds_amount, }), alertLevel: ESwapAlertLevel.WARNING, + action: { + actionType: ESwapAlertActionType.TOKEN_DETAIL_FETCHING, + }, }, ]; } @@ -1218,6 +1331,49 @@ class ContentJotaiActionsSwap extends ContextJotaiActionsBase { } }, ); + + swapTypeSwitchAction = contextAtomMethod( + async ( + get, + set, + type: ESwapTabSwitchType, + swapAccountNetworkId?: string, + ) => { + const swapSupportNetworks = get(swapNetworksIncludeAllNetworkAtom()); + const fromToken = get(swapSelectFromTokenAtom()); + const toToken = get(swapSelectToTokenAtom()); + if ( + fromToken && + !swapSupportNetworks.find( + (net) => net.networkId === fromToken?.networkId, + ) + ) { + set(swapSelectFromTokenAtom(), undefined); + } + if ( + toToken && + !swapSupportNetworks.find((net) => net.networkId === toToken?.networkId) + ) { + set(swapSelectToTokenAtom(), undefined); + } + if (type === ESwapTabSwitchType.BRIDGE) { + if (toToken && fromToken && toToken.networkId === fromToken.networkId) { + set(swapSelectToTokenAtom(), undefined); + } + } else if (type === ESwapTabSwitchType.SWAP) { + const fromNetworkDefault = + swapDefaultSetTokens[swapAccountNetworkId ?? '']; + if (fromToken) { + if (!fromToken.isNative && fromNetworkDefault.fromToken) { + set(swapSelectToTokenAtom(), fromNetworkDefault.fromToken); + } + } else if (fromNetworkDefault.fromToken) { + set(swapSelectFromTokenAtom(), fromNetworkDefault.fromToken); + } + } + set(swapTypeSwitchAtom(), type); + }, + ); } const createActions = memoFn(() => new ContentJotaiActionsSwap()); @@ -1245,6 +1401,7 @@ export const useSwapActions = () => { }, ); const swapLoadAllNetworkTokenList = actions.swapLoadAllNetworkTokenList.use(); + const swapTypeSwitchAction = actions.swapTypeSwitchAction.use(); const { cleanQuoteInterval, cleanApprovingInterval, closeQuoteEvent } = actions; @@ -1265,5 +1422,6 @@ export const useSwapActions = () => { quoteEventHandler, swapLoadAllNetworkTokenList, closeQuoteEvent, + swapTypeSwitchAction, }); }; diff --git a/packages/kit/src/states/jotai/contexts/swap/atoms.ts b/packages/kit/src/states/jotai/contexts/swap/atoms.ts index 456c49163e4..d0b2a5d4a65 100644 --- a/packages/kit/src/states/jotai/contexts/swap/atoms.ts +++ b/packages/kit/src/states/jotai/contexts/swap/atoms.ts @@ -7,10 +7,6 @@ import { ESwapProviderSort, swapSlippageAutoValue, } from '@onekeyhq/shared/types/swap/SwapProvider.constants'; -import { - ESwapReceiveAddressType, - ESwapSlippageSegmentKey, -} from '@onekeyhq/shared/types/swap/types'; import type { ESwapDirectionType, ESwapRateDifferenceUnit, @@ -23,6 +19,11 @@ import type { ISwapTokenCatch, ISwapTokenMetadata, } from '@onekeyhq/shared/types/swap/types'; +import { + ESwapReceiveAddressType, + ESwapSlippageSegmentKey, + ESwapTabSwitchType, +} from '@onekeyhq/shared/types/swap/types'; import { createJotaiContext } from '../../utils/createJotaiContext'; @@ -36,6 +37,10 @@ const { } = createJotaiContext(); export { ProviderJotaiContextSwap, contextAtomMethod }; +// swap bridge limit switch +export const { atom: swapTypeSwitchAtom, use: useSwapTypeSwitchAtom } = + contextAtom(ESwapTabSwitchType.SWAP); + // swap networks & tokens export const { atom: swapNetworks, use: useSwapNetworksAtom } = contextAtom< ISwapNetwork[] @@ -45,7 +50,13 @@ export const { atom: swapNetworksIncludeAllNetworkAtom, use: useSwapNetworksIncludeAllNetworkAtom, } = contextAtomComputed((get) => { - const networks = get(swapNetworks()); + let networks = get(swapNetworks()); + const swapType = get(swapTypeSwitchAtom()); + networks = networks.filter((net) => + swapType === ESwapTabSwitchType.BRIDGE + ? net.supportCrossChainSwap + : net.supportSingleSwap, + ); const allNetwork = { networkId: getNetworkIdsMap().onekeyall, name: dangerAllNetworkRepresent.name, diff --git a/packages/kit/src/views/Swap/components/SwapNetworkToggleGroup.tsx b/packages/kit/src/views/Swap/components/SwapNetworkToggleGroup.tsx index 1d051968f28..e215f1bc8d0 100644 --- a/packages/kit/src/views/Swap/components/SwapNetworkToggleGroup.tsx +++ b/packages/kit/src/views/Swap/components/SwapNetworkToggleGroup.tsx @@ -11,8 +11,8 @@ import { NetworksFilterItem } from '../../../components/NetworksFilterItem'; interface ISwapNetworkToggleGroupProps { networks: ISwapNetwork[]; + disableOtherNet?: boolean; moreNetworksCount?: number; - isOnlySupportSingleNetWork?: () => boolean; onSelectNetwork: (network: ISwapNetwork) => void; selectedNetwork?: ISwapNetwork; onMoreNetwork: () => void; @@ -22,6 +22,7 @@ const SwapNetworkToggleGroup = ({ networks, selectedNetwork, onSelectNetwork, + disableOtherNet, moreNetworksCount, onMoreNetwork, }: ISwapNetworkToggleGroupProps) => { @@ -37,6 +38,9 @@ const SwapNetworkToggleGroup = ({ {filteredNetworks.map((network) => ( { - onSelectNetwork(network); - }} + onPress={ + disableOtherNet && network.networkId !== selectedNetwork?.networkId + ? undefined + : () => { + onSelectNetwork(network); + } + } /> ))} {moreNetworksCount && moreNetworksCount > 0 ? ( ) : null} diff --git a/packages/kit/src/views/Swap/hooks/useSwapState.ts b/packages/kit/src/views/Swap/hooks/useSwapState.ts index acf8f5912dd..b572b331111 100644 --- a/packages/kit/src/views/Swap/hooks/useSwapState.ts +++ b/packages/kit/src/views/Swap/hooks/useSwapState.ts @@ -38,6 +38,7 @@ import { useSwapAddressInfo } from './useSwapAccount'; function useSwapWarningCheck() { const swapFromAddressInfo = useSwapAddressInfo(ESwapDirectionType.FROM); + const swapToAddressInfo = useSwapAddressInfo(ESwapDirectionType.TO); const [fromToken] = useSwapSelectFromTokenAtom(); const [toToken] = useSwapSelectToTokenAtom(); const [quoteCurrentSelect] = useSwapQuoteCurrentSelectAtom(); @@ -50,18 +51,29 @@ function useSwapWarningCheck() { networkId: undefined, accountInfo: undefined, }, + swapToAddressInfo: { + address: undefined, + networkId: undefined, + accountInfo: undefined, + }, }); const isFocused = useIsFocused(); const asyncRefContainer = useCallback(() => { if (refContainer.current.swapFromAddressInfo !== swapFromAddressInfo) { refContainer.current.swapFromAddressInfo = swapFromAddressInfo; } - }, [swapFromAddressInfo]); + if (refContainer.current.swapToAddressInfo !== swapToAddressInfo) { + refContainer.current.swapToAddressInfo = swapToAddressInfo; + } + }, [swapFromAddressInfo, swapToAddressInfo]); useEffect(() => { if (isFocused) { asyncRefContainer(); - void checkSwapWarning(refContainer.current.swapFromAddressInfo); + void checkSwapWarning( + refContainer.current.swapFromAddressInfo, + refContainer.current.swapToAddressInfo, + ); } }, [ asyncRefContainer, diff --git a/packages/kit/src/views/Swap/hooks/useSwapTokens.ts b/packages/kit/src/views/Swap/hooks/useSwapTokens.ts index 61f0d3df152..bd56f1782f0 100644 --- a/packages/kit/src/views/Swap/hooks/useSwapTokens.ts +++ b/packages/kit/src/views/Swap/hooks/useSwapTokens.ts @@ -74,11 +74,22 @@ export function useSwapInit(params?: ISwapInitParams) { setNetworkListFetching(false); return; } - const swapNetworksSortList = + let swapNetworksSortList = await backgroundApiProxy.simpleDb.swapNetworksSort.getRawData(); if (swapNetworksSortList?.data?.length) { - setSwapNetworks(swapNetworksSortList.data); - setNetworkListFetching(false); + const noSupportInfo = swapNetworksSortList?.data.every( + (net) => + isNil(net.supportCrossChainSwap) && isNil(net.supportSingleSwap), + ); + if (!noSupportInfo) { + setSwapNetworks(swapNetworksSortList.data); + setNetworkListFetching(false); + } else { + swapNetworksSortList = null; + void backgroundApiProxy.simpleDb.swapNetworksSort.setRawData({ + data: [], + }); + } } let networks: ISwapNetwork[] = []; const fetchNetworks = diff --git a/packages/kit/src/views/Swap/pages/components/SwapAccountAddressContainer.tsx b/packages/kit/src/views/Swap/pages/components/SwapAccountAddressContainer.tsx index d5129264925..8f45ea3e3a9 100644 --- a/packages/kit/src/views/Swap/pages/components/SwapAccountAddressContainer.tsx +++ b/packages/kit/src/views/Swap/pages/components/SwapAccountAddressContainer.tsx @@ -1,116 +1,122 @@ -import { useCallback, useMemo } from 'react'; +import { useMemo } from 'react'; import { useIntl } from 'react-intl'; -import type { IXStackProps } from '@onekeyhq/components'; +// import type { IXStackProps } from '@onekeyhq/components'; import { - Icon, + // Icon, + Image, SizableText, - Spinner, - Toast, + // Spinner, + // Toast, XStack, } from '@onekeyhq/components'; -import { useAccountSelectorCreateAddress } from '@onekeyhq/kit/src/components/AccountSelector/hooks/useAccountSelectorCreateAddress'; +// import { useAccountSelectorCreateAddress } from '@onekeyhq/kit/src/components/AccountSelector/hooks/useAccountSelectorCreateAddress'; import { - useSwapProviderSupportReceiveAddressAtom, - useSwapQuoteCurrentSelectAtom, + useSwapNetworksIncludeAllNetworkAtom, + // useSwapProviderSupportReceiveAddressAtom, + // useSwapQuoteCurrentSelectAtom, useSwapSelectFromTokenAtom, useSwapSelectToTokenAtom, + useSwapTypeSwitchAtom, } from '@onekeyhq/kit/src/states/jotai/contexts/swap'; -import { - useAccountManualCreatingAtom, - useSettingsAtom, -} from '@onekeyhq/kit-bg/src/states/jotai/atoms'; -import { getNetworkIdsMap } from '@onekeyhq/shared/src/config/networkIds'; +// import { +// useAccountManualCreatingAtom, +// useSettingsAtom, +// } from '@onekeyhq/kit-bg/src/states/jotai/atoms'; +// import { getNetworkIdsMap } from '@onekeyhq/shared/src/config/networkIds'; import { ETranslations } from '@onekeyhq/shared/src/locale'; -import platformEnv from '@onekeyhq/shared/src/platformEnv'; -import accountUtils from '@onekeyhq/shared/src/utils/accountUtils'; -import { ESwapDirectionType } from '@onekeyhq/shared/types/swap/types'; +// import platformEnv from '@onekeyhq/shared/src/platformEnv'; +// import accountUtils from '@onekeyhq/shared/src/utils/accountUtils'; +import { + ESwapDirectionType, + ESwapTabSwitchType, +} from '@onekeyhq/shared/types/swap/types'; -import { useSwapAddressInfo } from '../../hooks/useSwapAccount'; +// import { useSwapAddressInfo } from '../../hooks/useSwapAccount'; -const hitSlop = platformEnv.isNative - ? { - top: 8, - right: 8, - } - : undefined; -function AddressButton({ - address, - empty, - edited, - loading, - onPress, -}: { - address?: string; - empty?: boolean; - loading?: boolean; - edited?: boolean; -} & IXStackProps) { - const intl = useIntl(); - return ( - - - - {empty - ? intl.formatMessage({ id: ETranslations.swap_page_no_address }) - : address} - - {edited ? ( - - {intl.formatMessage({ - id: ETranslations.swap_account_to_address_edit, - })} - - ) : null} - - {loading ? : null} - {onPress && empty && !loading ? ( - - ) : null} - {onPress && !empty && !loading ? ( - - ) : null} - - ); -} +// const hitSlop = platformEnv.isNative +// ? { +// top: 8, +// right: 8, +// } +// : undefined; +// function AddressButton({ +// address, +// empty, +// edited, +// loading, +// onPress, +// }: { +// address?: string; +// empty?: boolean; +// loading?: boolean; +// edited?: boolean; +// } & IXStackProps) { +// const intl = useIntl(); +// return ( +// +// +// +// {empty +// ? intl.formatMessage({ id: ETranslations.swap_page_no_address }) +// : address} +// +// {edited ? ( +// +// {intl.formatMessage({ +// id: ETranslations.swap_account_to_address_edit, +// })} +// +// ) : null} +// +// {loading ? : null} +// {onPress && empty && !loading ? ( +// +// ) : null} +// {onPress && !empty && !loading ? ( +// +// ) : null} +// +// ); +// } interface ISwapAccountAddressContainerProps { type: ESwapDirectionType; @@ -118,205 +124,237 @@ interface ISwapAccountAddressContainerProps { } const SwapAccountAddressContainer = ({ type, - onToAnotherAddressModal, -}: ISwapAccountAddressContainerProps) => { +}: // onToAnotherAddressModal, +ISwapAccountAddressContainerProps) => { const intl = useIntl(); - const swapAddressInfo = useSwapAddressInfo(type); - const swapAnotherAddressInfo = useSwapAddressInfo( - type === ESwapDirectionType.FROM - ? ESwapDirectionType.TO - : ESwapDirectionType.FROM, - ); + // const swapAddressInfo = useSwapAddressInfo(type); + // const swapAnotherAddressInfo = useSwapAddressInfo( + // type === ESwapDirectionType.FROM + // ? ESwapDirectionType.TO + // : ESwapDirectionType.FROM, + // ); const [fromToken] = useSwapSelectFromTokenAtom(); + const [swapTypeSwitch] = useSwapTypeSwitchAtom(); + const [swapSupportAllNetwork] = useSwapNetworksIncludeAllNetworkAtom(); const [toToken] = useSwapSelectToTokenAtom(); - const [quoteResult] = useSwapQuoteCurrentSelectAtom(); - const [swapSupportReceiveAddress] = - useSwapProviderSupportReceiveAddressAtom(); - const { createAddress } = useAccountSelectorCreateAddress(); + // const [quoteResult] = useSwapQuoteCurrentSelectAtom(); + // const [swapSupportReceiveAddress] = + // useSwapProviderSupportReceiveAddressAtom(); + // const { createAddress } = useAccountSelectorCreateAddress(); - const [{ swapToAnotherAccountSwitchOn }] = useSettingsAtom(); - const [accountManualCreatingAtom, setAccountManualCreatingAtom] = - useAccountManualCreatingAtom(); + // const [{ swapToAnotherAccountSwitchOn }] = useSettingsAtom(); + // const [accountManualCreatingAtom, setAccountManualCreatingAtom] = + // useAccountManualCreatingAtom(); - const handleOnCreateAddress = useCallback(async () => { - if (!swapAddressInfo.accountInfo) return; - const networkId = swapAddressInfo.accountInfo.network?.id; - const walletId = swapAddressInfo.accountInfo.wallet?.id; - const indexedAccountId = swapAddressInfo.accountInfo.indexedAccount?.id; - const deriveType = swapAddressInfo.accountInfo.deriveType; - const account = { - walletId, - indexedAccountId, - deriveType, - networkId, - }; - const key = - networkId && walletId && (deriveType || indexedAccountId) - ? [networkId, deriveType, walletId, indexedAccountId].join('-') - : Math.random().toString(); - try { - setAccountManualCreatingAtom((prev) => ({ - ...prev, - key, - isLoading: true, - })); - await createAddress({ - num: type === ESwapDirectionType.FROM ? 0 : 1, - account, - selectAfterCreate: false, - }); - Toast.success({ - title: intl.formatMessage({ - id: ETranslations.swap_page_toast_address_generated, - }), - }); - } catch (e) { - Toast.error({ - title: intl.formatMessage({ - id: ETranslations.swap_page_toast_address_generated_fail, - }), - }); - } finally { - setAccountManualCreatingAtom((prev) => ({ - ...prev, - isLoading: false, - })); - } - }, [ - createAddress, - intl, - setAccountManualCreatingAtom, - swapAddressInfo.accountInfo, - type, - ]); + // const handleOnCreateAddress = useCallback(async () => { + // if (!swapAddressInfo.accountInfo) return; + // const networkId = swapAddressInfo.accountInfo.network?.id; + // const walletId = swapAddressInfo.accountInfo.wallet?.id; + // const indexedAccountId = swapAddressInfo.accountInfo.indexedAccount?.id; + // const deriveType = swapAddressInfo.accountInfo.deriveType; + // const account = { + // walletId, + // indexedAccountId, + // deriveType, + // networkId, + // }; + // const key = + // networkId && walletId && (deriveType || indexedAccountId) + // ? [networkId, deriveType, walletId, indexedAccountId].join('-') + // : Math.random().toString(); + // try { + // setAccountManualCreatingAtom((prev) => ({ + // ...prev, + // key, + // isLoading: true, + // })); + // await createAddress({ + // num: type === ESwapDirectionType.FROM ? 0 : 1, + // account, + // selectAfterCreate: false, + // }); + // Toast.success({ + // title: intl.formatMessage({ + // id: ETranslations.swap_page_toast_address_generated, + // }), + // }); + // } catch (e) { + // Toast.error({ + // title: intl.formatMessage({ + // id: ETranslations.swap_page_toast_address_generated_fail, + // }), + // }); + // } finally { + // setAccountManualCreatingAtom((prev) => ({ + // ...prev, + // isLoading: false, + // })); + // } + // }, [ + // createAddress, + // intl, + // setAccountManualCreatingAtom, + // swapAddressInfo.accountInfo, + // type, + // ]); - const addressComponent = useMemo(() => { - if (!fromToken && type === ESwapDirectionType.FROM) { - return null; - } - if (!toToken && type === ESwapDirectionType.TO) { - return null; - } - if ( - type === ESwapDirectionType.FROM && - (!swapAddressInfo.accountInfo?.wallet || - ((accountUtils.isHdWallet({ - walletId: swapAddressInfo.accountInfo?.wallet?.id, - }) || - accountUtils.isHwWallet({ - walletId: swapAddressInfo.accountInfo?.wallet?.id, - }) || - accountUtils.isQrWallet({ - walletId: swapAddressInfo.accountInfo?.wallet?.id, - })) && - !swapAddressInfo.accountInfo?.indexedAccount) || - (!swapAddressInfo.address && - !accountUtils.isHdWallet({ - walletId: swapAddressInfo.accountInfo?.wallet?.id, - }) && - !accountUtils.isHwWallet({ - walletId: swapAddressInfo.accountInfo?.wallet?.id, - }))) - ) { - return null; - } - // address same need hidden - if ( - ((fromToken && type === ESwapDirectionType.FROM) || - (toToken && type === ESwapDirectionType.TO)) && - swapAddressInfo.address && - swapAnotherAddressInfo.address && - swapAddressInfo.address === swapAnotherAddressInfo.address - ) { - return null; - } - // all net need hidden - if ( - swapAddressInfo.accountInfo?.network?.id === - getNetworkIdsMap().onekeyall || - swapAnotherAddressInfo.accountInfo?.network?.id === - getNetworkIdsMap().onekeyall - ) { - return null; - } - if ( - fromToken && - (!toToken || (toToken && !swapAnotherAddressInfo.address)) && - type === ESwapDirectionType.FROM && - swapAddressInfo.address - ) { - return null; - } - if ( - !swapAddressInfo.address && - (accountUtils.isHdWallet({ - walletId: swapAddressInfo.accountInfo?.wallet?.id, - }) || - accountUtils.isHwWallet({ - walletId: swapAddressInfo.accountInfo?.wallet?.id, - }) || - accountUtils.isQrWallet({ - walletId: swapAddressInfo.accountInfo?.wallet?.id, - })) - ) { - return ( - - ); - } - if ( - type === ESwapDirectionType.FROM || - !swapSupportReceiveAddress || - !quoteResult - ) { - return ( - + // const addressComponent = useMemo(() => { + // if (!fromToken && type === ESwapDirectionType.FROM) { + // return null; + // } + // if (!toToken && type === ESwapDirectionType.TO) { + // return null; + // } + // if ( + // type === ESwapDirectionType.FROM && + // (!swapAddressInfo.accountInfo?.wallet || + // ((accountUtils.isHdWallet({ + // walletId: swapAddressInfo.accountInfo?.wallet?.id, + // }) || + // accountUtils.isHwWallet({ + // walletId: swapAddressInfo.accountInfo?.wallet?.id, + // }) || + // accountUtils.isQrWallet({ + // walletId: swapAddressInfo.accountInfo?.wallet?.id, + // })) && + // !swapAddressInfo.accountInfo?.indexedAccount) || + // (!swapAddressInfo.address && + // !accountUtils.isHdWallet({ + // walletId: swapAddressInfo.accountInfo?.wallet?.id, + // }) && + // !accountUtils.isHwWallet({ + // walletId: swapAddressInfo.accountInfo?.wallet?.id, + // }))) + // ) { + // return null; + // } + // // address same need hidden + // if ( + // ((fromToken && type === ESwapDirectionType.FROM) || + // (toToken && type === ESwapDirectionType.TO)) && + // swapAddressInfo.address && + // swapAnotherAddressInfo.address && + // swapAddressInfo.address === swapAnotherAddressInfo.address + // ) { + // return null; + // } + // // all net need hidden + // if ( + // swapAddressInfo.accountInfo?.network?.id === + // getNetworkIdsMap().onekeyall || + // swapAnotherAddressInfo.accountInfo?.network?.id === + // getNetworkIdsMap().onekeyall + // ) { + // return null; + // } + // if ( + // fromToken && + // (!toToken || (toToken && !swapAnotherAddressInfo.address)) && + // type === ESwapDirectionType.FROM && + // swapAddressInfo.address + // ) { + // return null; + // } + // if ( + // !swapAddressInfo.address && + // (accountUtils.isHdWallet({ + // walletId: swapAddressInfo.accountInfo?.wallet?.id, + // }) || + // accountUtils.isHwWallet({ + // walletId: swapAddressInfo.accountInfo?.wallet?.id, + // }) || + // accountUtils.isQrWallet({ + // walletId: swapAddressInfo.accountInfo?.wallet?.id, + // })) + // ) { + // return ( + // + // ); + // } + // if ( + // type === ESwapDirectionType.FROM || + // !swapSupportReceiveAddress || + // !quoteResult + // ) { + // return ( + // + // ); + // } + // // to address + // return ( + // + // ); + // }, [ + // fromToken, + // type, + // toToken, + // swapAddressInfo.accountInfo?.wallet, + // swapAddressInfo.accountInfo?.indexedAccount, + // swapAddressInfo.accountInfo?.network?.id, + // swapAddressInfo.address, + // swapAnotherAddressInfo.address, + // swapAnotherAddressInfo.accountInfo?.network?.id, + // swapSupportReceiveAddress, + // quoteResult, + // onToAnotherAddressModal, + // swapToAnotherAccountSwitchOn, + // intl, + // accountManualCreatingAtom.isLoading, + // handleOnCreateAddress, + // ]); + + const networkComponent = useMemo(() => { + if (swapTypeSwitch === ESwapTabSwitchType.BRIDGE) { + const networkInfo = swapSupportAllNetwork.find( + (net) => + net.networkId === + (type === ESwapDirectionType.FROM + ? fromToken?.networkId + : toToken?.networkId), ); + if (networkInfo) { + return ( + + + + {networkInfo.name} + + + ); + } + return null; } - // to address - return ( - - ); + return null; }, [ - fromToken, type, - toToken, - swapAddressInfo.accountInfo?.wallet, - swapAddressInfo.accountInfo?.indexedAccount, - swapAddressInfo.accountInfo?.network?.id, - swapAddressInfo.address, - swapAnotherAddressInfo.address, - swapAnotherAddressInfo.accountInfo?.network?.id, - swapSupportReceiveAddress, - quoteResult, - onToAnotherAddressModal, - swapToAnotherAccountSwitchOn, - intl, - accountManualCreatingAtom.isLoading, - handleOnCreateAddress, + fromToken?.networkId, + swapSupportAllNetwork, + swapTypeSwitch, + toToken?.networkId, ]); return ( @@ -329,7 +367,7 @@ const SwapAccountAddressContainer = ({ : ETranslations.swap_page_to, })} - {addressComponent} + {networkComponent} ); }; diff --git a/packages/kit/src/views/Swap/pages/components/SwapAlertContainer.tsx b/packages/kit/src/views/Swap/pages/components/SwapAlertContainer.tsx index 25c7d9f1d48..91cc860865e 100644 --- a/packages/kit/src/views/Swap/pages/components/SwapAlertContainer.tsx +++ b/packages/kit/src/views/Swap/pages/components/SwapAlertContainer.tsx @@ -1,8 +1,20 @@ -import { memo, useMemo } from 'react'; +import { memo, useCallback, useMemo } from 'react'; -import { Alert, YStack } from '@onekeyhq/components'; -import type { ISwapAlertState } from '@onekeyhq/shared/types/swap/types'; -import { ESwapAlertLevel } from '@onekeyhq/shared/types/swap/types'; +import { useIntl } from 'react-intl'; + +import { Alert, Toast, YStack } from '@onekeyhq/components'; +import { useAccountSelectorCreateAddress } from '@onekeyhq/kit/src/components/AccountSelector/hooks/useAccountSelectorCreateAddress'; +import { useSwapSelectTokenDetailFetchingAtom } from '@onekeyhq/kit/src/states/jotai/contexts/swap'; +import { useAccountManualCreatingAtom } from '@onekeyhq/kit-bg/src/states/jotai/atoms'; +import { ETranslations } from '@onekeyhq/shared/src/locale'; +import type { + ISwapAlertActionData, + ISwapAlertState, +} from '@onekeyhq/shared/types/swap/types'; +import { + ESwapAlertActionType, + ESwapAlertLevel, +} from '@onekeyhq/shared/types/swap/types'; export interface ISwapAlertContainerProps { alerts: ISwapAlertState[]; @@ -22,7 +34,55 @@ const SwapAlertContainer = ({ alerts }: ISwapAlertContainerProps) => { }), [alerts], ); + const [selectTokenDetailLoading] = useSwapSelectTokenDetailFetchingAtom(); + const intl = useIntl(); + const [accountManualCreatingAtom, setAccountManualCreatingAtom] = + useAccountManualCreatingAtom(); + const { createAddress } = useAccountSelectorCreateAddress(); + const handleAlertAction = useCallback( + async (action?: { + actionType: ESwapAlertActionType; + actionLabel?: string; + actionData?: ISwapAlertActionData; + }) => { + if ( + action?.actionType === ESwapAlertActionType.CREATE_ADDRESS && + action.actionData?.account + ) { + try { + setAccountManualCreatingAtom((prev) => ({ + ...prev, + key: action?.actionData?.key, + isLoading: true, + })); + await createAddress({ + num: action?.actionData?.num ?? 0, + account: action?.actionData?.account, + selectAfterCreate: false, + }); + Toast.success({ + title: intl.formatMessage({ + id: ETranslations.swap_page_toast_address_generated, + }), + }); + } catch (e) { + Toast.error({ + title: intl.formatMessage({ + id: ETranslations.swap_page_toast_address_generated_fail, + }), + }); + } finally { + setAccountManualCreatingAtom((prev) => ({ + ...prev, + key: action?.actionData?.key, + isLoading: false, + })); + } + } + }, + [createAddress, intl, setAccountManualCreatingAtom], + ); if (alertsSorted?.some((item) => item.alertLevel === ESwapAlertLevel.ERROR)) { return ( @@ -36,10 +96,24 @@ const SwapAlertContainer = ({ alerts }: ISwapAlertContainerProps) => { ); } + return ( {alertsSorted?.map((item, index) => { - const { message, alertLevel } = item; + const { message, alertLevel, action } = item; + if ( + action?.actionType === ESwapAlertActionType.CREATE_ADDRESS && + action?.actionData?.key === accountManualCreatingAtom.key && + !accountManualCreatingAtom.isLoading + ) { + return null; + } + if ( + (selectTokenDetailLoading.from || selectTokenDetailLoading.to) && + action?.actionType === ESwapAlertActionType.TOKEN_DETAIL_FETCHING + ) { + return null; + } return ( { alertLevel === ESwapAlertLevel.WARNING ? 'warning' : 'default' } description={message} + action={{ + primary: action?.actionLabel ?? '', + onPrimaryPress: () => { + void handleAlertAction(action); + }, + isPrimaryLoading: + accountManualCreatingAtom.key === action?.actionData?.key && + accountManualCreatingAtom.isLoading, + }} /> ); }) ?? null} diff --git a/packages/kit/src/views/Swap/pages/components/SwapHeaderContainer.tsx b/packages/kit/src/views/Swap/pages/components/SwapHeaderContainer.tsx index 3e0e43c908f..bedc3a36c02 100644 --- a/packages/kit/src/views/Swap/pages/components/SwapHeaderContainer.tsx +++ b/packages/kit/src/views/Swap/pages/components/SwapHeaderContainer.tsx @@ -2,13 +2,37 @@ import { memo, useCallback } from 'react'; import { useIntl } from 'react-intl'; -import { Badge, Dialog, SizableText, XStack } from '@onekeyhq/components'; +import { + Badge, + Dialog, + EPageType, + SegmentControl, + SizableText, + XStack, +} from '@onekeyhq/components'; +import { + useSwapActions, + useSwapTypeSwitchAtom, +} from '@onekeyhq/kit/src/states/jotai/contexts/swap'; import { ETranslations } from '@onekeyhq/shared/src/locale'; +import { + ESwapDirectionType, + ESwapTabSwitchType, +} from '@onekeyhq/shared/types/swap/types'; + +import { useSwapAddressInfo } from '../../hooks/useSwapAccount'; import SwapHeaderRightActionContainer from './SwapHeaderRightActionContainer'; -const SwapHeaderContainer = () => { +interface ISwapHeaderContainerProps { + pageType?: EPageType; +} + +const SwapHeaderContainer = ({ pageType }: ISwapHeaderContainerProps) => { const intl = useIntl(); + const [swapTypeSwitch] = useSwapTypeSwitchAtom(); + const { swapTypeSwitchAction } = useSwapActions().current; + const { networkId } = useSwapAddressInfo(ESwapDirectionType.FROM); const headerRight = useCallback(() => , []); const onSwapLimit = useCallback(() => { @@ -26,23 +50,78 @@ const SwapHeaderContainer = () => { }), }); }, [intl]); - return ( - - - - {intl.formatMessage({ id: ETranslations.swap_page_swap })} - - - - - {intl.formatMessage({ id: ETranslations.swap_page_limit })} + + if (pageType !== EPageType.modal) { + return ( + + + { + if (swapTypeSwitch !== ESwapTabSwitchType.SWAP) { + void swapTypeSwitchAction(ESwapTabSwitchType.SWAP, networkId); + } + }} + > + {intl.formatMessage({ id: ETranslations.swap_page_swap })} - - {intl.formatMessage({ id: ETranslations.coming_soon })} - + + { + if (swapTypeSwitch !== ESwapTabSwitchType.BRIDGE) { + void swapTypeSwitchAction(ESwapTabSwitchType.BRIDGE, networkId); + } + }} + > + {intl.formatMessage({ id: ETranslations.swap_page_swap })} + + + + + {intl.formatMessage({ id: ETranslations.swap_page_limit })} + + + {intl.formatMessage({ id: ETranslations.coming_soon })} + + + {headerRight()} + + ); + } + return ( + + + { + void swapTypeSwitchAction(value as ESwapTabSwitchType, networkId); + }} + /> - {headerRight()} ); }; diff --git a/packages/kit/src/views/Swap/pages/components/SwapInputContainer.tsx b/packages/kit/src/views/Swap/pages/components/SwapInputContainer.tsx index ed899f733a6..bdac213faa7 100644 --- a/packages/kit/src/views/Swap/pages/components/SwapInputContainer.tsx +++ b/packages/kit/src/views/Swap/pages/components/SwapInputContainer.tsx @@ -9,12 +9,14 @@ import { useSwapAlertsAtom, } from '@onekeyhq/kit/src/states/jotai/contexts/swap'; import { useSettingsPersistAtom } from '@onekeyhq/kit-bg/src/states/jotai/atoms'; +import accountUtils from '@onekeyhq/shared/src/utils/accountUtils'; import type { ISwapToken } from '@onekeyhq/shared/types/swap/types'; import { ESwapDirectionType, ESwapRateDifferenceUnit, } from '@onekeyhq/shared/types/swap/types'; +import { useSwapAddressInfo } from '../../hooks/useSwapAccount'; import { useSwapSelectedTokenInfo } from '../../hooks/useSwapTokens'; import SwapAccountAddressContainer from './SwapAccountAddressContainer'; @@ -51,6 +53,7 @@ const SwapInputContainer = ({ }); const [settingsPersistAtom] = useSettingsPersistAtom(); const [alerts] = useSwapAlertsAtom(); + const { address, accountInfo } = useSwapAddressInfo(direction); const [rateDifference] = useRateDifferenceAtom(); const amountPrice = useMemo(() => { if (!token?.price) return '0.0'; @@ -65,9 +68,13 @@ const SwapInputContainer = ({ const fromInputHasError = useMemo( () => - alerts?.states.some((item) => item.inputShowError) && - direction === ESwapDirectionType.FROM, - [direction, alerts], + (alerts?.states.some((item) => item.inputShowError) && + direction === ESwapDirectionType.FROM) || + (!address && + (accountUtils.isHdWallet({ walletId: accountInfo?.wallet?.id }) || + accountUtils.isHwWallet({ walletId: accountInfo?.wallet?.id }) || + accountUtils.isQrWallet({ walletId: accountInfo?.wallet?.id }))), + [alerts?.states, direction, address, accountInfo], ); const valueMoreComponent = useMemo(() => { diff --git a/packages/kit/src/views/Swap/pages/components/SwapMainLand.tsx b/packages/kit/src/views/Swap/pages/components/SwapMainLand.tsx index 415afd68f9c..ddb8240a176 100644 --- a/packages/kit/src/views/Swap/pages/components/SwapMainLand.tsx +++ b/packages/kit/src/views/Swap/pages/components/SwapMainLand.tsx @@ -8,7 +8,6 @@ import { useSwapAlertsAtom, useSwapFromTokenAmountAtom, useSwapQuoteCurrentSelectAtom, - useSwapSelectTokenDetailFetchingAtom, } from '@onekeyhq/kit/src/states/jotai/contexts/swap'; import { EJotaiContextStoreNames, @@ -61,7 +60,6 @@ const SwapMainLoad = ({ swapInitParams, pageType }: ISwapMainLoadProps) => { const [{ swapRecentTokenPairs }] = useInAppNotificationAtom(); const [fromTokenAmount] = useSwapFromTokenAmountAtom(); const { selectFromToken, selectToToken } = useSwapActions().current; - const [selectTokenDetailLoading] = useSwapSelectTokenDetailFetchingAtom(); const onSelectToken = useCallback( (type: ESwapDirectionType) => { navigation.pushModal(EModalRoutes.SwapModal, { @@ -154,7 +152,7 @@ const SwapMainLoad = ({ swapInitParams, pageType }: ISwapMainLoadProps) => { pt: '$5', }} > - {pageType !== EPageType.modal ? : null} + { {alerts.states.length > 0 && !quoteLoading && !quoteEventFetching && - alerts.quoteId === (quoteResult?.quoteId ?? '') && - !selectTokenDetailLoading.from && - !selectTokenDetailLoading.to ? ( + alerts.quoteId === (quoteResult?.quoteId ?? '') ? ( ) : null} void; } diff --git a/packages/kit/src/views/Swap/pages/modal/SwapTokenSelectModal.tsx b/packages/kit/src/views/Swap/pages/modal/SwapTokenSelectModal.tsx index 06edcb1a721..8a6b446f8f2 100644 --- a/packages/kit/src/views/Swap/pages/modal/SwapTokenSelectModal.tsx +++ b/packages/kit/src/views/Swap/pages/modal/SwapTokenSelectModal.tsx @@ -31,10 +31,10 @@ import { useDebounce } from '@onekeyhq/kit/src/hooks/useDebounce'; import { useAccountSelectorActions } from '@onekeyhq/kit/src/states/jotai/contexts/accountSelector'; import { useSwapActions, - useSwapNetworksAtom, useSwapNetworksIncludeAllNetworkAtom, useSwapSelectFromTokenAtom, useSwapSelectToTokenAtom, + useSwapTypeSwitchAtom, } from '@onekeyhq/kit/src/states/jotai/contexts/swap'; import { useSettingsPersistAtom } from '@onekeyhq/kit-bg/src/states/jotai/atoms'; import { ETranslations } from '@onekeyhq/shared/src/locale'; @@ -53,6 +53,7 @@ import { } from '@onekeyhq/shared/types/swap/SwapProvider.constants'; import { ESwapDirectionType, + ESwapTabSwitchType, ETokenRiskLevel, type ISwapNetwork, type ISwapToken, @@ -82,10 +83,11 @@ const SwapTokenSelectPage = () => { const intl = useIntl(); const [searchKeyword, setSearchKeyword] = useState(''); const searchKeywordDebounce = useDebounce(searchKeyword, 500); - const [swapNetworks] = useSwapNetworksAtom(); + const [swapAllSupportNetworks] = useSwapNetworksIncludeAllNetworkAtom(); const [swapNetworksIncludeAllNetwork] = useSwapNetworksIncludeAllNetworkAtom(); const [fromToken] = useSwapSelectFromTokenAtom(); + const [swapTypeSwitch] = useSwapTypeSwitchAtom(); const swapFromAddressInfo = useSwapAddressInfo(ESwapDirectionType.FROM); const swapToAddressInfo = useSwapAddressInfo(ESwapDirectionType.TO); const [toToken] = useSwapSelectToTokenAtom(); @@ -102,17 +104,34 @@ const SwapTokenSelectPage = () => { ) ?? swapNetworksIncludeAllNetwork?.[0] ); } - } else if (toToken?.networkId) { - return ( - swapNetworksIncludeAllNetwork.find( - (item: ISwapNetwork) => item.networkId === toToken.networkId, - ) ?? swapNetworksIncludeAllNetwork?.[0] - ); + if (toToken?.networkId && swapTypeSwitch === ESwapTabSwitchType.SWAP) { + return ( + swapNetworksIncludeAllNetwork.find( + (item: ISwapNetwork) => item.networkId === toToken.networkId, + ) ?? swapNetworksIncludeAllNetwork?.[0] + ); + } + } else { + if (toToken?.networkId) { + return ( + swapNetworksIncludeAllNetwork.find( + (item: ISwapNetwork) => item.networkId === toToken.networkId, + ) ?? swapNetworksIncludeAllNetwork?.[0] + ); + } + if (fromToken?.networkId && swapTypeSwitch === ESwapTabSwitchType.SWAP) { + return ( + swapNetworksIncludeAllNetwork.find( + (item: ISwapNetwork) => item.networkId === fromToken.networkId, + ) ?? swapNetworksIncludeAllNetwork?.[0] + ); + } } return swapNetworksIncludeAllNetwork?.[0]; }, [ fromToken?.networkId, swapNetworksIncludeAllNetwork, + swapTypeSwitch, toToken?.networkId, type, ]); @@ -360,6 +379,14 @@ const SwapTokenSelectPage = () => { ], ); + const disableOtherNet = useMemo( + () => + type === ESwapDirectionType.TO && + !!fromToken && + swapTypeSwitch === ESwapTabSwitchType.SWAP, + [fromToken, swapTypeSwitch, type], + ); + const networkFilterData = useMemo(() => { let swapNetworksCommon: ISwapNetwork[] = []; let swapNetworksMoreCount; @@ -432,11 +459,13 @@ const SwapTokenSelectPage = () => { onMoreNetwork={() => { openChainSelector({ defaultNetworkId: currentSelectNetwork?.networkId, - networkIds: swapNetworks.map((item) => item.networkId), + networkIds: swapAllSupportNetworks + .filter((item) => !item.isAllNetworks) + .map((item) => item.networkId), grouped: false, onSelect: (network) => { if (!network) return; - const findSwapNetwork = swapNetworks.find( + const findSwapNetwork = swapAllSupportNetworks.find( (net) => net.networkId === network.id, ); if (!findSwapNetwork) return; @@ -448,6 +477,7 @@ const SwapTokenSelectPage = () => { networks={networkFilterData.swapNetworksCommon} moreNetworksCount={networkFilterData.swapNetworksMoreCount} selectedNetwork={currentSelectNetwork} + disableOtherNet={disableOtherNet} onSelectNetwork={onSelectCurrentNetwork} /> {currentNetworkPopularTokens.length > 0 && !searchKeywordDebounce ? ( diff --git a/packages/shared/types/swap/types.ts b/packages/shared/types/swap/types.ts index eeab5651b31..061703f4fe0 100644 --- a/packages/shared/types/swap/types.ts +++ b/packages/shared/types/swap/types.ts @@ -1,4 +1,6 @@ import type { useSwapAddressInfo } from '@onekeyhq/kit/src/views/Swap/hooks/useSwapAccount'; +import type { IDBWalletId } from '@onekeyhq/kit-bg/src/dbs/local/types'; +import type { IAccountDeriveTypes } from '@onekeyhq/kit-bg/src/vaults/types'; import type { IEventSourceCloseEvent, IEventSourceDoneEvent, @@ -10,10 +12,16 @@ import type { } from '@onekeyhq/shared/src/eventSource'; export enum EProtocolOfExchange { - SWAP = 'Swap', + SWAP = 'Swap', // swap and bridge LIMIT = 'Limit', // TODO } +export enum ESwapTabSwitchType { + SWAP = 'swap', + BRIDGE = 'bridge', + LIMIT = 'limit', +} + export enum ESwapReceiveAddressType { USER_ACCOUNT = 'user_account', INPUT = 'input', @@ -51,6 +59,8 @@ export interface ISwapInitParams { export interface ISwapNetworkBase { networkId: string; defaultSelectToken?: { from?: string; to?: string }; + supportCrossChainSwap?: boolean; + supportSingleSwap?: boolean; } export interface ISwapNetwork extends ISwapNetworkBase { @@ -296,6 +306,7 @@ export interface ISwapState { export interface ISwapCheckWarningDef { swapFromAddressInfo: ReturnType; + swapToAddressInfo: ReturnType; } export enum ESwapAlertLevel { @@ -303,12 +314,31 @@ export enum ESwapAlertLevel { WARNING = 'warning', ERROR = 'error', } + +export enum ESwapAlertActionType { + CREATE_ADDRESS = 'create_address', + TOKEN_DETAIL_FETCHING = 'token_detail_fetching', +} + +export interface ISwapAlertActionData { + num?: number; + key?: string; + account?: { + walletId: IDBWalletId | undefined; + networkId: string | undefined; + indexedAccountId: string | undefined; + deriveType: IAccountDeriveTypes; + }; +} export interface ISwapAlertState { message?: string; alertLevel?: ESwapAlertLevel; inputShowError?: boolean; - cb?: () => void; - cbLabel?: string; + action?: { + actionType: ESwapAlertActionType; + actionLabel?: string; + actionData?: ISwapAlertActionData; + }; } export interface ISwapQuoteEventAutoSlippage {