simple_voter/processor/
voter.rs

1use anchor_lang::prelude::*;
2use vipers::unwrap_int;
3
4use crate::{electorate_seeds, VoterContext};
5
6pub fn process_cast_votes(ctx: Context<VoterContext>, vote_side: u8) -> Result<()> {
7    let seeds: &[&[&[u8]]] = electorate_seeds!(ctx.accounts.electorate);
8    let cpi_ctx = CpiContext::new(
9        ctx.accounts.tribeca.program.to_account_info(),
10        ctx.accounts.to_set_vote_accounts(),
11    )
12    .with_signer(seeds);
13    govern::cpi::set_vote(cpi_ctx, vote_side, ctx.accounts.token_record.balance)?;
14
15    let token_record = &mut ctx.accounts.token_record;
16    token_record.unfinalized_votes = unwrap_int!(token_record.unfinalized_votes.checked_add(1));
17
18    Ok(())
19}
20
21pub fn process_withdraw_votes(ctx: Context<VoterContext>) -> Result<()> {
22    let seeds: &[&[&[u8]]] = electorate_seeds!(ctx.accounts.electorate);
23    let cpi_ctx = CpiContext::new(
24        ctx.accounts.tribeca.program.to_account_info(),
25        ctx.accounts.to_set_vote_accounts(),
26    )
27    .with_signer(seeds);
28    govern::cpi::set_vote(cpi_ctx, ctx.accounts.vote.side, 0)?;
29
30    let token_record = &mut ctx.accounts.token_record;
31    token_record.unfinalized_votes = unwrap_int!(token_record.unfinalized_votes.checked_sub(1));
32
33    Ok(())
34}
35
36impl<'info> VoterContext<'info> {
37    /// Conversion.
38    pub fn to_set_vote_accounts(&self) -> govern::cpi::accounts::SetVote<'info> {
39        govern::cpi::accounts::SetVote {
40            governor: self.tribeca.governor.to_account_info(),
41            proposal: self.proposal.to_account_info(),
42            vote: self.vote.to_account_info(),
43            electorate: self.electorate.to_account_info(),
44        }
45    }
46}