(Refactor) doesMethodExist function

This commit is contained in:
Vadim Arasev 2018-06-13 13:44:04 +03:00
parent 944db87184
commit 8da49125a0
7 changed files with 58 additions and 29 deletions

View File

@ -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}`);
}

View File

@ -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 = <div>
<div className="left">
<div className="form-el">

View File

@ -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 {

View File

@ -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';

View File

@ -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) {

View File

@ -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) {

View File

@ -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) {