From b7528bbdee3414aa3fb6ed8911506a205a58af6b Mon Sep 17 00:00:00 2001 From: Richard Ramos Date: Wed, 27 Jun 2018 20:45:31 -0400 Subject: [PATCH] Users can vote --- app/components/simple-voting/PollsList.js | 86 +++++++++++++++++++---- config/contracts.json | 10 +-- contracts/polls/PollManager.sol | 2 + 3 files changed, 75 insertions(+), 23 deletions(-) diff --git a/app/components/simple-voting/PollsList.js b/app/components/simple-voting/PollsList.js index f0c1aa9..1ebd27f 100644 --- a/app/components/simple-voting/PollsList.js +++ b/app/components/simple-voting/PollsList.js @@ -1,4 +1,4 @@ -import React, { Fragment, PureComponent } from 'react'; +import React, { Fragment, Component } from 'react'; import Card from '@material-ui/core/Card'; import CardContent from '@material-ui/core/CardContent'; import CardActions from '@material-ui/core/CardActions'; @@ -6,34 +6,89 @@ import Button from '@material-ui/core/Button'; import Typography from '@material-ui/core/Typography'; import Slider from '@material-ui/lab/Slider'; import Tooltip from '@material-ui/core/Tooltip'; +import PollManager from 'Embark/contracts/PollManager'; -class Poll extends PureComponent { - state = { - value: 0, - }; +class Poll extends Component { + + constructor(props){ + super(props); + + this.state = { value: 0, isSubmitting: false, ...props }; + } handleChange = (event, value) => { this.setState({ value }); }; + handleClick = (event) => { + event.preventDefault(); + + this.setState({isSubmitting: true}); + + const { customVote, poll } = PollManager.methods; + const { idPoll, value } = this.state; + const balance4Voting = value * value; + + const toSend = customVote(idPoll, balance4Voting); + + toSend.estimateGas() + .then(gasEstimated => { + console.log("customVote gas estimated: " + gasEstimated); + return toSend.send({gas: gasEstimated + 1000}); + }) + .then(res => { + console.log('sucess:', res); + this.setState({isSubmitting: false}); + return poll(idPoll).call(); + }) + .then(poll => { + this.setState(poll); + }) + .catch(res => { + console.log('fail:', res); + }) + .finally(() => { + this.setState({isSubmitting: false}); + }); + } + render(){ - const { _question, _totalCensus, _voters } = this.props; - const { value } = this.state; + const { _description, + _totalCensus, + _voters, + _qvResults, + _results, + _canVote, + value, + isSubmitting } = this.state; + + const disableVote = !_canVote || isSubmitting; + + const tokenBalance = 100; // TODO: read balance from cloned token + + const maxValue = Math.floor(Math.sqrt(tokenBalance)); + + + + return ( - Proposal: {_question} - - Total SNT Voted: {_totalCensus} - + Proposal: {_description} Number of voters: {_voters} + + Votes: {_qvResults} + + + SNT Allocated: {_results} + - + - - + + @@ -41,9 +96,10 @@ class Poll extends PureComponent { } } + const PollsList = ({ rawPolls }) => ( - {rawPolls.map((poll, idx) => )} + {rawPolls.map((poll, idx) => )} ) diff --git a/config/contracts.json b/config/contracts.json index 34d216f..e4e0910 100644 --- a/config/contracts.json +++ b/config/contracts.json @@ -68,7 +68,7 @@ "deploy": false }, "ProposalCuration": { - "deploy": true, + "deploy": false, "args": ["$SNT", "$TrustNetwork"] }, "Democracy": { @@ -79,16 +79,10 @@ ], "gasLimit": 5000000 }, - "SingleChoiceFactory": { - "deploy": false - }, "PollManager": { "deploy": true, "args": ["$MiniMeTokenFactory", "$SNT"] - }, - "SingleChoice": { - "deploy": false - } + } } } } diff --git a/contracts/polls/PollManager.sol b/contracts/polls/PollManager.sol index 1de98c7..fea10f1 100644 --- a/contracts/polls/PollManager.sol +++ b/contracts/polls/PollManager.sol @@ -185,6 +185,7 @@ contract PollManager is Controlled { returns( uint _startBlock, uint _endBlock, + bool _canVote, address _token, bool _canceled, string _description, @@ -203,6 +204,7 @@ contract PollManager is Controlled { _endBlock = p.endBlock; _token = p.token; _canceled = p.canceled; + _canVote = canVote(_idPoll); _description = p.description; _finalized = (!p.canceled) && (block.number >= _endBlock); _totalCensus = MiniMeToken(p.token).totalSupply();