Skip to content

Commit

Permalink
Add loader-v4 instruction constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
pgarg66 committed Sep 5, 2023
1 parent 3b10856 commit ba8ffa2
Showing 1 changed file with 110 additions and 1 deletion.
111 changes: 110 additions & 1 deletion sdk/program/src/loader_v4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
//!
//! This is the loader of the program runtime v2.
use crate::pubkey::Pubkey;
use crate::{
instruction::{AccountMeta, Instruction},
loader_v4_instruction::LoaderV4Instruction,
pubkey::Pubkey,
system_instruction,
};

crate::declare_id!("LoaderV411111111111111111111111111111111111");

Expand Down Expand Up @@ -41,6 +46,110 @@ impl LoaderV4State {
}
}

/// Returns the instructions required to initialize a program/buffer account.
pub fn create_buffer(
payer_address: &Pubkey,
buffer_address: &Pubkey,
lamports: u64,
) -> Instruction {
system_instruction::create_account(payer_address, buffer_address, lamports, 0, &id())
}

/// Returns the instructions required to set the length of the program account.
pub fn truncate(
program_address: &Pubkey,
authority: &Pubkey,
new_size: u32,
recipient_address: &Pubkey,
) -> Instruction {
Instruction::new_with_bincode(
id(),
&LoaderV4Instruction::Truncate { new_size },
vec![
AccountMeta::new(*program_address, true),
AccountMeta::new_readonly(*authority, true),
AccountMeta::new(*recipient_address, false),
],
)
}

/// Returns the instructions required to write a chunk of program data to a
/// buffer account.
pub fn write(
program_address: &Pubkey,
authority: &Pubkey,
offset: u32,
bytes: Vec<u8>,
) -> Instruction {
Instruction::new_with_bincode(
id(),
&LoaderV4Instruction::Write { offset, bytes },
vec![
AccountMeta::new(*program_address, false),
AccountMeta::new_readonly(*authority, true),
],
)
}

/// Returns the instructions required to deploy a program.
pub fn deploy(program_address: &Pubkey, authority: &Pubkey) -> Instruction {
Instruction::new_with_bincode(
id(),
&LoaderV4Instruction::Deploy,
vec![
AccountMeta::new(*program_address, false),
AccountMeta::new(*authority, true),
],
)
}

/// Returns the instructions required to deploy a program.
pub fn deploy_from_source(
program_address: &Pubkey,
authority: &Pubkey,
source_address: &Pubkey,
) -> Instruction {
Instruction::new_with_bincode(
id(),
&LoaderV4Instruction::Deploy,
vec![
AccountMeta::new(*program_address, false),
AccountMeta::new(*authority, true),
AccountMeta::new(*source_address, false),
],
)
}

/// Returns the instructions required to retract a program.
pub fn retract(program_address: &Pubkey, authority: &Pubkey) -> Instruction {
Instruction::new_with_bincode(
id(),
&LoaderV4Instruction::Retract,
vec![
AccountMeta::new(*authority, true),
AccountMeta::new(*program_address, false),
],
)
}

/// Returns the instructions required to transfer authority over a program.
pub fn transfer_authority(
program_address: &Pubkey,
authority: &Pubkey,
new_authority: Option<&Pubkey>,
) -> Instruction {
let mut accounts = vec![
AccountMeta::new(*program_address, false),
AccountMeta::new_readonly(*authority, true),
];

if let Some(new_auth) = new_authority {
accounts.push(AccountMeta::new_readonly(*new_auth, true));
}

Instruction::new_with_bincode(id(), &LoaderV4Instruction::TransferAuthority, accounts)
}

#[cfg(test)]
mod tests {
use {super::*, memoffset::offset_of};
Expand Down

0 comments on commit ba8ffa2

Please sign in to comment.