solana_workflow/instructions/
vote.rs

1use anchor_lang::prelude::*;
2
3use crate::pda::{CheckPoint, Mission, Status, VoteData};
4
5#[derive(Accounts)]
6pub struct Vote<'info> {
7    #[account(mut)]
8    pub user: Signer<'info>,
9
10    #[account(mut)]
11    pub mission: Account<'info, Mission>,
12
13    #[account()]
14    pub checkpoint: Account<'info, CheckPoint>,
15
16    /// CHECK:
17    pub workflow_program: AccountInfo<'info>,
18    pub system_program: Program<'info, System>,
19}
20
21#[derive(AnchorSerialize, AnchorDeserialize, Clone)]
22pub struct InputVote {
23    pub option: u16,
24}
25
26pub fn vote<'c: 'info, 'info>(
27    ctx: Context<'_, '_, 'c, 'info, Vote<'info>>,
28    vote: InputVote,
29    vec_coef: Vec<u8>,
30) -> Result<()> {
31    let mission = &mut ctx.accounts.mission;
32    let checkpoint = &ctx.accounts.checkpoint;
33
34    // let next_check_point_id = checkpoint.options[vote.option as usize];
35
36    match &checkpoint.options {
37        Some(options) => {
38            let next_checkpoint_id = options[vote.option as usize].next_id;
39
40            mission.current_vote_data = ctx.remaining_accounts[vote.option as usize].key();
41
42            VoteData::initialize(
43                ctx.accounts.user.to_account_info(),
44                &ctx.remaining_accounts[vote.option as usize],
45                mission.to_account_info(),
46                ctx.accounts.workflow_program.to_account_info(),
47                ctx.accounts.system_program.to_account_info(),
48                vec_coef[vote.option as usize].into(),
49                next_checkpoint_id,
50                vec_coef[vote.option as usize],
51            )?;
52        }
53        None => {
54            mission.status = Status::CLOSED;
55        }
56    }
57
58    Ok(())
59}