Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup network enums #8627

Merged
merged 4 commits into from
May 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 3 additions & 15 deletions app/scripts/controllers/incoming-transactions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
Expand Down Expand Up @@ -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 {}
}

Expand Down
54 changes: 48 additions & 6 deletions app/scripts/controllers/network/enums.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
9 changes: 6 additions & 3 deletions app/scripts/controllers/network/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 2 additions & 29 deletions app/scripts/controllers/network/util.js
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
12 changes: 6 additions & 6 deletions app/scripts/lib/account-tracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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

Expand Down
11 changes: 0 additions & 11 deletions app/scripts/lib/enums.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
}
2 changes: 1 addition & 1 deletion app/scripts/lib/select-chain-id.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
15 changes: 12 additions & 3 deletions test/unit/app/controllers/network/network-controller-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
Original file line number Diff line number Diff line change
@@ -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 () {
Expand All @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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}`} />
Expand All @@ -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
Expand Down