Skip to content

Commit

Permalink
fix: swap batch transaction opt (#6403)
Browse files Browse the repository at this point in the history
  • Loading branch information
ezailWang authored Dec 24, 2024
1 parent 0a11492 commit b5eb4ea
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 98 deletions.
108 changes: 27 additions & 81 deletions packages/kit/src/views/Swap/hooks/useSwapBuiltTx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import {
EProtocolOfExchange,
ESwapApproveTransactionStatus,
ESwapDirectionType,
SwapBuildUseMultiplePopoversNetworkIds,
} from '@onekeyhq/shared/types/swap/types';
import type { ISendTxOnSuccessData } from '@onekeyhq/shared/types/tx';

Expand All @@ -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() {
Expand All @@ -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();
Expand All @@ -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,
Expand Down Expand Up @@ -533,7 +541,7 @@ export function useSwapBuildTx() {
swapFromAddressInfo.accountInfo?.account?.id &&
swapFromAddressInfo.address
) {
if (settingsPersistAtom.swapBatchApproveAndSwap) {
if (isBatchTransfer) {
try {
setSwapBuildTxFetching(true);
let approvesInfo: IApproveInfo[] = [];
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
32 changes: 27 additions & 5 deletions packages/kit/src/views/Swap/hooks/useSwapState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -22,6 +23,7 @@ import {
ESwapAlertLevel,
ESwapDirectionType,
ESwapSlippageSegmentKey,
SwapBuildUseMultiplePopoversNetworkIds,
} from '@onekeyhq/shared/types/swap/types';

import { useDebounce } from '../../../hooks/useDebounce';
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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,
);
Expand Down Expand Up @@ -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,
});
Expand Down Expand Up @@ -268,7 +290,7 @@ export function useSwapActionState() {
quoteLoading,
quoteResultNoMatchDebounce,
selectedFromTokenBalance,
settingsPersistAtom.swapBatchApproveAndSwap,
isBatchTransfer,
swapFromAddressInfo.address,
swapToAddressInfo.address,
toToken,
Expand Down
24 changes: 12 additions & 12 deletions packages/kit/src/views/Swap/pages/components/SwapActionsState.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -44,6 +41,7 @@ import {
} from '../../hooks/useSwapAccount';
import {
useSwapActionState,
useSwapBatchTransfer,
useSwapSlippagePercentageModeInfo,
} from '../../hooks/useSwapState';

Expand Down Expand Up @@ -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,
);
Expand Down Expand Up @@ -273,7 +274,7 @@ const SwapActionsState = ({
);

const approveStepComponent = useMemo(() => {
if (swapActionState.isApprove && !swapBatchApproveAndSwap) {
if (swapActionState.isApprove && !isBatchTransfer) {
return (
<XStack
gap="$1"
Expand Down Expand Up @@ -334,11 +335,11 @@ const SwapActionsState = ({
md,
pageType,
swapActionState.isApprove,
swapBatchApproveAndSwap,
isBatchTransfer,
]);

const recipientComponent = useMemo(() => {
if (swapActionState.isApprove && !swapBatchApproveAndSwap) {
if (swapActionState.isApprove && !isBatchTransfer) {
return null;
}
if (shouldShowRecipient) {
Expand Down Expand Up @@ -402,7 +403,7 @@ const SwapActionsState = ({
pageType,
shouldShowRecipient,
swapActionState.isApprove,
swapBatchApproveAndSwap,
isBatchTransfer,
swapRecipientAddressInfo?.accountInfo?.accountName,
swapRecipientAddressInfo?.accountInfo?.walletName,
swapRecipientAddressInfo?.isExtAccount,
Expand All @@ -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(
Expand Down

0 comments on commit b5eb4ea

Please sign in to comment.