diff --git a/sdk/src/transaction.rs b/sdk/src/transaction.rs index 87ac77a960..b1af0adf18 100644 --- a/sdk/src/transaction.rs +++ b/sdk/src/transaction.rs @@ -200,14 +200,15 @@ impl Transaction { transaction.recent_blockhash = recent_blockhash; transaction } + /// Create a signed transaction - /// * `from_keypair` - The key used to sign the transaction. This key is stored as keys[0] + /// * `from_keypairs` - The keys used to sign the transaction. /// * `account_keys` - The keys for the transaction. These are the program state /// instances or lamport recipient keys. /// * `recent_blockhash` - The PoH hash. /// * `fee` - The transaction fee. /// * `program_ids` - The keys that identify programs used in the `instruction` vector. - /// * `instructions` - The programs and their arguments that the transaction will execute atomically + /// * `instructions` - Instructions that will be executed atomically. pub fn new_with_instructions( from_keypairs: &[&T], keys: &[Pubkey], @@ -232,6 +233,7 @@ impl Transaction { tx.sign(from_keypairs, recent_blockhash); tx } + pub fn data(&self, instruction_index: usize) -> &[u8] { &self.instructions[instruction_index].data } diff --git a/sdk/src/transaction_builder.rs b/sdk/src/transaction_builder.rs index ad16fbd727..9a5748681c 100644 --- a/sdk/src/transaction_builder.rs +++ b/sdk/src/transaction_builder.rs @@ -50,6 +50,20 @@ impl TransactionBuilder { } } + /// Create a new unsigned transaction from a single instruction + pub fn new_singleton(instruction: BuilderInstruction) -> Transaction { + Self::default().push(instruction).compile() + } + + /// Create a new unsigned transaction from a single instruction + pub fn new_with_instructions(instructions: Vec) -> Transaction { + let mut transaction_builder = Self::default(); + for instruction in instructions { + transaction_builder.push(instruction); + } + transaction_builder.compile() + } + /// Add an instruction. pub fn push(&mut self, instruction: BuilderInstruction) -> &mut Self { self.instructions.push(instruction); @@ -240,4 +254,25 @@ mod tests { assert_eq!(tx.instructions[1], Instruction::new(1, &0, vec![0])); assert_eq!(tx.instructions[2], Instruction::new(0, &0, vec![0])); } + + #[test] + fn test_transaction_builder_new_singleton() { + let ix = Instruction::new(Pubkey::default(), &0, vec![]); + assert_eq!( + TransactionBuilder::new_singleton(ix.clone()), + TransactionBuilder::default().push(ix.clone()).compile() + ); + } + + #[test] + fn test_transaction_builder_new_with_instructions() { + let ix = Instruction::new(Pubkey::default(), &0, vec![]); + assert_eq!( + TransactionBuilder::new_with_instructions(vec![ix.clone(), ix.clone()]), + TransactionBuilder::default() + .push(ix.clone()) + .push(ix.clone()) + .compile() + ); + } }