Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions project-13-getting-to-production/anchor/programs/voting/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@ pub mod voting {

pub fn vote(ctx: Context<Vote>, _candidate_name: String, _poll_id: u64) -> Result<()> {
let candidate = &mut ctx.accounts.candidate;
let poll = &ctx.accounts.poll;
let current_time = Clock::get()?.unix_timestamp;

if current_time < (poll.poll_start as i64) {
return Err(ErrorCode::VotingNotStarted.into());
}

if current_time > (poll.poll_end as i64) {
return Err(ErrorCode::VotingEnded.into());
}

candidate.candidate_votes += 1;
msg!("Voted for candidate: {}", candidate.candidate_name);
msg!("Votes: {}", candidate.candidate_votes);
Expand Down Expand Up @@ -120,4 +131,12 @@ pub struct Poll {
pub poll_start: u64,
pub poll_end: u64,
pub candidate_amount: u64,
}

#[error_code]
pub enum ErrorCode {
#[msg("Voting has not started yet")]
VotingNotStarted,
#[msg("Voting has ended")]
VotingEnded,
}
1 change: 1 addition & 0 deletions project-2-voting/anchor/programs/voting/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ pub struct InitializeCandidate<'info> {
#[account(mut)]
pub signer: Signer<'info>,

#[account(mut)]
pub poll_account: Account<'info, PollAccount>,

#[account(
Expand Down