1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-22 01:47:00 +01:00

Cleanup PR: Renaming getNetworkName and moving that to utils file (#19160)

This commit is contained in:
Niranjana Binoy 2023-05-18 09:53:11 -04:00 committed by GitHub
parent 3fe5d3fc4c
commit 56440a84db
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 41 additions and 68 deletions

View File

@ -230,27 +230,6 @@ class NetworkDropdown extends Component {
);
}
getNetworkName() {
const { providerConfig } = this.props;
const providerName = providerConfig.type;
const { t } = this.context;
switch (providerName) {
case NETWORK_TYPES.MAINNET:
return t('mainnet');
case NETWORK_TYPES.GOERLI:
return t('goerli');
case NETWORK_TYPES.SEPOLIA:
return t('sepolia');
case NETWORK_TYPES.LINEA_TESTNET:
return t('lineatestnet');
case NETWORK_TYPES.LOCALHOST:
return t('localhost');
default:
return providerConfig.nickname || t('unknownNetwork');
}
}
renderNetworkEntry(network) {
const {
providerConfig: { type: providerType },

View File

@ -5,6 +5,7 @@ import { ObjectInspector } from 'react-inspector';
import LedgerInstructionField from '../ledger-instruction-field';
import { MESSAGE_TYPE } from '../../../../shared/constants/app';
import {
getNetworkNameFromProviderType,
getURLHostName,
sanitizeString,
///: BEGIN:ONLY_INCLUDE_IN(build-mmi)
@ -30,7 +31,6 @@ import {
BackgroundColor,
///: END:ONLY_INCLUDE_IN
} from '../../../helpers/constants/design-system';
import { NETWORK_TYPES } from '../../../../shared/constants/network';
import { Numeric } from '../../../../shared/modules/Numeric';
import { EtherDenomination } from '../../../../shared/constants/common';
import ConfirmPageContainerNavigation from '../confirm-page-container/confirm-page-container-navigation';
@ -81,27 +81,6 @@ export default class SignatureRequestOriginal extends Component {
showSignatureRequestWarning: false,
};
getNetworkName() {
const { providerConfig } = this.props;
const providerName = providerConfig.type;
const { t } = this.context;
switch (providerName) {
case NETWORK_TYPES.MAINNET:
return t('mainnet');
case NETWORK_TYPES.GOERLI:
return t('goerli');
case NETWORK_TYPES.SEPOLIA:
return t('sepolia');
case NETWORK_TYPES.LINEA_TESTNET:
return t('lineatestnet');
case NETWORK_TYPES.LOCALHOST:
return t('localhost');
default:
return providerConfig.nickname || t('unknownNetwork');
}
}
msgHexToText = (hex) => {
try {
const stripped = stripHexPrefix(hex);
@ -348,6 +327,7 @@ export default class SignatureRequestOriginal extends Component {
render = () => {
const {
providerConfig,
messagesCount,
nativeCurrency,
currentCurrency,
@ -358,7 +338,11 @@ export default class SignatureRequestOriginal extends Component {
const { t } = this.context;
const rejectNText = t('rejectRequestsN', [messagesCount]);
const currentNetwork = this.getNetworkName();
const networkName = getNetworkNameFromProviderType(providerConfig.type);
const currentNetwork =
networkName === ''
? providerConfig.nickname || t('unknownNetwork')
: t(networkName);
const balanceInBaseAsset = conversionRate
? formatCurrency(

View File

@ -5,6 +5,7 @@ import LedgerInstructionField from '../ledger-instruction-field';
import {
sanitizeMessage,
getURLHostName,
getNetworkNameFromProviderType,
///: BEGIN:ONLY_INCLUDE_IN(build-mmi)
shortenAddress,
///: END:ONLY_INCLUDE_IN
@ -28,7 +29,6 @@ import {
///: END:ONLY_INCLUDE_IN
} from '../../../helpers/constants/design-system';
import NetworkAccountBalanceHeader from '../network-account-balance-header';
import { NETWORK_TYPES } from '../../../../shared/constants/network';
import { Numeric } from '../../../../shared/modules/Numeric';
import { EtherDenomination } from '../../../../shared/constants/common';
import ConfirmPageContainerNavigation from '../confirm-page-container/confirm-page-container-navigation';
@ -121,27 +121,6 @@ export default class SignatureRequest extends PureComponent {
)}`;
}
getNetworkName() {
const { providerConfig } = this.props;
const providerName = providerConfig.type;
const { t } = this.context;
switch (providerName) {
case NETWORK_TYPES.MAINNET:
return t('mainnet');
case NETWORK_TYPES.GOERLI:
return t('goerli');
case NETWORK_TYPES.SEPOLIA:
return t('sepolia');
case NETWORK_TYPES.LINEA_TESTNET:
return t('lineatestnet');
case NETWORK_TYPES.LOCALHOST:
return t('localhost');
default:
return providerConfig.nickname || t('unknownNetwork');
}
}
memoizedParseMessage = memoize((data) => {
const { message, domain = {}, primaryType, types } = JSON.parse(data);
const sanitizedMessage = sanitizeMessage(message, primaryType, types);
@ -170,6 +149,7 @@ export default class SignatureRequest extends PureComponent {
render() {
const {
providerConfig,
txData: {
msgParams: { data, origin, version },
type,
@ -196,7 +176,11 @@ export default class SignatureRequest extends PureComponent {
primaryType,
} = this.memoizedParseMessage(data);
const rejectNText = t('rejectRequestsN', [unapprovedMessagesCount]);
const currentNetwork = this.getNetworkName();
const networkName = getNetworkNameFromProviderType(providerConfig.type);
const currentNetwork =
networkName === ''
? providerConfig.nickname || t('unknownNetwork')
: t(networkName);
const balanceInBaseAsset = conversionRate
? formatCurrency(

View File

@ -10,7 +10,7 @@ import bowser from 'bowser';
///: BEGIN:ONLY_INCLUDE_IN(snaps)
import { getSnapPrefix } from '@metamask/snaps-utils';
///: END:ONLY_INCLUDE_IN
import { CHAIN_IDS } from '../../../shared/constants/network';
import { CHAIN_IDS, NETWORK_TYPES } from '../../../shared/constants/network';
import {
toChecksumHexAddress,
stripHexPrefix,
@ -584,3 +584,18 @@ export const sanitizeString = (value) => {
const regex = /\u202E/giu;
return value.replace(regex, '\\u202E');
};
/**
* This method checks current provider type and returns its string representation
*
* @param {*} provider
* @param {*} t
* @returns
*/
export const getNetworkNameFromProviderType = (providerName) => {
if (providerName === NETWORK_TYPES.RPC) {
return '';
}
return providerName;
};

View File

@ -918,4 +918,15 @@ describe('util', () => {
expect(util.isDefaultMetaMaskChain(CHAIN_IDS.CELO)).toBeFalsy();
});
});
describe('getNetworkNameFromProviderType()', () => {
it('should return providerConfig.type if the type is not rpc', () => {
expect(util.getNetworkNameFromProviderType('mainnet')).toStrictEqual(
'mainnet',
);
});
it('should return empty string if teh providerConfig.type is rpc', () => {
expect(util.getNetworkNameFromProviderType('rpc')).toStrictEqual('');
});
});
});