1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 01:39:44 +01:00

Cleanup network enums (#8627)

* cleanup & consolidate network enums
This commit is contained in:
Erik Marks 2020-05-20 08:57:45 -07:00 committed by GitHub
parent 9c1eae2965
commit f748664638
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 93 additions and 89 deletions

View File

@ -6,25 +6,14 @@ import { bnToHex } from '../lib/util'
import fetchWithTimeout from '../lib/fetch-with-timeout'
import {
MAINNET_CODE,
ROPSTEN_CODE,
RINKEBY_CODE,
KOVAN_CODE,
GOERLI_CODE,
ROPSTEN,
RINKEBY,
KOVAN,
GOERLI,
MAINNET,
NETWORK_TYPE_TO_ID_MAP,
} from './network/enums'
const networkTypeToIdMap = {
[ROPSTEN]: String(ROPSTEN_CODE),
[RINKEBY]: String(RINKEBY_CODE),
[KOVAN]: String(KOVAN_CODE),
[GOERLI]: String(GOERLI_CODE),
[MAINNET]: String(MAINNET_CODE),
}
const fetch = fetchWithTimeout({
timeout: 30000,
})
@ -185,10 +174,9 @@ export default class IncomingTransactionsController {
async _fetchTxs (address, fromBlock, networkType) {
let etherscanSubdomain = 'api'
const currentNetworkID = networkTypeToIdMap[networkType]
const supportedNetworkTypes = [ROPSTEN, RINKEBY, KOVAN, GOERLI, MAINNET]
const currentNetworkID = NETWORK_TYPE_TO_ID_MAP[networkType]?.networkId
if (supportedNetworkTypes.indexOf(networkType) === -1) {
if (!currentNetworkID) {
return {}
}

View File

@ -2,17 +2,59 @@ export const ROPSTEN = 'ropsten'
export const RINKEBY = 'rinkeby'
export const KOVAN = 'kovan'
export const MAINNET = 'mainnet'
export const LOCALHOST = 'localhost'
export const GOERLI = 'goerli'
export const LOCALHOST = 'localhost'
export const MAINNET_CODE = 1
export const ROPSTEN_CODE = 3
export const RINKEBY_CODE = 4
export const KOVAN_CODE = 42
export const GOERLI_CODE = 5
export const MAINNET_NETWORK_ID = 1
export const ROPSTEN_NETWORK_ID = 3
export const RINKEBY_NETWORK_ID = 4
export const GOERLI_NETWORK_ID = 5
export const KOVAN_NETWORK_ID = 42
export const MAINNET_CHAIN_ID = '0x1'
export const ROPSTEN_CHAIN_ID = '0x3'
export const RINKEBY_CHAIN_ID = '0x4'
export const GOERLI_CHAIN_ID = '0x5'
export const KOVAN_CHAIN_ID = '0x42'
export const ROPSTEN_DISPLAY_NAME = 'Ropsten'
export const RINKEBY_DISPLAY_NAME = 'Rinkeby'
export const KOVAN_DISPLAY_NAME = 'Kovan'
export const MAINNET_DISPLAY_NAME = 'Main Ethereum Network'
export const GOERLI_DISPLAY_NAME = 'Goerli'
export const INFURA_PROVIDER_TYPES = [
ROPSTEN,
RINKEBY,
KOVAN,
MAINNET,
GOERLI,
]
export const NETWORK_TYPE_TO_ID_MAP = {
[ROPSTEN]: { networkId: ROPSTEN_NETWORK_ID, chainId: ROPSTEN_CHAIN_ID },
[RINKEBY]: { networkId: RINKEBY_NETWORK_ID, chainId: RINKEBY_CHAIN_ID },
[KOVAN]: { networkId: KOVAN_NETWORK_ID, chainId: KOVAN_CHAIN_ID },
[GOERLI]: { networkId: GOERLI_NETWORK_ID, chainId: GOERLI_CHAIN_ID },
[MAINNET]: { networkId: MAINNET_NETWORK_ID, chainId: MAINNET_CHAIN_ID },
}
export const NETWORK_TO_NAME_MAP = {
[ROPSTEN]: ROPSTEN_DISPLAY_NAME,
[RINKEBY]: RINKEBY_DISPLAY_NAME,
[KOVAN]: KOVAN_DISPLAY_NAME,
[MAINNET]: MAINNET_DISPLAY_NAME,
[GOERLI]: GOERLI_DISPLAY_NAME,
[ROPSTEN_NETWORK_ID]: ROPSTEN_DISPLAY_NAME,
[RINKEBY_NETWORK_ID]: RINKEBY_DISPLAY_NAME,
[KOVAN_NETWORK_ID]: KOVAN_DISPLAY_NAME,
[GOERLI_NETWORK_ID]: GOERLI_DISPLAY_NAME,
[MAINNET_NETWORK_ID]: MAINNET_DISPLAY_NAME,
[ROPSTEN_CHAIN_ID]: ROPSTEN_DISPLAY_NAME,
[RINKEBY_CHAIN_ID]: RINKEBY_DISPLAY_NAME,
[KOVAN_CHAIN_ID]: KOVAN_DISPLAY_NAME,
[GOERLI_CHAIN_ID]: GOERLI_DISPLAY_NAME,
[MAINNET_CHAIN_ID]: MAINNET_DISPLAY_NAME,
}

View File

@ -14,9 +14,12 @@ import { createSwappableProxy, createEventEmitterProxy } from 'swappable-obj-pro
const networks = { networkList: {} }
import { ROPSTEN, RINKEBY, KOVAN, MAINNET, LOCALHOST, GOERLI } from './enums'
const INFURA_PROVIDER_TYPES = [ROPSTEN, RINKEBY, KOVAN, MAINNET, GOERLI]
import {
RINKEBY,
MAINNET,
LOCALHOST,
INFURA_PROVIDER_TYPES,
} from './enums'
const env = process.env.METAMASK_ENV
const METAMASK_DEBUG = process.env.METAMASK_DEBUG

View File

@ -1,33 +1,6 @@
import {
ROPSTEN,
RINKEBY,
KOVAN,
MAINNET,
GOERLI,
ROPSTEN_CODE,
RINKEBY_CODE,
KOVAN_CODE,
GOERLI_CODE,
ROPSTEN_DISPLAY_NAME,
RINKEBY_DISPLAY_NAME,
KOVAN_DISPLAY_NAME,
MAINNET_DISPLAY_NAME,
GOERLI_DISPLAY_NAME,
} from './enums'
import { NETWORK_TO_NAME_MAP } from './enums'
const networkToNameMap = {
[ROPSTEN]: ROPSTEN_DISPLAY_NAME,
[RINKEBY]: RINKEBY_DISPLAY_NAME,
[KOVAN]: KOVAN_DISPLAY_NAME,
[MAINNET]: MAINNET_DISPLAY_NAME,
[GOERLI]: GOERLI_DISPLAY_NAME,
[ROPSTEN_CODE]: ROPSTEN_DISPLAY_NAME,
[RINKEBY_CODE]: RINKEBY_DISPLAY_NAME,
[KOVAN_CODE]: KOVAN_DISPLAY_NAME,
[GOERLI_CODE]: GOERLI_DISPLAY_NAME,
}
export const getNetworkDisplayName = (key) => networkToNameMap[key]
export const getNetworkDisplayName = (key) => NETWORK_TO_NAME_MAP[key]
export function formatTxMetaForRpcResult (txMeta) {
return {

View File

@ -15,7 +15,7 @@ import pify from 'pify'
import Web3 from 'web3'
import SINGLE_CALL_BALANCES_ABI from 'single-call-balance-checker-abi'
import { bnToHex } from './util'
import { MAINNET_CODE, RINKEBY_CODE, ROPSTEN_CODE, KOVAN_CODE } from '../controllers/network/enums'
import { MAINNET_NETWORK_ID, RINKEBY_NETWORK_ID, ROPSTEN_NETWORK_ID, KOVAN_NETWORK_ID } from '../controllers/network/enums'
import {
SINGLE_CALL_BALANCES_ADDRESS,
@ -188,22 +188,22 @@ export default class AccountTracker {
async _updateAccounts () {
const accounts = this.store.getState().accounts
const addresses = Object.keys(accounts)
const currentNetwork = parseInt(this.network.getNetworkState())
const currentNetwork = this.network.getNetworkState()
switch (currentNetwork) {
case MAINNET_CODE:
case MAINNET_NETWORK_ID:
await this._updateAccountsViaBalanceChecker(addresses, SINGLE_CALL_BALANCES_ADDRESS)
break
case RINKEBY_CODE:
case RINKEBY_NETWORK_ID:
await this._updateAccountsViaBalanceChecker(addresses, SINGLE_CALL_BALANCES_ADDRESS_RINKEBY)
break
case ROPSTEN_CODE:
case ROPSTEN_NETWORK_ID:
await this._updateAccountsViaBalanceChecker(addresses, SINGLE_CALL_BALANCES_ADDRESS_ROPSTEN)
break
case KOVAN_CODE:
case KOVAN_NETWORK_ID:
await this._updateAccountsViaBalanceChecker(addresses, SINGLE_CALL_BALANCES_ADDRESS_KOVAN)
break

View File

@ -9,12 +9,6 @@ const PLATFORM_EDGE = 'Edge'
const PLATFORM_FIREFOX = 'Firefox'
const PLATFORM_OPERA = 'Opera'
const MAINNET_CHAIN_ID = '0x1'
const ROPSTEN_CHAIN_ID = '0x3'
const RINKEBY_CHAIN_ID = '0x4'
const KOVAN_CHAIN_ID = '0x2a'
const GOERLI_CHAIN_ID = '0x5'
export {
ENVIRONMENT_TYPE_POPUP,
ENVIRONMENT_TYPE_NOTIFICATION,
@ -25,9 +19,4 @@ export {
PLATFORM_EDGE,
PLATFORM_FIREFOX,
PLATFORM_OPERA,
MAINNET_CHAIN_ID,
ROPSTEN_CHAIN_ID,
RINKEBY_CHAIN_ID,
KOVAN_CHAIN_ID,
GOERLI_CHAIN_ID,
}

View File

@ -4,7 +4,7 @@ import {
RINKEBY_CHAIN_ID,
KOVAN_CHAIN_ID,
GOERLI_CHAIN_ID,
} from './enums'
} from '../controllers/network/enums'
const standardNetworkId = {
'1': MAINNET_CHAIN_ID,

View File

@ -67,13 +67,22 @@ describe('NetworkController', function () {
it('getNetworkDisplayName should return the correct network name', function () {
const tests = [
{
input: 3,
input: '3',
expected: 'Ropsten',
}, {
input: 4,
input: '4',
expected: 'Rinkeby',
}, {
input: 42,
input: '42',
expected: 'Kovan',
}, {
input: '0x3',
expected: 'Ropsten',
}, {
input: '0x4',
expected: 'Rinkeby',
}, {
input: '0x42',
expected: 'Kovan',
}, {
input: 'ropsten',

View File

@ -1,6 +1,6 @@
import { strict as assert } from 'assert'
import { throwIfAccountIsBlacklisted } from '../../../../../app/scripts/controllers/transactions/lib/recipient-blacklist-checker'
import { ROPSTEN_CODE, RINKEBY_CODE, KOVAN_CODE, GOERLI_CODE } from '../../../../../app/scripts/controllers/network/enums'
import { ROPSTEN_NETWORK_ID, RINKEBY_NETWORK_ID, KOVAN_NETWORK_ID, GOERLI_NETWORK_ID } from '../../../../../app/scripts/controllers/network/enums'
describe('Recipient Blacklist Checker', function () {
describe('#throwIfAccountIsBlacklisted', function () {
@ -19,7 +19,7 @@ describe('Recipient Blacklist Checker', function () {
]
it('does not fail on test networks', function () {
const networks = [ROPSTEN_CODE, RINKEBY_CODE, KOVAN_CODE, GOERLI_CODE]
const networks = [ROPSTEN_NETWORK_ID, RINKEBY_NETWORK_ID, KOVAN_NETWORK_ID, GOERLI_NETWORK_ID]
for (const networkId of networks) {
for (const account of publicAccounts) {
assert.doesNotThrow(() => throwIfAccountIsBlacklisted(networkId, account))

View File

@ -2,19 +2,19 @@ import React, { Component } from 'react'
import PropTypes from 'prop-types'
import classnames from 'classnames'
import {
MAINNET_CODE,
ROPSTEN_CODE,
RINKEBY_CODE,
KOVAN_CODE,
GOERLI_CODE,
MAINNET_NETWORK_ID,
ROPSTEN_NETWORK_ID,
RINKEBY_NETWORK_ID,
KOVAN_NETWORK_ID,
GOERLI_NETWORK_ID,
} from '../../../../../app/scripts/controllers/network/enums'
const networkToClassHash = {
[MAINNET_CODE]: 'mainnet',
[ROPSTEN_CODE]: 'ropsten',
[RINKEBY_CODE]: 'rinkeby',
[GOERLI_CODE]: 'goerli',
[KOVAN_CODE]: 'kovan',
const networkIdToTypeMap = {
[MAINNET_NETWORK_ID]: 'mainnet',
[ROPSTEN_NETWORK_ID]: 'ropsten',
[RINKEBY_NETWORK_ID]: 'rinkeby',
[GOERLI_NETWORK_ID]: 'goerli',
[KOVAN_NETWORK_ID]: 'kovan',
}
export default class NetworkDisplay extends Component {
@ -34,7 +34,7 @@ export default class NetworkDisplay extends Component {
renderNetworkIcon () {
const { network } = this.props
const networkClass = networkToClassHash[network]
const networkClass = networkIdToTypeMap[network]
return networkClass
? <div className={`network-display__icon network-display__icon--${networkClass}`} />
@ -51,7 +51,7 @@ export default class NetworkDisplay extends Component {
render () {
const { colored, network, provider: { type, nickname } } = this.props
const networkClass = networkToClassHash[network]
const networkClass = networkIdToTypeMap[network]
return (
<div