Remove Instruction wrapper structs and name functions after enum fields

This commit is contained in:
Greg Fitzgerald
2019-04-03 09:45:57 -06:00
committed by Grimes
parent 867f6f107b
commit 35298e01a8
53 changed files with 835 additions and 922 deletions

View File

@ -31,65 +31,63 @@ pub enum SystemInstruction {
Transfer { lamports: u64 },
}
impl SystemInstruction {
pub fn new_account(
from_id: &Pubkey,
to_id: &Pubkey,
lamports: u64,
space: u64,
program_id: &Pubkey,
) -> Instruction {
let account_metas = vec![
AccountMeta::new(*from_id, true),
AccountMeta::new(*to_id, false),
];
Instruction::new(
system_program::id(),
&SystemInstruction::CreateAccount {
lamports,
space,
program_id: *program_id,
},
account_metas,
)
}
pub fn create_account(
from_id: &Pubkey,
to_id: &Pubkey,
lamports: u64,
space: u64,
program_id: &Pubkey,
) -> Instruction {
let account_metas = vec![
AccountMeta::new(*from_id, true),
AccountMeta::new(*to_id, false),
];
Instruction::new(
system_program::id(),
&SystemInstruction::CreateAccount {
lamports,
space,
program_id: *program_id,
},
account_metas,
)
}
/// Create and sign a transaction to create a system account
pub fn new_user_account(from_id: &Pubkey, to_id: &Pubkey, lamports: u64) -> Instruction {
let program_id = system_program::id();
Self::new_account(from_id, to_id, lamports, 0, &program_id)
}
/// Create and sign a transaction to create a system account
pub fn create_user_account(from_id: &Pubkey, to_id: &Pubkey, lamports: u64) -> Instruction {
let program_id = system_program::id();
create_account(from_id, to_id, lamports, 0, &program_id)
}
pub fn new_assign(from_id: &Pubkey, program_id: &Pubkey) -> Instruction {
let account_metas = vec![AccountMeta::new(*from_id, true)];
Instruction::new(
system_program::id(),
&SystemInstruction::Assign {
program_id: *program_id,
},
account_metas,
)
}
pub fn assign(from_id: &Pubkey, program_id: &Pubkey) -> Instruction {
let account_metas = vec![AccountMeta::new(*from_id, true)];
Instruction::new(
system_program::id(),
&SystemInstruction::Assign {
program_id: *program_id,
},
account_metas,
)
}
pub fn new_transfer(from_id: &Pubkey, to_id: &Pubkey, lamports: u64) -> Instruction {
let account_metas = vec![
AccountMeta::new(*from_id, true),
AccountMeta::new(*to_id, false),
];
Instruction::new(
system_program::id(),
&SystemInstruction::Transfer { lamports },
account_metas,
)
}
pub fn transfer(from_id: &Pubkey, to_id: &Pubkey, lamports: u64) -> Instruction {
let account_metas = vec![
AccountMeta::new(*from_id, true),
AccountMeta::new(*to_id, false),
];
Instruction::new(
system_program::id(),
&SystemInstruction::Transfer { lamports },
account_metas,
)
}
/// Create and sign new SystemInstruction::Transfer transaction to many destinations
pub fn new_transfer_many(from_id: &Pubkey, to_lamports: &[(Pubkey, u64)]) -> Vec<Instruction> {
to_lamports
.iter()
.map(|(to_id, lamports)| SystemInstruction::new_transfer(from_id, to_id, *lamports))
.collect()
}
/// Create and sign new SystemInstruction::Transfer transaction to many destinations
pub fn transfer_many(from_id: &Pubkey, to_lamports: &[(Pubkey, u64)]) -> Vec<Instruction> {
to_lamports
.iter()
.map(|(to_id, lamports)| transfer(from_id, to_id, *lamports))
.collect()
}
#[cfg(test)]
@ -107,7 +105,7 @@ mod tests {
let carol_pubkey = Pubkey::new_rand();
let to_lamports = vec![(bob_pubkey, 1), (carol_pubkey, 2)];
let instructions = SystemInstruction::new_transfer_many(&alice_pubkey, &to_lamports);
let instructions = transfer_many(&alice_pubkey, &to_lamports);
assert_eq!(instructions.len(), 2);
assert_eq!(get_keys(&instructions[0]), vec![alice_pubkey, bob_pubkey]);
assert_eq!(get_keys(&instructions[1]), vec![alice_pubkey, carol_pubkey]);