2018-09-26 09:07:53 -07:00
|
|
|
//! The `system_transaction` module provides functionality for creating system transactions.
|
|
|
|
|
2019-10-19 18:23:27 -07:00
|
|
|
use crate::{
|
|
|
|
hash::Hash,
|
|
|
|
pubkey::Pubkey,
|
|
|
|
signature::{Keypair, KeypairUtil},
|
|
|
|
system_instruction,
|
|
|
|
transaction::Transaction,
|
|
|
|
};
|
2018-11-16 08:04:46 -08:00
|
|
|
|
2019-04-03 08:45:57 -07:00
|
|
|
/// Create and sign new SystemInstruction::CreateAccount transaction
|
|
|
|
pub fn create_account(
|
|
|
|
from_keypair: &Keypair,
|
2019-11-08 02:27:35 -08:00
|
|
|
to_keypair: &Keypair,
|
2019-04-03 08:45:57 -07:00
|
|
|
recent_blockhash: Hash,
|
|
|
|
lamports: u64,
|
|
|
|
space: u64,
|
|
|
|
program_id: &Pubkey,
|
|
|
|
) -> Transaction {
|
|
|
|
let from_pubkey = from_keypair.pubkey();
|
2019-11-08 02:27:35 -08:00
|
|
|
let to_pubkey = to_keypair.pubkey();
|
2019-09-20 14:10:39 -07:00
|
|
|
let create_instruction =
|
2019-11-08 02:27:35 -08:00
|
|
|
system_instruction::create_account(&from_pubkey, &to_pubkey, lamports, space, program_id);
|
2019-04-03 08:45:57 -07:00
|
|
|
let instructions = vec![create_instruction];
|
2019-11-08 02:27:35 -08:00
|
|
|
Transaction::new_signed_instructions(
|
|
|
|
&[from_keypair, to_keypair],
|
|
|
|
instructions,
|
|
|
|
recent_blockhash,
|
|
|
|
)
|
2019-04-03 08:45:57 -07:00
|
|
|
}
|
2019-02-01 07:36:35 -08:00
|
|
|
|
2019-04-03 08:45:57 -07:00
|
|
|
/// Create and sign new system_instruction::Assign transaction
|
2019-05-20 10:03:19 -07:00
|
|
|
pub fn assign(from_keypair: &Keypair, recent_blockhash: Hash, program_id: &Pubkey) -> Transaction {
|
2019-04-03 08:45:57 -07:00
|
|
|
let from_pubkey = from_keypair.pubkey();
|
|
|
|
let assign_instruction = system_instruction::assign(&from_pubkey, program_id);
|
|
|
|
let instructions = vec![assign_instruction];
|
|
|
|
Transaction::new_signed_instructions(&[from_keypair], instructions, recent_blockhash)
|
|
|
|
}
|
2019-03-21 09:03:50 -07:00
|
|
|
|
2019-04-03 08:45:57 -07:00
|
|
|
/// Create and sign new system_instruction::Transfer transaction
|
|
|
|
pub fn transfer(
|
|
|
|
from_keypair: &Keypair,
|
|
|
|
to: &Pubkey,
|
|
|
|
lamports: u64,
|
|
|
|
recent_blockhash: Hash,
|
|
|
|
) -> Transaction {
|
|
|
|
let from_pubkey = from_keypair.pubkey();
|
2019-04-23 12:30:42 -07:00
|
|
|
let transfer_instruction = system_instruction::transfer(&from_pubkey, to, lamports);
|
|
|
|
let instructions = vec![transfer_instruction];
|
2019-04-03 08:45:57 -07:00
|
|
|
Transaction::new_signed_instructions(&[from_keypair], instructions, recent_blockhash)
|
2018-09-26 09:07:53 -07:00
|
|
|
}
|
2019-12-27 12:35:49 -08:00
|
|
|
|
|
|
|
/// Create and sign new nonced system_instruction::Transfer transaction
|
|
|
|
pub fn nonced_transfer(
|
|
|
|
from_keypair: &Keypair,
|
|
|
|
to: &Pubkey,
|
|
|
|
lamports: u64,
|
|
|
|
nonce_account: &Pubkey,
|
|
|
|
nonce_authority: &Keypair,
|
|
|
|
nonce_hash: Hash,
|
|
|
|
) -> Transaction {
|
|
|
|
let from_pubkey = from_keypair.pubkey();
|
|
|
|
let transfer_instruction = system_instruction::transfer(&from_pubkey, to, lamports);
|
|
|
|
let instructions = vec![transfer_instruction];
|
|
|
|
Transaction::new_signed_with_nonce(
|
|
|
|
instructions,
|
|
|
|
Some(&from_pubkey),
|
|
|
|
&[from_keypair, nonce_authority],
|
|
|
|
nonce_account,
|
|
|
|
&nonce_authority.pubkey(),
|
|
|
|
nonce_hash,
|
|
|
|
)
|
|
|
|
}
|