From 5de2584187a883ae1cc31bc4e4c93a78256ca5ab Mon Sep 17 00:00:00 2001 From: Owen Craston Date: Thu, 16 Jan 2025 18:47:55 -0800 Subject: [PATCH] simplify selector --- app/core/Multichain/constants.ts | 49 +++++++++++++++++++++++++++++ app/selectors/multichain.ts | 53 +++++++++++++------------------- 2 files changed, 71 insertions(+), 31 deletions(-) diff --git a/app/core/Multichain/constants.ts b/app/core/Multichain/constants.ts index 1b00bd3f4d8e..dc808763016d 100644 --- a/app/core/Multichain/constants.ts +++ b/app/core/Multichain/constants.ts @@ -1,4 +1,5 @@ import { CaipChainId } from '@metamask/utils'; +import { isBtcMainnetAddress, isBtcTestnetAddress } from './utils'; export interface ProviderConfigWithImageUrl { rpcUrl?: string; @@ -21,3 +22,51 @@ export enum MultichainNetworks { BITCOIN = 'bip122:000000000019d6689c085ae165831e93', BITCOIN_TESTNET = 'bip122:000000000933ea01ad0ee984209779ba', } + +export const BITCOIN_TOKEN_IMAGE_URL = './app/images/bitcoin-logo.png'; + +export const MULTICHAIN_TOKEN_IMAGE_MAP = { + [MultichainNetworks.BITCOIN]: BITCOIN_TOKEN_IMAGE_URL, +} as const; + +export const MULTICHAIN_NETWORK_BLOCK_EXPLORER_URL_MAP = { + [MultichainNetworks.BITCOIN]: 'https://blockstream.info/address', + [MultichainNetworks.BITCOIN_TESTNET]: + 'https://blockstream.info/testnet/address', +} as const; + +export const MULTICHAIN_PROVIDER_CONFIGS: Record< + CaipChainId, + MultichainProviderConfig +> = { + [MultichainNetworks.BITCOIN]: { + chainId: MultichainNetworks.BITCOIN, + rpcUrl: '', // not used + ticker: 'BTC', + nickname: 'Bitcoin', + id: 'btc-mainnet', + type: 'rpc', + rpcPrefs: { + imageUrl: MULTICHAIN_TOKEN_IMAGE_MAP[MultichainNetworks.BITCOIN], + blockExplorerUrl: + MULTICHAIN_NETWORK_BLOCK_EXPLORER_URL_MAP[MultichainNetworks.BITCOIN], + }, + isAddressCompatible: isBtcMainnetAddress, + }, + [MultichainNetworks.BITCOIN_TESTNET]: { + chainId: MultichainNetworks.BITCOIN_TESTNET, + rpcUrl: '', // not used + ticker: 'BTC', + nickname: 'Bitcoin (testnet)', + id: 'btc-testnet', + type: 'rpc', + rpcPrefs: { + imageUrl: MULTICHAIN_TOKEN_IMAGE_MAP[MultichainNetworks.BITCOIN], + blockExplorerUrl: + MULTICHAIN_NETWORK_BLOCK_EXPLORER_URL_MAP[ + MultichainNetworks.BITCOIN_TESTNET + ], + }, + isAddressCompatible: isBtcTestnetAddress, + }, +}; diff --git a/app/selectors/multichain.ts b/app/selectors/multichain.ts index e721d5944cc0..d927c9a2ac62 100644 --- a/app/selectors/multichain.ts +++ b/app/selectors/multichain.ts @@ -39,7 +39,10 @@ import { isMainNet } from '../util/networks'; import { isBtcMainnetAddress } from '../core/Multichain/utils'; import { isEvmAccountType } from '@metamask/keyring-api'; import { createDeepEqualSelector } from './util'; -import { MultichainProviderConfig } from '../core/Multichain/constants'; +import { + MULTICHAIN_PROVIDER_CONFIGS, + MultichainProviderConfig, +} from '../core/Multichain/constants'; import { NetworkClientConfiguration, NetworkClientType, @@ -302,6 +305,10 @@ export type MultichainNetwork = { network: NetworkClientConfiguration | MultichainProviderConfig; }; +function getMultichainNetworkProviders(): MultichainProviderConfig[] { + return Object.values(MULTICHAIN_PROVIDER_CONFIGS); +} + export const selectMultichainNetwork = createSelector( [ selectMultichainIsEvm, @@ -368,40 +375,24 @@ export const selectMultichainNetwork = createSelector( // Non-EVM networks: // For non-EVM, we know we have a selected account, since the logic `isEvm` is based // on having a non-EVM account being selected! - if (!selectedAccount) { + const nonEvmNetworks = getMultichainNetworkProviders(); + const nonEvmNetwork = nonEvmNetworks.find((provider) => { + return provider.isAddressCompatible(selectedAccount.address); + }); + + if (!nonEvmNetwork) { throw new Error( - 'Could not find selected account for non-EVM network. This should never happen.', + 'Could not find non-EVM provider for the current configuration. This should never happen.', ); } - // For Bitcoin, return hardcoded network info - if (selectedAccount.type === 'bip122:p2wpkh') { - const isBitcoinMainnet = isBtcMainnetAddress(selectedAccount.address); - const chainId = isBitcoinMainnet - ? 'bip122:000000000019d6689c085ae165831e93' - : 'bip122:000000000933ea01ad0ee984209779ba'; - - return { - nickname: isBitcoinMainnet ? 'Bitcoin' : 'Bitcoin Testnet', - isEvmNetwork: false, - chainId: chainId as CaipChainId, - network: { - type: isBitcoinMainnet ? 'bitcoin' : 'bitcoin-testnet', - chainId, - ticker: 'BTC', - nickname: isBitcoinMainnet ? 'Bitcoin' : 'Bitcoin Testnet', - isAddressCompatible: (address: string) => { - return isBitcoinMainnet - ? address.startsWith('bc1') // Mainnet addresses - : address.startsWith('tb1'); // Testnet addresses - }, - } as MultichainProviderConfig, - }; - } - - throw new Error( - 'Could not find non-EVM provider for the current configuration. This should never happen.', - ); + return { + // TODO: Adapt this for other non-EVM networks + nickname: nonEvmNetwork.nickname, + isEvmNetwork: false, + chainId: nonEvmNetwork?.chainId, + network: nonEvmNetwork, + }; }, );