Add new preferred transaction constructors
This commit is contained in:
parent
4d53be8350
commit
36fb0a0aef
|
@ -200,14 +200,15 @@ impl Transaction {
|
||||||
transaction.recent_blockhash = recent_blockhash;
|
transaction.recent_blockhash = recent_blockhash;
|
||||||
transaction
|
transaction
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create a signed 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
|
/// * `account_keys` - The keys for the transaction. These are the program state
|
||||||
/// instances or lamport recipient keys.
|
/// instances or lamport recipient keys.
|
||||||
/// * `recent_blockhash` - The PoH hash.
|
/// * `recent_blockhash` - The PoH hash.
|
||||||
/// * `fee` - The transaction fee.
|
/// * `fee` - The transaction fee.
|
||||||
/// * `program_ids` - The keys that identify programs used in the `instruction` vector.
|
/// * `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>(
|
pub fn new_with_instructions<T: KeypairUtil>(
|
||||||
from_keypairs: &[&T],
|
from_keypairs: &[&T],
|
||||||
keys: &[Pubkey],
|
keys: &[Pubkey],
|
||||||
|
@ -232,6 +233,7 @@ impl Transaction {
|
||||||
tx.sign(from_keypairs, recent_blockhash);
|
tx.sign(from_keypairs, recent_blockhash);
|
||||||
tx
|
tx
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn data(&self, instruction_index: usize) -> &[u8] {
|
pub fn data(&self, instruction_index: usize) -> &[u8] {
|
||||||
&self.instructions[instruction_index].data
|
&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.
|
/// Add an instruction.
|
||||||
pub fn push(&mut self, instruction: BuilderInstruction) -> &mut Self {
|
pub fn push(&mut self, instruction: BuilderInstruction) -> &mut Self {
|
||||||
self.instructions.push(instruction);
|
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[1], Instruction::new(1, &0, vec![0]));
|
||||||
assert_eq!(tx.instructions[2], Instruction::new(0, &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()
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue