diff --git a/sdk/src/loader_instruction.rs b/sdk/src/loader_instruction.rs index 99ea5e347e..09b6465f5b 100644 --- a/sdk/src/loader_instruction.rs +++ b/sdk/src/loader_instruction.rs @@ -1,3 +1,6 @@ +use crate::pubkey::Pubkey; +use crate::transaction::{AccountMeta, Instruction}; + #[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)] pub enum LoaderInstruction { /// Write program data into an account @@ -16,3 +19,24 @@ pub enum LoaderInstruction { /// The transaction must be signed by key[0] Finalize, } + +impl LoaderInstruction { + pub fn new_write( + account_id: &Pubkey, + program_id: &Pubkey, + offset: u32, + bytes: Vec, + ) -> Instruction { + let account_metas = vec![AccountMeta::new(*account_id, true)]; + Instruction::new( + *program_id, + &LoaderInstruction::Write { offset, bytes }, + account_metas, + ) + } + + pub fn new_finalize(account_id: &Pubkey, program_id: &Pubkey) -> Instruction { + let account_metas = vec![AccountMeta::new(*account_id, true)]; + Instruction::new(*program_id, &LoaderInstruction::Finalize, account_metas) + } +} diff --git a/sdk/src/loader_transaction.rs b/sdk/src/loader_transaction.rs index 13a44992aa..1d7293cbc7 100644 --- a/sdk/src/loader_transaction.rs +++ b/sdk/src/loader_transaction.rs @@ -3,7 +3,7 @@ use crate::hash::Hash; use crate::loader_instruction::LoaderInstruction; use crate::pubkey::Pubkey; -use crate::signature::Keypair; +use crate::signature::{Keypair, KeypairUtil}; use crate::transaction::Transaction; pub struct LoaderTransaction {} @@ -17,15 +17,10 @@ impl LoaderTransaction { recent_blockhash: Hash, fee: u64, ) -> Transaction { - let instruction = LoaderInstruction::Write { offset, bytes }; - Transaction::new_signed( - from_keypair, - &[], - loader, - &instruction, - recent_blockhash, - fee, - ) + let write_instruction = + LoaderInstruction::new_write(&from_keypair.pubkey(), loader, offset, bytes); + let instructions = vec![write_instruction]; + Transaction::new_signed_instructions(&[from_keypair], instructions, recent_blockhash, fee) } pub fn new_finalize( @@ -34,14 +29,8 @@ impl LoaderTransaction { recent_blockhash: Hash, fee: u64, ) -> Transaction { - let instruction = LoaderInstruction::Finalize; - Transaction::new_signed( - from_keypair, - &[], - loader, - &instruction, - recent_blockhash, - fee, - ) + let finalize_instruction = LoaderInstruction::new_finalize(&from_keypair.pubkey(), loader); + let instructions = vec![finalize_instruction]; + Transaction::new_signed_instructions(&[from_keypair], instructions, recent_blockhash, fee) } } diff --git a/sdk/src/transaction.rs b/sdk/src/transaction.rs index a89027f8ca..0cc11a93dd 100644 --- a/sdk/src/transaction.rs +++ b/sdk/src/transaction.rs @@ -218,6 +218,18 @@ impl Transaction { Script::new(instructions).compile() } + pub fn new_signed_instructions( + from_keypairs: &[&T], + instructions: Vec, + recent_blockhash: Hash, + fee: u64, + ) -> Transaction { + let mut tx = Self::new(instructions); + tx.fee = fee; + tx.sign(from_keypairs, recent_blockhash); + tx + } + pub fn new_with_blockhash_and_fee( from_pubkey: &Pubkey, transaction_keys: &[Pubkey],