Skip to content

Commit

Permalink
fix: fix bugss (#4826)
Browse files Browse the repository at this point in the history
  • Loading branch information
ezailWang authored Jun 19, 2024
1 parent 22b4ecb commit ab43b42
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 43 deletions.
25 changes: 15 additions & 10 deletions packages/kit/src/states/jotai/contexts/swap/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
ESwapRateDifferenceUnit,
ESwapSlippageSegmentKey,
ESwapTxHistoryStatus,
ETokenRiskLevel,
} from '@onekeyhq/shared/types/swap/types';

import { ContextJotaiActionsBase } from '../../utils/ContextJotaiActionsBase';
Expand Down Expand Up @@ -92,15 +93,17 @@ class ContentJotaiActionsSwap extends ContextJotaiActionsBase {
const catchTokens = swapTokenMap.tokenCatch?.[key];
const dateNow = Date.now();
let catchCount = 0;
const newTokens = tokens.map((token) => {
const network = swapNetworksList.find(
(n) => n.networkId === token.networkId,
);
if (network) {
token.networkLogoURI = network.logoURI;
}
return token;
});
const newTokens = tokens
.filter((t) => t.riskLevel !== ETokenRiskLevel.SCAM)
.map((token) => {
const network = swapNetworksList.find(
(n) => n.networkId === token.networkId,
);
if (network) {
token.networkLogoURI = network.logoURI;
}
return token;
});
if (swapTokenMap.tokenCatch && catchTokens?.data) {
// have catch
if (JSON.stringify(catchTokens.data) !== JSON.stringify(newTokens)) {
Expand Down Expand Up @@ -326,7 +329,9 @@ class ContentJotaiActionsSwap extends ContextJotaiActionsBase {
status: ESwapApproveTransactionStatus.CANCEL,
};
});
// set(swapBuildTxFetchingAtom(), false);
if (txState.state !== ESwapTxHistoryStatus.SUCCESS) {
set(swapBuildTxFetchingAtom(), false);
}
}
} catch (e) {
console.error(e);
Expand Down
28 changes: 16 additions & 12 deletions packages/kit/src/views/Swap/hooks/useSwapBuiltTx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,12 @@ export function useSwapBuildTx() {
]);

const approveTx = useCallback(
async (amount: string, isMax?: boolean, onApproveSuccess?: () => void) => {
async (
amount: string,
isMax?: boolean,
onApproveSuccess?: () => void,
isResetApprove?: boolean,
) => {
const allowanceInfo = selectQuote?.allowanceResult;
if (
allowanceInfo &&
Expand All @@ -180,17 +185,16 @@ export function useSwapBuildTx() {
name: fromToken.name ?? fromToken.symbol,
},
};
if (!onApproveSuccess) {
setSwapApprovingTransaction({
provider: selectQuote.info.provider,
fromToken,
toToken,
amount,
useAddress: swapFromAddressInfo.address,
spenderAddress: allowanceInfo.allowanceTarget,
status: ESwapApproveTransactionStatus.PENDING,
});
}
setSwapApprovingTransaction({
provider: selectQuote.info.provider,
fromToken,
toToken,
amount,
useAddress: swapFromAddressInfo.address,
spenderAddress: allowanceInfo.allowanceTarget,
status: ESwapApproveTransactionStatus.PENDING,
isResetApprove,
});
await navigationToSendConfirm({
approveInfo,
onSuccess: onApproveSuccess || handleApproveTxSuccess,
Expand Down
13 changes: 11 additions & 2 deletions packages/kit/src/views/Swap/hooks/useSwapQuote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,12 @@ export function useSwapQuote() {
ETabRoutes.Swap,
(isFocus: boolean, isHiddenModel: boolean) => {
if (pageType !== EPageType.modal) {
if (isFocus && !isHiddenModel && !swapApprovingTxRef.current?.txId) {
if (
isFocus &&
!isHiddenModel &&
!swapApprovingTxRef.current?.txId &&
!swapApprovingTxRef.current?.isResetApprove
) {
void recoverQuoteInterval(activeAccountAddressRef.current);
} else {
cleanQuoteInterval();
Expand All @@ -121,7 +126,11 @@ export function useSwapQuote() {
const isFocused = useIsFocused();
useEffect(() => {
if (pageType === EPageType.modal) {
if (isFocused && !swapApprovingTxRef.current?.txId) {
if (
isFocused &&
!swapApprovingTxRef.current?.txId &&
!swapApprovingTxRef.current?.isResetApprove
) {
void recoverQuoteInterval(activeAccountAddressRef.current);
} else {
cleanQuoteInterval();
Expand Down
10 changes: 7 additions & 3 deletions packages/kit/src/views/Swap/hooks/useSwapState.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useCallback, useEffect, useMemo, useRef } from 'react';

import { useIsFocused } from '@react-navigation/core';
import BigNumber from 'bignumber.js';
import { useIntl } from 'react-intl';

Expand Down Expand Up @@ -48,16 +49,18 @@ function useSwapWarningCheck() {
accountInfo: undefined,
},
});

const isFocused = useIsFocused();
const asyncRefContainer = useCallback(() => {
if (refContainer.current.swapFromAddressInfo !== swapFromAddressInfo) {
refContainer.current.swapFromAddressInfo = swapFromAddressInfo;
}
}, [swapFromAddressInfo]);

useEffect(() => {
asyncRefContainer();
void checkSwapWarning(refContainer.current.swapFromAddressInfo);
if (isFocused) {
asyncRefContainer();
void checkSwapWarning(refContainer.current.swapFromAddressInfo);
}
}, [
asyncRefContainer,
checkSwapWarning,
Expand All @@ -67,6 +70,7 @@ function useSwapWarningCheck() {
fromTokenBalance,
quoteCurrentSelect,
networks,
isFocused,
]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ const SwapAccountAddressContainer = ({
if (
((fromToken && type === ESwapDirectionType.FROM) ||
(toToken && type === ESwapDirectionType.TO)) &&
swapAddressInfo.address &&
swapAnotherAddressInfo.address &&
swapAddressInfo.address === swapAnotherAddressInfo.address
) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ const SwapActionsState = ({
const handleApprove = useCallback(() => {
if (swapActionState.shoutResetApprove) {
Dialog.confirm({
onConfirmText: 'Continue',
onConfirmText: intl.formatMessage({
id: ETranslations.global_continue,
}),
onConfirm: () => {
onApprove(fromAmount, swapActionState.approveUnLimit, true);
},
Expand All @@ -65,7 +67,7 @@ const SwapActionsState = ({
description: intl.formatMessage({
id: ETranslations.swap_page_provider_approve_usdt_dialog_content,
}),
icon: 'TxStatusWarningCircleIllus',
icon: 'ErrorOutline',
});
} else {
onApprove(fromAmount, swapActionState.approveUnLimit);
Expand Down
11 changes: 8 additions & 3 deletions packages/kit/src/views/Swap/pages/components/SwapMainLand.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,14 @@ const SwapMainLoad = ({ swapInitParams, pageType }: ISwapMainLoadProps) => {
const onApprove = useCallback(
async (amount: string, isMax?: boolean, shoutResetApprove?: boolean) => {
if (shoutResetApprove) {
await approveTx(swapApproveResetValue, false, async () => {
await onApprove(amount, isMax);
});
await approveTx(
swapApproveResetValue,
false,
async () => {
await approveTx(amount, isMax, undefined, true);
},
true,
);
} else {
await approveTx(amount, isMax);
}
Expand Down
48 changes: 37 additions & 11 deletions packages/kit/src/views/Swap/pages/modal/SwapTokenSelectModal.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useCallback, useMemo, useState } from 'react';
import { useCallback, useEffect, useMemo, useState } from 'react';

import { useRoute } from '@react-navigation/core';
import BigNumber from 'bignumber.js';
Expand Down Expand Up @@ -83,26 +83,34 @@ const SwapTokenSelectPage = () => {
const syncDefaultNetworkSelect = useCallback(() => {
if (type === ESwapDirectionType.FROM) {
if (fromToken?.networkId) {
return swapNetworks.find(
(item: ISwapNetwork) => item.networkId === fromToken.networkId,
return (
swapNetworks.find(
(item: ISwapNetwork) => item.networkId === fromToken.networkId,
) ?? swapNetworks?.[0]
);
}
if (swapFromAddressInfo.networkId) {
return swapNetworks.find(
(item: ISwapNetwork) =>
item.networkId === swapToAddressInfo.networkId,
return (
swapNetworks.find(
(item: ISwapNetwork) =>
item.networkId === swapToAddressInfo.networkId,
) ?? swapNetworks?.[0]
);
}
} else {
if (toToken?.networkId) {
return swapNetworks.find(
(item: ISwapNetwork) => item.networkId === toToken.networkId,
return (
swapNetworks.find(
(item: ISwapNetwork) => item.networkId === toToken.networkId,
) ?? swapNetworks?.[0]
);
}
if (swapToAddressInfo.networkId) {
return swapNetworks.find(
(item: ISwapNetwork) =>
item.networkId === swapToAddressInfo.networkId,
return (
swapNetworks.find(
(item: ISwapNetwork) =>
item.networkId === swapToAddressInfo.networkId,
) ?? swapNetworks?.[0]
);
}

Expand All @@ -119,6 +127,24 @@ const SwapTokenSelectPage = () => {
const [currentSelectNetwork, setCurrentSelectNetwork] = useState<
ISwapNetwork | undefined
>(syncDefaultNetworkSelect);

useEffect(() => {
const accountNet =
type === ESwapDirectionType.FROM
? swapFromAddressInfo.networkId
: swapToAddressInfo.networkId;
if (
currentSelectNetwork?.networkId &&
currentSelectNetwork?.networkId !== accountNet
) {
void updateSelectedAccountNetwork({
num: type === ESwapDirectionType.FROM ? 0 : 1,
networkId: currentSelectNetwork?.networkId,
});
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

const { fetchLoading, currentTokens } = useSwapTokenList(
type,
currentSelectNetwork?.networkId,
Expand Down
2 changes: 2 additions & 0 deletions packages/shared/types/swap/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export enum ETokenRiskLevel {
WARNING = 2,
SPAM = 1000,
MALICIOUS = 1001,
SCAM = 1002,
}

export interface ISwapInitParams {
Expand Down Expand Up @@ -136,6 +137,7 @@ export interface ISwapApproveTransaction {
spenderAddress: string;
amount: string;
status: ESwapApproveTransactionStatus;
isResetApprove?: boolean;
txId?: string;
blockNumber?: number;
}
Expand Down

0 comments on commit ab43b42

Please sign in to comment.