gemachain_program/
loader_instruction.rs

1use crate::{
2    instruction::{AccountMeta, Instruction},
3    pubkey::Pubkey,
4    sysvar::rent,
5};
6
7#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
8pub enum LoaderInstruction {
9    /// Write program data into an account
10    ///
11    /// # Account references
12    ///   0. [WRITE, SIGNER] Account to write to
13    Write {
14        /// Offset at which to write the given bytes
15        offset: u32,
16
17        /// Serialized program data
18        #[serde(with = "serde_bytes")]
19        bytes: Vec<u8>,
20    },
21
22    /// Finalize an account loaded with program data for execution
23    ///
24    /// The exact preparation steps is loader specific but on success the loader must set the executable
25    /// bit of the account.
26    ///
27    /// # Account references
28    ///   0. [WRITE, SIGNER] The account to prepare for execution
29    ///   1. [] Rent sysvar
30    Finalize,
31}
32
33pub fn write(
34    account_pubkey: &Pubkey,
35    program_id: &Pubkey,
36    offset: u32,
37    bytes: Vec<u8>,
38) -> Instruction {
39    let account_metas = vec![AccountMeta::new(*account_pubkey, true)];
40    Instruction::new_with_bincode(
41        *program_id,
42        &LoaderInstruction::Write { offset, bytes },
43        account_metas,
44    )
45}
46
47pub fn finalize(account_pubkey: &Pubkey, program_id: &Pubkey) -> Instruction {
48    let account_metas = vec![
49        AccountMeta::new(*account_pubkey, true),
50        AccountMeta::new_readonly(rent::id(), false),
51    ];
52    Instruction::new_with_bincode(*program_id, &LoaderInstruction::Finalize, account_metas)
53}