Skip to content

Commit

Permalink
simplify selector
Browse files Browse the repository at this point in the history
  • Loading branch information
owencraston committed Jan 17, 2025
1 parent d44df67 commit 5de2584
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 31 deletions.
49 changes: 49 additions & 0 deletions app/core/Multichain/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { CaipChainId } from '@metamask/utils';
import { isBtcMainnetAddress, isBtcTestnetAddress } from './utils';

export interface ProviderConfigWithImageUrl {
rpcUrl?: string;
Expand All @@ -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,
},
};
53 changes: 22 additions & 31 deletions app/selectors/multichain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -302,6 +305,10 @@ export type MultichainNetwork = {
network: NetworkClientConfiguration | MultichainProviderConfig;
};

function getMultichainNetworkProviders(): MultichainProviderConfig[] {
return Object.values(MULTICHAIN_PROVIDER_CONFIGS);
}

export const selectMultichainNetwork = createSelector(
[
selectMultichainIsEvm,
Expand Down Expand Up @@ -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) => {

Check failure on line 379 in app/selectors/multichain.ts

View workflow job for this annotation

GitHub Actions / scripts (lint)

Unexpected block statement surrounding arrow body; move the returned value immediately after the `=>`
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,
};
},
);

Expand Down

0 comments on commit 5de2584

Please sign in to comment.