From 5cd04909609121543351d540c175771e77a79185 Mon Sep 17 00:00:00 2001 From: AnhMTV Date: Fri, 31 May 2024 14:55:26 +0700 Subject: [PATCH] [Issue-102] Re-enable evm token in getAddress list --- .../src/hooks/screen/home/useReceiveQR.ts | 124 +++++++++--------- 1 file changed, 64 insertions(+), 60 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 7db1d32ab4..b0248ed9c7 100644 --- a/packages/extension-koni-ui/src/hooks/screen/home/useReceiveQR.ts +++ b/packages/extension-koni-ui/src/hooks/screen/home/useReceiveQR.ts @@ -5,9 +5,8 @@ import type { KeypairType } from '@subwallet/keyring/types'; import { _ChainAsset } from '@subwallet/chain-list/types'; import { AccountJson, AccountProxy } from '@subwallet/extension-base/background/types'; -import { _getMultiChainAsset, _isAssetFungibleToken, _isNativeToken } from '@subwallet/extension-base/services/chain-service/utils'; +import { _getMultiChainAsset, _isAssetFungibleToken, _isChainEvmCompatible } from '@subwallet/extension-base/services/chain-service/utils'; import { AccountSelectorModalId } from '@subwallet/extension-koni-ui/components/Modal/AccountSelectorModal'; -import { SUPPORT_CHAINS } from '@subwallet/extension-koni-ui/constants'; import { RECEIVE_QR_MODAL, RECEIVE_TOKEN_SELECTOR_MODAL } from '@subwallet/extension-koni-ui/constants/modal'; import { useChainAssets } from '@subwallet/extension-koni-ui/hooks/assets'; import { RootState } from '@subwallet/extension-koni-ui/stores'; @@ -24,69 +23,13 @@ type ReceiveSelectedResult = { selectedNetwork?: string; }; -function getTokenSelectorItem (asset: _ChainAsset, accountProxy: AccountProxy): ReceiveTokenItemType | null { - if (!_isAssetFungibleToken(asset) || !SUPPORT_CHAINS.includes(asset.originChain)) { - return null; - } - - let targetAccount: AccountJson | undefined; - - for (const account of accountProxy.accounts) { - const accountType = getKeypairTypeByAddress(account.address); - - if ((accountType === 'ethereum' && asset.originChain === 'ethereum' && _isNativeToken(asset)) || - (accountType === 'bitcoin-84' && asset.originChain === 'bitcoin') || - (accountType === 'bitcoin-86' && asset.originChain === 'bitcoin' && asset.metadata?.runeId) || - (accountType === 'bittest-84' && asset.originChain === 'bitcoinTestnet')) { - targetAccount = { - ...account, - type: accountType - }; - - break; - } - } - - if (!targetAccount) { - return null; - } - - const isRune = !!asset.metadata?.runeId; - const order = (() => { - if (isRune) { - return 2; - } - - if (asset.originChain === 'bitcoin') { - return 1; - } - - if (asset.originChain === 'bitcoinTestnet') { - return 3; - } - - if (asset.originChain === 'ethereum') { - return 4; - } - - return 99; - })(); - - return { - ...asset, - address: targetAccount.address, - addressType: targetAccount.type as KeypairType, - isRune, - order - }; -} - export default function useReceiveQR (tokenGroupSlug?: string) { const { activeModal, inactiveModal } = useContext(ModalContext); const isAllAccount = useSelector((state: RootState) => state.accountState.isAllAccount); const accountProxies = useSelector((state: RootState) => state.accountState.accountProxies); const currentAccountProxy = useSelector((state: RootState) => state.accountState.currentAccountProxy); const assetRegistryMap = useChainAssets().getChainAssetRegistry(); + const chainInfoMap = useSelector((state: RootState) => state.chainStore.chainInfoMap); const [tokenSelectorItems, setTokenSelectorItems] = useState([]); const [{ selectedAccountProxyAddress, selectedAccountProxyId, selectedNetwork }, setReceiveSelectedResult] = useState( { selectedAccountProxyId: isAllAccount ? undefined : currentAccountProxy?.proxyId } @@ -100,6 +43,67 @@ export default function useReceiveQR (tokenGroupSlug?: string) { return accountProxies.filter((ap) => !checkIsAccountAll(ap.proxyId)); }, [isAllAccount, accountProxies]); + const evmChains = useMemo(() => { + return Object.values(chainInfoMap).filter((chain) => _isChainEvmCompatible(chain)).map((chain) => chain.slug); + }, [chainInfoMap]); + + const getTokenSelectorItem = useCallback((asset: _ChainAsset, accountProxy: AccountProxy) => { + if (!_isAssetFungibleToken(asset)) { + return null; + } + + let targetAccount: AccountJson | undefined; + + for (const account of accountProxy.accounts) { + const accountType = getKeypairTypeByAddress(account.address); + + if ((accountType === 'ethereum' && evmChains.includes(asset.originChain)) || + (accountType === 'bitcoin-84' && asset.originChain === 'bitcoin') || + (accountType === 'bitcoin-86' && asset.originChain === 'bitcoin' && asset.metadata?.runeId) || + (accountType === 'bittest-84' && asset.originChain === 'bitcoinTestnet')) { + targetAccount = { + ...account, + type: accountType + }; + + break; + } + } + + if (!targetAccount) { + return null; + } + + const isRune = !!asset.metadata?.runeId; + const order = (() => { + if (isRune) { + return 2; + } + + if (asset.originChain === 'bitcoin') { + return 1; + } + + if (asset.originChain === 'bitcoinTestnet') { + return 3; + } + + if (asset.originChain === 'ethereum') { + return 4; + } + + return 99; + })(); + + return { + ...asset, + address: targetAccount.address, + addressType: targetAccount.type as KeypairType, + isRune, + order + }; + }, [evmChains]); + const getTokenSelectorItems = useCallback((accountProxy: AccountProxy) => { // if tokenGroupSlug is token slug if (tokenGroupSlug && assetRegistryMap[tokenGroupSlug]) { @@ -139,7 +143,7 @@ export default function useReceiveQR (tokenGroupSlug?: string) { result.sort((a, b) => a.order - b.order); return result; - }, [tokenGroupSlug, assetRegistryMap]); + }, [tokenGroupSlug, assetRegistryMap, getTokenSelectorItem]); const onOpenReceive = useCallback(() => { if (!currentAccountProxy) {