From 8da49125a025f14a88a558ca1dc78df4a7ef9b77 Mon Sep 17 00:00:00 2001 From: Vadim Arasev Date: Wed, 13 Jun 2018 13:44:04 +0300 Subject: [PATCH] (Refactor) doesMethodExist function --- src/components/BallotCard.jsx | 18 +++++++------ src/components/BallotKeysMetadata.jsx | 6 ++--- src/components/NewBallot.jsx | 5 ++-- src/constants.js | 1 - src/contracts/VotingToChangeKeys.contract.js | 25 ++++++++++++------- .../VotingToChangeMinThreshold.contract.js | 16 +++++++++--- src/contracts/VotingToChangeProxy.contract.js | 16 +++++++++--- 7 files changed, 58 insertions(+), 29 deletions(-) diff --git a/src/components/BallotCard.jsx b/src/components/BallotCard.jsx index d68ebec..bfff5f3 100644 --- a/src/components/BallotCard.jsx +++ b/src/components/BallotCard.jsx @@ -3,7 +3,6 @@ import moment from "moment"; import { observable, action, computed } from "mobx"; import { inject, observer } from "mobx-react"; import { toAscii } from "../helpers"; -import { constants } from "../constants"; import { messages } from "../messages"; import swal from "sweetalert2"; @@ -48,8 +47,15 @@ export class BallotCard extends React.Component { } @computed get finalizeDescription () { - const _finalizeDescription = this.isFinalized ? '' : constants.CARD_FINALIZE_DESCRIPTION; - return _finalizeDescription; + if (this.isFinalized) { + return ''; + } + const { contractsStore, votingType } = this.props; + let description = 'Finalization is available after ballot time is finished'; + if (this.getContract(contractsStore, votingType).doesMethodExist('canBeFinalizedNow')) { + description += ' or all validators are voted'; + } + return description; } @computed get votesForNumber() { @@ -315,11 +321,7 @@ export class BallotCard extends React.Component { repeatGetProperty = async (contractsStore, contractType, id, methodID, tryID) => { try { - let contract = this.getContract(contractsStore, contractType); - if (!contract[methodID]) { - return null; - } - let val = await contract[methodID](id); + let val = await this.getContract(contractsStore, contractType)[methodID](id); if (tryID > 0) { console.log(`success from Try ${tryID + 1}`); } diff --git a/src/components/BallotKeysMetadata.jsx b/src/components/BallotKeysMetadata.jsx index b04c7e0..fd9bf6c 100644 --- a/src/components/BallotKeysMetadata.jsx +++ b/src/components/BallotKeysMetadata.jsx @@ -7,10 +7,10 @@ import "react-select/dist/react-select.css"; @observer export class BallotKeysMetadata extends React.Component { render() { - const options = this.props.contractsStore.validatorsMetadata.slice(); - const { ballotStore } = this.props; + const { ballotStore, contractsStore } = this.props; + const options = contractsStore.validatorsMetadata.slice(); let newVotingPayoutKeys = ''; - if (ballotStore.isNewValidatorPersonalData) { + if (ballotStore.isNewValidatorPersonalData && contractsStore.votingToChangeKeys.doesMethodExist('createBallotToAddNewValidator')) { newVotingPayoutKeys =
diff --git a/src/components/NewBallot.jsx b/src/components/NewBallot.jsx index 27b3ad3..7e24460 100644 --- a/src/components/NewBallot.jsx +++ b/src/components/NewBallot.jsx @@ -140,8 +140,9 @@ export class NewBallot extends React.Component { }; let method; if ( - ballotStore.ballotKeys.keysBallotType === ballotStore.KeysBallotType.add && - ballotStore.ballotKeys.keyType === ballotStore.KeyType.mining + inputToMethod.ballotType === ballotStore.KeysBallotType.add && + inputToMethod.affectedKeyType === ballotStore.KeyType.mining && + (inputToMethod.newVotingKey || inputToMethod.newPayoutKey) ) { method = contractsStore.votingToChangeKeys.createBallotToAddNewValidator(inputToMethod); } else { diff --git a/src/constants.js b/src/constants.js index 96da876..edd6858 100644 --- a/src/constants.js +++ b/src/constants.js @@ -1,5 +1,4 @@ let constants = {}; -constants.CARD_FINALIZE_DESCRIPTION = "Finalization is available after ballot time is finished or all validators are voted"; constants.organization = 'poanetwork'; constants.repoName = 'poa-chain-spec'; constants.addressesSourceFile = 'contracts.json'; diff --git a/src/contracts/VotingToChangeKeys.contract.js b/src/contracts/VotingToChangeKeys.contract.js index 24fade4..d0ab019 100644 --- a/src/contracts/VotingToChangeKeys.contract.js +++ b/src/contracts/VotingToChangeKeys.contract.js @@ -38,6 +38,10 @@ export default class VotingToChangeKeys { return this.votingToChangeKeysInstance.methods.areBallotParamsValid(ballotType, affectedKey, affectedKeyType, miningKey).call(); } + doesMethodExist(methodName) { + return this.votingToChangeKeysInstance && this.votingToChangeKeysInstance.methods[methodName]; + } + getStartTime(_id) { return this.votingToChangeKeysInstance.methods.getStartTime(_id).call(); } @@ -47,14 +51,14 @@ export default class VotingToChangeKeys { } votingState(_id) { - if (this.votingToChangeKeysInstance.methods.votingState) { + if (this.doesMethodExist('votingState')) { return this.votingToChangeKeysInstance.methods.votingState(_id).call(); } return null; } getCreator(_id) { - if (this.votingToChangeKeysInstance.methods.getCreator) { + if (this.doesMethodExist('getCreator')) { return this.votingToChangeKeysInstance.methods.getCreator(_id).call(); } return null; @@ -85,7 +89,10 @@ export default class VotingToChangeKeys { } canBeFinalizedNow(_id) { - return this.votingToChangeKeysInstance.methods.canBeFinalizedNow(_id).call(); + if (this.doesMethodExist('canBeFinalizedNow')) { + return this.votingToChangeKeysInstance.methods.canBeFinalizedNow(_id).call(); + } + return null; } getBallotType(_id) { @@ -101,17 +108,17 @@ export default class VotingToChangeKeys { } getNewVotingKey(_id) { - if (!this.votingToChangeKeysInstance.methods.getNewVotingKey) { - return ""; + if (this.doesMethodExist('getNewVotingKey')) { + return this.votingToChangeKeysInstance.methods.getNewVotingKey(_id).call(); } - return this.votingToChangeKeysInstance.methods.getNewVotingKey(_id).call(); + return ""; } getNewPayoutKey(_id) { - if (!this.votingToChangeKeysInstance.methods.getNewPayoutKey) { - return ""; + if (this.doesMethodExist('getNewPayoutKey')) { + return this.votingToChangeKeysInstance.methods.getNewPayoutKey(_id).call(); } - return this.votingToChangeKeysInstance.methods.getNewPayoutKey(_id).call(); + return ""; } getMiningKey(_id) { diff --git a/src/contracts/VotingToChangeMinThreshold.contract.js b/src/contracts/VotingToChangeMinThreshold.contract.js index 107537e..acafc97 100644 --- a/src/contracts/VotingToChangeMinThreshold.contract.js +++ b/src/contracts/VotingToChangeMinThreshold.contract.js @@ -30,6 +30,13 @@ export default class VotingToChangeMinThreshold { } //getters + doesMethodExist(methodName) { + if (this.votingToChangeMinThresholdInstance.methods[methodName]) { + return true; + } + return false; + } + getStartTime(_id) { return this.votingToChangeMinThresholdInstance.methods.getStartTime(_id).call(); } @@ -39,14 +46,14 @@ export default class VotingToChangeMinThreshold { } votingState(_id) { - if (this.votingToChangeMinThresholdInstance.methods.votingState) { + if (this.doesMethodExist('votingState')) { return this.votingToChangeMinThresholdInstance.methods.votingState(_id).call(); } return null; } getCreator(_id) { - if (this.votingToChangeMinThresholdInstance.methods.getCreator) { + if (this.doesMethodExist('getCreator')) { return this.votingToChangeMinThresholdInstance.methods.getCreator(_id).call(); } return null; @@ -77,7 +84,10 @@ export default class VotingToChangeMinThreshold { } canBeFinalizedNow(_id) { - return this.votingToChangeMinThresholdInstance.methods.canBeFinalizedNow(_id).call(); + if (this.doesMethodExist('canBeFinalizedNow')) { + return this.votingToChangeMinThresholdInstance.methods.canBeFinalizedNow(_id).call(); + } + return null; } getProposedValue(_id) { diff --git a/src/contracts/VotingToChangeProxy.contract.js b/src/contracts/VotingToChangeProxy.contract.js index 31ce1a0..e7b2ce9 100644 --- a/src/contracts/VotingToChangeProxy.contract.js +++ b/src/contracts/VotingToChangeProxy.contract.js @@ -30,6 +30,13 @@ export default class VotingToChangeProxy { } //getters + doesMethodExist(methodName) { + if (this.votingToChangeProxyInstance.methods[methodName]) { + return true; + } + return false; + } + getStartTime(_id) { return this.votingToChangeProxyInstance.methods.getStartTime(_id).call(); } @@ -39,14 +46,14 @@ export default class VotingToChangeProxy { } votingState(_id) { - if (this.votingToChangeProxyInstance.methods.votingState) { + if (this.doesMethodExist('votingState')) { return this.votingToChangeProxyInstance.methods.votingState(_id).call(); } return null; } getCreator(_id) { - if (this.votingToChangeProxyInstance.methods.getCreator) { + if (this.doesMethodExist('getCreator')) { return this.votingToChangeProxyInstance.methods.getCreator(_id).call(); } return null; @@ -77,7 +84,10 @@ export default class VotingToChangeProxy { } canBeFinalizedNow(_id) { - return this.votingToChangeProxyInstance.methods.canBeFinalizedNow(_id).call(); + if (this.doesMethodExist('canBeFinalizedNow')) { + return this.votingToChangeProxyInstance.methods.canBeFinalizedNow(_id).call(); + } + return null; } getProposedValue(_id) {