From 6ac549b05ffbac382d9ed676acbcf4d0bce01df4 Mon Sep 17 00:00:00 2001 From: S2kael Date: Wed, 25 Oct 2023 14:55:33 +0700 Subject: [PATCH] [Issue-2075] Filter list account when receive --- .../src/hooks/screen/home/useReceiveQR.ts | 96 +++++++++++-------- 1 file changed, 57 insertions(+), 39 deletions(-) diff --git a/packages/extension-koni-ui/src/hooks/screen/home/useReceiveQR.ts b/packages/extension-koni-ui/src/hooks/screen/home/useReceiveQR.ts index 7f662f6ee9..fd2795f56a 100644 --- a/packages/extension-koni-ui/src/hooks/screen/home/useReceiveQR.ts +++ b/packages/extension-koni-ui/src/hooks/screen/home/useReceiveQR.ts @@ -1,7 +1,7 @@ // Copyright 2019-2022 @subwallet/extension-koni-ui authors & contributors // SPDX-License-Identifier: Apache-2.0 -import { _ChainAsset, _ChainInfo } from '@subwallet/chain-list/types'; +import { _ChainAsset } from '@subwallet/chain-list/types'; import { MantaPayConfig } from '@subwallet/extension-base/background/KoniTypes'; import { AccountJson } from '@subwallet/extension-base/background/types'; import { _MANTA_ZK_CHAIN_GROUP, _ZK_ASSET_PREFIX } from '@subwallet/extension-base/services/chain-service/constants'; @@ -9,8 +9,7 @@ import { _getMultiChainAsset, _isAssetFungibleToken, _isChainEvmCompatible } fro import { AccountSelectorModalId } from '@subwallet/extension-koni-ui/components/Modal/AccountSelectorModal'; import { RECEIVE_QR_MODAL, RECEIVE_TOKEN_SELECTOR_MODAL } from '@subwallet/extension-koni-ui/constants/modal'; import { RootState } from '@subwallet/extension-koni-ui/stores'; -import { AccountType } from '@subwallet/extension-koni-ui/types'; -import { findAccountByAddress, getAccountType, isAccountAll as checkIsAccountAll } from '@subwallet/extension-koni-ui/utils'; +import { findAccountByAddress, isAccountAll as checkIsAccountAll } from '@subwallet/extension-koni-ui/utils'; import { findNetworkJsonByGenesisHash } from '@subwallet/extension-koni-ui/utils/chain/getNetworkJsonByGenesisHash'; import { ModalContext } from '@subwallet/react-ui'; import { useCallback, useContext, useEffect, useMemo, useState } from 'react'; @@ -23,51 +22,34 @@ type ReceiveSelectedResult = { selectedNetwork?: string; }; -function getAccountTypeByTokenGroup ( +// Return array of chain which have token in multi chain asset +const getChainsByTokenGroup = ( tokenGroupSlug: string, - assetRegistryMap: Record, - chainInfoMap: Record): AccountType { + assetRegistryMap: Record +): string[] => { // case tokenGroupSlug is token slug if (assetRegistryMap[tokenGroupSlug]) { - const chainSlug = assetRegistryMap[tokenGroupSlug].originChain; - - if (_isChainEvmCompatible(chainInfoMap[chainSlug])) { - return 'ETHEREUM'; - } else { - return 'SUBSTRATE'; - } + return [assetRegistryMap[tokenGroupSlug].originChain]; } // case tokenGroupSlug is multiChainAsset slug - const assetRegistryItems: _ChainAsset[] = Object.values(assetRegistryMap); + const assetRegistryItems: _ChainAsset[] = Object.values(assetRegistryMap) + .filter((assetItem) => { + return _isAssetFungibleToken(assetItem) && + _getMultiChainAsset(assetItem) === tokenGroupSlug; + }); - const typesCheck: AccountType[] = []; + const map: Record = {}; for (const assetItem of assetRegistryItems) { - if (!_isAssetFungibleToken(assetItem) || (_getMultiChainAsset(assetItem) !== tokenGroupSlug)) { - continue; - } - const chainSlug = assetRegistryMap[assetItem.slug].originChain; - const currentType = _isChainEvmCompatible(chainInfoMap[chainSlug]) ? 'ETHEREUM' : 'SUBSTRATE'; - - if (!typesCheck.includes(currentType)) { - typesCheck.push(currentType); - } - - if (typesCheck.length === 2) { - break; - } + map[chainSlug] = true; } - if (!typesCheck.length || typesCheck.length === 2) { - return 'ALL'; - } - - return typesCheck[0]; -} + return Object.keys(map); +}; function isMantaPayEnabled (account: AccountJson | null, configs: MantaPayConfig[]) { for (const config of configs) { @@ -96,13 +78,49 @@ export default function useReceiveQR (tokenGroupSlug?: string) { } if (tokenGroupSlug) { - const targetAccountType = getAccountTypeByTokenGroup(tokenGroupSlug, assetRegistryMap, chainInfoMap); + const chains = getChainsByTokenGroup(tokenGroupSlug, assetRegistryMap); - if (targetAccountType === 'ALL') { - return accounts.filter((a) => !checkIsAccountAll(a.address)); - } + return accounts.filter((account) => { + const isEvm = isEthereumAddress(account.address); + const isAll = checkIsAccountAll(account.address); + + if (isAll) { + return false; + } - return accounts.filter((a) => getAccountType(a.address) === targetAccountType); + if (account.isHardware) { + if (!isEvm) { + const availableGen: string[] = account.availableGenesisHashes || []; + const networks = availableGen + .map((gen) => findNetworkJsonByGenesisHash(chainInfoMap, gen)?.slug) + .filter((slug) => slug) as string[]; + + return networks.some((n) => chains.includes(n)); + } else { + return chains.some((chain) => { + const info = chainInfoMap[chain]; + + if (info) { + return _isChainEvmCompatible(info); + } else { + return false; + } + }); + } + } else { + for (const chain of chains) { + const info = chainInfoMap[chain]; + + if (info) { + if (isEvm === _isChainEvmCompatible(info)) { + return true; + } + } + } + + return false; + } + }); } return accounts.filter((a) => !checkIsAccountAll(a.address));