Adding start and ending block fields

This commit is contained in:
Richard Ramos 2018-09-27 09:48:24 -04:00
parent 71ad38ec42
commit 635040e253
2 changed files with 105 additions and 19 deletions

View File

@ -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 ?
<Button type="submit" variant="extendedFab" aria-label="add" className={classes.button}>Submit</Button> :
<CircularProgress style={{ margin: '10px 10px 10px 50%' }} />
@ -102,10 +143,16 @@ const InnerForm = ({
const StyledForm = withStyles(styles)(InnerForm);
const AddPoll = withFormik({
mapPropsToValues: props => ({ title: '', ballots: ''}),
mapPropsToValues: props => ({ title: '', ballots: '', startBlock: '', endBlock: ''}),
validate(values, props){
return web3.eth.getBlockNumber()
.then(currentBlock => {
console.log(currentBlock);
const errors = {};
const { title, ballots } = values;
const { title, ballots, startBlock, endBlock } = values;
const ballotOptions = ballots.toString().split("|")
if(title.toString().trim() === "") {
errors.title = "Required";
@ -113,21 +160,60 @@ const AddPoll = withFormik({
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;
return errors;
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 }) {
const { title, ballots } = values;
const { title, ballots, startBlock, endBlock } = values;
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 endTime = currentBlock + (oneDayinBlocks * 90);
const endTime = endBlock ? endBlock : ((startBlock ? startBlock : currentBlock) + (oneDayinBlocks * 90));
const options = ballots.split("|");
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);
toSend.estimateGas()

View File

@ -18,7 +18,7 @@ import CircularProgress from '@material-ui/core/CircularProgress';
import { withStyles } from '@material-ui/core/styles';
import { VotingContext } from '../../context';
import rlp from 'rlp';
window.PollManager = PollManager;
const styles = {
card: {
display: 'flex',
@ -212,7 +212,7 @@ class Poll extends PureComponent {
{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 && cantVote && <span>Voting is not enabled for this poll</span>}
{balance != 0 && cantVote && <span>Voting is disabled for this poll</span>}
</Typography>}