2019-03-21 08:14:14 -06:00
|
|
|
use serde::Serialize;
|
2019-07-27 12:11:51 -07:00
|
|
|
use solana_sdk::client::Client;
|
2019-10-24 22:38:57 -07:00
|
|
|
use solana_sdk::instruction::AccountMeta;
|
2019-04-03 09:45:57 -06:00
|
|
|
use solana_sdk::loader_instruction;
|
2019-05-07 15:00:54 -07:00
|
|
|
use solana_sdk::message::Message;
|
2019-03-02 21:03:36 -07:00
|
|
|
use solana_sdk::pubkey::Pubkey;
|
2019-10-24 22:38:57 -07:00
|
|
|
use solana_sdk::signature::{Keypair, KeypairUtil, Signature};
|
2019-04-03 09:45:57 -06:00
|
|
|
use solana_sdk::system_instruction;
|
2019-10-24 22:38:57 -07:00
|
|
|
use solana_sdk::transport::Result;
|
2019-03-02 21:03:36 -07:00
|
|
|
|
2019-07-27 12:11:51 -07:00
|
|
|
pub fn load_program<T: Client>(
|
|
|
|
bank_client: &T,
|
2019-03-27 07:34:01 -06:00
|
|
|
from_keypair: &Keypair,
|
2019-05-23 23:20:04 -07:00
|
|
|
loader_pubkey: &Pubkey,
|
2019-03-21 08:14:14 -06:00
|
|
|
program: Vec<u8>,
|
|
|
|
) -> Pubkey {
|
|
|
|
let program_keypair = Keypair::new();
|
|
|
|
let program_pubkey = program_keypair.pubkey();
|
2019-03-02 21:03:36 -07:00
|
|
|
|
2019-09-20 14:10:39 -07:00
|
|
|
let instruction = system_instruction::create_account(
|
2019-03-27 07:34:01 -06:00
|
|
|
&from_keypair.pubkey(),
|
2019-03-21 08:14:14 -06:00
|
|
|
&program_pubkey,
|
2019-03-02 21:03:36 -07:00
|
|
|
1,
|
|
|
|
program.len() as u64,
|
2019-05-23 23:20:04 -07:00
|
|
|
loader_pubkey,
|
2019-03-02 21:03:36 -07:00
|
|
|
);
|
2019-03-27 07:34:01 -06:00
|
|
|
bank_client
|
2019-04-03 15:11:08 -06:00
|
|
|
.send_instruction(&from_keypair, instruction)
|
2019-03-27 07:34:01 -06:00
|
|
|
.unwrap();
|
2019-03-02 21:03:36 -07:00
|
|
|
|
2019-10-24 22:38:57 -07:00
|
|
|
let chunk_size = 256; // Size of the chunk needs to fit into the transaction
|
2019-03-02 21:03:36 -07:00
|
|
|
let mut offset = 0;
|
|
|
|
for chunk in program.chunks(chunk_size) {
|
2019-03-21 08:14:14 -06:00
|
|
|
let instruction =
|
2019-05-23 23:20:04 -07:00
|
|
|
loader_instruction::write(&program_pubkey, loader_pubkey, offset, chunk.to_vec());
|
2019-05-07 15:00:54 -07:00
|
|
|
let message = Message::new_with_payer(vec![instruction], Some(&from_keypair.pubkey()));
|
2019-03-27 07:34:01 -06:00
|
|
|
bank_client
|
2019-05-07 15:00:54 -07:00
|
|
|
.send_message(&[from_keypair, &program_keypair], message)
|
2019-03-27 07:34:01 -06:00
|
|
|
.unwrap();
|
2019-03-02 21:03:36 -07:00
|
|
|
offset += chunk_size as u32;
|
|
|
|
}
|
|
|
|
|
2019-05-23 23:20:04 -07:00
|
|
|
let instruction = loader_instruction::finalize(&program_pubkey, loader_pubkey);
|
2019-05-07 15:00:54 -07:00
|
|
|
let message = Message::new_with_payer(vec![instruction], Some(&from_keypair.pubkey()));
|
2019-03-27 07:34:01 -06:00
|
|
|
bank_client
|
2019-05-07 15:00:54 -07:00
|
|
|
.send_message(&[from_keypair, &program_keypair], message)
|
2019-03-27 07:34:01 -06:00
|
|
|
.unwrap();
|
2019-03-21 08:14:14 -06:00
|
|
|
|
|
|
|
program_pubkey
|
|
|
|
}
|
2019-03-02 21:03:36 -07:00
|
|
|
|
2019-10-24 22:38:57 -07:00
|
|
|
pub fn run_program<T: Client, D: Serialize>(
|
|
|
|
bank_client: &T,
|
|
|
|
from_keypair: &Keypair,
|
|
|
|
loader_pubkey: &Pubkey,
|
|
|
|
account_metas: Vec<AccountMeta>,
|
|
|
|
data: &D,
|
|
|
|
) -> Result<Signature> {
|
|
|
|
let instruction = loader_instruction::invoke_main(loader_pubkey, data, account_metas);
|
|
|
|
bank_client.send_instruction(from_keypair, instruction)
|
2019-03-02 21:03:36 -07:00
|
|
|
}
|