Users can vote

This commit is contained in:
Richard Ramos 2018-06-27 20:45:31 -04:00
parent bfad0ae88e
commit b7528bbdee
3 changed files with 75 additions and 23 deletions

View File

@ -1,4 +1,4 @@
import React, { Fragment, PureComponent } from 'react'; import React, { Fragment, Component } from 'react';
import Card from '@material-ui/core/Card'; import Card from '@material-ui/core/Card';
import CardContent from '@material-ui/core/CardContent'; import CardContent from '@material-ui/core/CardContent';
import CardActions from '@material-ui/core/CardActions'; 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 Typography from '@material-ui/core/Typography';
import Slider from '@material-ui/lab/Slider'; import Slider from '@material-ui/lab/Slider';
import Tooltip from '@material-ui/core/Tooltip'; import Tooltip from '@material-ui/core/Tooltip';
import PollManager from 'Embark/contracts/PollManager';
class Poll extends PureComponent { class Poll extends Component {
state = {
value: 0, constructor(props){
}; super(props);
this.state = { value: 0, isSubmitting: false, ...props };
}
handleChange = (event, value) => { handleChange = (event, value) => {
this.setState({ 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(){ render(){
const { _question, _totalCensus, _voters } = this.props; const { _description,
const { value } = this.state; _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 ( return (
<Card> <Card>
<CardContent> <CardContent>
<Typography variant="headline">Proposal: {_question}</Typography> <Typography variant="headline">Proposal: {_description}</Typography>
<Typography variant="subheading" color="textSecondary">
Total SNT Voted: {_totalCensus}
</Typography>
<Typography variant="subheading" color="textSecondary"> <Typography variant="subheading" color="textSecondary">
Number of voters: {_voters} Number of voters: {_voters}
</Typography> </Typography>
<Typography variant="subheading" color="textSecondary">
Votes: {_qvResults}
</Typography>
<Typography variant="subheading" color="textSecondary">
SNT Allocated: {_results}
</Typography>
</CardContent> </CardContent>
<Tooltip id="tooltip-icon" placement="top" title={`${value * value} SNT`}> <Tooltip id="tooltip-icon" placement="top" title={`${value} votes`}>
<CardActions> <CardActions>
<Slider value={value} min={0} max={6} step={1} onChange={this.handleChange} /> <Slider value={value} min={0} max={maxValue} step={1} onChange={this.handleChange} />
<Button variant="contained" color="primary">Vote</Button> <Button variant="contained" disabled={disableVote} color="primary" onClick={this.handleClick}>Vote</Button>
</CardActions> </CardActions>
</Tooltip> </Tooltip>
</Card> </Card>
@ -41,9 +96,10 @@ class Poll extends PureComponent {
} }
} }
const PollsList = ({ rawPolls }) => ( const PollsList = ({ rawPolls }) => (
<Fragment> <Fragment>
{rawPolls.map((poll, idx) => <Poll key={idx} {...poll} />)} {rawPolls.map((poll, idx) => <Poll key={idx} idPoll={idx} {...poll} />)}
</Fragment> </Fragment>
) )

View File

@ -68,7 +68,7 @@
"deploy": false "deploy": false
}, },
"ProposalCuration": { "ProposalCuration": {
"deploy": true, "deploy": false,
"args": ["$SNT", "$TrustNetwork"] "args": ["$SNT", "$TrustNetwork"]
}, },
"Democracy": { "Democracy": {
@ -79,16 +79,10 @@
], ],
"gasLimit": 5000000 "gasLimit": 5000000
}, },
"SingleChoiceFactory": {
"deploy": false
},
"PollManager": { "PollManager": {
"deploy": true, "deploy": true,
"args": ["$MiniMeTokenFactory", "$SNT"] "args": ["$MiniMeTokenFactory", "$SNT"]
}, }
"SingleChoice": {
"deploy": false
}
} }
} }
} }

View File

@ -185,6 +185,7 @@ contract PollManager is Controlled {
returns( returns(
uint _startBlock, uint _startBlock,
uint _endBlock, uint _endBlock,
bool _canVote,
address _token, address _token,
bool _canceled, bool _canceled,
string _description, string _description,
@ -203,6 +204,7 @@ contract PollManager is Controlled {
_endBlock = p.endBlock; _endBlock = p.endBlock;
_token = p.token; _token = p.token;
_canceled = p.canceled; _canceled = p.canceled;
_canVote = canVote(_idPoll);
_description = p.description; _description = p.description;
_finalized = (!p.canceled) && (block.number >= _endBlock); _finalized = (!p.canceled) && (block.number >= _endBlock);
_totalCensus = MiniMeToken(p.token).totalSupply(); _totalCensus = MiniMeToken(p.token).totalSupply();