From b5eb4ea34b302b9f0ac6b2bff4d1b0c8a37acb87 Mon Sep 17 00:00:00 2001 From: JellyWang <38491708+ezailWang@users.noreply.github.com> Date: Tue, 24 Dec 2024 15:13:02 +0800 Subject: [PATCH] fix: swap batch transaction opt (#6403) --- .../src/views/Swap/hooks/useSwapBuiltTx.ts | 108 +++++------------- .../kit/src/views/Swap/hooks/useSwapState.ts | 32 +++++- .../pages/components/SwapActionsState.tsx | 24 ++-- 3 files changed, 66 insertions(+), 98 deletions(-) diff --git a/packages/kit/src/views/Swap/hooks/useSwapBuiltTx.ts b/packages/kit/src/views/Swap/hooks/useSwapBuiltTx.ts index afc55f0612c..67b134fb896 100644 --- a/packages/kit/src/views/Swap/hooks/useSwapBuiltTx.ts +++ b/packages/kit/src/views/Swap/hooks/useSwapBuiltTx.ts @@ -32,7 +32,6 @@ import { EProtocolOfExchange, ESwapApproveTransactionStatus, ESwapDirectionType, - SwapBuildUseMultiplePopoversNetworkIds, } from '@onekeyhq/shared/types/swap/types'; import type { ISendTxOnSuccessData } from '@onekeyhq/shared/types/tx'; @@ -52,7 +51,10 @@ import { } from '../../../states/jotai/contexts/swap'; import { useSwapAddressInfo } from './useSwapAccount'; -import { useSwapSlippagePercentageModeInfo } from './useSwapState'; +import { + useSwapBatchTransfer, + useSwapSlippagePercentageModeInfo, +} from './useSwapState'; import { useSwapTxHistoryActions } from './useSwapTxHistory'; export function useSwapBuildTx() { @@ -66,7 +68,6 @@ export function useSwapBuildTx() { const [, setSwapBuildTxFetching] = useSwapBuildTxFetchingAtom(); const [inAppNotificationAtom, setInAppNotificationAtom] = useInAppNotificationAtom(); - const [settingsPersistAtom] = useSettingsPersistAtom(); const [, setSwapFromTokenAmount] = useSwapFromTokenAmountAtom(); const [, setSwapShouldRefreshQuote] = useSwapShouldRefreshQuoteAtom(); const [swapTypeSwitch] = useSwapTypeSwitchAtom(); @@ -81,7 +82,14 @@ export function useSwapBuildTx() { accountId: swapFromAddressInfo.accountInfo?.account?.id ?? '', networkId: swapFromAddressInfo.networkId ?? '', }); + const pageType = usePageType(); + + const isBatchTransfer = useSwapBatchTransfer( + swapFromAddressInfo.networkId, + swapFromAddressInfo.accountInfo?.account?.id, + ); + const syncRecentTokenPairs = useCallback( async ({ swapFromToken, @@ -533,7 +541,7 @@ export function useSwapBuildTx() { swapFromAddressInfo.accountInfo?.account?.id && swapFromAddressInfo.address ) { - if (settingsPersistAtom.swapBatchApproveAndSwap) { + if (isBatchTransfer) { try { setSwapBuildTxFetching(true); let approvesInfo: IApproveInfo[] = []; @@ -569,80 +577,18 @@ export function useSwapBuildTx() { } const createBuildTxRes = await createBuildTx(); if (createBuildTxRes) { - if ( - SwapBuildUseMultiplePopoversNetworkIds.includes( - fromToken.networkId, - ) - ) { - await navigationToSendConfirm({ - approvesInfo: [approvesInfo[0]], - isInternalSwap: true, - onSuccess: async (data: ISendTxOnSuccessData[]) => { - if (approvesInfo.length > 1) { - await navigationToSendConfirm({ - approvesInfo: [approvesInfo[1]], - // tron network does not support use pre fee info - feeInfo: - SwapBuildUseMultiplePopoversNetworkIds.includes( - fromToken.networkId, - ) - ? undefined - : data?.[0]?.feeInfo, - isInternalSwap: true, - onSuccess: async (dataRes: ISendTxOnSuccessData[]) => { - await navigationToSendConfirm({ - transfersInfo: createBuildTxRes.transferInfo - ? [createBuildTxRes.transferInfo] - : undefined, - encodedTx: createBuildTxRes.encodedTx, - feeInfo: - SwapBuildUseMultiplePopoversNetworkIds.includes( - fromToken.networkId, - ) - ? undefined - : dataRes?.[0]?.feeInfo, - swapInfo: createBuildTxRes.swapInfo, - isInternalSwap: true, - onSuccess: handleBuildTxSuccess, - onCancel: cancelBuildTx, - }); - }, - onCancel: cancelBuildTx, - }); - } else { - await navigationToSendConfirm({ - transfersInfo: createBuildTxRes.transferInfo - ? [createBuildTxRes.transferInfo] - : undefined, - encodedTx: createBuildTxRes.encodedTx, - swapInfo: createBuildTxRes.swapInfo, - feeInfo: - SwapBuildUseMultiplePopoversNetworkIds.includes( - fromToken.networkId, - ) - ? undefined - : data?.[0]?.feeInfo, - isInternalSwap: true, - onSuccess: handleBuildTxSuccess, - onCancel: cancelBuildTx, - }); - } - }, - onCancel: cancelBuildTx, - }); - } else { - await navigationToSendConfirm({ - isInternalSwap: true, - transfersInfo: createBuildTxRes.transferInfo - ? [createBuildTxRes.transferInfo] - : undefined, - encodedTx: createBuildTxRes.encodedTx, - swapInfo: createBuildTxRes.swapInfo, - approvesInfo, - onSuccess: handleBuildTxSuccess, - onCancel: cancelBuildTx, - }); - } + await navigationToSendConfirm({ + isInternalSwap: true, + transfersInfo: createBuildTxRes.transferInfo + ? [createBuildTxRes.transferInfo] + : undefined, + encodedTx: createBuildTxRes.encodedTx, + swapInfo: createBuildTxRes.swapInfo, + approvesInfo, + onSuccess: handleBuildTxSuccess, + onCancel: cancelBuildTx, + }); + void syncRecentTokenPairs({ swapFromToken: fromToken, swapToToken: toToken, @@ -723,16 +669,16 @@ export function useSwapBuildTx() { swapFromAddressInfo.networkId, swapFromAddressInfo.accountInfo?.account?.id, swapFromAddressInfo.address, - settingsPersistAtom.swapBatchApproveAndSwap, + isBatchTransfer, setSwapBuildTxFetching, createBuildTx, + navigationToSendConfirm, + cancelBuildTx, syncRecentTokenPairs, slippageItem.value, isFirstTimeSwap, pageType, setPersistSettings, - navigationToSendConfirm, - cancelBuildTx, handleBuildTxSuccess, setSwapShouldRefreshQuote, setInAppNotificationAtom, diff --git a/packages/kit/src/views/Swap/hooks/useSwapState.ts b/packages/kit/src/views/Swap/hooks/useSwapState.ts index eba28460f60..23e7dcd99b8 100644 --- a/packages/kit/src/views/Swap/hooks/useSwapState.ts +++ b/packages/kit/src/views/Swap/hooks/useSwapState.ts @@ -10,6 +10,7 @@ import { useSettingsPersistAtom, } from '@onekeyhq/kit-bg/src/states/jotai/atoms'; import { ETranslations } from '@onekeyhq/shared/src/locale'; +import accountUtils from '@onekeyhq/shared/src/utils/accountUtils'; import { swapQuoteIntervalMaxCount, swapSlippageAutoValue, @@ -22,6 +23,7 @@ import { ESwapAlertLevel, ESwapDirectionType, ESwapSlippageSegmentKey, + SwapBuildUseMultiplePopoversNetworkIds, } from '@onekeyhq/shared/types/swap/types'; import { useDebounce } from '../../../hooks/useDebounce'; @@ -121,6 +123,20 @@ export function useSwapQuoteEventFetching() { ); } +export function useSwapBatchTransfer(networkId?: string, accountId?: string) { + const [settingsPersistAtom] = useSettingsPersistAtom(); + const isExternalAccount = accountUtils.isExternalAccount({ + accountId: accountId ?? '', + }); + const isUnSupportBatchTransferNet = + SwapBuildUseMultiplePopoversNetworkIds.includes(networkId ?? ''); + return ( + settingsPersistAtom.swapBatchApproveAndSwap && + !isUnSupportBatchTransferNet && + !isExternalAccount + ); +} + export function useSwapActionState() { const intl = useIntl(); const quoteLoading = useSwapQuoteLoading(); @@ -130,7 +146,6 @@ export function useSwapActionState() { const [fromTokenAmount] = useSwapFromTokenAmountAtom(); const [fromToken] = useSwapSelectFromTokenAtom(); const [toToken] = useSwapSelectToTokenAtom(); - const [settingsPersistAtom] = useSettingsPersistAtom(); const [shouldRefreshQuote] = useSwapShouldRefreshQuoteAtom(); const [swapQuoteApproveAllowanceUnLimit] = useSwapQuoteApproveAllowanceUnLimitAtom(); @@ -141,8 +156,15 @@ export function useSwapActionState() { const swapFromAddressInfo = useSwapAddressInfo(ESwapDirectionType.FROM); const swapToAddressInfo = useSwapAddressInfo(ESwapDirectionType.TO); const [quoteIntervalCount] = useSwapQuoteIntervalCountAtom(); - const isRefreshQuote = - quoteIntervalCount > swapQuoteIntervalMaxCount || shouldRefreshQuote; + const isBatchTransfer = useSwapBatchTransfer( + swapFromAddressInfo.networkId, + swapFromAddressInfo.accountInfo?.account?.id, + ); + const isRefreshQuote = useMemo( + () => quoteIntervalCount > swapQuoteIntervalMaxCount || shouldRefreshQuote, + [quoteIntervalCount, shouldRefreshQuote], + ); + const hasError = alerts.states.some( (item) => item.alertLevel === ESwapAlertLevel.ERROR, ); @@ -196,7 +218,7 @@ export function useSwapActionState() { } if (quoteCurrentSelect && quoteCurrentSelect.allowanceResult) { infoRes.label = intl.formatMessage({ - id: settingsPersistAtom.swapBatchApproveAndSwap + id: isBatchTransfer ? ETranslations.swap_page_approve_and_swap : ETranslations.global_approve, }); @@ -268,7 +290,7 @@ export function useSwapActionState() { quoteLoading, quoteResultNoMatchDebounce, selectedFromTokenBalance, - settingsPersistAtom.swapBatchApproveAndSwap, + isBatchTransfer, swapFromAddressInfo.address, swapToAddressInfo.address, toToken, diff --git a/packages/kit/src/views/Swap/pages/components/SwapActionsState.tsx b/packages/kit/src/views/Swap/pages/components/SwapActionsState.tsx index 7bb470c3d6f..c96d83d4a72 100644 --- a/packages/kit/src/views/Swap/pages/components/SwapActionsState.tsx +++ b/packages/kit/src/views/Swap/pages/components/SwapActionsState.tsx @@ -26,10 +26,7 @@ import { useSwapSelectFromTokenAtom, useSwapSelectToTokenAtom, } from '@onekeyhq/kit/src/states/jotai/contexts/swap'; -import { - useSettingsAtom, - useSettingsPersistAtom, -} from '@onekeyhq/kit-bg/src/states/jotai/atoms'; +import { useSettingsAtom } from '@onekeyhq/kit-bg/src/states/jotai/atoms'; import { ETranslations } from '@onekeyhq/shared/src/locale'; import { openUrlExternal } from '@onekeyhq/shared/src/utils/openUrlUtils'; import { @@ -44,6 +41,7 @@ import { } from '../../hooks/useSwapAccount'; import { useSwapActionState, + useSwapBatchTransfer, useSwapSlippagePercentageModeInfo, } from '../../hooks/useSwapState'; @@ -145,7 +143,10 @@ const SwapActionsState = ({ const [swapProviderSupportReceiveAddress] = useSwapProviderSupportReceiveAddressAtom(); const [{ swapEnableRecipientAddress }] = useSettingsAtom(); - const [{ swapBatchApproveAndSwap }] = useSettingsPersistAtom(); + const isBatchTransfer = useSwapBatchTransfer( + swapFromAddressInfo.networkId, + swapFromAddressInfo.accountInfo?.account?.id, + ); const swapRecipientAddressInfo = useSwapRecipientAddressInfo( swapEnableRecipientAddress, ); @@ -273,7 +274,7 @@ const SwapActionsState = ({ ); const approveStepComponent = useMemo(() => { - if (swapActionState.isApprove && !swapBatchApproveAndSwap) { + if (swapActionState.isApprove && !isBatchTransfer) { return ( { - if (swapActionState.isApprove && !swapBatchApproveAndSwap) { + if (swapActionState.isApprove && !isBatchTransfer) { return null; } if (shouldShowRecipient) { @@ -402,7 +403,7 @@ const SwapActionsState = ({ pageType, shouldShowRecipient, swapActionState.isApprove, - swapBatchApproveAndSwap, + isBatchTransfer, swapRecipientAddressInfo?.accountInfo?.accountName, swapRecipientAddressInfo?.accountInfo?.walletName, swapRecipientAddressInfo?.isExtAccount, @@ -411,9 +412,8 @@ const SwapActionsState = ({ const haveTips = useMemo( () => - shouldShowRecipient || - (swapActionState.isApprove && !swapBatchApproveAndSwap), - [shouldShowRecipient, swapActionState.isApprove, swapBatchApproveAndSwap], + shouldShowRecipient || (swapActionState.isApprove && !isBatchTransfer), + [shouldShowRecipient, swapActionState.isApprove, isBatchTransfer], ); const actionComponent = useMemo(