From 8f7279519ec4c419aaef8d52dd35cf21b1f8f9a4 Mon Sep 17 00:00:00 2001 From: Vadim Date: Thu, 9 Jan 2020 16:02:58 +0300 Subject: [PATCH] Improvements and optimizations --- src/App.js | 2 +- src/components/BallotCard/index.js | 74 +++++------- src/components/BallotKeysMetadata/index.js | 2 +- src/components/NewBallot/index.js | 2 +- src/components/NewBallotMenuInfo/index.js | 6 +- src/contracts/VotingToChangeKeys.contract.js | 4 +- .../VotingToChangeMinThreshold.contract.js | 4 +- src/contracts/VotingToChangeProxy.contract.js | 4 +- src/index.js | 16 +-- src/stores/ContractsStore.js | 112 ++++++++++-------- src/utils/getWeb3.js | 9 +- 11 files changed, 109 insertions(+), 126 deletions(-) diff --git a/src/App.js b/src/App.js index 3d7f82d..31d1289 100644 --- a/src/App.js +++ b/src/App.js @@ -57,7 +57,7 @@ class App extends Component { html: messages.networkMatchError(contractsStore.netId), type: 'warning' }) - } else if (contractsStore.votingKey && !contractsStore.isValidVotingKey) { + } else if (!contractsStore.isValidVotingKey) { swal({ title: 'Warning!', html: messages.invalidVotingKeyMsg(contractsStore.votingKey), diff --git a/src/components/BallotCard/index.js b/src/components/BallotCard/index.js index 144b16c..f4a76fd 100644 --- a/src/components/BallotCard/index.js +++ b/src/components/BallotCard/index.js @@ -375,6 +375,27 @@ export class BallotCard extends React.Component { this.quorumState = await this.repeatGetProperty(contractsStore, votingType, id, 'getQuorumState', 0) } + networkAndKeyValidation = async () => { + const { contractsStore } = this.props + try { + await enableWallet(contractsStore.updateKeys) + } catch (error) { + swal('Error', error.message, 'error') + return false + } + if (contractsStore.isEmptyVotingKey) { + swal('Warning!', messages.NO_METAMASK_MSG, 'warning') + return false + } else if (!contractsStore.networkMatch) { + swal('Warning!', messages.networkMatchError(contractsStore.netId), 'warning') + return false + } else if (!contractsStore.isValidVotingKey) { + swal('Warning!', messages.invalidVotingKeyMsg(contractsStore.votingKey), 'warning') + return false + } + return true + } + vote = async ({ choice }) => { if (this.isCanceled) { swal('Warning!', messages.INVALID_VOTE_MSG, 'warning') @@ -385,26 +406,12 @@ export class BallotCard extends React.Component { return } + if (!(await this.networkAndKeyValidation())) { + return + } + const { commonStore, contractsStore, id, votingType, ballotsStore, pos } = this.props - - try { - await enableWallet(contractsStore.updateKeys) - } catch (error) { - swal('Error', error.message, 'error') - return - } - const { push } = this.props.routing - if (!contractsStore.votingKey) { - swal('Warning!', messages.NO_METAMASK_MSG, 'warning') - return - } else if (!contractsStore.networkMatch) { - swal('Warning!', messages.networkMatchError(contractsStore.netId), 'warning') - return - } else if (!contractsStore.isValidVotingKey) { - swal('Warning!', messages.invalidVotingKeyMsg(contractsStore.votingKey), 'warning') - return - } commonStore.showLoading() let isValidVote = await this.isValidVote() if (!isValidVote) { @@ -484,21 +491,7 @@ export class BallotCard extends React.Component { const { push } = this.props.routing const contract = this.getContract(contractsStore, votingType) - try { - await enableWallet(contractsStore.updateKeys) - } catch (error) { - swal('Error', error.message, 'error') - return - } - - if (!contractsStore.votingKey) { - swal('Warning!', messages.NO_METAMASK_MSG, 'warning') - return - } else if (!contractsStore.networkMatch) { - swal('Warning!', messages.networkMatchError(contractsStore.netId), 'warning') - return - } else if (!contractsStore.isValidVotingKey) { - swal('Warning!', messages.invalidVotingKeyMsg(contractsStore.votingKey), 'warning') + if (!(await this.networkAndKeyValidation())) { return } @@ -555,23 +548,10 @@ export class BallotCard extends React.Component { const { commonStore, contractsStore, id, votingType, ballotsStore, pos } = this.props const { push } = this.props.routing - try { - await enableWallet(contractsStore.updateKeys) - } catch (error) { - swal('Error', error.message, 'error') + if (!(await this.networkAndKeyValidation())) { return } - if (!contractsStore.votingKey) { - swal('Warning!', messages.NO_METAMASK_MSG, 'warning') - return - } else if (!contractsStore.networkMatch) { - swal('Warning!', messages.networkMatchError(contractsStore.netId), 'warning') - return - } else if (!contractsStore.isValidVotingKey) { - swal('Warning!', messages.invalidVotingKeyMsg(contractsStore.votingKey), 'warning') - return - } if (this.isFinalized) { swal('Warning!', messages.ALREADY_FINALIZED_MSG, 'warning') return diff --git a/src/components/BallotKeysMetadata/index.js b/src/components/BallotKeysMetadata/index.js index 2015263..3d1bccb 100644 --- a/src/components/BallotKeysMetadata/index.js +++ b/src/components/BallotKeysMetadata/index.js @@ -45,7 +45,7 @@ export class BallotKeysMetadata extends React.Component { Minimum {minThreshold} from {validatorsLength} validators are required to pass the  proposal -
  • You can create {keys} ballot(s) for keys
  • +
  • You can create {Number(keys)} ballot(s) for keys
  • - You can create {validatorLimitsMinThreshold} ballot(s) for consensus + You can create {Number(validatorLimitsMinThreshold)} ballot(s) for consensus
  • -
  • You can create {proxy} ballot(s) for proxy
  • +
  • You can create {Number(proxy)} ballot(s) for proxy
  • ) diff --git a/src/contracts/VotingToChangeKeys.contract.js b/src/contracts/VotingToChangeKeys.contract.js index be61d85..5b486f9 100644 --- a/src/contracts/VotingToChangeKeys.contract.js +++ b/src/contracts/VotingToChangeKeys.contract.js @@ -90,7 +90,7 @@ export default class VotingToChangeKeys { return _limitPerValidator - _activeBallots } - async minBallotDuration() { - return await this.instance.methods.minBallotDuration().call() + minBallotDuration() { + return this.instance.methods.minBallotDuration().call() } } diff --git a/src/contracts/VotingToChangeMinThreshold.contract.js b/src/contracts/VotingToChangeMinThreshold.contract.js index 827d903..fb3d3c0 100644 --- a/src/contracts/VotingToChangeMinThreshold.contract.js +++ b/src/contracts/VotingToChangeMinThreshold.contract.js @@ -82,7 +82,7 @@ export default class VotingToChangeMinThreshold { return _limitPerValidator - _activeBallots } - async minBallotDuration() { - return await this.instance.methods.minBallotDuration().call() + minBallotDuration() { + return this.instance.methods.minBallotDuration().call() } } diff --git a/src/contracts/VotingToChangeProxy.contract.js b/src/contracts/VotingToChangeProxy.contract.js index b207ad0..4e7a251 100644 --- a/src/contracts/VotingToChangeProxy.contract.js +++ b/src/contracts/VotingToChangeProxy.contract.js @@ -78,7 +78,7 @@ export default class VotingToChangeProxy { return _limitPerValidator - _activeBallots } - async minBallotDuration() { - return await this.instance.methods.minBallotDuration().call() + minBallotDuration() { + return this.instance.methods.minBallotDuration().call() } } diff --git a/src/index.js b/src/index.js index 138d278..5f7c621 100644 --- a/src/index.js +++ b/src/index.js @@ -88,13 +88,9 @@ class AppMainRouter extends Component { await Promise.all(promises) - await this.setKeys(web3Config.defaultAccount) + await contractsStore.updateKeys(web3Config.defaultAccount) - contractsStore.getKeysBallotThreshold() - contractsStore.getProxyBallotThreshold() - contractsStore.getBallotCancelingThreshold() - - await contractsStore.getBallotsLimits() + await contractsStore.getMinBallotDurationsAndThresholds() await contractsStore.getAllValidatorMetadata() await contractsStore.getAllBallots() @@ -109,14 +105,6 @@ class AppMainRouter extends Component { this.initChain() } - setKeys = async account => { - await contractsStore.setMiningKey(account) - await contractsStore.setVotingKey(account) - - console.log('votingKey', contractsStore.votingKey) - console.log('miningKey', contractsStore.miningKey) - } - render() { return ( diff --git a/src/stores/ContractsStore.js b/src/stores/ContractsStore.js index f730851..5d98682 100644 --- a/src/stores/ContractsStore.js +++ b/src/stores/ContractsStore.js @@ -49,39 +49,26 @@ class ContractsStore { @observable injectedWeb3 constructor() { - this.votingKey = null - this.miningKey = null + this.votingKey = '0x0000000000000000000000000000000000000000' + this.miningKey = '0x0000000000000000000000000000000000000000' this.validatorsMetadata = {} this.validatorLimits = { keys: null, minThreshold: null, proxy: null } this.minBallotDuration = { keys: 0, minThreshold: 0, proxy: 0 } this.injectedWeb3 = false } + @computed + get isEmptyVotingKey() { + return !this.votingKey || this.votingKey === '0x0000000000000000000000000000000000000000' + } + @computed get isValidVotingKey() { + if (this.isEmptyVotingKey) return false if (this.miningKey && this.miningKey !== '0x0000000000000000000000000000000000000000') return true return false } - @action('Get keys ballot threshold') - getKeysBallotThreshold = async () => { - this.keysBallotThreshold = await this.ballotsStorage.instance.methods.getBallotThreshold(1).call() - this.minThresholdBallotThreshold = this.keysBallotThreshold - } - - @action('Get proxy ballot threshold') - getProxyBallotThreshold = async () => { - this.proxyBallotThreshold = await this.ballotsStorage.instance.methods.getProxyThreshold().call() - this.emissionFundsBallotThreshold = this.proxyBallotThreshold - } - - @action('Get ballot canceling threshold') - getBallotCancelingThreshold = async () => { - this.ballotCancelingThreshold = this.votingToManageEmissionFunds - ? Number(await this.votingToManageEmissionFunds.ballotCancelingThreshold()) - : 0 - } - @action('Set web3Instance') setWeb3Instance = web3Config => { this.web3Instance = web3Config.web3Instance @@ -206,17 +193,24 @@ class ContractsStore { @action('Set mining key') setMiningKey = async account => { - try { - this.miningKey = await this.keysManager.instance.methods.miningKeyByVoting(account).call() - } catch (e) { - console.log(e) - this.miningKey = '0x0000000000000000000000000000000000000000' + let miningKey = '0x0000000000000000000000000000000000000000' + if (account && account !== '0x0000000000000000000000000000000000000000') { + try { + miningKey = await this.keysManager.instance.methods.miningKeyByVoting(account).call() + } catch (e) { + console.log(e) + } } + this.miningKey = miningKey } @action('Update keys') updateKeys = async account => { - if (this.votingKey === account) return + account = account || '0x0000000000000000000000000000000000000000' + + if (this.votingKey && this.votingKey.toLowerCase() === account.toLowerCase()) { + return + } this.setVotingKey(account) await this.setMiningKey(account) @@ -423,41 +417,63 @@ class ContractsStore { async getBallotsLimits() { return new Promise(async resolve => { if (this.web3Instance && this.netId) { - const limitPerValidator = await this.ballotsStorage.instance.methods.getBallotLimitPerValidator().call() + let keysLimit = 0 + let minThresholdLimit = 0 + let proxyLimit = 0 - const getKeysLimit = await this.votingToChangeKeys.getBallotLimit(this.miningKey, limitPerValidator) - const getMinThresholdLimit = await this.votingToChangeMinThreshold.getBallotLimit( - this.miningKey, - limitPerValidator - ) - const getProxyLimit = await this.votingToChangeProxy.getBallotLimit(this.miningKey, limitPerValidator) + if (this.isValidVotingKey) { + const limitPerValidator = await this.ballotsStorage.instance.methods.getBallotLimitPerValidator().call() + keysLimit = await this.votingToChangeKeys.getBallotLimit(this.miningKey, limitPerValidator) + minThresholdLimit = await this.votingToChangeMinThreshold.getBallotLimit(this.miningKey, limitPerValidator) + proxyLimit = await this.votingToChangeProxy.getBallotLimit(this.miningKey, limitPerValidator) + } - const getKeysMinBallotDuration = await this.votingToChangeKeys.minBallotDuration() - const getMinThresholdMinBallotDuration = await this.votingToChangeMinThreshold.minBallotDuration() - const getProxyMinBallotDuration = await this.votingToChangeProxy.minBallotDuration() + this.validatorLimits.keys = keysLimit + this.validatorLimits.minThreshold = minThresholdLimit + this.validatorLimits.proxy = proxyLimit + } + resolve() + }) + } + + @action + async getMinBallotDurationsAndThresholds() { + return new Promise(async resolve => { + if (this.web3Instance && this.netId) { + const getKeysMinBallotDuration = this.votingToChangeKeys.minBallotDuration() + const getMinThresholdMinBallotDuration = this.votingToChangeMinThreshold.minBallotDuration() + const getProxyMinBallotDuration = this.votingToChangeProxy.minBallotDuration() + + const getBallotThreshold = this.ballotsStorage.instance.methods.getBallotThreshold(1).call() + const getProxyThreshold = this.ballotsStorage.instance.methods.getProxyThreshold().call() + const getBallotCancelingThreshold = this.votingToManageEmissionFunds + ? this.votingToManageEmissionFunds.ballotCancelingThreshold() + : 0 await Promise.all([ - getKeysLimit, - getMinThresholdLimit, - getProxyLimit, getKeysMinBallotDuration, getMinThresholdMinBallotDuration, - getProxyMinBallotDuration + getProxyMinBallotDuration, + getBallotThreshold, + getProxyThreshold, + getBallotCancelingThreshold ]).then( ([ - keysLimit, - minThresholdLimit, - proxyLimit, keysMinBallotDuration, minThresholdMinBallotDuration, - proxyMinBallotDuration + proxyMinBallotDuration, + keysBallotThreshold, + proxyBallotThreshold, + cancelingThreshold ]) => { - this.validatorLimits.keys = keysLimit - this.validatorLimits.minThreshold = minThresholdLimit - this.validatorLimits.proxy = proxyLimit this.minBallotDuration.keys = keysMinBallotDuration this.minBallotDuration.minThreshold = minThresholdMinBallotDuration this.minBallotDuration.proxy = proxyMinBallotDuration + this.keysBallotThreshold = keysBallotThreshold + this.minThresholdBallotThreshold = keysBallotThreshold + this.proxyBallotThreshold = proxyBallotThreshold + this.emissionFundsBallotThreshold = proxyBallotThreshold + this.ballotCancelingThreshold = cancelingThreshold resolve() } ) diff --git a/src/utils/getWeb3.js b/src/utils/getWeb3.js index 49c7dd6..6c3fc92 100644 --- a/src/utils/getWeb3.js +++ b/src/utils/getWeb3.js @@ -10,15 +10,14 @@ export async function enableWallet(updateKeys) { try { await window.ethereum.enable() } catch (e) { + await updateKeys(null) throw Error(messages.USER_DENIED_ACCOUNT_ACCESS) } const web3 = new Web3(window.ethereum) const accounts = await web3.eth.getAccounts() - if (accounts[0]) { - await updateKeys(accounts[0]) - } + await updateKeys(accounts[0]) } } @@ -76,10 +75,10 @@ export default async function getWeb3(netId, updateKeys) { } if (web3.currentProvider.publicConfigStore) { - let currentAccount = defaultAccount ? defaultAccount.toLowerCase() : '' + let currentAccount = defaultAccount ? defaultAccount.toLowerCase() : null web3.currentProvider.publicConfigStore.on('update', function(obj) { const account = obj.selectedAddress - if (account && account !== currentAccount) { + if (account !== currentAccount) { currentAccount = account updateKeys(account) }