From ba8ffa2a882d8ecc75935d01f6859d2ea35a1c7d Mon Sep 17 00:00:00 2001 From: Pankaj Garg Date: Tue, 5 Sep 2023 12:43:16 -0700 Subject: [PATCH] Add loader-v4 instruction constructors --- sdk/program/src/loader_v4.rs | 111 ++++++++++++++++++++++++++++++++++- 1 file changed, 110 insertions(+), 1 deletion(-) diff --git a/sdk/program/src/loader_v4.rs b/sdk/program/src/loader_v4.rs index c1728cf1e078c7..a8941fd99600d0 100644 --- a/sdk/program/src/loader_v4.rs +++ b/sdk/program/src/loader_v4.rs @@ -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"); @@ -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, +) -> 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};