mirror of
https://github.com/status-im/snt-voting.git
synced 2025-02-23 15:48:10 +00:00
Adding start and ending block fields
This commit is contained in:
parent
71ad38ec42
commit
635040e253
@ -91,6 +91,47 @@ const InnerForm = ({
|
|||||||
/>
|
/>
|
||||||
|
|
||||||
|
|
||||||
|
<TextField
|
||||||
|
id="startBlock"
|
||||||
|
label="Start Block (optional)"
|
||||||
|
className={classes.textField}
|
||||||
|
value={values.startBlock}
|
||||||
|
onChange={handleChange}
|
||||||
|
margin="normal"
|
||||||
|
fullWidth
|
||||||
|
error={!!errors.startBlock}
|
||||||
|
InputProps={{
|
||||||
|
classes: {
|
||||||
|
input: classes.textFieldInput
|
||||||
|
},
|
||||||
|
}}
|
||||||
|
InputLabelProps={{
|
||||||
|
className: classes.textFieldFormLabel
|
||||||
|
}}
|
||||||
|
helperText={errors.startBlock}
|
||||||
|
/>
|
||||||
|
|
||||||
|
<TextField
|
||||||
|
id="endBlock"
|
||||||
|
label="End Block (optional)"
|
||||||
|
className={classes.textField}
|
||||||
|
value={values.endBlock}
|
||||||
|
onChange={handleChange}
|
||||||
|
margin="normal"
|
||||||
|
fullWidth
|
||||||
|
error={!!errors.endBlock}
|
||||||
|
InputProps={{
|
||||||
|
classes: {
|
||||||
|
input: classes.textFieldInput
|
||||||
|
},
|
||||||
|
}}
|
||||||
|
InputLabelProps={{
|
||||||
|
className: classes.textFieldFormLabel
|
||||||
|
}}
|
||||||
|
helperText={errors.endBlock}
|
||||||
|
/>
|
||||||
|
|
||||||
|
|
||||||
{!isSubmitting ?
|
{!isSubmitting ?
|
||||||
<Button type="submit" variant="extendedFab" aria-label="add" className={classes.button}>Submit</Button> :
|
<Button type="submit" variant="extendedFab" aria-label="add" className={classes.button}>Submit</Button> :
|
||||||
<CircularProgress style={{ margin: '10px 10px 10px 50%' }} />
|
<CircularProgress style={{ margin: '10px 10px 10px 50%' }} />
|
||||||
@ -102,37 +143,82 @@ const InnerForm = ({
|
|||||||
|
|
||||||
const StyledForm = withStyles(styles)(InnerForm);
|
const StyledForm = withStyles(styles)(InnerForm);
|
||||||
const AddPoll = withFormik({
|
const AddPoll = withFormik({
|
||||||
mapPropsToValues: props => ({ title: '', ballots: ''}),
|
mapPropsToValues: props => ({ title: '', ballots: '', startBlock: '', endBlock: ''}),
|
||||||
validate(values, props){
|
validate(values, props){
|
||||||
const errors = {};
|
|
||||||
const { title, ballots } = values;
|
|
||||||
const ballotOptions = ballots.toString().split("|")
|
|
||||||
if(title.toString().trim() === "") {
|
|
||||||
errors.title = "Required";
|
|
||||||
}
|
|
||||||
if(ballotOptions.filter(n => n).length == 1) {
|
|
||||||
errors.ballots = "A minimum of 2 options is required if using multiple options";
|
|
||||||
}
|
|
||||||
|
|
||||||
return errors;
|
|
||||||
|
return web3.eth.getBlockNumber()
|
||||||
|
.then(currentBlock => {
|
||||||
|
console.log(currentBlock);
|
||||||
|
|
||||||
|
const errors = {};
|
||||||
|
const { title, ballots, startBlock, endBlock } = values;
|
||||||
|
const ballotOptions = ballots.toString().split("|")
|
||||||
|
if(title.toString().trim() === "") {
|
||||||
|
errors.title = "Required";
|
||||||
|
}
|
||||||
|
if(ballotOptions.filter(n => n).length == 1) {
|
||||||
|
errors.ballots = "A minimum of 2 options is required if using multiple options";
|
||||||
|
}
|
||||||
|
let sBlock;
|
||||||
|
if(startBlock != ""){
|
||||||
|
var parsed = parseInt(startBlock, 10);
|
||||||
|
if (isNaN(parsed)) {
|
||||||
|
errors.startBlock = "Invalid Start Block"
|
||||||
|
} else {
|
||||||
|
sBlock = parsed;
|
||||||
|
|
||||||
|
if(sBlock < currentBlock){
|
||||||
|
errors.startBlock = "Block number is in the past. Recommended: " + (currentBlock + 60) + " (will be mined in ~10min)"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let eBlock;
|
||||||
|
if(endBlock != ""){
|
||||||
|
var parsed = parseInt(endBlock, 10);
|
||||||
|
if (isNaN(parsed)) {
|
||||||
|
errors.endBlock = "Invalid End Block"
|
||||||
|
} else {
|
||||||
|
eBlock = parsed;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(eBlock < sBlock){
|
||||||
|
errors.endBlock = "End block must occur after start block"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Object.keys(errors).length) {
|
||||||
|
throw errors;
|
||||||
|
}
|
||||||
|
|
||||||
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
async handleSubmit(values, { setSubmitting, setErrors, props, resetForm }) {
|
async handleSubmit(values, { setSubmitting, setErrors, props, resetForm }) {
|
||||||
const { title, ballots } = values;
|
const { title, ballots, startBlock, endBlock } = values;
|
||||||
const { eth: { getBlockNumber } } = window.web3;
|
const { eth: { getBlockNumber } } = window.web3;
|
||||||
const addPoll = PollManager.methods["addPoll(uint256,bytes,uint8)"];
|
|
||||||
|
const addPollCustomBlock = PollManager.methods["addPoll(uint256,uint256,bytes,uint8)"];
|
||||||
|
const addPollOnlyEndBlock = PollManager.methods["addPoll(uint256,bytes,uint8)"];
|
||||||
|
|
||||||
const currentBlock = await getBlockNumber();
|
const currentBlock = await getBlockNumber();
|
||||||
const endTime = currentBlock + (oneDayinBlocks * 90);
|
const endTime = endBlock ? endBlock : ((startBlock ? startBlock : currentBlock) + (oneDayinBlocks * 90));
|
||||||
const options = ballots.split("|");
|
const options = ballots.split("|");
|
||||||
const encodedDesc = "0x" + rlp.encode([title, options]).toString('hex');
|
const encodedDesc = "0x" + rlp.encode([title, options]).toString('hex');
|
||||||
|
|
||||||
const toSend = addPoll(endTime, encodedDesc, options.length || 0);
|
|
||||||
|
|
||||||
|
|
||||||
|
let toSend;
|
||||||
|
if(startBlock){
|
||||||
|
toSend = addPollCustomBlock(startBlock, endTime, encodedDesc, options.length || 0);
|
||||||
|
} else {
|
||||||
|
toSend = addPollOnlyEndBlock(endTime, encodedDesc, options.length || 0);
|
||||||
|
}
|
||||||
setSubmitting(true);
|
setSubmitting(true);
|
||||||
|
|
||||||
toSend.estimateGas()
|
toSend.estimateGas()
|
||||||
.then(gasEstimated => {
|
.then(gasEstimated => {
|
||||||
console.log("addPoll gas estimated: "+gasEstimated);
|
console.log("addPoll gas estimated: "+ gasEstimated);
|
||||||
return toSend.send({gas: gasEstimated + 100000});
|
return toSend.send({gas: gasEstimated + 100000});
|
||||||
})
|
})
|
||||||
.then(res => {
|
.then(res => {
|
||||||
|
@ -18,7 +18,7 @@ import CircularProgress from '@material-ui/core/CircularProgress';
|
|||||||
import { withStyles } from '@material-ui/core/styles';
|
import { withStyles } from '@material-ui/core/styles';
|
||||||
import { VotingContext } from '../../context';
|
import { VotingContext } from '../../context';
|
||||||
import rlp from 'rlp';
|
import rlp from 'rlp';
|
||||||
window.PollManager = PollManager;
|
|
||||||
const styles = {
|
const styles = {
|
||||||
card: {
|
card: {
|
||||||
display: 'flex',
|
display: 'flex',
|
||||||
@ -212,7 +212,7 @@ class Poll extends PureComponent {
|
|||||||
|
|
||||||
{cantVote && <Typography variant="body2" color="error">
|
{cantVote && <Typography variant="body2" color="error">
|
||||||
{balance == 0 && <span>Voting disabled for proposals made when there was no SNT in the account</span>}
|
{balance == 0 && <span>Voting disabled for proposals made when there was no SNT in the account</span>}
|
||||||
{balance != 0 && cantVote && <span>Voting is not enabled for this poll</span>}
|
{balance != 0 && cantVote && <span>Voting is disabled for this poll</span>}
|
||||||
</Typography>}
|
</Typography>}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user