I have governance platform written in ink!, where I can create poll with pollId, number of options, and with poll_start_time
and poll_end_time
. And this is fine.
So when poll is started I can't call vote function, and revert an error PollIsNotStarted
.
Create poll function:
#[ink(message)]
pub fn create_poll(
&mut self,
poll_id: String,
number_of_options: u32,
poll_start_timestamp: Timestamp,
poll_end_timestamp: Timestamp,
) -> Result<(), Error> {
let current_timestamp = self.env().block_timestamp();
let poll_info = self.poll_data.get(&poll_id).unwrap_or_default();
if poll_info.number_of_options != 0 {
return Err(Error::PollIdAlreadyExists);
}
if number_of_options < 2 {
return Err(Error::InvalidNumberOfOption)
}
if poll_start_timestamp < current_timestamp {
return Err(Error::InvalidStartTimestamp)
}
if poll_end_timestamp <= poll_start_timestamp {
return Err(Error::InvalidEndTimestamp)
}
let poll_info = PollInfo {
number_of_options,
poll_start_timestamp,
poll_end_timestamp,
};
self.poll_data.insert(&poll_id, &poll_info);
self.env().emit_event(PollCreated {
poll_id: poll_id.clone(),
number_of_options,
poll_start_timestamp,
poll_end_timestamp,
});
Ok(())
}
Vote function:
#[ink(message)]
pub fn vote(
&mut self,
poll_id: String,
voting_option: u32,
number_of_votes: Balance,
) -> Result<(), Error>{
let caller = self.env().caller();
if number_of_votes <= 0 {
return Err(Error::InvalidNumberOfVotes)
}
let poll_info = self.poll_data.get(&poll_id).unwrap_or_default();
let current_timestamp = self.env().block_timestamp();
if current_timestamp < poll_info.poll_start_timestamp {
return Err(Error::PollIsNotStarted)
}
if current_timestamp > poll_info.poll_end_timestamp {
return Err(Error::PollIsFinished);
}
if voting_option > poll_info.number_of_options{
return Err(Error::InvalidNumberOfOption)
}
let mut voting_info = self.voting.get(&(poll_id.clone(), caller)).unwrap_or_default();
if voting_info.voting_option == 0 {
voting_info.voting_option = voting_option;
} else {
if voting_info.voting_option != voting_option {
return Err(Error::VoteDenied)
}
}
voting_info.number_of_votes += number_of_votes;
self.voting.insert(&(poll_id.clone(), caller), &voting_info);
self.env().emit_event(Voted {
poll_id: poll_id.clone(),
voter: caller,
voting_option,
number_of_votes,
});
Ok(().into())
}
I think this is about Timestamps, I am not sure. When I erase timestamps in vote function, it works fine. Where I am wrong?
create_poll
what time did you select? May be that time doesn't meet and that's why it's givePollIsNotStarted
PollIsNotStarted