Skip to content

Commit

Permalink
Merge branch 'x' into ext-auth
Browse files Browse the repository at this point in the history
  • Loading branch information
huhuanming authored Oct 9, 2024
2 parents c8ba027 + e99c2ec commit 546a714
Show file tree
Hide file tree
Showing 14 changed files with 814 additions and 349 deletions.
2 changes: 2 additions & 0 deletions packages/kit-bg/src/services/ServiceSwap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ export default class ServiceSwap extends ServiceBase {
logoURI: clientNetwork.logoURI,
networkId: network.networkId,
defaultSelectToken: network.defaultSelectToken,
supportCrossChainSwap: network.supportCrossChainSwap,
supportSingleSwap: network.supportSingleSwap,
};
}
return null;
Expand Down
160 changes: 159 additions & 1 deletion packages/kit/src/states/jotai/contexts/swap/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { numberFormat } from '@onekeyhq/shared/src/utils/numberUtils';
import { equalTokenNoCaseSensitive } from '@onekeyhq/shared/src/utils/tokenUtils';
import {
swapApprovingStateFetchInterval,
swapDefaultSetTokens,
swapHistoryStateFetchRiceIntervalCount,
swapQuoteFetchInterval,
swapQuoteIntervalMaxCount,
Expand All @@ -25,8 +26,10 @@ import {
import type {
IFetchQuotesParams,
IFetchTokensParams,
ISwapAlertActionData,
ISwapAlertState,
ISwapApproveTransaction,
ISwapNetwork,
ISwapQuoteEvent,
ISwapQuoteEventAutoSlippage,
ISwapQuoteEventData,
Expand All @@ -35,12 +38,14 @@ import type {
ISwapToken,
} from '@onekeyhq/shared/types/swap/types';
import {
ESwapAlertActionType,
ESwapAlertLevel,
ESwapApproveTransactionStatus,
ESwapDirectionType,
ESwapFetchCancelCause,
ESwapRateDifferenceUnit,
ESwapSlippageSegmentKey,
ESwapTabSwitchType,
ESwapTxHistoryStatus,
} from '@onekeyhq/shared/types/swap/types';

Expand All @@ -57,6 +62,7 @@ import {
swapFromTokenAmountAtom,
swapManualSelectQuoteProvidersAtom,
swapNetworks,
swapNetworksIncludeAllNetworkAtom,
swapQuoteActionLockAtom,
swapQuoteCurrentSelectAtom,
swapQuoteEventTotalCountAtom,
Expand All @@ -75,6 +81,7 @@ import {
swapTokenFetchingAtom,
swapTokenMapAtom,
swapTokenMetadataAtom,
swapTypeSwitchAtom,
} from './atoms';

class ContentJotaiActionsSwap extends ContextJotaiActionsBase {
Expand Down Expand Up @@ -156,13 +163,28 @@ class ContentJotaiActionsSwap extends ContextJotaiActionsBase {

selectFromToken = contextAtomMethod(async (get, set, token: ISwapToken) => {
const fromToken = get(swapSelectFromTokenAtom());
const toToken = get(swapSelectToTokenAtom());
const swapTypeSwitchValue = get(swapTypeSwitchAtom());
if (
fromToken?.networkId !== token.networkId ||
fromToken?.contractAddress !== token.contractAddress
) {
this.cleanManualSelectQuoteProviders.call(set);
this.resetSwapSlippage.call(set);
await this.syncNetworksSort.call(set, token.networkId);
if (
token.networkId !== toToken?.networkId &&
swapTypeSwitchValue === ESwapTabSwitchType.SWAP
) {
if (token.isNative) {
set(swapSelectToTokenAtom(), undefined);
} else {
const defaultTokenSet = swapDefaultSetTokens[token.networkId];
if (defaultTokenSet.fromToken) {
set(swapSelectToTokenAtom(), defaultTokenSet.fromToken);
}
}
}
}
set(swapSelectFromTokenAtom(), token);
});
Expand Down Expand Up @@ -690,15 +712,54 @@ class ContentJotaiActionsSwap extends ContextJotaiActionsBase {
}
};

checkAddressNeedCreate = (
swapSupportAllNetworks: ISwapNetwork[],
fromToken: ISwapToken,
addressInfo: ReturnType<typeof useSwapAddressInfo>,
) => {
const netInfo = swapSupportAllNetworks.find(
(net) => net.networkId === fromToken.networkId,
);
const networkId = addressInfo.accountInfo?.network?.id;
const walletId = addressInfo.accountInfo?.wallet?.id;
const indexedAccountId = addressInfo.accountInfo?.indexedAccount?.id;
const deriveType = addressInfo.accountInfo?.deriveType;
const account = {
walletId,
indexedAccountId,
deriveType,
networkId,
};
const key =
networkId && walletId && (deriveType || indexedAccountId)
? [networkId, deriveType, walletId, indexedAccountId].join('-')
: Math.random().toString();
return {
message: `No ${netInfo?.name ?? ''} address`,
alertLevel: ESwapAlertLevel.INFO,
action: {
actionType: ESwapAlertActionType.CREATE_ADDRESS,
actionLabel: 'Create',
actionData: {
num: 0,
key,
account,
} as ISwapAlertActionData,
},
};
};

checkSwapWarning = contextAtomMethod(
async (
get,
set,
swapFromAddressInfo: ReturnType<typeof useSwapAddressInfo>,
swapToAddressInfo: ReturnType<typeof useSwapAddressInfo>,
) => {
const fromToken = get(swapSelectFromTokenAtom());
const toToken = get(swapSelectToTokenAtom());
const networks = get(swapNetworks());
const swapSupportAllNetworks = get(swapNetworksIncludeAllNetworkAtom());
const quoteResult = get(swapQuoteCurrentSelectAtom());
const tokenMetadata = get(swapTokenMetadataAtom());
const quoteResultList = get(swapQuoteListAtom());
Expand Down Expand Up @@ -774,8 +835,51 @@ class ContentJotaiActionsSwap extends ContextJotaiActionsBase {
];
}

// provider best check
// check from address
if (
fromToken &&
!swapFromAddressInfo.address &&
(accountUtils.isHdWallet({
walletId: swapFromAddressInfo.accountInfo?.wallet?.id,
}) ||
accountUtils.isHwWallet({
walletId: swapFromAddressInfo.accountInfo?.wallet?.id,
}) ||
accountUtils.isQrWallet({
walletId: swapFromAddressInfo.accountInfo?.wallet?.id,
}))
) {
const alertAction = this.checkAddressNeedCreate(
swapSupportAllNetworks,
fromToken,
swapFromAddressInfo,
);
alertsRes = [...alertsRes, alertAction];
}
// check to address
if (
toToken &&
!swapToAddressInfo.address &&
(accountUtils.isHdWallet({
walletId: swapToAddressInfo.accountInfo?.wallet?.id,
}) ||
accountUtils.isHwWallet({
walletId: swapToAddressInfo.accountInfo?.wallet?.id,
}) ||
accountUtils.isQrWallet({
walletId: swapToAddressInfo.accountInfo?.wallet?.id,
}))
) {
const alertAction = this.checkAddressNeedCreate(
swapSupportAllNetworks,
toToken,
swapToAddressInfo,
);
alertsRes = [...alertsRes, alertAction];
}

if (quoteResult?.toAmount && !quoteResult.isBest) {
// provider best check
alertsRes = [
...alertsRes,
{
Expand Down Expand Up @@ -825,6 +929,9 @@ class ContentJotaiActionsSwap extends ContextJotaiActionsBase {
{ number: '100%' },
),
alertLevel: ESwapAlertLevel.WARNING,
action: {
actionType: ESwapAlertActionType.TOKEN_DETAIL_FETCHING,
},
},
];
} else if (difference.lt(swapRateDifferenceMax)) {
Expand All @@ -842,6 +949,9 @@ class ContentJotaiActionsSwap extends ContextJotaiActionsBase {
},
),
alertLevel: ESwapAlertLevel.WARNING,
action: {
actionType: ESwapAlertActionType.TOKEN_DETAIL_FETCHING,
},
},
];
}
Expand Down Expand Up @@ -911,6 +1021,9 @@ class ContentJotaiActionsSwap extends ContextJotaiActionsBase {
id: ETranslations.swap_page_alert_fee_exceeds_amount,
}),
alertLevel: ESwapAlertLevel.WARNING,
action: {
actionType: ESwapAlertActionType.TOKEN_DETAIL_FETCHING,
},
},
];
}
Expand Down Expand Up @@ -1218,6 +1331,49 @@ class ContentJotaiActionsSwap extends ContextJotaiActionsBase {
}
},
);

swapTypeSwitchAction = contextAtomMethod(
async (
get,
set,
type: ESwapTabSwitchType,
swapAccountNetworkId?: string,
) => {
const swapSupportNetworks = get(swapNetworksIncludeAllNetworkAtom());
const fromToken = get(swapSelectFromTokenAtom());
const toToken = get(swapSelectToTokenAtom());
if (
fromToken &&
!swapSupportNetworks.find(
(net) => net.networkId === fromToken?.networkId,
)
) {
set(swapSelectFromTokenAtom(), undefined);
}
if (
toToken &&
!swapSupportNetworks.find((net) => net.networkId === toToken?.networkId)
) {
set(swapSelectToTokenAtom(), undefined);
}
if (type === ESwapTabSwitchType.BRIDGE) {
if (toToken && fromToken && toToken.networkId === fromToken.networkId) {
set(swapSelectToTokenAtom(), undefined);
}
} else if (type === ESwapTabSwitchType.SWAP) {
const fromNetworkDefault =
swapDefaultSetTokens[swapAccountNetworkId ?? ''];
if (fromToken) {
if (!fromToken.isNative && fromNetworkDefault.fromToken) {
set(swapSelectToTokenAtom(), fromNetworkDefault.fromToken);
}
} else if (fromNetworkDefault.fromToken) {
set(swapSelectFromTokenAtom(), fromNetworkDefault.fromToken);
}
}
set(swapTypeSwitchAtom(), type);
},
);
}

const createActions = memoFn(() => new ContentJotaiActionsSwap());
Expand Down Expand Up @@ -1245,6 +1401,7 @@ export const useSwapActions = () => {
},
);
const swapLoadAllNetworkTokenList = actions.swapLoadAllNetworkTokenList.use();
const swapTypeSwitchAction = actions.swapTypeSwitchAction.use();
const { cleanQuoteInterval, cleanApprovingInterval, closeQuoteEvent } =
actions;

Expand All @@ -1265,5 +1422,6 @@ export const useSwapActions = () => {
quoteEventHandler,
swapLoadAllNetworkTokenList,
closeQuoteEvent,
swapTypeSwitchAction,
});
};
21 changes: 16 additions & 5 deletions packages/kit/src/states/jotai/contexts/swap/atoms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ import {
ESwapProviderSort,
swapSlippageAutoValue,
} from '@onekeyhq/shared/types/swap/SwapProvider.constants';
import {
ESwapReceiveAddressType,
ESwapSlippageSegmentKey,
} from '@onekeyhq/shared/types/swap/types';
import type {
ESwapDirectionType,
ESwapRateDifferenceUnit,
Expand All @@ -23,6 +19,11 @@ import type {
ISwapTokenCatch,
ISwapTokenMetadata,
} from '@onekeyhq/shared/types/swap/types';
import {
ESwapReceiveAddressType,
ESwapSlippageSegmentKey,
ESwapTabSwitchType,
} from '@onekeyhq/shared/types/swap/types';

import { createJotaiContext } from '../../utils/createJotaiContext';

Expand All @@ -36,6 +37,10 @@ const {
} = createJotaiContext();
export { ProviderJotaiContextSwap, contextAtomMethod };

// swap bridge limit switch
export const { atom: swapTypeSwitchAtom, use: useSwapTypeSwitchAtom } =
contextAtom<ESwapTabSwitchType>(ESwapTabSwitchType.SWAP);

// swap networks & tokens
export const { atom: swapNetworks, use: useSwapNetworksAtom } = contextAtom<
ISwapNetwork[]
Expand All @@ -45,7 +50,13 @@ export const {
atom: swapNetworksIncludeAllNetworkAtom,
use: useSwapNetworksIncludeAllNetworkAtom,
} = contextAtomComputed<ISwapNetwork[]>((get) => {
const networks = get(swapNetworks());
let networks = get(swapNetworks());
const swapType = get(swapTypeSwitchAtom());
networks = networks.filter((net) =>
swapType === ESwapTabSwitchType.BRIDGE
? net.supportCrossChainSwap
: net.supportSingleSwap,
);
const allNetwork = {
networkId: getNetworkIdsMap().onekeyall,
name: dangerAllNetworkRepresent.name,
Expand Down
19 changes: 14 additions & 5 deletions packages/kit/src/views/Swap/components/SwapNetworkToggleGroup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import { NetworksFilterItem } from '../../../components/NetworksFilterItem';

interface ISwapNetworkToggleGroupProps {
networks: ISwapNetwork[];
disableOtherNet?: boolean;
moreNetworksCount?: number;
isOnlySupportSingleNetWork?: () => boolean;
onSelectNetwork: (network: ISwapNetwork) => void;
selectedNetwork?: ISwapNetwork;
onMoreNetwork: () => void;
Expand All @@ -22,6 +22,7 @@ const SwapNetworkToggleGroup = ({
networks,
selectedNetwork,
onSelectNetwork,
disableOtherNet,
moreNetworksCount,
onMoreNetwork,
}: ISwapNetworkToggleGroupProps) => {
Expand All @@ -37,23 +38,31 @@ const SwapNetworkToggleGroup = ({
{filteredNetworks.map((network) => (
<NetworksFilterItem
key={network.networkId}
disabled={Boolean(
disableOtherNet && network.networkId !== selectedNetwork?.networkId,
)}
networkImageUri={network.logoURI}
tooltipContent={
network.isAllNetworks
? intl.formatMessage({ id: ETranslations.global_all_networks })
: network.name
}
isSelected={network?.networkId === selectedNetwork?.networkId}
onPress={() => {
onSelectNetwork(network);
}}
onPress={
disableOtherNet && network.networkId !== selectedNetwork?.networkId
? undefined
: () => {
onSelectNetwork(network);
}
}
/>
))}
{moreNetworksCount && moreNetworksCount > 0 ? (
<NetworksFilterItem
disabled={disableOtherNet}
networkName={`${moreNetworksCount}+`}
flex={1}
onPress={onMoreNetwork}
onPress={disableOtherNet ? undefined : onMoreNetwork}
/>
) : null}
</XStack>
Expand Down
Loading

0 comments on commit 546a714

Please sign in to comment.