solana/sdk/src/system_instruction.rs

69 lines
1.9 KiB
Rust
Raw Normal View History

2018-12-14 20:39:10 -08:00
use crate::pubkey::Pubkey;
2019-02-28 03:48:44 -08:00
use crate::system_program;
2019-03-19 12:03:20 -07:00
use crate::transaction::{AccountMeta, Instruction};
2018-11-16 08:04:46 -08:00
2019-03-13 13:37:24 -07:00
#[derive(Serialize, Debug, Clone, PartialEq)]
pub enum SystemError {
AccountAlreadyInUse,
ResultWithNegativeLamports,
SourceNotSystemAccount,
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
2018-11-16 08:04:46 -08:00
pub enum SystemInstruction {
/// Create a new account
/// * Transaction::keys[0] - source
/// * Transaction::keys[1] - new account key
2019-03-05 16:28:14 -08:00
/// * lamports - number of lamports to transfer to the new account
2018-11-16 08:04:46 -08:00
/// * space - memory to allocate if greater then zero
/// * program_id - the program id of the new account
CreateAccount {
2019-03-05 16:28:14 -08:00
lamports: u64,
2018-11-16 08:04:46 -08:00
space: u64,
program_id: Pubkey,
},
/// Assign account to a program
/// * Transaction::keys[0] - account to assign
Assign { program_id: Pubkey },
2019-03-05 16:28:14 -08:00
/// Move lamports
2018-11-16 08:04:46 -08:00
/// * Transaction::keys[0] - source
/// * Transaction::keys[1] - destination
2019-03-05 16:28:14 -08:00
Move { lamports: u64 },
2018-11-16 08:04:46 -08:00
}
2019-02-28 03:48:44 -08:00
impl SystemInstruction {
2019-03-03 14:43:51 -08:00
pub fn new_program_account(
from_id: &Pubkey,
to_id: &Pubkey,
2019-03-05 16:28:14 -08:00
lamports: u64,
2019-03-03 14:43:51 -08:00
space: u64,
program_id: &Pubkey,
) -> Instruction {
let account_metas = vec![
AccountMeta::new(*from_id, true),
AccountMeta::new(*to_id, false),
];
Instruction::new(
2019-03-03 14:43:51 -08:00
system_program::id(),
&SystemInstruction::CreateAccount {
2019-03-05 16:28:14 -08:00
lamports,
2019-03-03 14:43:51 -08:00
space,
program_id: *program_id,
2019-03-03 14:43:51 -08:00
},
account_metas,
2019-03-03 14:43:51 -08:00
)
}
pub fn new_move(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(
2019-02-28 03:48:44 -08:00
system_program::id(),
2019-03-05 16:28:14 -08:00
&SystemInstruction::Move { lamports },
account_metas,
2019-02-28 03:48:44 -08:00
)
}
}