Add new preferred transaction constructors
This commit is contained in:
		| @@ -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<T: KeypairUtil>( | ||||
|         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 | ||||
|     } | ||||
|   | ||||
| @@ -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<BuilderInstruction>) -> 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() | ||||
|         ); | ||||
|     } | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user