diff --git a/src/components/BallotCard.jsx b/src/components/BallotCard.jsx index 1bd09f2..d64d59c 100644 --- a/src/components/BallotCard.jsx +++ b/src/components/BallotCard.jsx @@ -167,7 +167,10 @@ export class BallotCard extends React.Component { @action("Get votingState") getVotingState = async () => { const { contractsStore, id, votingType } = this.props; - const votingState = await this.repeatGetProperty(contractsStore, votingType, id, "votingState", 0); + let votingState = this.props.votingState; + if (!votingState) { + votingState = await this.repeatGetProperty(contractsStore, votingType, id, "votingState", 0); + } if (votingState) { // getTimes this.startTime = moment.utc(votingState.startTime * 1000).format(USDateTimeFormat); diff --git a/src/components/BallotKeysCard.jsx b/src/components/BallotKeysCard.jsx index f0bf70c..dd8475f 100644 --- a/src/components/BallotKeysCard.jsx +++ b/src/components/BallotKeysCard.jsx @@ -36,11 +36,13 @@ export class BallotKeysCard extends React.Component { @action("Get votingState of keys ballot") getVotingState = async () => { const { contractsStore, id } = this.props; - let votingState; - try { - votingState = await contractsStore.votingToChangeKeys.votingState(id); - } catch(e) { - console.log(e.message); + let votingState = this.props.votingState; + if (!votingState) { + try { + votingState = await contractsStore.votingToChangeKeys.votingState(id); + } catch(e) { + console.log(e.message); + } } if (votingState) { this.affectedKey = votingState.affectedKey; @@ -213,7 +215,7 @@ export class BallotKeysCard extends React.Component { } render () { - let { id } = this.props; + let { id, votingState } = this.props; let affectedKeyClassName; let affectedKey =

{this.affectedKey}

; @@ -240,7 +242,7 @@ export class BallotKeysCard extends React.Component { } return ( - +

Action

diff --git a/src/components/BallotMinThresholdCard.jsx b/src/components/BallotMinThresholdCard.jsx index 83c0c04..2ae3b2b 100644 --- a/src/components/BallotMinThresholdCard.jsx +++ b/src/components/BallotMinThresholdCard.jsx @@ -22,7 +22,11 @@ export class BallotMinThresholdCard extends React.Component { constructor(props) { super(props); - this.getProposedValue(this.props.id); + if (this.props.votingState) { + this.proposedValue = this.props.votingState.proposedValue; + } else { + this.getProposedValue(this.props.id); + } } isSearchPattern = () => { @@ -35,9 +39,9 @@ export class BallotMinThresholdCard extends React.Component { } render () { - let { id } = this.props; + let { id, votingState } = this.props; return ( - +

Proposed min threshold

diff --git a/src/components/BallotProxyCard.jsx b/src/components/BallotProxyCard.jsx index afd0848..642ed00 100644 --- a/src/components/BallotProxyCard.jsx +++ b/src/components/BallotProxyCard.jsx @@ -35,8 +35,13 @@ export class BallotProxyCard extends React.Component { constructor(props) { super(props); - this.getProposedAddress(); - this.getContractType(); + if (this.props.votingState) { + this.proposedAddress = this.props.votingState.proposedValue; + this.contractType = this.props.votingState.contractType; + } else { + this.getProposedAddress(); + this.getContractType(); + } } isSearchPattern = () => { @@ -50,9 +55,9 @@ export class BallotProxyCard extends React.Component { } render () { - const { ballotStore, id } = this.props; + const { ballotStore, id, votingState } = this.props; return ( - +

Contract type

diff --git a/src/contracts/VotingToChangeKeys.contract.js b/src/contracts/VotingToChangeKeys.contract.js index 80df2c2..a37e0ca 100644 --- a/src/contracts/VotingToChangeKeys.contract.js +++ b/src/contracts/VotingToChangeKeys.contract.js @@ -56,6 +56,10 @@ export default class VotingToChangeKeys { return this.votingToChangeKeysInstance.methods.getEndTime(_id).call(); } + nextBallotId() { + return this.votingToChangeKeysInstance.methods.nextBallotId().call(); + } + votingState(_id) { if (this.doesMethodExist('votingState')) { return this.votingToChangeKeysInstance.methods.votingState(_id).call(); diff --git a/src/contracts/VotingToChangeMinThreshold.contract.js b/src/contracts/VotingToChangeMinThreshold.contract.js index e8198b9..dc7ecdb 100644 --- a/src/contracts/VotingToChangeMinThreshold.contract.js +++ b/src/contracts/VotingToChangeMinThreshold.contract.js @@ -51,6 +51,10 @@ export default class VotingToChangeMinThreshold { return this.votingToChangeMinThresholdInstance.methods.getEndTime(_id).call(); } + nextBallotId() { + return this.votingToChangeMinThresholdInstance.methods.nextBallotId().call(); + } + votingState(_id) { if (this.doesMethodExist('votingState')) { return this.votingToChangeMinThresholdInstance.methods.votingState(_id).call(); diff --git a/src/contracts/VotingToChangeProxy.contract.js b/src/contracts/VotingToChangeProxy.contract.js index c46ddd9..593777c 100644 --- a/src/contracts/VotingToChangeProxy.contract.js +++ b/src/contracts/VotingToChangeProxy.contract.js @@ -51,6 +51,10 @@ export default class VotingToChangeProxy { return this.votingToChangeProxyInstance.methods.getEndTime(_id).call(); } + nextBallotId() { + return this.votingToChangeProxyInstance.methods.nextBallotId().call(); + } + votingState(_id) { if (this.doesMethodExist('votingState')) { return this.votingToChangeProxyInstance.methods.votingState(_id).call(); diff --git a/src/stores/ContractsStore.js b/src/stores/ContractsStore.js index ab1a674..48a674d 100644 --- a/src/stores/ContractsStore.js +++ b/src/stores/ContractsStore.js @@ -154,78 +154,90 @@ class ContractsStore { @action("Get all keys ballots") getAllBallots = async () => { - let allKeysBallots, allMinThresholdBallots, allProxyBallots; + let keysNextBallotId = 0, minThresholdNextBallotId = 0, proxyNextBallotId = 0; try { - [allKeysBallots, allMinThresholdBallots, allProxyBallots] = await this.getAllBallotsIDsInternal(); + [keysNextBallotId, minThresholdNextBallotId, proxyNextBallotId] = await this.getAllBallotsNextIDs(); + keysNextBallotId = Number(keysNextBallotId); + minThresholdNextBallotId = Number(minThresholdNextBallotId); + proxyNextBallotId = Number(proxyNextBallotId); } catch (e) { console.log(e.message); } - let allKeysBallotsIDs = this.getCards(allKeysBallots, "votingToChangeKeys"); - let allMinThresholdBallotsIDs = this.getCards(allMinThresholdBallots, "votingToChangeMinThreshold"); - let allProxyBallotsIDs = this.getCards(allProxyBallots, "votingToChangeProxy"); + const allKeysPromise = this.getCards(keysNextBallotId, "votingToChangeKeys"); + const allMinThresholdPromise = this.getCards(minThresholdNextBallotId, "votingToChangeMinThreshold"); + const allProxyPromise = this.getCards(proxyNextBallotId, "votingToChangeProxy"); - await Promise.all([allKeysBallotsIDs, allMinThresholdBallotsIDs, allProxyBallotsIDs]); + await Promise.all([allKeysPromise, allMinThresholdPromise, allProxyPromise]); - let allBallotsIDsLength = allKeysBallotsIDs.length + allMinThresholdBallotsIDs.length + allProxyBallotsIDs.length; + const allBallotsIDsLength = keysNextBallotId + minThresholdNextBallotId + proxyNextBallotId; - if (allBallotsIDsLength == 0) { + if (allBallotsIDsLength === 0) { commonStore.hideLoading(); } } - getCards = async (allBallots, contractType) => { - let allBallotsIDs = []; - if (allBallots) { - allBallotsIDs = allBallots.map((event) => event.returnValues.id) - for (let i = allBallotsIDs.length - 1; i >= 0; i--) { - + getCards = async (nextBallotId, contractType) => { + if (nextBallotId) { + for (let id = nextBallotId - 1; id >= 0; id--) { let startTime = 0; + let votingState; + try { - startTime = await this[contractType].getStartTime(allBallotsIDs[i]); + votingState = await this[contractType].votingState(id); } catch(e) { console.log(e.message); } + if (votingState) { + startTime = votingState.startTime; + } else { + try { + startTime = await this[contractType].getStartTime(id); + } catch(e) { + console.log(e.message); + } + } + let card; switch(contractType) { - case "votingToChangeKeys": + case "votingToChangeKeys": card = + id={id} + type={ballotStore.BallotType.keys} + key={ballotsStore.ballotCards.length} + startTime={startTime} + votingState={votingState}/> break; - case "votingToChangeMinThreshold": + case "votingToChangeMinThreshold": card = + id={id} + type={ballotStore.BallotType.minThreshold} + key={ballotsStore.ballotCards.length} + startTime={startTime} + votingState={votingState}/> break; - case "votingToChangeProxy": + case "votingToChangeProxy": card = + id={id} + type={ballotStore.BallotType.proxy} + key={ballotsStore.ballotCards.length} + startTime={startTime} + votingState={votingState}/> break; } ballotsStore.ballotCards.push(card); } - - return allBallotsIDs; } } - @action("Get all keys ballots internal") - getAllBallotsIDsInternal = async () => { - let getAllKeysBallotsIDs = this.votingToChangeKeys.votingToChangeKeysInstance.getPastEvents('BallotCreated', {fromBlock: 0}); - let getAllMinThresholdBallotsIDs = this.votingToChangeMinThreshold.votingToChangeMinThresholdInstance.getPastEvents('BallotCreated', {fromBlock: 0}); - let getAllProxyBallotsIDs = this.votingToChangeProxy.votingToChangeProxyInstance.getPastEvents('BallotCreated', {fromBlock: 0}); - - return Promise.all([getAllKeysBallotsIDs, getAllMinThresholdBallotsIDs, getAllProxyBallotsIDs]); + @action("Get all keys next ballot ids") + getAllBallotsNextIDs = async () => { + const keysNextBallotId = this.votingToChangeKeys.nextBallotId(); + const minThresholdNextBallotId = this.votingToChangeMinThreshold.nextBallotId(); + const proxyNextBallotId = this.votingToChangeProxy.nextBallotId(); + return Promise.all([keysNextBallotId, minThresholdNextBallotId, proxyNextBallotId]); } @action