Add NETWORK_TYPE_RPC constant (#10203)

* Refactor network display props
* Add NETWORK_TYPE_RPC constant
* Consolidate network constants
feature/default_network_editable
Erik Marks 4 years ago committed by GitHub
parent 90289ec22a
commit 7077ee68dc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 13
      app/scripts/controllers/network/network.js
  2. 1
      shared/constants/network.js
  3. 3
      test/unit/actions/config_test.js
  4. 4
      test/unit/localhostState.js
  5. 3
      ui/app/components/app/dropdowns/network-dropdown.js
  6. 5
      ui/app/components/app/loading-network-screen/loading-network-screen.container.js
  7. 46
      ui/app/components/app/network-display/network-display.component.js
  8. 10
      ui/app/components/app/network-display/network-display.container.js
  9. 3
      ui/app/ducks/metamask/metamask.js
  10. 8
      ui/app/helpers/constants/common.js
  11. 7
      ui/app/pages/settings/networks-tab/networks-tab.component.js
  12. 5
      ui/app/pages/settings/networks-tab/networks-tab.container.js
  13. 3
      ui/app/pages/swaps/view-quote/tests/view-quote-price-difference.test.js
  14. 6
      ui/app/selectors/selectors.js

@ -13,6 +13,7 @@ import {
RINKEBY, RINKEBY,
MAINNET, MAINNET,
INFURA_PROVIDER_TYPES, INFURA_PROVIDER_TYPES,
NETWORK_TYPE_RPC,
NETWORK_TYPE_TO_ID_MAP, NETWORK_TYPE_TO_ID_MAP,
MAINNET_CHAIN_ID, MAINNET_CHAIN_ID,
RINKEBY_CHAIN_ID, RINKEBY_CHAIN_ID,
@ -26,7 +27,7 @@ const env = process.env.METAMASK_ENV
let defaultProviderConfigOpts let defaultProviderConfigOpts
if (process.env.IN_TEST === 'true') { if (process.env.IN_TEST === 'true') {
defaultProviderConfigOpts = { defaultProviderConfigOpts = {
type: 'rpc', type: NETWORK_TYPE_RPC,
rpcUrl: 'http://localhost:8545', rpcUrl: 'http://localhost:8545',
chainId: '0x539', chainId: '0x539',
nickname: 'Localhost 8545', nickname: 'Localhost 8545',
@ -160,7 +161,7 @@ export default class NetworkController extends EventEmitter {
setRpcTarget(rpcUrl, chainId, ticker = 'ETH', nickname = '', rpcPrefs) { setRpcTarget(rpcUrl, chainId, ticker = 'ETH', nickname = '', rpcPrefs) {
this.setProviderConfig({ this.setProviderConfig({
type: 'rpc', type: NETWORK_TYPE_RPC,
rpcUrl, rpcUrl,
chainId, chainId,
ticker, ticker,
@ -172,8 +173,8 @@ export default class NetworkController extends EventEmitter {
async setProviderType(type, rpcUrl = '', ticker = 'ETH', nickname = '') { async setProviderType(type, rpcUrl = '', ticker = 'ETH', nickname = '') {
assert.notEqual( assert.notEqual(
type, type,
'rpc', NETWORK_TYPE_RPC,
`NetworkController - cannot call "setProviderType" with type 'rpc'. use "setRpcTarget"`, `NetworkController - cannot call "setProviderType" with type "${NETWORK_TYPE_RPC}". Use "setRpcTarget"`,
) )
assert( assert(
INFURA_PROVIDER_TYPES.includes(type), INFURA_PROVIDER_TYPES.includes(type),
@ -208,7 +209,7 @@ export default class NetworkController extends EventEmitter {
getNetworkIdentifier() { getNetworkIdentifier() {
const provider = this.providerStore.getState() const provider = this.providerStore.getState()
return provider.type === 'rpc' ? provider.rpcUrl : provider.type return provider.type === NETWORK_TYPE_RPC ? provider.rpcUrl : provider.type
} }
// //
@ -227,7 +228,7 @@ export default class NetworkController extends EventEmitter {
if (isInfura) { if (isInfura) {
this._configureInfuraProvider(type, this._infuraProjectId) this._configureInfuraProvider(type, this._infuraProjectId)
// url-based rpc endpoints // url-based rpc endpoints
} else if (type === 'rpc') { } else if (type === NETWORK_TYPE_RPC) {
this._configureStandardProvider(rpcUrl, chainId) this._configureStandardProvider(rpcUrl, chainId)
} else { } else {
throw new Error( throw new Error(

@ -3,6 +3,7 @@ export const RINKEBY = 'rinkeby'
export const KOVAN = 'kovan' export const KOVAN = 'kovan'
export const MAINNET = 'mainnet' export const MAINNET = 'mainnet'
export const GOERLI = 'goerli' export const GOERLI = 'goerli'
export const NETWORK_TYPE_RPC = 'rpc'
export const MAINNET_NETWORK_ID = '1' export const MAINNET_NETWORK_ID = '1'
export const ROPSTEN_NETWORK_ID = '3' export const ROPSTEN_NETWORK_ID = '3'

@ -2,6 +2,7 @@ import assert from 'assert'
import freeze from 'deep-freeze-strict' import freeze from 'deep-freeze-strict'
import reducers from '../../../ui/app/ducks' import reducers from '../../../ui/app/ducks'
import * as actionConstants from '../../../ui/app/store/actionConstants' import * as actionConstants from '../../../ui/app/store/actionConstants'
import { NETWORK_TYPE_RPC } from '../../../shared/constants/network'
describe('config view actions', function () { describe('config view actions', function () {
const initialState = { const initialState = {
@ -25,7 +26,7 @@ describe('config view actions', function () {
} }
const result = reducers(initialState, action) const result = reducers(initialState, action)
assert.equal(result.metamask.provider.type, 'rpc') assert.equal(result.metamask.provider.type, NETWORK_TYPE_RPC)
assert.equal(result.metamask.provider.rpcUrl, 'foo') assert.equal(result.metamask.provider.rpcUrl, 'foo')
}) })
}) })

@ -1,3 +1,5 @@
import { NETWORK_TYPE_RPC } from '../../shared/constants/network'
/** /**
* @typedef {Object} FirstTimeState * @typedef {Object} FirstTimeState
* @property {Object} config Initial configuration parameters * @property {Object} config Initial configuration parameters
@ -11,7 +13,7 @@ const initialState = {
config: {}, config: {},
NetworkController: { NetworkController: {
provider: { provider: {
type: 'rpc', type: NETWORK_TYPE_RPC,
rpcUrl: 'http://localhost:8545', rpcUrl: 'http://localhost:8545',
chainId: '0x539', chainId: '0x539',
}, },

@ -10,6 +10,7 @@ import {
NETWORKS_FORM_ROUTE, NETWORKS_FORM_ROUTE,
} from '../../../helpers/constants/routes' } from '../../../helpers/constants/routes'
import { ENVIRONMENT_TYPE_FULLSCREEN } from '../../../../../shared/constants/app' import { ENVIRONMENT_TYPE_FULLSCREEN } from '../../../../../shared/constants/app'
import { NETWORK_TYPE_RPC } from '../../../../../shared/constants/network'
import { import {
getEnvironmentType, getEnvironmentType,
isPrefixedFormattedHexString, isPrefixedFormattedHexString,
@ -117,7 +118,7 @@ class NetworkDropdown extends Component {
return reversedRpcListDetail.map((entry) => { return reversedRpcListDetail.map((entry) => {
const { rpcUrl, chainId, ticker = 'ETH', nickname = '' } = entry const { rpcUrl, chainId, ticker = 'ETH', nickname = '' } = entry
const isCurrentRpcTarget = const isCurrentRpcTarget =
provider.type === 'rpc' && rpcUrl === provider.rpcUrl provider.type === NETWORK_TYPE_RPC && rpcUrl === provider.rpcUrl
return ( return (
<DropdownMenuItem <DropdownMenuItem

@ -1,4 +1,5 @@
import { connect } from 'react-redux' import { connect } from 'react-redux'
import { NETWORK_TYPE_RPC } from '../../../../../shared/constants/network'
import * as actions from '../../../store/actions' import * as actions from '../../../store/actions'
import { getNetworkIdentifier } from '../../../selectors' import { getNetworkIdentifier } from '../../../selectors'
import LoadingNetworkScreen from './loading-network-screen.component' import LoadingNetworkScreen from './loading-network-screen.component'
@ -9,7 +10,9 @@ const mapStateToProps = (state) => {
const { rpcUrl, chainId, ticker, nickname, type } = provider const { rpcUrl, chainId, ticker, nickname, type } = provider
const setProviderArgs = const setProviderArgs =
type === 'rpc' ? [rpcUrl, chainId, ticker, nickname] : [provider.type] type === NETWORK_TYPE_RPC
? [rpcUrl, chainId, ticker, nickname]
: [provider.type]
return { return {
isLoadingNetwork: network === 'loading', isLoadingNetwork: network === 'loading',

@ -1,21 +1,7 @@
import React, { Component } from 'react' import React, { Component } from 'react'
import PropTypes from 'prop-types' import PropTypes from 'prop-types'
import classnames from 'classnames' import classnames from 'classnames'
import { import { NETWORK_TYPE_RPC } from '../../../../../shared/constants/network'
MAINNET_NETWORK_ID,
ROPSTEN_NETWORK_ID,
RINKEBY_NETWORK_ID,
KOVAN_NETWORK_ID,
GOERLI_NETWORK_ID,
} from '../../../../../shared/constants/network'
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 { export default class NetworkDisplay extends Component {
static defaultProps = { static defaultProps = {
@ -23,9 +9,9 @@ export default class NetworkDisplay extends Component {
} }
static propTypes = { static propTypes = {
networkNickname: PropTypes.string.isRequired,
networkType: PropTypes.string.isRequired,
colored: PropTypes.bool, colored: PropTypes.bool,
network: PropTypes.string,
provider: PropTypes.object,
} }
static contextTypes = { static contextTypes = {
@ -33,12 +19,11 @@ export default class NetworkDisplay extends Component {
} }
renderNetworkIcon() { renderNetworkIcon() {
const { network } = this.props const { networkType } = this.props
const networkClass = networkIdToTypeMap[network]
return networkClass ? ( return networkType ? (
<div <div
className={`network-display__icon network-display__icon--${networkClass}`} className={`network-display__icon network-display__icon--${networkType}`}
/> />
) : ( ) : (
<div <div
@ -52,24 +37,19 @@ export default class NetworkDisplay extends Component {
} }
render() { render() {
const { const { colored, networkNickname, networkType } = this.props
colored,
network,
provider: { type, nickname },
} = this.props
const networkClass = networkIdToTypeMap[network]
return ( return (
<div <div
className={classnames('network-display__container', { className={classnames('network-display__container', {
'network-display__container--colored': colored, 'network-display__container--colored': colored,
[`network-display__container--${networkClass}`]: [`network-display__container--${networkType}`]:
colored && networkClass, colored && networkType,
})} })}
> >
{networkClass ? ( {networkType ? (
<div <div
className={`network-display__icon network-display__icon--${networkClass}`} className={`network-display__icon network-display__icon--${networkType}`}
/> />
) : ( ) : (
<div <div
@ -81,7 +61,9 @@ export default class NetworkDisplay extends Component {
/> />
)} )}
<div className="network-display__name"> <div className="network-display__name">
{type === 'rpc' && nickname ? nickname : this.context.t(type)} {networkType === NETWORK_TYPE_RPC && networkNickname
? networkNickname
: this.context.t(networkType)}
</div> </div>
</div> </div>
) )

@ -1,10 +1,14 @@
import { connect } from 'react-redux' import { connect } from 'react-redux'
import NetworkDisplay from './network-display.component' import NetworkDisplay from './network-display.component'
const mapStateToProps = ({ metamask: { network, provider } }) => { const mapStateToProps = ({
metamask: {
provider: { nickname, type },
},
}) => {
return { return {
network, networkNickname: nickname,
provider, networkType: type,
} }
} }

@ -1,5 +1,6 @@
import * as actionConstants from '../../store/actionConstants' import * as actionConstants from '../../store/actionConstants'
import { ALERT_TYPES } from '../../../../shared/constants/alerts' import { ALERT_TYPES } from '../../../../shared/constants/alerts'
import { NETWORK_TYPE_RPC } from '../../../../shared/constants/network'
export default function reduceMetamask(state = {}, action) { export default function reduceMetamask(state = {}, action) {
const metamaskState = { const metamaskState = {
@ -63,7 +64,7 @@ export default function reduceMetamask(state = {}, action) {
return { return {
...metamaskState, ...metamaskState,
provider: { provider: {
type: 'rpc', type: NETWORK_TYPE_RPC,
rpcUrl: action.value, rpcUrl: action.value,
}, },
} }

@ -5,14 +5,6 @@ export const WEI = 'WEI'
export const PRIMARY = 'PRIMARY' export const PRIMARY = 'PRIMARY'
export const SECONDARY = 'SECONDARY' export const SECONDARY = 'SECONDARY'
export const NETWORK_TYPES = {
KOVAN: 'kovan',
MAINNET: 'mainnet',
RINKEBY: 'rinkeby',
ROPSTEN: 'ropsten',
GOERLI: 'goerli',
}
export const GAS_ESTIMATE_TYPES = { export const GAS_ESTIMATE_TYPES = {
SLOW: 'SLOW', SLOW: 'SLOW',
AVERAGE: 'AVERAGE', AVERAGE: 'AVERAGE',

@ -1,6 +1,7 @@
import React, { PureComponent } from 'react' import React, { PureComponent } from 'react'
import PropTypes from 'prop-types' import PropTypes from 'prop-types'
import classnames from 'classnames' import classnames from 'classnames'
import { NETWORK_TYPE_RPC } from '../../../../../shared/constants/network'
import Button from '../../../components/ui/button' import Button from '../../../components/ui/button'
import LockIcon from '../../../components/ui/lock-icon' import LockIcon from '../../../components/ui/lock-icon'
import { import {
@ -90,7 +91,7 @@ export default class NetworksTab extends PureComponent {
const listItemNetworkIsSelected = selectRpcUrl && selectRpcUrl === rpcUrl const listItemNetworkIsSelected = selectRpcUrl && selectRpcUrl === rpcUrl
const listItemUrlIsProviderUrl = rpcUrl === providerUrl const listItemUrlIsProviderUrl = rpcUrl === providerUrl
const listItemTypeIsProviderNonRpcType = const listItemTypeIsProviderNonRpcType =
providerType !== 'rpc' && currentProviderType === providerType providerType !== NETWORK_TYPE_RPC && currentProviderType === providerType
const listItemNetworkIsCurrentProvider = const listItemNetworkIsCurrentProvider =
!networkIsSelected && !networkIsSelected &&
!networksTabIsInAddMode && !networksTabIsInAddMode &&
@ -118,12 +119,12 @@ export default class NetworksTab extends PureComponent {
className={classnames('networks-tab__networks-list-name', { className={classnames('networks-tab__networks-list-name', {
'networks-tab__networks-list-name--selected': displayNetworkListItemAsSelected, 'networks-tab__networks-list-name--selected': displayNetworkListItemAsSelected,
'networks-tab__networks-list-name--disabled': 'networks-tab__networks-list-name--disabled':
currentProviderType !== 'rpc' && currentProviderType !== NETWORK_TYPE_RPC &&
!displayNetworkListItemAsSelected, !displayNetworkListItemAsSelected,
})} })}
> >
{label || this.context.t(labelKey)} {label || this.context.t(labelKey)}
{currentProviderType !== 'rpc' && ( {currentProviderType !== NETWORK_TYPE_RPC && (
<LockIcon width="14px" height="17px" fill="#cdcdcd" /> <LockIcon width="14px" height="17px" fill="#cdcdcd" />
)} )}
</div> </div>

@ -11,6 +11,7 @@ import {
} from '../../../store/actions' } from '../../../store/actions'
import { NETWORKS_FORM_ROUTE } from '../../../helpers/constants/routes' import { NETWORKS_FORM_ROUTE } from '../../../helpers/constants/routes'
import { ENVIRONMENT_TYPE_FULLSCREEN } from '../../../../../shared/constants/app' import { ENVIRONMENT_TYPE_FULLSCREEN } from '../../../../../shared/constants/app'
import { NETWORK_TYPE_RPC } from '../../../../../shared/constants/network'
import { getEnvironmentType } from '../../../../../app/scripts/lib/util' import { getEnvironmentType } from '../../../../../app/scripts/lib/util'
import NetworksTab from './networks-tab.component' import NetworksTab from './networks-tab.component'
import { defaultNetworksData } from './networks-tab.constants' import { defaultNetworksData } from './networks-tab.constants'
@ -37,7 +38,7 @@ const mapStateToProps = (state, ownProps) => {
return { return {
label: rpc.nickname, label: rpc.nickname,
iconColor: '#6A737D', iconColor: '#6A737D',
providerType: 'rpc', providerType: NETWORK_TYPE_RPC,
rpcUrl: rpc.rpcUrl, rpcUrl: rpc.rpcUrl,
chainId: rpc.chainId, chainId: rpc.chainId,
ticker: rpc.ticker, ticker: rpc.ticker,
@ -61,7 +62,7 @@ const mapStateToProps = (state, ownProps) => {
networksToRender.find((network) => { networksToRender.find((network) => {
return ( return (
network.rpcUrl === provider.rpcUrl || network.rpcUrl === provider.rpcUrl ||
(network.providerType !== 'rpc' && (network.providerType !== NETWORK_TYPE_RPC &&
network.providerType === provider.type) network.providerType === provider.type)
) )
}) || {} }) || {}

@ -3,6 +3,7 @@ import React from 'react'
import { shallow } from 'enzyme' import { shallow } from 'enzyme'
import { Provider } from 'react-redux' import { Provider } from 'react-redux'
import configureMockStore from 'redux-mock-store' import configureMockStore from 'redux-mock-store'
import { NETWORK_TYPE_RPC } from '../../../../../../shared/constants/network'
import ViewQuotePriceDifference from '../view-quote-price-difference' import ViewQuotePriceDifference from '../view-quote-price-difference'
describe('View Price Quote Difference', function () { describe('View Price Quote Difference', function () {
@ -11,7 +12,7 @@ describe('View Price Quote Difference', function () {
const state = { const state = {
metamask: { metamask: {
tokens: [], tokens: [],
provider: { type: 'rpc', nickname: '', rpcUrl: '' }, provider: { type: NETWORK_TYPE_RPC, nickname: '', rpcUrl: '' },
preferences: { showFiatInTestnets: true }, preferences: { showFiatInTestnets: true },
currentCurrency: 'usd', currentCurrency: 'usd',
conversionRate: 600.0, conversionRate: 600.0,

@ -1,7 +1,7 @@
import { stripHexPrefix } from 'ethereumjs-util' import { stripHexPrefix } from 'ethereumjs-util'
import { createSelector } from 'reselect' import { createSelector } from 'reselect'
import { addHexPrefix } from '../../../app/scripts/lib/util' import { addHexPrefix } from '../../../app/scripts/lib/util'
import { NETWORK_TYPES } from '../helpers/constants/common' import { MAINNET, NETWORK_TYPE_RPC } from '../../../shared/constants/network'
import { import {
shortenAddress, shortenAddress,
checksumAddress, checksumAddress,
@ -21,7 +21,7 @@ export function getNetworkIdentifier(state) {
export function getMetricsNetworkIdentifier(state) { export function getMetricsNetworkIdentifier(state) {
const { provider } = state.metamask const { provider } = state.metamask
return provider.type === 'rpc' ? provider.rpcUrl : provider.type return provider.type === NETWORK_TYPE_RPC ? provider.rpcUrl : provider.type
} }
export function getCurrentChainId(state) { export function getCurrentChainId(state) {
@ -281,7 +281,7 @@ function getSuggestedTokenCount(state) {
export function getIsMainnet(state) { export function getIsMainnet(state) {
const networkType = getNetworkIdentifier(state) const networkType = getNetworkIdentifier(state)
return networkType === NETWORK_TYPES.MAINNET return networkType === MAINNET
} }
export function getPreferences({ metamask }) { export function getPreferences({ metamask }) {

Loading…
Cancel
Save