Skip to content

Commit b5eb4ea

Browse files
authored
fix: swap batch transaction opt (#6403)
1 parent 0a11492 commit b5eb4ea

File tree

3 files changed

+66
-98
lines changed

3 files changed

+66
-98
lines changed

packages/kit/src/views/Swap/hooks/useSwapBuiltTx.ts

Lines changed: 27 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ import {
3232
EProtocolOfExchange,
3333
ESwapApproveTransactionStatus,
3434
ESwapDirectionType,
35-
SwapBuildUseMultiplePopoversNetworkIds,
3635
} from '@onekeyhq/shared/types/swap/types';
3736
import type { ISendTxOnSuccessData } from '@onekeyhq/shared/types/tx';
3837

@@ -52,7 +51,10 @@ import {
5251
} from '../../../states/jotai/contexts/swap';
5352

5453
import { useSwapAddressInfo } from './useSwapAccount';
55-
import { useSwapSlippagePercentageModeInfo } from './useSwapState';
54+
import {
55+
useSwapBatchTransfer,
56+
useSwapSlippagePercentageModeInfo,
57+
} from './useSwapState';
5658
import { useSwapTxHistoryActions } from './useSwapTxHistory';
5759

5860
export function useSwapBuildTx() {
@@ -66,7 +68,6 @@ export function useSwapBuildTx() {
6668
const [, setSwapBuildTxFetching] = useSwapBuildTxFetchingAtom();
6769
const [inAppNotificationAtom, setInAppNotificationAtom] =
6870
useInAppNotificationAtom();
69-
const [settingsPersistAtom] = useSettingsPersistAtom();
7071
const [, setSwapFromTokenAmount] = useSwapFromTokenAmountAtom();
7172
const [, setSwapShouldRefreshQuote] = useSwapShouldRefreshQuoteAtom();
7273
const [swapTypeSwitch] = useSwapTypeSwitchAtom();
@@ -81,7 +82,14 @@ export function useSwapBuildTx() {
8182
accountId: swapFromAddressInfo.accountInfo?.account?.id ?? '',
8283
networkId: swapFromAddressInfo.networkId ?? '',
8384
});
85+
8486
const pageType = usePageType();
87+
88+
const isBatchTransfer = useSwapBatchTransfer(
89+
swapFromAddressInfo.networkId,
90+
swapFromAddressInfo.accountInfo?.account?.id,
91+
);
92+
8593
const syncRecentTokenPairs = useCallback(
8694
async ({
8795
swapFromToken,
@@ -533,7 +541,7 @@ export function useSwapBuildTx() {
533541
swapFromAddressInfo.accountInfo?.account?.id &&
534542
swapFromAddressInfo.address
535543
) {
536-
if (settingsPersistAtom.swapBatchApproveAndSwap) {
544+
if (isBatchTransfer) {
537545
try {
538546
setSwapBuildTxFetching(true);
539547
let approvesInfo: IApproveInfo[] = [];
@@ -569,80 +577,18 @@ export function useSwapBuildTx() {
569577
}
570578
const createBuildTxRes = await createBuildTx();
571579
if (createBuildTxRes) {
572-
if (
573-
SwapBuildUseMultiplePopoversNetworkIds.includes(
574-
fromToken.networkId,
575-
)
576-
) {
577-
await navigationToSendConfirm({
578-
approvesInfo: [approvesInfo[0]],
579-
isInternalSwap: true,
580-
onSuccess: async (data: ISendTxOnSuccessData[]) => {
581-
if (approvesInfo.length > 1) {
582-
await navigationToSendConfirm({
583-
approvesInfo: [approvesInfo[1]],
584-
// tron network does not support use pre fee info
585-
feeInfo:
586-
SwapBuildUseMultiplePopoversNetworkIds.includes(
587-
fromToken.networkId,
588-
)
589-
? undefined
590-
: data?.[0]?.feeInfo,
591-
isInternalSwap: true,
592-
onSuccess: async (dataRes: ISendTxOnSuccessData[]) => {
593-
await navigationToSendConfirm({
594-
transfersInfo: createBuildTxRes.transferInfo
595-
? [createBuildTxRes.transferInfo]
596-
: undefined,
597-
encodedTx: createBuildTxRes.encodedTx,
598-
feeInfo:
599-
SwapBuildUseMultiplePopoversNetworkIds.includes(
600-
fromToken.networkId,
601-
)
602-
? undefined
603-
: dataRes?.[0]?.feeInfo,
604-
swapInfo: createBuildTxRes.swapInfo,
605-
isInternalSwap: true,
606-
onSuccess: handleBuildTxSuccess,
607-
onCancel: cancelBuildTx,
608-
});
609-
},
610-
onCancel: cancelBuildTx,
611-
});
612-
} else {
613-
await navigationToSendConfirm({
614-
transfersInfo: createBuildTxRes.transferInfo
615-
? [createBuildTxRes.transferInfo]
616-
: undefined,
617-
encodedTx: createBuildTxRes.encodedTx,
618-
swapInfo: createBuildTxRes.swapInfo,
619-
feeInfo:
620-
SwapBuildUseMultiplePopoversNetworkIds.includes(
621-
fromToken.networkId,
622-
)
623-
? undefined
624-
: data?.[0]?.feeInfo,
625-
isInternalSwap: true,
626-
onSuccess: handleBuildTxSuccess,
627-
onCancel: cancelBuildTx,
628-
});
629-
}
630-
},
631-
onCancel: cancelBuildTx,
632-
});
633-
} else {
634-
await navigationToSendConfirm({
635-
isInternalSwap: true,
636-
transfersInfo: createBuildTxRes.transferInfo
637-
? [createBuildTxRes.transferInfo]
638-
: undefined,
639-
encodedTx: createBuildTxRes.encodedTx,
640-
swapInfo: createBuildTxRes.swapInfo,
641-
approvesInfo,
642-
onSuccess: handleBuildTxSuccess,
643-
onCancel: cancelBuildTx,
644-
});
645-
}
580+
await navigationToSendConfirm({
581+
isInternalSwap: true,
582+
transfersInfo: createBuildTxRes.transferInfo
583+
? [createBuildTxRes.transferInfo]
584+
: undefined,
585+
encodedTx: createBuildTxRes.encodedTx,
586+
swapInfo: createBuildTxRes.swapInfo,
587+
approvesInfo,
588+
onSuccess: handleBuildTxSuccess,
589+
onCancel: cancelBuildTx,
590+
});
591+
646592
void syncRecentTokenPairs({
647593
swapFromToken: fromToken,
648594
swapToToken: toToken,
@@ -723,16 +669,16 @@ export function useSwapBuildTx() {
723669
swapFromAddressInfo.networkId,
724670
swapFromAddressInfo.accountInfo?.account?.id,
725671
swapFromAddressInfo.address,
726-
settingsPersistAtom.swapBatchApproveAndSwap,
672+
isBatchTransfer,
727673
setSwapBuildTxFetching,
728674
createBuildTx,
675+
navigationToSendConfirm,
676+
cancelBuildTx,
729677
syncRecentTokenPairs,
730678
slippageItem.value,
731679
isFirstTimeSwap,
732680
pageType,
733681
setPersistSettings,
734-
navigationToSendConfirm,
735-
cancelBuildTx,
736682
handleBuildTxSuccess,
737683
setSwapShouldRefreshQuote,
738684
setInAppNotificationAtom,

packages/kit/src/views/Swap/hooks/useSwapState.ts

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
useSettingsPersistAtom,
1111
} from '@onekeyhq/kit-bg/src/states/jotai/atoms';
1212
import { ETranslations } from '@onekeyhq/shared/src/locale';
13+
import accountUtils from '@onekeyhq/shared/src/utils/accountUtils';
1314
import {
1415
swapQuoteIntervalMaxCount,
1516
swapSlippageAutoValue,
@@ -22,6 +23,7 @@ import {
2223
ESwapAlertLevel,
2324
ESwapDirectionType,
2425
ESwapSlippageSegmentKey,
26+
SwapBuildUseMultiplePopoversNetworkIds,
2527
} from '@onekeyhq/shared/types/swap/types';
2628

2729
import { useDebounce } from '../../../hooks/useDebounce';
@@ -121,6 +123,20 @@ export function useSwapQuoteEventFetching() {
121123
);
122124
}
123125

126+
export function useSwapBatchTransfer(networkId?: string, accountId?: string) {
127+
const [settingsPersistAtom] = useSettingsPersistAtom();
128+
const isExternalAccount = accountUtils.isExternalAccount({
129+
accountId: accountId ?? '',
130+
});
131+
const isUnSupportBatchTransferNet =
132+
SwapBuildUseMultiplePopoversNetworkIds.includes(networkId ?? '');
133+
return (
134+
settingsPersistAtom.swapBatchApproveAndSwap &&
135+
!isUnSupportBatchTransferNet &&
136+
!isExternalAccount
137+
);
138+
}
139+
124140
export function useSwapActionState() {
125141
const intl = useIntl();
126142
const quoteLoading = useSwapQuoteLoading();
@@ -130,7 +146,6 @@ export function useSwapActionState() {
130146
const [fromTokenAmount] = useSwapFromTokenAmountAtom();
131147
const [fromToken] = useSwapSelectFromTokenAtom();
132148
const [toToken] = useSwapSelectToTokenAtom();
133-
const [settingsPersistAtom] = useSettingsPersistAtom();
134149
const [shouldRefreshQuote] = useSwapShouldRefreshQuoteAtom();
135150
const [swapQuoteApproveAllowanceUnLimit] =
136151
useSwapQuoteApproveAllowanceUnLimitAtom();
@@ -141,8 +156,15 @@ export function useSwapActionState() {
141156
const swapFromAddressInfo = useSwapAddressInfo(ESwapDirectionType.FROM);
142157
const swapToAddressInfo = useSwapAddressInfo(ESwapDirectionType.TO);
143158
const [quoteIntervalCount] = useSwapQuoteIntervalCountAtom();
144-
const isRefreshQuote =
145-
quoteIntervalCount > swapQuoteIntervalMaxCount || shouldRefreshQuote;
159+
const isBatchTransfer = useSwapBatchTransfer(
160+
swapFromAddressInfo.networkId,
161+
swapFromAddressInfo.accountInfo?.account?.id,
162+
);
163+
const isRefreshQuote = useMemo(
164+
() => quoteIntervalCount > swapQuoteIntervalMaxCount || shouldRefreshQuote,
165+
[quoteIntervalCount, shouldRefreshQuote],
166+
);
167+
146168
const hasError = alerts.states.some(
147169
(item) => item.alertLevel === ESwapAlertLevel.ERROR,
148170
);
@@ -196,7 +218,7 @@ export function useSwapActionState() {
196218
}
197219
if (quoteCurrentSelect && quoteCurrentSelect.allowanceResult) {
198220
infoRes.label = intl.formatMessage({
199-
id: settingsPersistAtom.swapBatchApproveAndSwap
221+
id: isBatchTransfer
200222
? ETranslations.swap_page_approve_and_swap
201223
: ETranslations.global_approve,
202224
});
@@ -268,7 +290,7 @@ export function useSwapActionState() {
268290
quoteLoading,
269291
quoteResultNoMatchDebounce,
270292
selectedFromTokenBalance,
271-
settingsPersistAtom.swapBatchApproveAndSwap,
293+
isBatchTransfer,
272294
swapFromAddressInfo.address,
273295
swapToAddressInfo.address,
274296
toToken,

packages/kit/src/views/Swap/pages/components/SwapActionsState.tsx

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,7 @@ import {
2626
useSwapSelectFromTokenAtom,
2727
useSwapSelectToTokenAtom,
2828
} from '@onekeyhq/kit/src/states/jotai/contexts/swap';
29-
import {
30-
useSettingsAtom,
31-
useSettingsPersistAtom,
32-
} from '@onekeyhq/kit-bg/src/states/jotai/atoms';
29+
import { useSettingsAtom } from '@onekeyhq/kit-bg/src/states/jotai/atoms';
3330
import { ETranslations } from '@onekeyhq/shared/src/locale';
3431
import { openUrlExternal } from '@onekeyhq/shared/src/utils/openUrlUtils';
3532
import {
@@ -44,6 +41,7 @@ import {
4441
} from '../../hooks/useSwapAccount';
4542
import {
4643
useSwapActionState,
44+
useSwapBatchTransfer,
4745
useSwapSlippagePercentageModeInfo,
4846
} from '../../hooks/useSwapState';
4947

@@ -145,7 +143,10 @@ const SwapActionsState = ({
145143
const [swapProviderSupportReceiveAddress] =
146144
useSwapProviderSupportReceiveAddressAtom();
147145
const [{ swapEnableRecipientAddress }] = useSettingsAtom();
148-
const [{ swapBatchApproveAndSwap }] = useSettingsPersistAtom();
146+
const isBatchTransfer = useSwapBatchTransfer(
147+
swapFromAddressInfo.networkId,
148+
swapFromAddressInfo.accountInfo?.account?.id,
149+
);
149150
const swapRecipientAddressInfo = useSwapRecipientAddressInfo(
150151
swapEnableRecipientAddress,
151152
);
@@ -273,7 +274,7 @@ const SwapActionsState = ({
273274
);
274275

275276
const approveStepComponent = useMemo(() => {
276-
if (swapActionState.isApprove && !swapBatchApproveAndSwap) {
277+
if (swapActionState.isApprove && !isBatchTransfer) {
277278
return (
278279
<XStack
279280
gap="$1"
@@ -334,11 +335,11 @@ const SwapActionsState = ({
334335
md,
335336
pageType,
336337
swapActionState.isApprove,
337-
swapBatchApproveAndSwap,
338+
isBatchTransfer,
338339
]);
339340

340341
const recipientComponent = useMemo(() => {
341-
if (swapActionState.isApprove && !swapBatchApproveAndSwap) {
342+
if (swapActionState.isApprove && !isBatchTransfer) {
342343
return null;
343344
}
344345
if (shouldShowRecipient) {
@@ -402,7 +403,7 @@ const SwapActionsState = ({
402403
pageType,
403404
shouldShowRecipient,
404405
swapActionState.isApprove,
405-
swapBatchApproveAndSwap,
406+
isBatchTransfer,
406407
swapRecipientAddressInfo?.accountInfo?.accountName,
407408
swapRecipientAddressInfo?.accountInfo?.walletName,
408409
swapRecipientAddressInfo?.isExtAccount,
@@ -411,9 +412,8 @@ const SwapActionsState = ({
411412

412413
const haveTips = useMemo(
413414
() =>
414-
shouldShowRecipient ||
415-
(swapActionState.isApprove && !swapBatchApproveAndSwap),
416-
[shouldShowRecipient, swapActionState.isApprove, swapBatchApproveAndSwap],
415+
shouldShowRecipient || (swapActionState.isApprove && !isBatchTransfer),
416+
[shouldShowRecipient, swapActionState.isApprove, isBatchTransfer],
417417
);
418418

419419
const actionComponent = useMemo(

0 commit comments

Comments
 (0)