2018-09-26 10:07:53 -06:00
|
|
|
//! 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;
|
2019-03-21 10:03:50 -06:00
|
|
|
use crate::signature::{Keypair, KeypairUtil};
|
2018-12-14 20:39:10 -08:00
|
|
|
use crate::system_instruction::SystemInstruction;
|
|
|
|
use crate::system_program;
|
2019-03-21 10:03:50 -06:00
|
|
|
use crate::transaction::Transaction;
|
2018-11-16 08:04:46 -08:00
|
|
|
|
2019-02-01 08:36:35 -07:00
|
|
|
pub struct SystemTransaction {}
|
2018-09-26 10:07:53 -06:00
|
|
|
|
2019-02-01 08:36:35 -07:00
|
|
|
impl SystemTransaction {
|
2018-11-16 08:04:46 -08:00
|
|
|
/// Create and sign new SystemInstruction::CreateAccount transaction
|
2019-04-02 17:01:56 -06:00
|
|
|
pub fn new_account(
|
2018-09-26 10:07:53 -06:00
|
|
|
from_keypair: &Keypair,
|
2019-03-09 19:28:43 -08:00
|
|
|
to: &Pubkey,
|
2019-03-02 10:25:16 -08:00
|
|
|
recent_blockhash: Hash,
|
2019-03-05 16:28:14 -08:00
|
|
|
lamports: u64,
|
2018-09-26 10:07:53 -06:00
|
|
|
space: u64,
|
2019-03-09 19:28:43 -08:00
|
|
|
program_id: &Pubkey,
|
2019-03-29 17:29:20 -06:00
|
|
|
_fee: u64,
|
2019-02-01 08:36:35 -07:00
|
|
|
) -> Transaction {
|
2019-03-21 10:03:50 -06:00
|
|
|
let from_pubkey = from_keypair.pubkey();
|
|
|
|
let create_instruction =
|
2019-04-02 17:01:56 -06:00
|
|
|
SystemInstruction::new_account(&from_pubkey, to, lamports, space, program_id);
|
2019-03-21 10:03:50 -06:00
|
|
|
let instructions = vec![create_instruction];
|
2019-03-29 17:29:20 -06:00
|
|
|
Transaction::new_signed_instructions(&[from_keypair], instructions, recent_blockhash)
|
2018-09-26 10:07:53 -06:00
|
|
|
}
|
2019-02-01 08:36:35 -07:00
|
|
|
|
|
|
|
/// Create and sign a transaction to create a system account
|
2019-04-02 17:01:56 -06:00
|
|
|
pub fn new_user_account(
|
2019-02-01 08:36:35 -07:00
|
|
|
from_keypair: &Keypair,
|
2019-03-09 19:28:43 -08:00
|
|
|
to: &Pubkey,
|
2019-03-05 16:28:14 -08:00
|
|
|
lamports: u64,
|
2019-03-02 10:25:16 -08:00
|
|
|
recent_blockhash: Hash,
|
2019-02-01 08:36:35 -07:00
|
|
|
fee: u64,
|
|
|
|
) -> Transaction {
|
|
|
|
let program_id = system_program::id();
|
2019-04-02 17:01:56 -06:00
|
|
|
Self::new_account(
|
2019-03-02 10:20:10 -08:00
|
|
|
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,
|
2019-03-09 19:28:43 -08:00
|
|
|
&program_id,
|
2019-03-02 10:20:10 -08:00
|
|
|
fee,
|
|
|
|
)
|
2019-02-01 08:36:35 -07:00
|
|
|
}
|
2019-03-21 10:03:50 -06:00
|
|
|
|
2018-11-16 08:04:46 -08:00
|
|
|
/// Create and sign new SystemInstruction::Assign transaction
|
2019-02-01 08:36:35 -07:00
|
|
|
pub fn new_assign(
|
|
|
|
from_keypair: &Keypair,
|
2019-03-02 10:25:16 -08:00
|
|
|
recent_blockhash: Hash,
|
2019-03-09 19:28:43 -08:00
|
|
|
program_id: &Pubkey,
|
2019-03-29 17:29:20 -06:00
|
|
|
_fee: u64,
|
2019-02-01 08:36:35 -07:00
|
|
|
) -> Transaction {
|
2019-03-21 10:03:50 -06:00
|
|
|
let from_pubkey = from_keypair.pubkey();
|
|
|
|
let assign_instruction = SystemInstruction::new_assign(&from_pubkey, program_id);
|
|
|
|
let instructions = vec![assign_instruction];
|
2019-03-29 17:29:20 -06:00
|
|
|
Transaction::new_signed_instructions(&[from_keypair], instructions, recent_blockhash)
|
2018-09-26 10:07:53 -06:00
|
|
|
}
|
2019-03-21 10:03:50 -06:00
|
|
|
|
2019-04-02 21:52:07 -06:00
|
|
|
/// Create and sign new SystemInstruction::Transfer transaction
|
|
|
|
pub fn new_transfer(
|
2018-09-26 10:07:53 -06:00
|
|
|
from_keypair: &Keypair,
|
2019-03-09 19:28:43 -08:00
|
|
|
to: &Pubkey,
|
2019-03-05 16:28:14 -08:00
|
|
|
lamports: u64,
|
2019-03-02 10:25:16 -08:00
|
|
|
recent_blockhash: Hash,
|
2019-03-29 17:29:20 -06:00
|
|
|
_fee: u64,
|
2019-02-01 08:36:35 -07:00
|
|
|
) -> Transaction {
|
2019-03-21 10:03:50 -06:00
|
|
|
let from_pubkey = from_keypair.pubkey();
|
2019-04-02 21:52:07 -06:00
|
|
|
let move_instruction = SystemInstruction::new_transfer(&from_pubkey, to, lamports);
|
2019-03-21 10:03:50 -06:00
|
|
|
let instructions = vec![move_instruction];
|
2019-03-29 17:29:20 -06:00
|
|
|
Transaction::new_signed_instructions(&[from_keypair], instructions, recent_blockhash)
|
2018-09-26 10:07:53 -06:00
|
|
|
}
|
|
|
|
}
|