solana_workflow/pda/
workflow.rs1use crate::{funcs, BpfWriter};
2use anchor_lang::prelude::*;
3
4#[account]
9#[derive(InitSpace)]
10pub struct Workflow {
11 pub author: Pubkey,
12 pub workflow_id: u64,
13 pub start: u16,
14 #[max_len(50)]
15 pub title: String,
16 pub no_variable: u8,
17}
18
19impl Workflow {
20 pub const SEED_PREFIX: &'static [u8; 8] = b"workflow";
21 pub fn increase(&mut self) {}
22}
23
24#[derive(InitSpace, AnchorSerialize, AnchorDeserialize, Clone)]
26pub struct VoteOption {
27 #[max_len(10)]
28 pub title: String,
29 pub next_id: u16,
30}
31
32#[account]
33#[derive(InitSpace)]
34pub struct CheckPoint {
35 pub workflow_id: u64,
36 pub id: u16,
37 #[max_len(50)]
38 pub title: String,
39 #[max_len(10)]
40 pub options: Option<Vec<VoteOption>>,
41}
42
43impl CheckPoint {
44 pub const SEED_PREFIX: &'static [u8; 10] = b"checkpoint";
45 pub const SIZE: usize = 1000;
46
47 fn from<'info>(x: &'info AccountInfo<'info>) -> Account<'info, Self> {
48 Account::try_from_unchecked(x).unwrap()
49 }
50
51 pub fn serialize(&self, info: AccountInfo) -> Result<()> {
52 let dst: &mut [u8] = &mut info.try_borrow_mut_data().unwrap();
53 let mut writer: BpfWriter<&mut [u8]> = BpfWriter::new(dst);
54 CheckPoint::try_serialize(self, &mut writer)
55 }
56
57 pub fn create(
58 &mut self,
59 workflow_id: u64,
60 id: u16,
61 title: String,
62 options: Option<Vec<VoteOption>>,
63 ) -> Result<()> {
64 self.workflow_id = workflow_id;
65 self.id = id;
66 self.title = title;
67 self.options = options;
68 Ok(())
69 }
70
71 pub fn initialize<'info>(
72 payer: AccountInfo<'info>,
73 checkpoint: &'info AccountInfo<'info>,
74 workflow: AccountInfo<'info>,
75 workflow_program: AccountInfo<'info>,
76 system_program: AccountInfo<'info>,
77 workflow_id: u64,
78 id: u16,
79 title: String,
80 options: Option<Vec<VoteOption>>,
81 ) -> Result<()> {
82 let binding = workflow.key();
83 let seeds: &[&[u8]] = &[&id.to_le_bytes(), b"checkpoint", binding.as_ref()];
84
85 let (_, bump) = Pubkey::find_program_address(seeds, &workflow_program.key());
86
87 funcs::create_account(
88 system_program,
89 payer.to_account_info(),
90 checkpoint.to_account_info(),
91 &seeds,
92 bump,
93 CheckPoint::SIZE,
94 &workflow_program.key(),
95 )?;
96
97 let mut run = CheckPoint::from(&checkpoint);
99 run.create(workflow_id, id, title, options)?;
100
101 run.serialize(checkpoint.to_account_info())?;
103 Ok(())
104 }
105}