solana_workflow/instructions/
create_workflow.rs1use crate::pda;
2use anchor_lang::prelude::*;
3use pda::workflow::CheckPoint;
4use pda::workflow::VoteOption;
5use pda::workflow::Workflow;
6
7#[derive(Accounts)]
8pub struct CreateWorkflow<'info> {
9 #[account(mut)]
10 pub user: Signer<'info>,
11 #[account(
12 init_if_needed,
13 payer = user,
14 space = 8 + Workflow::INIT_SPACE,
15 seeds = [Workflow::SEED_PREFIX, user.key().as_ref()],
16 bump,
17 )]
18 pub workflow: Account<'info, Workflow>,
19 #[account()]
20 pub workflow_program: AccountInfo<'info>,
22 pub system_program: Program<'info, System>,
23}
24
25#[derive(AnchorSerialize, AnchorDeserialize, Clone)]
26pub struct InputCheckPoint {
27 id: u16,
28 title: String,
29 options: Option<Vec<VoteOption>>,
30 vote_machine_address: Pubkey,
31}
32
33pub fn create_workflow<'c: 'info, 'info>(
34 ctx: Context<'_, '_, 'c, 'info, CreateWorkflow<'info>>,
35 title: String,
36 start: u16,
37 workflow_id: u64,
38 input_checkpoints: Vec<InputCheckPoint>,
39) -> Result<()> {
40 let _ = input_checkpoints;
41 let workflow = &mut ctx.accounts.workflow;
42 workflow.title = title.clone();
43 workflow.start = start;
44 workflow.workflow_id = workflow_id;
45 workflow.author = ctx.accounts.user.key();
46
47 let remaining_accounts_iter = &mut ctx.remaining_accounts.iter();
48
49 for input_checkpoint in input_checkpoints.iter() {
50 CheckPoint::initialize(
51 ctx.accounts.user.to_account_info(),
52 next_account_info(remaining_accounts_iter)?,
53 ctx.accounts.workflow.to_account_info(),
54 ctx.accounts.workflow_program.to_account_info(),
55 ctx.accounts.system_program.to_account_info(),
56 workflow_id,
57 input_checkpoint.id,
58 input_checkpoint.title.clone(),
59 input_checkpoint.options.clone(),
60 )?;
61 }
62
63 Ok(())
64}