2019-03-21 07:14:14 -07:00
|
|
|
use serde::Serialize;
|
2020-01-28 17:03:20 -08:00
|
|
|
use solana_sdk::{
|
|
|
|
client::Client,
|
|
|
|
instruction::{AccountMeta, Instruction},
|
|
|
|
loader_instruction,
|
|
|
|
message::Message,
|
|
|
|
pubkey::Pubkey,
|
2020-02-20 13:28:55 -08:00
|
|
|
signature::{Keypair, Signer},
|
2020-01-28 17:03:20 -08:00
|
|
|
system_instruction,
|
|
|
|
};
|
2019-03-02 20:03:36 -08:00
|
|
|
|
2019-07-27 12:11:51 -07:00
|
|
|
pub fn load_program<T: Client>(
|
|
|
|
bank_client: &T,
|
2019-03-27 06:34:01 -07:00
|
|
|
from_keypair: &Keypair,
|
2019-05-23 23:20:04 -07:00
|
|
|
loader_pubkey: &Pubkey,
|
2019-03-21 07:14:14 -07:00
|
|
|
program: Vec<u8>,
|
|
|
|
) -> Pubkey {
|
|
|
|
let program_keypair = Keypair::new();
|
|
|
|
let program_pubkey = program_keypair.pubkey();
|
2019-03-02 20:03:36 -08:00
|
|
|
|
2019-09-20 14:10:39 -07:00
|
|
|
let instruction = system_instruction::create_account(
|
2019-03-27 06:34:01 -07:00
|
|
|
&from_keypair.pubkey(),
|
2019-03-21 07:14:14 -07:00
|
|
|
&program_pubkey,
|
2019-03-02 20:03:36 -08:00
|
|
|
1,
|
|
|
|
program.len() as u64,
|
2019-05-23 23:20:04 -07:00
|
|
|
loader_pubkey,
|
2019-03-02 20:03:36 -08:00
|
|
|
);
|
2019-03-27 06:34:01 -07:00
|
|
|
bank_client
|
2019-11-08 02:27:35 -08:00
|
|
|
.send_message(
|
|
|
|
&[from_keypair, &program_keypair],
|
2020-03-11 14:37:23 -07:00
|
|
|
Message::new(&[instruction]),
|
2019-11-08 02:27:35 -08:00
|
|
|
)
|
2019-03-27 06:34:01 -07:00
|
|
|
.unwrap();
|
2019-03-02 20:03:36 -08:00
|
|
|
|
2019-10-25 16:22:41 -07:00
|
|
|
let chunk_size = 256; // Size of chunk just needs to fit into tx
|
2019-03-02 20:03:36 -08:00
|
|
|
let mut offset = 0;
|
|
|
|
for chunk in program.chunks(chunk_size) {
|
2019-03-21 07:14:14 -07:00
|
|
|
let instruction =
|
2019-05-23 23:20:04 -07:00
|
|
|
loader_instruction::write(&program_pubkey, loader_pubkey, offset, chunk.to_vec());
|
2020-03-11 14:37:23 -07:00
|
|
|
let message = Message::new_with_payer(&[instruction], Some(&from_keypair.pubkey()));
|
2019-03-27 06:34:01 -07:00
|
|
|
bank_client
|
2019-05-07 15:00:54 -07:00
|
|
|
.send_message(&[from_keypair, &program_keypair], message)
|
2019-03-27 06:34:01 -07:00
|
|
|
.unwrap();
|
2019-03-02 20:03:36 -08:00
|
|
|
offset += chunk_size as u32;
|
|
|
|
}
|
|
|
|
|
2019-05-23 23:20:04 -07:00
|
|
|
let instruction = loader_instruction::finalize(&program_pubkey, loader_pubkey);
|
2020-03-11 14:37:23 -07:00
|
|
|
let message = Message::new_with_payer(&[instruction], Some(&from_keypair.pubkey()));
|
2019-03-27 06:34:01 -07:00
|
|
|
bank_client
|
2019-05-07 15:00:54 -07:00
|
|
|
.send_message(&[from_keypair, &program_keypair], message)
|
2019-03-27 06:34:01 -07:00
|
|
|
.unwrap();
|
2019-03-21 07:14:14 -07:00
|
|
|
|
|
|
|
program_pubkey
|
|
|
|
}
|
2019-03-02 20:03:36 -08:00
|
|
|
|
2019-10-25 16:22:41 -07:00
|
|
|
// Return an Instruction that invokes `program_id` with `data` and required
|
|
|
|
// a signature from `from_pubkey`.
|
|
|
|
pub fn create_invoke_instruction<T: Serialize>(
|
|
|
|
from_pubkey: Pubkey,
|
|
|
|
program_id: Pubkey,
|
|
|
|
data: &T,
|
|
|
|
) -> Instruction {
|
|
|
|
let account_metas = vec![AccountMeta::new(from_pubkey, true)];
|
|
|
|
Instruction::new(program_id, data, account_metas)
|
2019-03-02 20:03:36 -08:00
|
|
|
}
|