2019-01-07 12:45:51 -08:00
|
|
|
import AllValidators from './components/AllValidators'
|
2018-07-04 16:10:26 -07:00
|
|
|
import App from './App'
|
2017-12-11 17:35:57 -08:00
|
|
|
import KeysManager from './contracts/KeysManager.contract'
|
2018-07-03 18:54:51 -07:00
|
|
|
import Metadata from './contracts/Metadata.contract'
|
2019-01-07 10:43:51 -08:00
|
|
|
import ProofOfPhysicalAddress from './contracts/ProofOfPhysicalAddress.contract'
|
2018-07-04 16:10:26 -07:00
|
|
|
import React, { Component } from 'react'
|
|
|
|
import ReactDOM from 'react-dom'
|
2018-07-03 18:54:51 -07:00
|
|
|
import createBrowserHistory from 'history/createBrowserHistory'
|
2019-01-07 10:43:51 -08:00
|
|
|
import getWeb3, { setWeb3 } from './utils/getWeb3'
|
|
|
|
import helpers from './utils/helpers'
|
2018-07-04 16:10:26 -07:00
|
|
|
import networkAddresses from './contracts/addresses'
|
2019-01-07 10:43:51 -08:00
|
|
|
import registerServiceWorker from './utils/registerServiceWorker'
|
2019-01-07 12:45:51 -08:00
|
|
|
import { BaseLoader } from './components/BaseLoader'
|
2019-01-09 09:42:32 -08:00
|
|
|
import { ButtonConfirm } from './components/ButtonConfirm'
|
|
|
|
import { ButtonFinalize } from './components/ButtonFinalize'
|
2019-01-07 12:45:51 -08:00
|
|
|
import { Footer } from './components/Footer'
|
|
|
|
import { Header } from './components/Header'
|
|
|
|
import { Loading } from './components/Loading'
|
2019-01-08 10:43:41 -08:00
|
|
|
import { Router, Route, Redirect } from 'react-router-dom'
|
2019-01-07 12:45:51 -08:00
|
|
|
import { SearchBar } from './components/SearchBar'
|
2019-01-07 10:43:51 -08:00
|
|
|
import { constants } from './utils/constants'
|
2019-01-07 12:45:51 -08:00
|
|
|
import { getNetworkBranch } from './utils/utils'
|
2019-01-07 10:43:51 -08:00
|
|
|
import { messages } from './utils/messages'
|
2018-01-23 13:32:28 -08:00
|
|
|
|
2019-01-07 12:45:51 -08:00
|
|
|
import 'react-select/dist/react-select.css'
|
|
|
|
|
2017-12-11 23:01:59 -08:00
|
|
|
const history = createBrowserHistory()
|
2018-07-17 14:38:28 -07:00
|
|
|
const baseRootPath = '/poa-dapps-validators'
|
2018-10-15 11:36:13 -07:00
|
|
|
const setMetadataPath = `${baseRootPath}/set`
|
|
|
|
const pendingChangesPath = `${baseRootPath}/pending-changes`
|
2018-07-04 04:40:25 -07:00
|
|
|
const navigationData = [
|
|
|
|
{
|
2018-07-17 14:38:28 -07:00
|
|
|
icon: 'link-icon-all',
|
|
|
|
title: 'All',
|
|
|
|
url: baseRootPath
|
2018-07-04 04:40:25 -07:00
|
|
|
},
|
|
|
|
{
|
2018-07-17 14:38:28 -07:00
|
|
|
icon: 'link-icon-set-metadata',
|
|
|
|
title: 'Set Metadata',
|
2018-10-15 11:36:13 -07:00
|
|
|
url: setMetadataPath
|
2018-07-04 04:40:25 -07:00
|
|
|
},
|
|
|
|
{
|
2018-07-17 14:38:28 -07:00
|
|
|
icon: 'link-icon-pending-changes',
|
|
|
|
title: 'Pending Changes',
|
2018-10-15 11:36:13 -07:00
|
|
|
url: pendingChangesPath
|
2018-07-17 14:38:28 -07:00
|
|
|
}
|
|
|
|
]
|
2018-07-04 04:40:25 -07:00
|
|
|
|
2017-12-11 23:01:59 -08:00
|
|
|
class AppMainRouter extends Component {
|
2018-07-03 18:54:51 -07:00
|
|
|
constructor(props) {
|
2018-07-17 14:38:28 -07:00
|
|
|
super(props)
|
2018-07-03 18:54:51 -07:00
|
|
|
|
2018-07-17 14:38:28 -07:00
|
|
|
history.listen(this.onRouteChange.bind(this))
|
2018-07-04 16:10:26 -07:00
|
|
|
|
2018-07-17 14:38:28 -07:00
|
|
|
this.onSetRender = this.onSetRender.bind(this)
|
|
|
|
this.onPendingChangesRender = this.onPendingChangesRender.bind(this)
|
2017-12-11 23:01:59 -08:00
|
|
|
this.onAllValidatorsRender = this.onAllValidatorsRender.bind(this)
|
2018-07-17 14:38:28 -07:00
|
|
|
this.onConfirmPendingChange = this.onConfirmPendingChange.bind(this)
|
|
|
|
this.onFinalize = this.onFinalize.bind(this)
|
|
|
|
this.onSearch = this.onSearch.bind(this)
|
|
|
|
this.onNetworkChange = this.onNetworkChange.bind(this)
|
|
|
|
this.toggleMobileMenu = this.toggleMobileMenu.bind(this)
|
2018-07-18 11:17:09 -07:00
|
|
|
this.getNetIdClass = this.getNetIdClass.bind(this)
|
2018-07-04 16:10:26 -07:00
|
|
|
|
2017-12-11 23:01:59 -08:00
|
|
|
this.state = {
|
2018-10-15 11:36:13 -07:00
|
|
|
showSearch: history.location.pathname !== setMetadataPath,
|
2018-06-29 00:54:14 -07:00
|
|
|
keysManager: null,
|
2017-12-11 23:01:59 -08:00
|
|
|
metadataContract: null,
|
|
|
|
poaConsensus: null,
|
2018-06-29 00:54:14 -07:00
|
|
|
votingKey: null,
|
2018-08-07 01:53:06 -07:00
|
|
|
miningKey: null,
|
2017-12-12 00:29:28 -08:00
|
|
|
loading: true,
|
2019-01-08 10:43:41 -08:00
|
|
|
childLoading: true,
|
2017-12-29 22:27:13 -08:00
|
|
|
searchTerm: '',
|
2018-01-25 01:26:29 -08:00
|
|
|
injectedWeb3: true,
|
2018-01-25 00:26:31 -08:00
|
|
|
netId: '',
|
2018-07-03 18:54:51 -07:00
|
|
|
error: false,
|
2018-07-04 09:37:52 -07:00
|
|
|
title: navigationData[0].title,
|
|
|
|
showMobileMenu: false
|
2017-12-11 23:01:59 -08:00
|
|
|
}
|
2018-07-17 14:38:28 -07:00
|
|
|
getWeb3()
|
|
|
|
.then(async web3Config => {
|
|
|
|
return networkAddresses(web3Config)
|
2018-02-01 14:48:48 -08:00
|
|
|
})
|
2018-07-17 14:38:28 -07:00
|
|
|
.then(async config => {
|
|
|
|
const { web3Config, addresses } = config
|
2018-10-11 00:08:49 -07:00
|
|
|
await this.initContracts({
|
2018-07-17 14:38:28 -07:00
|
|
|
web3: web3Config.web3Instance,
|
|
|
|
netId: web3Config.netId,
|
|
|
|
addresses
|
|
|
|
})
|
|
|
|
this.setState({
|
|
|
|
votingKey: web3Config.defaultAccount,
|
2018-10-10 08:55:10 -07:00
|
|
|
miningKey: await this.state.keysManager.miningKeyByVoting(web3Config.defaultAccount),
|
|
|
|
injectedWeb3: web3Config.injectedWeb3
|
2018-07-17 14:38:28 -07:00
|
|
|
})
|
2017-12-11 23:01:59 -08:00
|
|
|
})
|
2018-07-17 14:38:28 -07:00
|
|
|
.catch(error => {
|
|
|
|
console.error(error.message)
|
|
|
|
this.setState({ loading: false, error: true })
|
|
|
|
helpers.generateAlert('error', 'Error!', error.message)
|
2017-12-11 23:01:59 -08:00
|
|
|
})
|
|
|
|
}
|
2018-10-11 00:08:49 -07:00
|
|
|
async initContracts({ web3, netId, addresses }) {
|
2018-10-10 08:55:10 -07:00
|
|
|
const keysManager = new KeysManager()
|
|
|
|
await keysManager.init({
|
|
|
|
web3,
|
|
|
|
netId,
|
|
|
|
addresses
|
|
|
|
})
|
|
|
|
const metadataContract = new Metadata()
|
|
|
|
await metadataContract.init({
|
|
|
|
web3,
|
|
|
|
netId,
|
|
|
|
addresses
|
|
|
|
})
|
|
|
|
let proofOfPhysicalAddressContract = new ProofOfPhysicalAddress()
|
|
|
|
try {
|
|
|
|
await proofOfPhysicalAddressContract.init({
|
|
|
|
web3,
|
|
|
|
netId,
|
|
|
|
addresses
|
|
|
|
})
|
|
|
|
} catch (e) {
|
|
|
|
console.error('Error initializing ProofOfPhysicalAddress', e)
|
|
|
|
proofOfPhysicalAddressContract = null
|
|
|
|
}
|
|
|
|
this.setState({
|
|
|
|
keysManager,
|
|
|
|
metadataContract,
|
|
|
|
proofOfPhysicalAddressContract,
|
|
|
|
loading: false,
|
|
|
|
netId
|
|
|
|
})
|
|
|
|
}
|
2018-07-03 18:54:51 -07:00
|
|
|
onRouteChange() {
|
2018-10-15 11:36:13 -07:00
|
|
|
if (history.location.pathname === setMetadataPath) {
|
2018-07-03 18:54:51 -07:00
|
|
|
this.setState({ showSearch: false })
|
2018-07-04 04:40:25 -07:00
|
|
|
|
2018-07-03 18:54:51 -07:00
|
|
|
if (this.state.injectedWeb3 === false) {
|
2018-07-17 14:38:28 -07:00
|
|
|
helpers.generateAlert('warning', 'Warning!', 'Metamask was not found')
|
2017-12-29 22:27:13 -08:00
|
|
|
}
|
2017-12-11 23:01:59 -08:00
|
|
|
} else {
|
2018-07-03 18:54:51 -07:00
|
|
|
this.setState({ showSearch: true })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
checkForVotingKey(cb) {
|
|
|
|
if (this.state.votingKey && !this.state.loading) {
|
2018-07-17 14:38:28 -07:00
|
|
|
return cb()
|
2018-01-23 13:32:28 -08:00
|
|
|
}
|
2018-07-17 14:38:28 -07:00
|
|
|
helpers.generateAlert('warning', 'Warning!', messages.noMetamaskAccount)
|
|
|
|
return ''
|
2018-01-23 13:32:28 -08:00
|
|
|
}
|
2018-07-04 09:37:52 -07:00
|
|
|
toggleMobileMenu = () => {
|
|
|
|
this.setState(prevState => ({ showMobileMenu: !prevState.showMobileMenu }))
|
|
|
|
}
|
2018-07-03 18:54:51 -07:00
|
|
|
async _onBtnClick({ event, methodToCall, successMsg }) {
|
2018-07-17 14:38:28 -07:00
|
|
|
event.preventDefault()
|
2018-01-23 13:32:28 -08:00
|
|
|
this.checkForVotingKey(async () => {
|
2018-07-03 18:54:51 -07:00
|
|
|
this.setState({ loading: true })
|
2018-07-17 14:38:28 -07:00
|
|
|
const miningKey = event.currentTarget.getAttribute('miningkey')
|
2018-07-03 18:54:51 -07:00
|
|
|
try {
|
2018-01-23 13:32:28 -08:00
|
|
|
let result = await this.state.metadataContract[methodToCall]({
|
|
|
|
miningKeyToConfirm: miningKey,
|
2018-08-07 01:53:06 -07:00
|
|
|
senderVotingKey: this.state.votingKey,
|
|
|
|
senderMiningKey: this.state.miningKey
|
2018-07-17 14:38:28 -07:00
|
|
|
})
|
|
|
|
console.log(result)
|
2018-07-03 18:54:51 -07:00
|
|
|
this.setState({ loading: false })
|
2018-07-17 14:38:28 -07:00
|
|
|
helpers.generateAlert('success', 'Congratulations!', successMsg)
|
2018-07-03 18:54:51 -07:00
|
|
|
} catch (error) {
|
|
|
|
this.setState({ loading: false })
|
2018-07-17 14:38:28 -07:00
|
|
|
console.error(error.message)
|
|
|
|
helpers.generateAlert('error', 'Error!', error.message)
|
2018-01-23 13:32:28 -08:00
|
|
|
}
|
|
|
|
})
|
2017-12-12 23:27:31 -08:00
|
|
|
}
|
2017-12-13 16:39:17 -08:00
|
|
|
async onConfirmPendingChange(event) {
|
|
|
|
await this._onBtnClick({
|
|
|
|
event,
|
|
|
|
methodToCall: 'confirmPendingChange',
|
|
|
|
successMsg: 'You have successfully confirmed the change!'
|
2018-07-17 14:38:28 -07:00
|
|
|
})
|
2017-12-13 16:39:17 -08:00
|
|
|
}
|
2018-07-03 18:54:51 -07:00
|
|
|
async onFinalize(event) {
|
2017-12-13 16:39:17 -08:00
|
|
|
await this._onBtnClick({
|
|
|
|
event,
|
|
|
|
methodToCall: 'finalize',
|
|
|
|
successMsg: 'You have successfully finalized the change!'
|
2018-07-17 14:38:28 -07:00
|
|
|
})
|
2017-12-13 16:39:17 -08:00
|
|
|
}
|
2017-12-12 23:27:31 -08:00
|
|
|
onPendingChangesRender() {
|
2019-01-09 09:42:32 -08:00
|
|
|
const networkBranch = this.getValidatorsNetworkBranch()
|
2019-01-07 12:45:51 -08:00
|
|
|
return this.state.loading || this.state.error ? null : (
|
2018-07-17 14:38:28 -07:00
|
|
|
<AllValidators
|
|
|
|
methodToCall="getAllPendingChanges"
|
2019-01-08 10:43:41 -08:00
|
|
|
onLoadingChange={this.onChildLoadingChange}
|
|
|
|
ref="AllValidatorsRef"
|
2018-07-17 14:38:28 -07:00
|
|
|
searchTerm={this.state.searchTerm}
|
2018-07-18 11:17:09 -07:00
|
|
|
viewTitle={navigationData[2]['title']}
|
2019-01-08 10:43:41 -08:00
|
|
|
web3Config={this.state}
|
2018-07-17 14:38:28 -07:00
|
|
|
>
|
2019-01-09 09:42:32 -08:00
|
|
|
<ButtonFinalize networkBranch={networkBranch} onClick={this.onFinalize} />
|
|
|
|
<ButtonConfirm networkBranch={networkBranch} onClick={this.onConfirmPendingChange} />
|
2018-07-17 14:38:28 -07:00
|
|
|
</AllValidators>
|
|
|
|
)
|
2017-12-12 23:27:31 -08:00
|
|
|
}
|
2017-12-11 23:01:59 -08:00
|
|
|
onAllValidatorsRender() {
|
2019-01-08 10:43:41 -08:00
|
|
|
return this.state.loading || this.state.error ? null : (
|
2018-07-18 11:17:09 -07:00
|
|
|
<AllValidators
|
|
|
|
methodToCall="getAllValidatorsData"
|
2019-01-08 10:43:41 -08:00
|
|
|
onLoadingChange={this.onChildLoadingChange}
|
2019-01-07 12:45:51 -08:00
|
|
|
searchTerm={this.state.searchTerm}
|
2018-07-18 11:17:09 -07:00
|
|
|
viewTitle={navigationData[0]['title']}
|
2019-01-07 12:45:51 -08:00
|
|
|
web3Config={this.state}
|
2018-07-18 11:17:09 -07:00
|
|
|
/>
|
2018-07-17 14:38:28 -07:00
|
|
|
)
|
2017-12-13 02:01:21 -08:00
|
|
|
}
|
2018-07-18 11:17:09 -07:00
|
|
|
getNetIdClass() {
|
2018-10-10 08:55:10 -07:00
|
|
|
const { netId } = this.state
|
|
|
|
if (netId in constants.NETWORKS) {
|
|
|
|
return constants.NETWORKS[netId].TESTNET ? 'sokol' : ''
|
|
|
|
}
|
|
|
|
return ''
|
2018-07-18 11:17:09 -07:00
|
|
|
}
|
2018-07-03 18:54:51 -07:00
|
|
|
onSearch(term) {
|
|
|
|
this.setState({ searchTerm: term.target.value.toLowerCase() })
|
2017-12-11 23:01:59 -08:00
|
|
|
}
|
2018-07-03 18:54:51 -07:00
|
|
|
async onNetworkChange(e) {
|
2018-10-10 08:55:10 -07:00
|
|
|
this.setState({ loading: true })
|
|
|
|
|
2018-07-17 14:38:28 -07:00
|
|
|
const netId = e.value
|
|
|
|
const web3 = setWeb3(netId)
|
2018-07-04 16:10:26 -07:00
|
|
|
|
2018-07-17 14:38:28 -07:00
|
|
|
networkAddresses({ netId }).then(async config => {
|
|
|
|
const { addresses } = config
|
2018-10-11 00:08:49 -07:00
|
|
|
await this.initContracts({ web3, netId, addresses })
|
2018-01-25 00:02:25 -08:00
|
|
|
})
|
|
|
|
}
|
2019-01-07 12:45:51 -08:00
|
|
|
getValidatorsNetworkBranch = () => {
|
|
|
|
return this.state.netId ? getNetworkBranch(this.state.netId) : null
|
|
|
|
}
|
|
|
|
onSetRender() {
|
|
|
|
if (!this.state.votingKey) {
|
|
|
|
return null // prevent rendering if the keys are not loaded yet
|
|
|
|
}
|
|
|
|
return <App web3Config={this.state} viewTitle={navigationData[1]['title']} />
|
|
|
|
}
|
2019-01-08 10:43:41 -08:00
|
|
|
onChildLoadingChange = (isLoading = true) => {
|
|
|
|
if (!isLoading) {
|
|
|
|
this.setState({ childLoading: false })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-03 18:54:51 -07:00
|
|
|
render() {
|
2019-01-07 12:45:51 -08:00
|
|
|
const networkBranch = this.getValidatorsNetworkBranch()
|
2018-07-03 18:54:51 -07:00
|
|
|
|
2019-01-07 12:45:51 -08:00
|
|
|
return networkBranch ? (
|
2018-07-17 14:38:28 -07:00
|
|
|
<Router history={history}>
|
2019-01-07 12:45:51 -08:00
|
|
|
<div
|
|
|
|
className={`lo-App ${!this.state.showSearch ? 'lo-App-no-search-bar' : ''} ${
|
|
|
|
this.state.showMobileMenu ? 'lo-App-menu-open' : ''
|
|
|
|
}`}
|
|
|
|
>
|
2019-01-08 10:43:41 -08:00
|
|
|
{this.state.loading || this.state.childLoading ? <Loading networkBranch={networkBranch} /> : null}
|
2018-07-04 09:37:52 -07:00
|
|
|
<Header
|
2018-07-18 11:17:09 -07:00
|
|
|
baseRootPath={baseRootPath}
|
2019-01-07 12:45:51 -08:00
|
|
|
networkBranch={networkBranch}
|
2018-07-17 14:38:28 -07:00
|
|
|
onChange={this.onNetworkChange}
|
|
|
|
onMenuToggle={this.toggleMobileMenu}
|
2018-07-18 11:17:09 -07:00
|
|
|
showMobileMenu={this.state.showMobileMenu}
|
2018-07-04 09:37:52 -07:00
|
|
|
/>
|
2019-01-07 12:45:51 -08:00
|
|
|
{this.state.showSearch ? <SearchBar networkBranch={networkBranch} onSearch={this.onSearch} /> : null}
|
|
|
|
<section
|
|
|
|
className={`lo-App_Content lo-App_Content-${networkBranch} ${
|
|
|
|
this.state.showMobileMenu ? 'lo-App_Content-mobile-menu-open' : ''
|
|
|
|
}`}
|
2018-07-17 14:38:28 -07:00
|
|
|
>
|
2019-01-08 10:43:41 -08:00
|
|
|
<Route
|
|
|
|
exact
|
|
|
|
path={`/`}
|
|
|
|
render={props => (
|
|
|
|
<Redirect
|
|
|
|
to={{
|
|
|
|
pathname: baseRootPath
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
/>
|
2018-10-15 11:36:13 -07:00
|
|
|
<Route exact path={baseRootPath} render={this.onAllValidatorsRender} web3Config={this.state} />
|
2019-01-08 10:43:41 -08:00
|
|
|
<Route exact path={pendingChangesPath} render={this.onPendingChangesRender} />
|
|
|
|
<Route exact path={setMetadataPath} render={this.onSetRender} />
|
2019-01-07 12:45:51 -08:00
|
|
|
</section>
|
|
|
|
<Footer baseRootPath={baseRootPath} networkBranch={networkBranch} />
|
|
|
|
</div>
|
2017-12-11 23:01:59 -08:00
|
|
|
</Router>
|
2019-01-07 12:45:51 -08:00
|
|
|
) : (
|
|
|
|
<BaseLoader />
|
2017-12-11 23:01:59 -08:00
|
|
|
)
|
|
|
|
}
|
2018-01-31 13:24:52 -08:00
|
|
|
}
|
2017-12-11 17:35:57 -08:00
|
|
|
|
2018-07-17 14:38:28 -07:00
|
|
|
ReactDOM.render(<AppMainRouter />, document.getElementById('root'))
|
|
|
|
registerServiceWorker()
|