magicblock_magic_program_api/
args.rs

1use 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, // compute units your action will use
40    pub escrow_authority: u8, // index of account authorizing action on actor pda
41    pub destination_program: Pubkey, // address of destination program
42    pub accounts: Vec<ShortAccountMeta>, // short account metas
43}
44
45#[derive(Clone, Serialize, Deserialize, Debug, PartialEq, Eq)]
46pub enum CommitTypeArgs {
47    Standalone(Vec<u8>), // indices on accounts
48    WithBaseActions {
49        committed_accounts: Vec<u8>, // indices of accounts
50        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/// A compact account meta used for base-layer actions.
91///
92/// Unlike `solana_sdk::instruction::AccountMeta`, this type **does not** carry an
93/// `is_signer` flag. Users cannot request signatures: the only signer available
94/// is the validator.
95#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize)]
96pub struct ShortAccountMeta {
97    pub pubkey: Pubkey,
98    /// Whether this account should be marked **writable**
99    /// in the Base layer instruction built from this action.
100    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    /// Unique identifier for this task
143    pub id: u64,
144    /// Unsigned instructions to execute when triggered
145    pub instructions: Vec<Instruction>,
146    /// Authority that can modify or cancel this task
147    pub authority: Pubkey,
148    /// How frequently the task should be executed, in milliseconds
149    pub execution_interval_millis: u64,
150    /// Number of times this task will be executed
151    pub iterations: u64,
152}
153
154#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
155pub struct CancelTaskRequest {
156    /// Unique identifier for the task to cancel
157    pub task_id: u64,
158    /// Authority that can cancel this task
159    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}