solana/sdk/src/system_transaction.rs

143 lines
4.1 KiB
Rust
Raw Normal View History

//! The `system_transaction` module provides functionality for creating system transactions.
2018-12-14 20:39:10 -08:00
use crate::hash::Hash;
use crate::pubkey::Pubkey;
use crate::signature::Keypair;
use crate::system_instruction::SystemInstruction;
use crate::system_program;
use crate::transaction::{CompiledInstruction, Transaction};
2018-11-16 08:04:46 -08:00
pub struct SystemTransaction {}
impl SystemTransaction {
2018-11-16 08:04:46 -08:00
/// Create and sign new SystemInstruction::CreateAccount transaction
pub fn new_program_account(
from_keypair: &Keypair,
to: &Pubkey,
2019-03-02 10:25:16 -08:00
recent_blockhash: Hash,
2019-03-05 16:28:14 -08:00
lamports: u64,
space: u64,
program_id: &Pubkey,
fee: u64,
) -> Transaction {
2018-11-16 08:04:46 -08:00
let create = SystemInstruction::CreateAccount {
2019-03-05 16:28:14 -08:00
lamports, //TODO, the lamports to allocate might need to be higher then 0 in the future
space,
program_id: *program_id,
};
2019-03-15 10:02:28 -06:00
Transaction::new_signed(
from_keypair,
&[*to],
&system_program::id(),
&create,
2019-03-02 10:25:16 -08:00
recent_blockhash,
fee,
)
}
/// Create and sign a transaction to create a system account
pub fn new_account(
from_keypair: &Keypair,
to: &Pubkey,
2019-03-05 16:28:14 -08:00
lamports: u64,
2019-03-02 10:25:16 -08:00
recent_blockhash: Hash,
fee: u64,
) -> Transaction {
let program_id = system_program::id();
2019-03-02 10:20:10 -08:00
Self::new_program_account(
from_keypair,
to,
2019-03-02 10:25:16 -08:00
recent_blockhash,
2019-03-05 16:28:14 -08:00
lamports,
2019-03-02 10:20:10 -08:00
0,
&program_id,
2019-03-02 10:20:10 -08:00
fee,
)
}
2018-11-16 08:04:46 -08:00
/// Create and sign new SystemInstruction::Assign transaction
pub fn new_assign(
from_keypair: &Keypair,
2019-03-02 10:25:16 -08:00
recent_blockhash: Hash,
program_id: &Pubkey,
fee: u64,
) -> Transaction {
let assign = SystemInstruction::Assign {
program_id: *program_id,
};
2019-03-15 10:02:28 -06:00
Transaction::new_signed(
from_keypair,
&[],
&system_program::id(),
&assign,
2019-03-02 10:25:16 -08:00
recent_blockhash,
fee,
)
}
2018-11-16 08:04:46 -08:00
/// Create and sign new SystemInstruction::Move transaction
pub fn new_move(
from_keypair: &Keypair,
to: &Pubkey,
2019-03-05 16:28:14 -08:00
lamports: u64,
2019-03-02 10:25:16 -08:00
recent_blockhash: Hash,
fee: u64,
) -> Transaction {
2019-03-05 16:28:14 -08:00
let move_lamports = SystemInstruction::Move { lamports };
2019-03-15 10:02:28 -06:00
Transaction::new_signed(
from_keypair,
&[*to],
&system_program::id(),
2019-03-05 16:28:14 -08:00
&move_lamports,
2019-03-02 10:25:16 -08:00
recent_blockhash,
fee,
)
}
2018-11-16 08:04:46 -08:00
/// Create and sign new SystemInstruction::Move transaction to many destinations
pub fn new_move_many(
from: &Keypair,
moves: &[(Pubkey, u64)],
2019-03-02 10:25:16 -08:00
recent_blockhash: Hash,
fee: u64,
) -> Transaction {
let instructions: Vec<_> = moves
.iter()
.enumerate()
.map(|(i, (_, amount))| {
2019-03-05 16:28:14 -08:00
let spend = SystemInstruction::Move { lamports: *amount };
CompiledInstruction::new(0, &spend, vec![0, i as u8 + 1])
})
.collect();
let to_keys: Vec<_> = moves.iter().map(|(to_key, _)| *to_key).collect();
2019-03-15 10:02:28 -06:00
Transaction::new_with_compiled_instructions(
&[from],
&to_keys,
2019-03-02 10:25:16 -08:00
recent_blockhash,
fee,
vec![system_program::id()],
instructions,
)
}
}
#[cfg(test)]
mod tests {
use super::*;
2018-12-14 20:39:10 -08:00
use crate::signature::KeypairUtil;
#[test]
fn test_move_many() {
let from = Keypair::new();
let t1 = Keypair::new();
let t2 = Keypair::new();
let moves = vec![(t1.pubkey(), 1), (t2.pubkey(), 2)];
2019-02-09 09:20:43 -08:00
let tx = SystemTransaction::new_move_many(&from, &moves, Hash::default(), 0);
assert_eq!(tx.account_keys[0], from.pubkey());
assert_eq!(tx.account_keys[1], t1.pubkey());
assert_eq!(tx.account_keys[2], t2.pubkey());
assert_eq!(tx.instructions.len(), 2);
assert_eq!(tx.instructions[0].accounts, vec![0, 1]);
assert_eq!(tx.instructions[1].accounts, vec![0, 2]);
}
}