Skip to content

Commit

Permalink
refactor: Refactor isTestNet to accept chain ID
Browse files Browse the repository at this point in the history
The `isTestNet` utility function now accepts a chain ID rather than a
network ID. The chain ID is easier for us to access because we know the
chain ID for all networks statically, as it's a required network
configuration property. The network ID on the other hand is looked up
at runtime, and is sometimes not present.

In practice we had already been passing in chain ID as network ID in
many places This hasn't caused issues because they are equivalent for
most (though not all) networks.

This relates to MetaMask/mobile-planning#1226
  • Loading branch information
Gudahtt committed Sep 6, 2023
1 parent 3ae4a10 commit a805805
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 29 deletions.
8 changes: 2 additions & 6 deletions app/components/UI/ApproveTransactionReview/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,6 @@ class ApproveTransactionReview extends PureComponent {
* Object that represents the navigator
*/
navigation: PropTypes.object,
/**
* Network id
*/
networkId: PropTypes.string,
/**
* True if transaction is over the available funds
*/
Expand Down Expand Up @@ -699,7 +695,7 @@ class ApproveTransactionReview extends PureComponent {
gasError,
activeTabUrl,
transaction: { origin, from, to },
networkId,
chainId,
over,
gasEstimateType,
onUpdatingValuesStart,
Expand All @@ -721,7 +717,7 @@ class ApproveTransactionReview extends PureComponent {
isGasEstimateStatusIn,
} = this.props;
const styles = this.getStyles();
const isTestNetwork = isTestNet(networkId);
const isTestNetwork = isTestNet(chainId);

const originIsDeeplink =
origin === ORIGIN_DEEPLINK || origin === ORIGIN_QR_CODE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ import AppConstants from '../../../../core/AppConstants';
import WarningMessage from '../../../Views/SendFlow/WarningMessage';
import {
selectChainId,
selectNetwork,
selectTicker,
} from '../../../../selectors/networkController';
import {
Expand Down Expand Up @@ -181,9 +180,9 @@ class TransactionReviewInformation extends PureComponent {
*/
onCancelPress: PropTypes.func,
/**
* Network id
* The chain ID for the current selected network
*/
networkId: PropTypes.string,
chainId: PropTypes.string,
/**
* Indicates whether custom nonce should be shown in transaction editor
*/
Expand Down Expand Up @@ -357,8 +356,8 @@ class TransactionReviewInformation extends PureComponent {
};

isTestNetwork = () => {
const { networkId } = this.props;
return isTestNet(networkId);
const { chainId } = this.props;
return isTestNet(chainId);
};

getRenderTotalsEIP1559 = ({
Expand Down Expand Up @@ -721,7 +720,7 @@ class TransactionReviewInformation extends PureComponent {
}

const mapStateToProps = (state) => ({
networkId: selectNetwork(state),
networkId: selectChainId(state),
conversionRate: selectConversionRate(state),
currentCurrency: selectCurrentCurrency(state),
nativeCurrency: selectNativeCurrency(state),
Expand Down
9 changes: 1 addition & 8 deletions app/components/Views/SendFlow/Confirm/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ import {
} from '../../../../core/GasPolling/GasPolling';
import {
selectChainId,
selectNetwork,
selectProviderType,
selectTicker,
} from '../../../../selectors/networkController';
Expand Down Expand Up @@ -166,10 +165,6 @@ class Confirm extends PureComponent {
* Chain Id
*/
chainId: PropTypes.string,
/**
* Network id
*/
networkId: PropTypes.string,
/**
* Indicates whether hex data should be shown in transaction editor
*/
Expand Down Expand Up @@ -937,7 +932,6 @@ class Confirm extends PureComponent {
showHexData,
showCustomNonce,
primaryCurrency,
networkId,
chainId,
gasEstimateType,
isNativeTokenBuySupported,
Expand Down Expand Up @@ -968,7 +962,7 @@ class Confirm extends PureComponent {
gasEstimateType === GAS_ESTIMATE_TYPES.NONE;
const isQRHardwareWalletDevice = isQRHardwareAccount(fromSelectedAddress);

const isTestNetwork = isTestNet(networkId);
const isTestNetwork = isTestNet(chainId);

const errorPress = isTestNetwork ? this.goToFaucet : this.buyEth;
const errorLinkText = isTestNetwork
Expand Down Expand Up @@ -1144,7 +1138,6 @@ const mapStateToProps = (state) => ({
contractBalances: selectContractBalances(state),
conversionRate: selectConversionRate(state),
currentCurrency: selectCurrentCurrency(state),
networkId: selectNetwork(state),
providerType: selectProviderType(state),
showHexData: state.settings.showHexData,
showCustomNonce: state.settings.showCustomNonce,
Expand Down
29 changes: 20 additions & 9 deletions app/util/networks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ import {
LINEA_MAINNET,
} from '../../../app/constants/network';
import { NetworkSwitchErrorType } from '../../../app/constants/error';
import { query } from '@metamask/controller-utils';
import {
NetworksChainId,
NetworkType,
query,
} from '@metamask/controller-utils';
import Engine from '../../core/Engine';
import { toLowerCaseEquals } from '../general';
import { fastSplit } from '../number';
Expand Down Expand Up @@ -178,15 +182,22 @@ export const getTestNetImageByChainId = (chainId) => {
}
};

export const isTestNet = (networkId) => {
const networkName = getNetworkName(networkId);
/**
* A list of chain IDs for known testnets
*/
const TESTNET_CHAIN_IDS = [
NetworksChainId[NetworkType.goerli],
NetworksChainId[NetworkType.sepolia],
NetworksChainId[NetworkType['linea-goerli']],
];

return (
networkName === GOERLI ||
networkName === SEPOLIA ||
networkName === LINEA_GOERLI
);
};
/**
* Determine whether the given chain ID is for a known testnet.
*
* @param {string} chainId - The chain ID of the network to check
* @returns {boolean} `true` if the given chain ID is for a known testnet, `false` otherwise
*/
export const isTestNet = (chainId) => TESTNET_CHAIN_IDS.includes(chainId);

export function getNetworkTypeById(id) {
if (!id) {
Expand Down
20 changes: 20 additions & 0 deletions app/util/networks/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { NetworksChainId, NetworkType } from '@metamask/controller-utils';
import {
isMainNet,
isTestNet,
getNetworkName,
getAllNetworks,
getNetworkTypeById,
Expand Down Expand Up @@ -62,6 +64,24 @@ describe('NetworkUtils::isMainNet', () => {
});
});

describe('NetworkUtils::isTestNet', () => {
const testnets = [
NetworkType.goerli,
NetworkType.sepolia,
NetworkType['linea-goerli'],
];

for (const networkType of testnets) {
it(`should return true if the given chain ID is for '${networkType}'`, () => {
expect(isTestNet(NetworksChainId[networkType])).toEqual(true);
});
}

it(`should return false if the given chain ID is not a known testnet`, () => {
expect(isTestNet('42')).toEqual(false);
});
});

describe('NetworkUtils::getNetworkName', () => {
it(`should get network name for ${MAINNET} id`, () => {
const main = getNetworkName(String(1));
Expand Down

0 comments on commit a805805

Please sign in to comment.