2019-03-14 20:42:01 -06:00
|
|
|
use crate::bank::Bank;
|
2019-03-16 14:30:10 -06:00
|
|
|
use solana_sdk::pubkey::Pubkey;
|
2019-03-17 09:55:42 -06:00
|
|
|
use solana_sdk::script::Script;
|
2019-03-16 14:30:10 -06:00
|
|
|
use solana_sdk::signature::{Keypair, KeypairUtil};
|
|
|
|
use solana_sdk::system_instruction::SystemInstruction;
|
2019-03-17 09:55:42 -06:00
|
|
|
use solana_sdk::transaction::{Instruction, Transaction, TransactionError};
|
2019-03-14 20:42:01 -06:00
|
|
|
|
|
|
|
pub struct BankClient<'a> {
|
|
|
|
bank: &'a Bank,
|
|
|
|
keypair: Keypair,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> BankClient<'a> {
|
|
|
|
pub fn new(bank: &'a Bank, keypair: Keypair) -> Self {
|
|
|
|
Self { bank, keypair }
|
|
|
|
}
|
|
|
|
|
2019-03-16 14:30:10 -06:00
|
|
|
pub fn pubkey(&self) -> Pubkey {
|
|
|
|
self.keypair.pubkey()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn process_transaction(&self, mut tx: Transaction) -> Result<(), TransactionError> {
|
2019-03-14 20:42:01 -06:00
|
|
|
tx.sign(&[&self.keypair], self.bank.last_blockhash());
|
2019-03-16 17:37:18 -06:00
|
|
|
self.bank.process_transaction(&tx)
|
2019-03-16 14:30:10 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Create and process a transaction.
|
|
|
|
pub fn process_script(&self, script: Script) -> Result<(), TransactionError> {
|
2019-03-17 09:55:42 -06:00
|
|
|
self.process_transaction(script.compile())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create and process a transaction from a list of instructions.
|
|
|
|
pub fn process_instructions(
|
|
|
|
&self,
|
|
|
|
instructions: Vec<Instruction>,
|
|
|
|
) -> Result<(), TransactionError> {
|
|
|
|
self.process_script(Script::new(instructions))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create and process a transaction from a single instruction.
|
|
|
|
pub fn process_instruction(&self, instruction: Instruction) -> Result<(), TransactionError> {
|
|
|
|
self.process_instructions(vec![instruction])
|
2019-03-16 14:30:10 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Transfer lamports to pubkey
|
|
|
|
pub fn transfer(&self, lamports: u64, pubkey: &Pubkey) -> Result<(), TransactionError> {
|
|
|
|
let move_instruction = SystemInstruction::new_move(&self.pubkey(), pubkey, lamports);
|
2019-03-17 09:55:42 -06:00
|
|
|
self.process_instruction(move_instruction)
|
2019-03-14 20:42:01 -06:00
|
|
|
}
|
|
|
|
}
|