2

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?

9
  • Just confirm, you created a poll first before the vote function correct? Maybe you can share your test case code.
    – Josh
    Aug 4, 2023 at 18:55
  • Yes, I do create a poll first. Do you think the problem is in tests? I didn't yet wrote the test code.
    – jvn
    Aug 5, 2023 at 7:57
  • while create_poll what time did you select? May be that time doesn't meet and that's why it's give PollIsNotStarted
    – Ganesh11
    Aug 6, 2023 at 15:26
  • I select time in the future as a start time, and let's say 2 minutes after start time as a poll end time. And when poll is finished I still got same error that the PollIsNotStarted
    – jvn
    Aug 6, 2023 at 17:42
  • I tried everything here, and I had something, when I create a poll and when poll is started I can't vote until I create second poll, and than I can vote for the first poll I create (when poll is started). Do anyone have a clue what is going on?
    – jvn
    Aug 7, 2023 at 11:05

1 Answer 1

1

Obviously, problem is that when running a localnode, the node does only produce new blocks when transactions come in. So when you deploy contract and there is no transaction, timestamps gets not updated.

I deployed contract on Rococo test network, and work just fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.