Skip to content

Commit

Permalink
fix: swap some issue fix ok-34419 ok-34424 ok-34427 ok-34422 (#6369)
Browse files Browse the repository at this point in the history
  • Loading branch information
ezailWang authored Dec 18, 2024
1 parent bda3c03 commit aaab291
Show file tree
Hide file tree
Showing 29 changed files with 1,029 additions and 59 deletions.
2 changes: 0 additions & 2 deletions packages/kit-bg/src/states/jotai/atoms/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ export type ISettingsPersistAtom = {
inscriptionProtection: boolean;
isFirstTimeSwap: boolean;
swapBatchApproveAndSwap: boolean;
swapEnableRecipientAddress: boolean;

hardwareConnectSrc: EOnekeyDomain;
currencyInfo: {
Expand Down Expand Up @@ -59,7 +58,6 @@ export const settingsAtomInitialValue: ISettingsPersistAtom = {
inscriptionProtection: true,
isFirstTimeSwap: true,
swapBatchApproveAndSwap: true,
swapEnableRecipientAddress: false,
hardwareConnectSrc: EOnekeyDomain.ONEKEY_SO,
currencyInfo: {
id: 'usd',
Expand Down
69 changes: 55 additions & 14 deletions packages/kit/src/states/jotai/contexts/swap/atoms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ import BigNumber from 'bignumber.js';

import { getNetworkIdsMap } from '@onekeyhq/shared/src/config/networkIds';
import { dangerAllNetworkRepresent } from '@onekeyhq/shared/src/config/presetNetworks';
import { ESwapProviderSort } from '@onekeyhq/shared/types/swap/SwapProvider.constants';
import {
ESwapProviderSort,
swapProviderRecommendApprovedWeights,
} from '@onekeyhq/shared/types/swap/SwapProvider.constants';
import type {
ESwapDirectionType,
ESwapRateDifferenceUnit,
Expand Down Expand Up @@ -80,6 +83,11 @@ export const {
accountInfo: IAccountSelectorActiveAccountInfo | undefined;
}>({ networkId: undefined, address: undefined, accountInfo: undefined });

export const {
atom: swapEnableRecipientAddressAtom,
use: useSwapEnableRecipientAddressAtom,
} = contextAtom<boolean>(false);

// swap select token
export const {
atom: swapSelectFromTokenAtom,
Expand Down Expand Up @@ -230,12 +238,51 @@ export const {
}
return bVal.comparedTo(aVal);
});
let recommendedSorted = receivedSorted.slice();
const recommendedSortedApproved = recommendedSorted.filter(
(item) =>
!item.allowanceResult && item.toAmount && item.approvedInfo?.isApproved,
);
// check allowance result
if (
sortType === ESwapProviderSort.RECOMMENDED ||
sortType === ESwapProviderSort.RECEIVED
receivedSorted.length > 0 &&
recommendedSortedApproved.length > 0 &&
receivedSorted[0].allowanceResult
) {
const recommendedSortedApprovedSorted = recommendedSortedApproved
.slice()
.sort((a, b) => {
const aVal = new BigNumber(a.toAmount || 0);
const bVal = new BigNumber(b.toAmount || 0);
return bVal.comparedTo(aVal);
});
const recommendedSortedAllowanceSortedBestAmountBN = new BigNumber(
recommendedSortedApprovedSorted[0].toAmount || 0,
);
const receivedSortedBestAmountBN = new BigNumber(
receivedSorted[0].toAmount || 0,
);
if (
recommendedSortedAllowanceSortedBestAmountBN
.multipliedBy(swapProviderRecommendApprovedWeights)
.gt(receivedSortedBestAmountBN)
) {
recommendedSorted = recommendedSorted.filter(
(item) => item.quoteId !== recommendedSortedApprovedSorted[0].quoteId,
);
recommendedSorted = [
recommendedSortedApprovedSorted[0],
...recommendedSorted,
];
}
}

if (sortType === ESwapProviderSort.RECEIVED) {
sortedList = [...receivedSorted];
}
if (sortType === ESwapProviderSort.RECOMMENDED) {
sortedList = [...recommendedSorted];
}
sortedList = sortedList.slice().sort((a, b) => {
if (a.limit && b.limit) {
const aMin = new BigNumber(a.limit?.min || 0);
Expand All @@ -259,19 +306,13 @@ export const {
return 0;
});
return sortedList.map((p) => {
if (
p.info.provider === receivedSorted?.[0]?.info?.provider &&
p.info.providerName === receivedSorted?.[0]?.info?.providerName &&
p.toAmount
) {
p.receivedBest = true;
if (p.quoteId === recommendedSorted?.[0]?.quoteId && p.toAmount) {
p.isBest = true;
}
if (
p.info.provider === gasFeeSorted?.[0]?.info?.provider &&
p.info.providerName === gasFeeSorted?.[0]?.info?.providerName &&
p.toAmount
) {
if (p.quoteId === receivedSorted?.[0]?.quoteId && p.toAmount) {
p.receivedBest = true;
}
if (p.quoteId === gasFeeSorted?.[0]?.quoteId && p.toAmount) {
p.minGasCost = true;
}
return p;
Expand Down
10 changes: 10 additions & 0 deletions packages/kit/src/views/Swap/components/SwapProviderListItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@ const SwapProviderListItem = ({
{providerResult.isBest ||
providerResult.receivedBest ||
providerResult.minGasCost ||
providerResult.approvedInfo?.isApproved ||
providerResult.isAntiMEV ? (
<XStack flexWrap="wrap" justifyContent="flex-end" m={-3} flex={1}>
{providerResult.isBest ? (
Expand Down Expand Up @@ -361,6 +362,15 @@ const SwapProviderListItem = ({
</Badge>
</Stack>
) : null}
{providerResult.approvedInfo?.isApproved ? (
<Stack p={3}>
<Badge badgeType="info">
{intl.formatMessage({
id: ETranslations.provider_label_approved,
})}
</Badge>
</Stack>
) : null}
</XStack>
) : null}
</XStack>
Expand Down
49 changes: 26 additions & 23 deletions packages/kit/src/views/Swap/pages/components/SwapActionsState.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
} from '@onekeyhq/components';
import {
useSwapActions,
useSwapEnableRecipientAddressAtom,
useSwapFromTokenAmountAtom,
useSwapQuoteCurrentSelectAtom,
useSwapSelectFromTokenAtom,
Expand Down Expand Up @@ -128,7 +129,7 @@ const SwapActionsState = ({
const swapActionState = useSwapActionState();
const { slippageItem } = useSwapSlippagePercentageModeInfo();
const swapSlippageRef = useRef(slippageItem);
const [{ swapEnableRecipientAddress }] = useSettingsPersistAtom();
const [swapEnableRecipientAddress] = useSwapEnableRecipientAddressAtom();
const [{ swapBatchApproveAndSwap }] = useSettingsPersistAtom();
const swapRecipientAddressInfo = useSwapRecipientAddressInfo(
swapEnableRecipientAddress,
Expand Down Expand Up @@ -243,15 +244,13 @@ const SwapActionsState = ({
const shouldShowRecipient = useMemo(
() =>
swapEnableRecipientAddress &&
swapRecipientAddressInfo?.showAddress &&
fromToken &&
toToken &&
currentQuoteRes?.toTokenInfo.networkId === toToken.networkId,
[
swapEnableRecipientAddress,
currentQuoteRes?.toTokenInfo.networkId,
fromToken,
swapRecipientAddressInfo?.showAddress,
toToken,
],
);
Expand Down Expand Up @@ -349,27 +348,31 @@ const SwapActionsState = ({
textDecorationLine="underline"
onPress={onOpenRecipientAddress}
>
{swapRecipientAddressInfo?.showAddress}
</SizableText>

<SizableText
numberOfLines={1}
flexShrink={0}
size="$bodyMd"
color="$textSubdued"
>
{`(${
!swapRecipientAddressInfo?.isExtAccount
? `${
swapRecipientAddressInfo?.accountInfo?.walletName ?? ''
}-${
swapRecipientAddressInfo?.accountInfo?.accountName ?? ''
}`
: intl.formatMessage({
id: ETranslations.swap_page_recipient_external_account,
})
})`}
{swapRecipientAddressInfo?.showAddress ??
intl.formatMessage({
id: ETranslations.swap_page_recipient_add,
})}
</SizableText>
{swapRecipientAddressInfo?.showAddress ? (
<SizableText
numberOfLines={1}
flexShrink={0}
size="$bodyMd"
color="$textSubdued"
>
{`(${
!swapRecipientAddressInfo?.isExtAccount
? `${
swapRecipientAddressInfo?.accountInfo?.walletName ?? ''
}-${
swapRecipientAddressInfo?.accountInfo?.accountName ?? ''
}`
: intl.formatMessage({
id: ETranslations.swap_page_recipient_external_account,
})
})`}
</SizableText>
) : null}
</XStack>
</XStack>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
HeaderIconButton,
} from '@onekeyhq/components/src/layouts/Navigation/Header';
import useAppNavigation from '@onekeyhq/kit/src/hooks/useAppNavigation';
import { useSwapEnableRecipientAddressAtom } from '@onekeyhq/kit/src/states/jotai/contexts/swap';
import {
EJotaiContextStoreNames,
useInAppNotificationAtom,
Expand Down Expand Up @@ -225,10 +226,10 @@ const SwapSlippageCustomContent = ({
const SwapSettingsDialogContent = () => {
const intl = useIntl();
const { slippageItem } = useSwapSlippagePercentageModeInfo();
const [
{ swapBatchApproveAndSwap, swapEnableRecipientAddress },
setPersistSettings,
] = useSettingsPersistAtom();
const [swapEnableRecipientAddress, setSwapEnableRecipientAddress] =
useSwapEnableRecipientAddressAtom();
const [{ swapBatchApproveAndSwap }, setPersistSettings] =
useSettingsPersistAtom();
const [, setNoPersistSettings] = useSettingsAtom();
const rightTrigger = useMemo(
() => (
Expand Down Expand Up @@ -309,10 +310,7 @@ const SwapSettingsDialogContent = () => {
})}
value={swapEnableRecipientAddress}
onChange={(v) => {
setPersistSettings((s) => ({
...s,
swapEnableRecipientAddress: v,
}));
setSwapEnableRecipientAddress(v);
if (!v) {
setNoPersistSettings((s) => ({
...s,
Expand Down Expand Up @@ -389,23 +387,29 @@ const SwapHeaderRightActionContainer = ({
showFooter: true,
});
}, [intl, pageType]);

return (
<HeaderButtonGroup>
{slippageTitle ? (
<Button
<XStack
onPress={onOpenSwapSettings}
size="medium"
variant="tertiary"
borderRadius="$3"
bg="$bgSubdued"
cursor="pointer"
px="$2"
py="$1"
gap="$1"
alignItems="center"
justifyContent="center"
hoverStyle={{
bg: '$bgHover',
}}
pressStyle={{
bg: '$bgActive',
}}
>
<XStack alignItems="center" gap="$1">
{slippageTitle}
<Icon name="SliderHorOutline" size="$6" color="$iconSubdued" />
</XStack>
</Button>
{slippageTitle}
<Icon name="SliderHorOutline" size="$6" color="$iconSubdued" />
</XStack>
) : (
<HeaderIconButton
icon="SliderHorOutline"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
} from '@onekeyhq/components';
import { useDebounce } from '@onekeyhq/kit/src/hooks/useDebounce';
import {
useSwapEnableRecipientAddressAtom,
useSwapFromTokenAmountAtom,
useSwapProviderSupportReceiveAddressAtom,
useSwapSelectFromTokenAtom,
Expand Down Expand Up @@ -58,12 +59,13 @@ const SwapQuoteResult = ({
const [swapTokenMetadata] = useSwapTokenMetadataAtom();
const [swapProviderSupportReceiveAddress] =
useSwapProviderSupportReceiveAddressAtom();
const [swapEnableRecipientAddress] = useSwapEnableRecipientAddressAtom();
const swapQuoteLoading = useSwapQuoteLoading();
const intl = useIntl();
const { onSlippageHandleClick, slippageItem } = useSwapSlippageActions();

const swapRecipientAddress = useSwapRecipientAddressInfo(
settingsPersistAtom.swapEnableRecipientAddress,
swapEnableRecipientAddress,
);

const calculateTaxItem = useCallback(
Expand Down Expand Up @@ -195,7 +197,7 @@ const SwapQuoteResult = ({
>
<Divider mt="$4" />
{swapProviderSupportReceiveAddress &&
settingsPersistAtom.swapEnableRecipientAddress ? (
swapEnableRecipientAddress ? (
<SwapCommonInfoItem
title={intl.formatMessage({
id: ETranslations.global_recipient,
Expand Down
Loading

0 comments on commit aaab291

Please sign in to comment.