-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcast_vote.rs
76 lines (63 loc) · 2.12 KB
/
cast_vote.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
use anchor_lang::prelude::*;
use crate::{error::CastVoteError, Member, Poll, Project, Vote, VoteType, VOTE_SEED};
#[derive(Accounts)]
pub struct CastVote<'info> {
#[account(mut)]
pub voter: Signer<'info>,
pub project: Account<'info, Project>,
// The member who initialized the poll
#[account(
mut,
constraint = poll_initializor_member.member_pubkey.key() != voter.key() @CastVoteError::SelfVote
)]
pub poll_initializor_member: Account<'info, Member>,
// The poll account
#[account(
mut,
constraint = poll.project == project.key() @CastVoteError::InvalidProject,
constraint = poll.user == poll_initializor_member.member_pubkey.key() @CastVoteError::InvalidPoll,
)]
pub poll: Account<'info, Poll>,
#[account(
init,
payer = voter,
space = Vote::INIT_SPACE + 8,
seeds = [
VOTE_SEED.as_bytes(),
poll.key().as_ref(),
voter.key().as_ref(),
],
bump
)]
pub vote: Account<'info, Vote>,
pub system_program: Program<'info, System>,
}
impl<'info> CastVote<'info> {
pub fn cast_vote(&mut self, vote_type: VoteType) -> Result<()> {
// Check if the project is active
if !self.project.is_active {
return Err(CastVoteError::ProjectClosed.into());
}
// Check if the voting period has ended
if self.poll.close_time < Clock::get()?.unix_timestamp as u64 {
return Err(CastVoteError::TimeExpired.into());
}
if vote_type == VoteType::Reject {
self.poll.rejections = self.poll.rejections.checked_add(1).unwrap();
}
self.poll_initializor_member.score = self
.poll_initializor_member
.score
.checked_add(vote_type as u64)
.unwrap();
self.poll.votes = self.poll.votes.checked_add(1).unwrap();
self.vote.set_inner(Vote {
voter: self.voter.key(),
project: self.project.key(),
poll: self.poll.key(),
vote_type,
bump: self.vote.bump,
});
Ok(())
}
}