Added voting

This commit is contained in:
Richard Ramos 2018-05-30 10:32:38 -04:00
parent 08d5665245
commit 2f2a3fee27
6 changed files with 58 additions and 13 deletions

View File

@ -93,6 +93,8 @@ class ProposalManager extends Component {
: '' : ''
} }
<h2>Add proposal</h2> <h2>Add proposal</h2>
<p>Execute this on the console if proposal submit is not allowed</p>
<code>await ProposalCuration.methods.setSubmitPrice(web3.eth.defaultAccount, true, 1).send();</code>
<h3>Price: {this.state.submitPrice}</h3> <h3>Price: {this.state.submitPrice}</h3>
<Form> <Form>
<FormGroup> <FormGroup>

View File

@ -50,6 +50,7 @@ class ProposalContainer extends Component {
let proposalList = []; let proposalList = [];
for(let i = from; i < from + qty && i < this.state.total; i++){ for(let i = from; i < from + qty && i < this.state.total; i++){
let res = await ProposalCuration.methods.proposals(i).call(); let res = await ProposalCuration.methods.proposals(i).call();
res.id = i;
proposalList.push(res); proposalList.push(res);
} }
cb(proposalList); cb(proposalList);

View File

@ -1,25 +1,65 @@
import web3 from "Embark/web3" import web3 from "Embark/web3"
import EmbarkJS from 'Embark/EmbarkJS'; import EmbarkJS from 'Embark/EmbarkJS';
import React from 'react'; import React, {Component, Fragment} from 'react';
import { Button } from 'react-bootstrap'; import { Button } from 'react-bootstrap';
import ProposalManager from 'Embark/contracts/ProposalManager';
import ProposalCuration from 'Embark/contracts/ProposalCuration';
class ProposalForm extends React.Component { class ProposalForm extends Component {
constructor(props) { constructor(props) {
super(props); super(props);
this.state = {}; this.state = {
decision: 0
};
} }
handleClick(e){ componentDidMount(){
__embarkContext.execWhenReady(async () => {
ProposalManager.options.address = await ProposalCuration.methods.proposalManager().call();
let proposal = await ProposalManager.methods.getProposal(this.props.proposalId).call();
this.setState({decision: proposal.vote});
});
}
async handleClick(e, vote){
e.preventDefault(); e.preventDefault();
let choice = 0;
if(vote == 'APPROVE')
choice = 2;
else
choice = 1;
let proposal = this.props.proposalId;
let receipt = await ProposalManager.methods.voteProposal(this.props.proposalId, choice)
.send({from: web3.eth.defaultAccount});
if(receipt.status == '0x1'){
this.setState({
decision: choice
// TODO: show results
});
}
console.log(receipt);
// TODO: handle error
} }
render(){ render(){
console.log(this.state);
return <div> return <div>
<Button onClick={(e) => this.handleClick(e) }>Vote</Button> {
this.state.decision != 0 ?
<span>You voted for: {this.state.decision.toString() == '1' ? 'REJECT' : 'APPROVE'}<br /></span>
: ''
}
<Button onClick={(e) => this.handleClick(e, 'APPROVE') }>Approve</Button>
<Button onClick={(e) => this.handleClick(e, 'REJECT') }>Reject</Button>
<b><br />TODO: Verify if vote is allowed for this proposal</b>
<b><br />TODO: Show time until proposal votation is closed</b>
</div>; </div>;
} }
} }
export default ProposalForm; export default ProposalForm;

View File

@ -8,7 +8,6 @@ class Proposal extends React.Component {
constructor(props) { constructor(props) {
super(props); super(props);
this.state = { this.state = {
url: null, url: null,
title: null, title: null,
@ -58,7 +57,7 @@ class Proposal extends React.Component {
<h3>{ this.state.title }</h3> <h3>{ this.state.title }</h3>
<p>{ this.state.description }</p> <p>{ this.state.description }</p>
<a href={ this.state.url } target="_blank">{ this.state.url }</a> <a href={ this.state.url } target="_blank">{ this.state.url }</a>
<ProposalForm /> <ProposalForm proposalId={this.props.data.id} />
</div>); </div>);
} }

View File

@ -56,7 +56,8 @@ contract Democracy {
bytes32 topic; bytes32 topic;
bytes32 txHash; bytes32 txHash;
bool approved; bool approved;
(topic, txHash, approved) = proposalManager.getProposal(_proposalId);
(topic, txHash, approved, ) = proposalManager.getProposal(_proposalId);
require(approved); require(approved);
require( require(
txHash == keccak256( txHash == keccak256(

View File

@ -65,7 +65,7 @@ contract ProposalManager is Controlled {
p.topic = _topic; p.topic = _topic;
p.txHash = _txHash; p.txHash = _txHash;
p.blockStart = block.number + 1000; //will be replaced by configurations p.blockStart = block.number + 0; //will be replaced by configurations
p.voteBlockEnd = p.blockStart + 10000; //dummy value p.voteBlockEnd = p.blockStart + 10000; //dummy value
emit ProposalSet(_topic, proposalId, _txHash); emit ProposalSet(_topic, proposalId, _txHash);
} }
@ -143,12 +143,14 @@ contract ProposalManager is Controlled {
returns ( returns (
bytes32 topic, bytes32 topic,
bytes32 txHash, bytes32 txHash,
bool approved bool approved,
Vote vote
) )
{ {
Proposal memory p = proposals[_proposalId]; Proposal storage p = proposals[_proposalId];
return (p.topic, p.txHash, p.result == Vote.Approve); return (p.topic, p.txHash, p.result == Vote.Approve, p.voteMap[msg.sender]);
} }
function offchainTabulateVoteResult(uint256 _proposalId) function offchainTabulateVoteResult(uint256 _proposalId)
external external