From faaee4ab79783563d5b9a783b130d2fdadcbb5e6 Mon Sep 17 00:00:00 2001 From: Vadim Date: Mon, 27 May 2019 17:08:47 +0300 Subject: [PATCH] Fix a bug in displaying a text note near the Finalize button --- src/components/BallotCard/index.js | 25 +++++++- src/contracts/VotingToChangeKeys.contract.js | 4 ++ .../VotingToChangeMinThreshold.contract.js | 4 ++ src/contracts/VotingToChangeProxy.contract.js | 4 ++ src/index.js | 3 +- src/stores/ContractsStore.js | 57 ++++++++++++++----- 6 files changed, 82 insertions(+), 15 deletions(-) diff --git a/src/components/BallotCard/index.js b/src/components/BallotCard/index.js index c20c318..932353d 100644 --- a/src/components/BallotCard/index.js +++ b/src/components/BallotCard/index.js @@ -23,7 +23,9 @@ const zeroTimeTo = '00:00' @observer export class BallotCard extends React.Component { @observable cancelDeadline = 0 + @observable nowTimeUnix @observable startTime + @observable startTimeUnix @observable endTime @observable timeTo = {} @observable @@ -56,6 +58,7 @@ export class BallotCard extends React.Component { @observable hasAlreadyVoted @observable memo @observable quorumState + @observable minBallotDuration @computed get cancelOrFinalizeButtonDisplayName() { @@ -101,7 +104,7 @@ export class BallotCard extends React.Component { return `You can cancel this ballot within ${this.timeToCancel.displayValue}` } else { let description = 'Finalization is available after ballot time is finished' - if (this.canBeFinalized !== null) { + if (this.canBeFinalized !== null && this.nowTimeUnix - this.startTimeUnix > this.minBallotDuration) { description += ' or all validators are voted' } return description @@ -208,6 +211,10 @@ export class BallotCard extends React.Component { const msStart = start.diff(_now) const msFinish = finish.diff(_now) + this.nowTimeUnix = moment() + .utc() + .unix() + if (msCancel > 0 && !this.isCanceled) { this.timeToCancel.val = msCancel this.timeToCancel.displayValue = this.formatMs(msCancel, ':mm:ss') @@ -248,6 +255,7 @@ export class BallotCard extends React.Component { .utc((votingState.creationTime + contractsStore.ballotCancelingThreshold) * 1000) .format(USDateTimeFormat) } + this.startTimeUnix = moment.utc(votingState.startTime * 1000) / 1000 this.startTime = moment.utc(votingState.startTime * 1000).format(USDateTimeFormat) this.endTime = moment.utc(votingState.endTime * 1000).format(USDateTimeFormat) // getCreator @@ -297,6 +305,8 @@ export class BallotCard extends React.Component { if (votingType === 'votingToManageEmissionFunds') { this.getQuorumState() } + + this.minBallotDuration = this.getMinBallotDuration(contractsStore, votingType) } formatMs(ms, format) { @@ -605,6 +615,19 @@ export class BallotCard extends React.Component { } } + getMinBallotDuration(contractsStore, votingType) { + switch (votingType) { + case 'votingToChangeKeys': + return contractsStore.minBallotDuration.keys + case 'votingToChangeMinThreshold': + return contractsStore.minBallotDuration.minThreshold + case 'votingToChangeProxy': + return contractsStore.minBallotDuration.proxy + default: + return 0 + } + } + componentDidMount() { this.interval = setInterval(() => { this.calcTimeTo() diff --git a/src/contracts/VotingToChangeKeys.contract.js b/src/contracts/VotingToChangeKeys.contract.js index 8b1147e..b0b6137 100644 --- a/src/contracts/VotingToChangeKeys.contract.js +++ b/src/contracts/VotingToChangeKeys.contract.js @@ -85,4 +85,8 @@ export default class VotingToChangeKeys { const _activeBallots = await this.instance.methods.validatorActiveBallots(_miningKey).call() return _limitPerValidator - _activeBallots } + + async minBallotDuration() { + return await this.instance.methods.minBallotDuration().call() + } } diff --git a/src/contracts/VotingToChangeMinThreshold.contract.js b/src/contracts/VotingToChangeMinThreshold.contract.js index 9c33dd7..a08ad2f 100644 --- a/src/contracts/VotingToChangeMinThreshold.contract.js +++ b/src/contracts/VotingToChangeMinThreshold.contract.js @@ -77,4 +77,8 @@ export default class VotingToChangeMinThreshold { const _activeBallots = await this.instance.methods.validatorActiveBallots(_miningKey).call() return _limitPerValidator - _activeBallots } + + async minBallotDuration() { + return await this.instance.methods.minBallotDuration().call() + } } diff --git a/src/contracts/VotingToChangeProxy.contract.js b/src/contracts/VotingToChangeProxy.contract.js index 4e2370b..2fce9ef 100644 --- a/src/contracts/VotingToChangeProxy.contract.js +++ b/src/contracts/VotingToChangeProxy.contract.js @@ -73,4 +73,8 @@ export default class VotingToChangeProxy { const _activeBallots = await this.instance.methods.validatorActiveBallots(_miningKey).call() return _limitPerValidator - _activeBallots } + + async minBallotDuration() { + return await this.instance.methods.minBallotDuration().call() + } } diff --git a/src/index.js b/src/index.js index f9248c7..f8970f4 100644 --- a/src/index.js +++ b/src/index.js @@ -73,7 +73,8 @@ class AppMainRouter extends Component { contractsStore.getKeysBallotThreshold() contractsStore.getProxyBallotThreshold() contractsStore.getBallotCancelingThreshold() - contractsStore.getBallotsLimits() + + await contractsStore.getBallotsLimits() await contractsStore.getAllValidatorMetadata() await contractsStore.getAllBallots() diff --git a/src/stores/ContractsStore.js b/src/stores/ContractsStore.js index 842f46e..e07e3fb 100644 --- a/src/stores/ContractsStore.js +++ b/src/stores/ContractsStore.js @@ -43,6 +43,7 @@ class ContractsStore { @observable emissionFundsBallotThreshold @observable ballotCancelingThreshold @observable validatorLimits + @observable minBallotDuration @observable validatorsMetadata @observable netId @@ -51,6 +52,7 @@ class ContractsStore { this.miningKey = null this.validatorsMetadata = {} this.validatorLimits = { keys: null, minThreshold: null, proxy: null } + this.minBallotDuration = { keys: 0, minThreshold: 0, proxy: 0 } } @computed @@ -388,21 +390,50 @@ class ContractsStore { @action async getBallotsLimits() { - if (this.web3Instance && this.netId) { - const limitPerValidator = await this.ballotsStorage.instance.methods.getBallotLimitPerValidator().call() + return new Promise(async resolve => { + if (this.web3Instance && this.netId) { + const limitPerValidator = await this.ballotsStorage.instance.methods.getBallotLimitPerValidator().call() - let getKeysLimit = await this.votingToChangeKeys.getBallotLimit(this.miningKey, limitPerValidator) - let getMinThresholdLimit = await this.votingToChangeMinThreshold.getBallotLimit(this.miningKey, limitPerValidator) - let getProxyLimit = await this.votingToChangeProxy.getBallotLimit(this.miningKey, limitPerValidator) + 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) - await Promise.all([getKeysLimit, getMinThresholdLimit, getProxyLimit]).then( - ([keysLimit, minThresholdLimit, proxyLimit]) => { - this.validatorLimits.keys = keysLimit - this.validatorLimits.minThreshold = minThresholdLimit - this.validatorLimits.proxy = proxyLimit - } - ) - } + const getKeysMinBallotDuration = await this.votingToChangeKeys.minBallotDuration() + const getMinThresholdMinBallotDuration = await this.votingToChangeMinThreshold.minBallotDuration() + const getProxyMinBallotDuration = await this.votingToChangeProxy.minBallotDuration() + + await Promise.all([ + getKeysLimit, + getMinThresholdLimit, + getProxyLimit, + getKeysMinBallotDuration, + getMinThresholdMinBallotDuration, + getProxyMinBallotDuration + ]).then( + ([ + keysLimit, + minThresholdLimit, + proxyLimit, + keysMinBallotDuration, + minThresholdMinBallotDuration, + proxyMinBallotDuration + ]) => { + this.validatorLimits.keys = keysLimit + this.validatorLimits.minThreshold = minThresholdLimit + this.validatorLimits.proxy = proxyLimit + this.minBallotDuration.keys = keysMinBallotDuration + this.minBallotDuration.minThreshold = minThresholdMinBallotDuration + this.minBallotDuration.proxy = proxyMinBallotDuration + resolve() + } + ) + } else { + resolve() + } + }) } @action