magicblock_magic_program_api/
args.rs1use serde::{Deserialize, Serialize};
2use solana_program::{
3 account_info::AccountInfo,
4 instruction::{AccountMeta, Instruction},
5};
6
7use crate::Pubkey;
8
9#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
10pub struct ActionArgs {
11 pub escrow_index: u8,
12 pub data: Vec<u8>,
13}
14
15impl ActionArgs {
16 pub fn default(data: Vec<u8>) -> Self {
17 Self {
18 escrow_index: 255,
19 data,
20 }
21 }
22 pub fn escrow_index(&self) -> u8 {
23 self.escrow_index
24 }
25
26 pub fn data(&self) -> &Vec<u8> {
27 &self.data
28 }
29
30 pub fn with_escrow_index(mut self, index: u8) -> Self {
31 self.escrow_index = index;
32 self
33 }
34}
35
36#[derive(Clone, Serialize, Deserialize, Debug, PartialEq, Eq)]
37pub struct BaseActionArgs {
38 pub args: ActionArgs,
39 pub compute_units: u32, pub escrow_authority: u8, pub destination_program: Pubkey, pub accounts: Vec<ShortAccountMeta>, }
44
45#[derive(Clone, Serialize, Deserialize, Debug, PartialEq, Eq)]
46pub enum CommitTypeArgs {
47 Standalone(Vec<u8>), WithBaseActions {
49 committed_accounts: Vec<u8>, base_actions: Vec<BaseActionArgs>,
51 },
52}
53
54impl CommitTypeArgs {
55 pub fn committed_accounts_indices(&self) -> &Vec<u8> {
56 match self {
57 Self::Standalone(value) => value,
58 Self::WithBaseActions {
59 committed_accounts, ..
60 } => committed_accounts,
61 }
62 }
63}
64
65#[derive(Clone, Serialize, Deserialize, Debug, PartialEq, Eq)]
66pub enum UndelegateTypeArgs {
67 Standalone,
68 WithBaseActions { base_actions: Vec<BaseActionArgs> },
69}
70
71#[derive(Clone, Serialize, Deserialize, Debug, PartialEq, Eq)]
72pub struct CommitAndUndelegateArgs {
73 pub commit_type: CommitTypeArgs,
74 pub undelegate_type: UndelegateTypeArgs,
75}
76
77impl CommitAndUndelegateArgs {
78 pub fn committed_accounts_indices(&self) -> &Vec<u8> {
79 self.commit_type.committed_accounts_indices()
80 }
81}
82
83#[derive(Clone, Serialize, Deserialize, Debug, PartialEq, Eq)]
84pub enum MagicBaseIntentArgs {
85 BaseActions(Vec<BaseActionArgs>),
86 Commit(CommitTypeArgs),
87 CommitAndUndelegate(CommitAndUndelegateArgs),
88}
89
90#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize)]
96pub struct ShortAccountMeta {
97 pub pubkey: Pubkey,
98 pub is_writable: bool,
101}
102impl From<AccountMeta> for ShortAccountMeta {
103 fn from(value: AccountMeta) -> Self {
104 Self {
105 pubkey: value.pubkey,
106 is_writable: value.is_writable,
107 }
108 }
109}
110
111impl<'a> From<AccountInfo<'a>> for ShortAccountMeta {
112 fn from(value: AccountInfo<'a>) -> Self {
113 Self::from(&value)
114 }
115}
116
117impl<'a> From<&AccountInfo<'a>> for ShortAccountMeta {
118 fn from(value: &AccountInfo<'a>) -> Self {
119 Self {
120 pubkey: *value.key,
121 is_writable: value.is_writable,
122 }
123 }
124}
125
126#[derive(Clone, Serialize, Deserialize, Debug, PartialEq, Eq)]
127pub struct ScheduleTaskArgs {
128 pub task_id: u64,
129 pub execution_interval_millis: u64,
130 pub iterations: u64,
131 pub instructions: Vec<Instruction>,
132}
133
134#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
135pub enum TaskRequest {
136 Schedule(ScheduleTaskRequest),
137 Cancel(CancelTaskRequest),
138}
139
140#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
141pub struct ScheduleTaskRequest {
142 pub id: u64,
144 pub instructions: Vec<Instruction>,
146 pub authority: Pubkey,
148 pub execution_interval_millis: u64,
150 pub iterations: u64,
152}
153
154#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
155pub struct CancelTaskRequest {
156 pub task_id: u64,
158 pub authority: Pubkey,
160}
161
162impl TaskRequest {
163 pub fn id(&self) -> u64 {
164 match self {
165 Self::Schedule(request) => request.id,
166 Self::Cancel(request) => request.task_id,
167 }
168 }
169}