(Refactor) Store networks' information in constant object

This commit is contained in:
Vadim Arasev 2018-10-11 11:17:07 +03:00
parent bc00a5af18
commit 3564270dcb
21 changed files with 111 additions and 154 deletions

View File

@ -7,7 +7,7 @@ import { Route } from 'react-router-dom'
import { inject, observer } from 'mobx-react'
import swal from 'sweetalert2'
import { messages } from './messages'
import { isTestnet } from './helpers'
import { constants } from './constants'
@inject('commonStore', 'contractsStore')
@observer
@ -124,8 +124,8 @@ class App extends Component {
}
getNetIdClass() {
const { contractsStore } = this.props
return isTestnet(contractsStore.netId) ? 'sokol' : ''
const { netId } = this.props.contractsStore
return netId in constants.NETWORKS && constants.NETWORKS[netId].TESTNET ? 'sokol' : ''
}
render() {
@ -142,6 +142,8 @@ class App extends Component {
''
)
const isTestnet = contractsStore.netId in constants.NETWORKS && constants.NETWORKS[contractsStore.netId].TESTNET
return (
<section className={`content ${this.state.showMobileMenu ? 'content-menu-open' : ''}`}>
{loading}
@ -155,7 +157,7 @@ class App extends Component {
{search}
<div
className={`app-container ${this.state.showMobileMenu ? 'app-container-open-mobile-menu' : ''} ${
isTestnet(contractsStore.netId) ? 'sokol' : ''
isTestnet ? 'sokol' : ''
}`}
>
<div className="container">

View File

@ -9,16 +9,11 @@ const styles = netId => {
backgroundColor: 'rgba(47, 109, 99, 0.8)'
}
switch (netId) {
case constants.NETID_SOKOL:
case constants.NETID_DAI_TEST:
return sokol
case constants.NETID_CORE:
case constants.NETID_DAI:
return core
default:
return {}
if (netId in constants.NETWORKS) {
return constants.NETWORKS[netId].TESTNET ? sokol : core
}
return core
}
const Loading = ({ netId }) => (
<div className="loading-container" style={styles(netId)}>

View File

@ -82,7 +82,7 @@ export class BallotEmissionFundsMetadata extends React.Component {
} else if (this.noActiveBallotExists !== true) {
note = <p>To be able to create a new ballot, the previous ballot of this type must be finalized.</p>
}
if (contractsStore.netId === constants.NETID_SOKOL) {
if (constants.NETWORKS[contractsStore.netId].NAME.toLowerCase() === 'sokol') {
explorerLink = `https://sokol.poaexplorer.com/address/search/${contractsStore.emissionFunds.address}`
} else {
explorerLink = `https://poaexplorer.com/address/${contractsStore.emissionFunds.address}`

View File

@ -2,10 +2,10 @@ import React from 'react'
import moment from 'moment'
import { Link } from 'react-router-dom'
import Socials from './Socials.jsx'
import { isTestnet } from '../helpers'
import { constants } from '../constants'
export const Footer = ({ netId }) => {
const footerClassName = isTestnet(netId) ? 'sokol' : ''
const footerClassName = netId in constants.NETWORKS && constants.NETWORKS[netId].TESTNET ? 'sokol' : ''
return (
<footer className={`footer ${footerClassName}`}>

View File

@ -7,10 +7,10 @@ import menuOpenIconBase from '../assets/images/icons/icon-close.svg'
import menuOpenIconSokol from '../assets/images/icons/icon-close-sokol.svg'
import NavigationLinks from './NavigationLinks.jsx'
import MobileMenuLinks from './MobileMenuLinks.jsx'
import { isTestnet } from '../helpers'
import { constants } from '../constants'
export const Header = ({ netId, baseRootPath, navigationData, showMobileMenu, onMenuToggle }) => {
const thisIsTestnet = isTestnet(netId)
const thisIsTestnet = netId in constants.NETWORKS && constants.NETWORKS[netId].TESTNET
const headerClassName = thisIsTestnet ? 'sokol' : ''
const logoImageName = thisIsTestnet ? logoSokol : logoBase
const menuIcon = thisIsTestnet ? menuIconSokol : menuIconBase

View File

@ -24,10 +24,34 @@ constants.minBallotDurationInDays = 2
constants.startTimeOffsetInMinutes = 5
constants.endTimeDefaultInMinutes = 2890
constants.getTransactionReceiptInterval = 5000
constants.NETID_SOKOL = '77'
constants.NETID_CORE = '99'
constants.NETID_DAI_TEST = '79'
constants.NETID_DAI = '100'
constants.NETWORKS = {
'77': {
NAME: 'Sokol',
RPC: 'https://sokol.poa.network',
BRANCH: 'sokol',
TESTNET: true
},
'99': {
NAME: 'Core',
RPC: 'https://core.poa.network',
BRANCH: 'core',
TESTNET: false
},
'79': {
NAME: 'Dai-Test',
RPC: 'http://40.112.48.125',
BRANCH: 'dai-test',
TESTNET: true
},
'100': {
NAME: 'Dai',
RPC: 'https://dai.poa.network',
BRANCH: 'dai',
TESTNET: false
}
}
module.exports = {
constants
}

View File

@ -1,15 +1,15 @@
import Web3 from 'web3'
import { networkAddresses } from './addresses'
import helpers from './helpers'
import { constants } from '../constants'
export default class BallotsStorage {
async init({ web3, netId }) {
const { BALLOTS_STORAGE_ADDRESS } = networkAddresses(netId)
const { BALLOTS_STORAGE_ADDRESS } = networkAddresses()
console.log('Ballots Storage address', BALLOTS_STORAGE_ADDRESS)
const web3_10 = new Web3(web3.currentProvider)
const branch = helpers.getBranch(netId)
const ballotsStorageAbi = await helpers.getABI(branch, 'BallotStorage')
const ballotsStorageAbi = await helpers.getABI(constants.NETWORKS[netId].BRANCH, 'BallotStorage')
this.ballotsStorageInstance = new web3_10.eth.Contract(ballotsStorageAbi, BALLOTS_STORAGE_ADDRESS)
}

View File

@ -3,7 +3,7 @@ import { networkAddresses } from './addresses'
export default class EmissionFunds {
async init({ web3, netId }) {
const { EMISSION_FUNDS_ADDRESS } = networkAddresses(netId)
const { EMISSION_FUNDS_ADDRESS } = networkAddresses()
console.log('EmissionFunds address', EMISSION_FUNDS_ADDRESS)
this.web3_10 = new Web3(web3.currentProvider)
this.address = EMISSION_FUNDS_ADDRESS

View File

@ -1,16 +1,15 @@
import Web3 from 'web3'
import { networkAddresses } from './addresses'
import helpers from './helpers'
import { constants } from '../constants'
export default class KeysManager {
async init({ web3, netId }) {
const { KEYS_MANAGER_ADDRESS } = networkAddresses(netId)
const { KEYS_MANAGER_ADDRESS } = networkAddresses()
console.log('KeysManager address', KEYS_MANAGER_ADDRESS)
const web3_10 = new Web3(web3.currentProvider)
const branch = helpers.getBranch(netId)
const keysManagerABI = await helpers.getABI(branch, 'KeysManager')
const keysManagerABI = await helpers.getABI(constants.NETWORKS[netId].BRANCH, 'KeysManager')
this.instance = new web3_10.eth.Contract(keysManagerABI, KEYS_MANAGER_ADDRESS)
this.address = KEYS_MANAGER_ADDRESS

View File

@ -1,16 +1,15 @@
import Web3 from 'web3'
import { networkAddresses } from './addresses'
import helpers from './helpers'
import { constants } from '../constants'
export default class POAConsensus {
export default class PoaConsensus {
async init({ web3, netId }) {
const { POA_ADDRESS } = networkAddresses(netId)
const { POA_ADDRESS } = networkAddresses()
console.log('POA address', POA_ADDRESS)
const web3_10 = new Web3(web3.currentProvider)
const branch = helpers.getBranch(netId)
const poaConsensusAbi = await helpers.getABI(branch, 'PoaNetworkConsensus')
const poaConsensusAbi = await helpers.getABI(constants.NETWORKS[netId].BRANCH, 'PoaNetworkConsensus')
this.poaInstance = new web3_10.eth.Contract(poaConsensusAbi, POA_ADDRESS)
}

View File

@ -1,15 +1,15 @@
import Web3 from 'web3'
import { networkAddresses } from './addresses'
import helpers from './helpers'
import { constants } from '../constants'
export default class ProxyStorage {
async init({ web3, netId }) {
const { PROXY_ADDRESS } = networkAddresses(netId)
const { PROXY_ADDRESS } = networkAddresses()
console.log('Proxy Storage address', PROXY_ADDRESS)
const web3_10 = new Web3(web3.currentProvider)
const branch = helpers.getBranch(netId)
const proxyStorageAbi = await helpers.getABI(branch, 'ProxyStorage')
const proxyStorageAbi = await helpers.getABI(constants.NETWORKS[netId].BRANCH, 'ProxyStorage')
this.proxyStorageInstance = new web3_10.eth.Contract(proxyStorageAbi, PROXY_ADDRESS)
}

View File

@ -2,16 +2,15 @@ import Web3 from 'web3'
import { networkAddresses } from './addresses'
import helpers from './helpers'
import { toAscii } from '../helpers'
import { constants } from '../constants'
export default class ValidatorMetadata {
async init({ web3, netId }) {
const { METADATA_ADDRESS } = networkAddresses(netId)
const { METADATA_ADDRESS } = networkAddresses()
console.log('Metadata address', METADATA_ADDRESS)
const web3_10 = new Web3(web3.currentProvider)
const branch = helpers.getBranch(netId)
const MetadataAbi = await helpers.getABI(branch, 'ValidatorMetadata')
const MetadataAbi = await helpers.getABI(constants.NETWORKS[netId].BRANCH, 'ValidatorMetadata')
this.metadataInstance = new web3_10.eth.Contract(MetadataAbi, METADATA_ADDRESS)
}

View File

@ -1,16 +1,15 @@
import Web3 from 'web3'
import { networkAddresses } from './addresses'
import helpers from './helpers'
import { constants } from '../constants'
export default class VotingToChangeKeys {
async init({ web3, netId }) {
const { VOTING_TO_CHANGE_KEYS_ADDRESS } = networkAddresses(netId)
const { VOTING_TO_CHANGE_KEYS_ADDRESS } = networkAddresses()
console.log('VotingToChangeKeys address', VOTING_TO_CHANGE_KEYS_ADDRESS)
const web3_10 = new Web3(web3.currentProvider)
const branch = helpers.getBranch(netId)
const votingToChangeKeysABI = await helpers.getABI(branch, 'VotingToChangeKeys')
const votingToChangeKeysABI = await helpers.getABI(constants.NETWORKS[netId].BRANCH, 'VotingToChangeKeys')
this.votingToChangeKeysInstance = new web3_10.eth.Contract(votingToChangeKeysABI, VOTING_TO_CHANGE_KEYS_ADDRESS)
this.gasPrice = web3_10.utils.toWei('1', 'gwei')

View File

@ -1,16 +1,18 @@
import Web3 from 'web3'
import { networkAddresses } from './addresses'
import helpers from './helpers'
import { constants } from '../constants'
export default class VotingToChangeMinThreshold {
async init({ web3, netId }) {
const { VOTING_TO_CHANGE_MIN_THRESHOLD_ADDRESS } = networkAddresses(netId)
const { VOTING_TO_CHANGE_MIN_THRESHOLD_ADDRESS } = networkAddresses()
console.log('VotingToChangeMinThreshold address', VOTING_TO_CHANGE_MIN_THRESHOLD_ADDRESS)
const web3_10 = new Web3(web3.currentProvider)
const branch = helpers.getBranch(netId)
const votingToChangeMinThresholdABI = await helpers.getABI(branch, 'VotingToChangeMinThreshold')
const votingToChangeMinThresholdABI = await helpers.getABI(
constants.NETWORKS[netId].BRANCH,
'VotingToChangeMinThreshold'
)
this.votingToChangeMinThresholdInstance = new web3_10.eth.Contract(
votingToChangeMinThresholdABI,

View File

@ -1,16 +1,15 @@
import Web3 from 'web3'
import { networkAddresses } from './addresses'
import helpers from './helpers'
import { constants } from '../constants'
export default class VotingToChangeProxy {
async init({ web3, netId }) {
const { VOTING_TO_CHANGE_PROXY_ADDRESS } = networkAddresses(netId)
const { VOTING_TO_CHANGE_PROXY_ADDRESS } = networkAddresses()
console.log('VotingToChangeProxy address', VOTING_TO_CHANGE_PROXY_ADDRESS)
const web3_10 = new Web3(web3.currentProvider)
const branch = helpers.getBranch(netId)
const votingToChangeProxyABI = await helpers.getABI(branch, 'VotingToChangeProxyAddress')
const votingToChangeProxyABI = await helpers.getABI(constants.NETWORKS[netId].BRANCH, 'VotingToChangeProxyAddress')
this.votingToChangeProxyInstance = new web3_10.eth.Contract(votingToChangeProxyABI, VOTING_TO_CHANGE_PROXY_ADDRESS)
this.gasPrice = web3_10.utils.toWei('1', 'gwei')

View File

@ -1,16 +1,18 @@
import Web3 from 'web3'
import { networkAddresses } from './addresses'
import helpers from './helpers'
import { constants } from '../constants'
export default class VotingToManageEmissionFunds {
async init({ web3, netId }) {
const { VOTING_TO_MANAGE_EMISSION_FUNDS_ADDRESS } = networkAddresses(netId)
const { VOTING_TO_MANAGE_EMISSION_FUNDS_ADDRESS } = networkAddresses()
console.log('VotingToManageEmissionFunds address', VOTING_TO_MANAGE_EMISSION_FUNDS_ADDRESS)
const web3_10 = new Web3(web3.currentProvider)
const branch = helpers.getBranch(netId)
const votingToManageEmissionFundsABI = await helpers.getABI(branch, 'VotingToManageEmissionFunds')
const votingToManageEmissionFundsABI = await helpers.getABI(
constants.NETWORKS[netId].BRANCH,
'VotingToManageEmissionFunds'
)
this.instance = new web3_10.eth.Contract(votingToManageEmissionFundsABI, VOTING_TO_MANAGE_EMISSION_FUNDS_ADDRESS)
this.address = VOTING_TO_MANAGE_EMISSION_FUNDS_ADDRESS

View File

@ -1,4 +1,3 @@
import { constants } from '../constants'
import { addressesURL, wrongRepoAlert } from './helpers'
// const local = {
// VOTING_TO_CHANGE_KEYS_ADDRESS: '0xecdbe3937cf6ff27f70480855cfe03254f915b48',
@ -9,13 +8,10 @@ import { addressesURL, wrongRepoAlert } from './helpers'
// POA_ADDRESS: '0xf472e0e43570b9afaab67089615080cf7c20018d',
// }
let SOKOL_ADDRESSES = {}
let CORE_ADDRESSES = {}
let DAI_TEST_ADDRESSES = {}
let DAI_ADDRESSES = {}
let ADDRESSES = {}
async function getContractsAddresses(branch) {
let addr = addressesURL(branch)
const addr = addressesURL(branch)
let response
try {
response = await fetch(addr)
@ -23,43 +19,16 @@ async function getContractsAddresses(branch) {
return wrongRepoAlert(addr)
}
let contracts = await response.json()
const contracts = await response.json()
switch (branch) {
case 'core':
CORE_ADDRESSES = contracts
break
case 'dai':
DAI_ADDRESSES = contracts
break
case 'sokol':
SOKOL_ADDRESSES = contracts
break
case 'dai-test':
DAI_TEST_ADDRESSES = contracts
break
default:
CORE_ADDRESSES = contracts
break
}
ADDRESSES = contracts
}
function getAddresses(netId) {
switch (netId) {
case constants.NETID_SOKOL:
return SOKOL_ADDRESSES
case constants.NETID_DAI_TEST:
return DAI_TEST_ADDRESSES
case constants.NETID_CORE:
return CORE_ADDRESSES
case constants.NETID_DAI:
return DAI_ADDRESSES
default:
return CORE_ADDRESSES
}
function networkAddresses() {
return ADDRESSES
}
module.exports = {
getContractsAddresses,
networkAddresses: getAddresses
networkAddresses
}

View File

@ -27,25 +27,9 @@ function wrongRepoAlert(addr) {
swal('Error!', messages.wrongRepo(addr), 'error')
}
function getBranch(netId) {
switch (netId) {
case constants.NETID_SOKOL:
return 'sokol'
case constants.NETID_DAI_TEST:
return 'dai-test'
case constants.NETID_CORE:
return 'core'
case constants.NETID_DAI:
return 'dai'
default:
return 'core'
}
}
module.exports = {
addressesURL,
ABIURL,
getABI,
wrongRepoAlert,
getBranch
wrongRepoAlert
}

View File

@ -5,8 +5,7 @@ let getWeb3 = () => {
return new Promise((resolve, reject) => {
// Wait for loading completion to avoid race conditions with web3 injection timing.
window.addEventListener('load', async () => {
let results
let web3 = window.web3
let web3
// Checking if Web3 has been injected by the browser (Mist/MetaMask)
if (window.ethereum) {
@ -19,8 +18,8 @@ let getWeb3 = () => {
reject({ message: messages.USER_DENIED_ACCOUNT_ACCESS })
return
}
} else if (typeof web3 !== 'undefined') {
web3 = new window.Web3(web3.currentProvider)
} else if (typeof window.web3 !== 'undefined') {
web3 = new window.Web3(window.web3.currentProvider)
console.log('Injected web3 detected.')
} else {
console.error('Metamask not found')
@ -28,50 +27,40 @@ let getWeb3 = () => {
return
}
let errorMsg = null
web3.version.getNetwork((err, netId) => {
let netIdName
let errorMsg = null
console.log('netId', netId)
switch (netId) {
case constants.NETID_DAI:
netIdName = 'Dai'
console.log('This is Dai', netId)
break
case constants.NETID_CORE:
netIdName = 'Core'
console.log('This is Core', netId)
break
case constants.NETID_DAI_TEST:
netIdName = 'Dai-Test'
console.log('This is Dai-Test', netId)
break
case constants.NETID_SOKOL:
netIdName = 'Sokol'
console.log('This is Sokol', netId)
break
default:
netIdName = 'ERROR'
errorMsg = messages.WRONG_NETWORK_MSG
console.log('This is an unknown network.', netId)
if (netId in constants.NETWORKS) {
netIdName = constants.NETWORKS[netId].NAME
console.log(`This is ${netIdName}`)
} else {
netIdName = 'ERROR'
errorMsg = messages.WRONG_NETWORK_MSG
console.log('This is an unknown network.')
}
document.title = `${netIdName} - POA Network Governance DApp`
if (errorMsg !== null) {
reject({ message: errorMsg })
return
}
var defaultAccount = web3.eth.defaultAccount || null
if (defaultAccount === null) {
reject({ message: messages.NO_METAMASK_MSG })
return
}
results = {
resolve({
web3Instance: web3,
netIdName,
netId,
injectedWeb3: true,
defaultAccount
}
resolve(results)
})
})
})
})

View File

@ -59,12 +59,7 @@ function sendTransactionByVotingKey(props, to, data, cb, warning) {
)
}
function isTestnet(netId) {
return netId === constants.NETID_SOKOL || netId === constants.NETID_DAI_TEST
}
module.exports = {
toAscii,
sendTransactionByVotingKey,
isTestnet
sendTransactionByVotingKey
}

View File

@ -11,7 +11,6 @@ import ballotStore from './stores/BallotStore'
import ballotsStore from './stores/BallotsStore'
import contractsStore from './stores/ContractsStore'
import { getContractsAddresses } from './contracts/addresses'
import { getBranch } from './contracts/helpers'
import swal from 'sweetalert2'
import getWeb3 from './getWeb3'
import 'babel-polyfill'
@ -35,7 +34,7 @@ class AppMainRouter extends Component {
getWeb3()
.then(async web3Config => {
await getContractsAddresses(getBranch(web3Config.netId))
await getContractsAddresses(constants.NETWORKS[web3Config.netId].BRANCH)
contractsStore.setWeb3Instance(web3Config)
@ -48,7 +47,7 @@ class AppMainRouter extends Component {
const setVotingToChangeProxy = contractsStore.setVotingToChangeProxy(web3Config)
const setValidatorMetadata = contractsStore.setValidatorMetadata(web3Config)
const promises = [
let promises = [
setPoaConsensus,
setBallotsStorage,
setKeysManager,
@ -59,8 +58,9 @@ class AppMainRouter extends Component {
setValidatorMetadata
]
if (web3Config.netId === constants.NETID_SOKOL) {
// if we're in Sokol
const networkName = constants.NETWORKS[web3Config.netId].NAME.toLowerCase()
if (networkName === 'core' || networkName === 'sokol') {
// if we're in Core or Sokol
promises.push(contractsStore.setEmissionFunds(web3Config))
promises.push(contractsStore.setVotingToManageEmissionFunds(web3Config))
}