magicblock_magic_program_api/
args.rs

1use serde::{Deserialize, Serialize};
2use solana_program::{account_info::AccountInfo, instruction::AccountMeta};
3
4use crate::Pubkey;
5
6#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
7pub struct ActionArgs {
8    pub escrow_index: u8,
9    pub data: Vec<u8>,
10}
11
12#[derive(Clone, Serialize, Deserialize, Debug, PartialEq, Eq)]
13pub struct BaseActionArgs {
14    pub args: ActionArgs,
15    pub compute_units: u32, // compute units your action will use
16    pub escrow_authority: u8, // index of account authorizing action on actor pda
17    pub destination_program: Pubkey, // address of destination program
18    pub accounts: Vec<ShortAccountMeta>, // short account metas
19}
20
21#[derive(Clone, Serialize, Deserialize, Debug, PartialEq, Eq)]
22pub enum CommitTypeArgs {
23    Standalone(Vec<u8>), // indices on accounts
24    WithBaseActions {
25        committed_accounts: Vec<u8>, // indices of accounts
26        base_actions: Vec<BaseActionArgs>,
27    },
28}
29
30impl CommitTypeArgs {
31    pub fn committed_accounts_indices(&self) -> &Vec<u8> {
32        match self {
33            Self::Standalone(value) => value,
34            Self::WithBaseActions {
35                committed_accounts, ..
36            } => committed_accounts,
37        }
38    }
39}
40
41#[derive(Clone, Serialize, Deserialize, Debug, PartialEq, Eq)]
42pub enum UndelegateTypeArgs {
43    Standalone,
44    WithBaseActions { base_actions: Vec<BaseActionArgs> },
45}
46
47#[derive(Clone, Serialize, Deserialize, Debug, PartialEq, Eq)]
48pub struct CommitAndUndelegateArgs {
49    pub commit_type: CommitTypeArgs,
50    pub undelegate_type: UndelegateTypeArgs,
51}
52
53impl CommitAndUndelegateArgs {
54    pub fn committed_accounts_indices(&self) -> &Vec<u8> {
55        self.commit_type.committed_accounts_indices()
56    }
57}
58
59#[derive(Clone, Serialize, Deserialize, Debug, PartialEq, Eq)]
60pub enum MagicBaseIntentArgs {
61    BaseActions(Vec<BaseActionArgs>),
62    Commit(CommitTypeArgs),
63    CommitAndUndelegate(CommitAndUndelegateArgs),
64}
65
66/// A compact account meta used for base-layer actions.
67///
68/// Unlike `solana_sdk::instruction::AccountMeta`, this type **does not** carry an
69/// `is_signer` flag. Users cannot request signatures: the only signer available
70/// is the validator.
71#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize)]
72pub struct ShortAccountMeta {
73    pub pubkey: Pubkey,
74    /// Whether this account should be marked **writable**
75    /// in the Base layer instruction built from this action.
76    pub is_writable: bool,
77}
78impl From<AccountMeta> for ShortAccountMeta {
79    fn from(value: AccountMeta) -> Self {
80        Self {
81            pubkey: value.pubkey,
82            is_writable: value.is_writable,
83        }
84    }
85}
86
87impl<'a> From<AccountInfo<'a>> for ShortAccountMeta {
88    fn from(value: AccountInfo<'a>) -> Self {
89        Self::from(&value)
90    }
91}
92
93impl<'a> From<&AccountInfo<'a>> for ShortAccountMeta {
94    fn from(value: &AccountInfo<'a>) -> Self {
95        Self {
96            pubkey: *value.key,
97            is_writable: value.is_writable,
98        }
99    }
100}