Migrate loader to high-level instructions
This commit is contained in:
parent
148e08a8a5
commit
58f071b7a0
|
@ -1,3 +1,6 @@
|
||||||
|
use crate::pubkey::Pubkey;
|
||||||
|
use crate::transaction::{AccountMeta, Instruction};
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
|
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
|
||||||
pub enum LoaderInstruction {
|
pub enum LoaderInstruction {
|
||||||
/// Write program data into an account
|
/// Write program data into an account
|
||||||
|
@ -16,3 +19,24 @@ pub enum LoaderInstruction {
|
||||||
/// The transaction must be signed by key[0]
|
/// The transaction must be signed by key[0]
|
||||||
Finalize,
|
Finalize,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl LoaderInstruction {
|
||||||
|
pub fn new_write(
|
||||||
|
account_id: &Pubkey,
|
||||||
|
program_id: &Pubkey,
|
||||||
|
offset: u32,
|
||||||
|
bytes: Vec<u8>,
|
||||||
|
) -> Instruction {
|
||||||
|
let account_metas = vec![AccountMeta::new(*account_id, true)];
|
||||||
|
Instruction::new(
|
||||||
|
*program_id,
|
||||||
|
&LoaderInstruction::Write { offset, bytes },
|
||||||
|
account_metas,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn new_finalize(account_id: &Pubkey, program_id: &Pubkey) -> Instruction {
|
||||||
|
let account_metas = vec![AccountMeta::new(*account_id, true)];
|
||||||
|
Instruction::new(*program_id, &LoaderInstruction::Finalize, account_metas)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
use crate::hash::Hash;
|
use crate::hash::Hash;
|
||||||
use crate::loader_instruction::LoaderInstruction;
|
use crate::loader_instruction::LoaderInstruction;
|
||||||
use crate::pubkey::Pubkey;
|
use crate::pubkey::Pubkey;
|
||||||
use crate::signature::Keypair;
|
use crate::signature::{Keypair, KeypairUtil};
|
||||||
use crate::transaction::Transaction;
|
use crate::transaction::Transaction;
|
||||||
|
|
||||||
pub struct LoaderTransaction {}
|
pub struct LoaderTransaction {}
|
||||||
|
@ -17,15 +17,10 @@ impl LoaderTransaction {
|
||||||
recent_blockhash: Hash,
|
recent_blockhash: Hash,
|
||||||
fee: u64,
|
fee: u64,
|
||||||
) -> Transaction {
|
) -> Transaction {
|
||||||
let instruction = LoaderInstruction::Write { offset, bytes };
|
let write_instruction =
|
||||||
Transaction::new_signed(
|
LoaderInstruction::new_write(&from_keypair.pubkey(), loader, offset, bytes);
|
||||||
from_keypair,
|
let instructions = vec![write_instruction];
|
||||||
&[],
|
Transaction::new_signed_instructions(&[from_keypair], instructions, recent_blockhash, fee)
|
||||||
loader,
|
|
||||||
&instruction,
|
|
||||||
recent_blockhash,
|
|
||||||
fee,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new_finalize(
|
pub fn new_finalize(
|
||||||
|
@ -34,14 +29,8 @@ impl LoaderTransaction {
|
||||||
recent_blockhash: Hash,
|
recent_blockhash: Hash,
|
||||||
fee: u64,
|
fee: u64,
|
||||||
) -> Transaction {
|
) -> Transaction {
|
||||||
let instruction = LoaderInstruction::Finalize;
|
let finalize_instruction = LoaderInstruction::new_finalize(&from_keypair.pubkey(), loader);
|
||||||
Transaction::new_signed(
|
let instructions = vec![finalize_instruction];
|
||||||
from_keypair,
|
Transaction::new_signed_instructions(&[from_keypair], instructions, recent_blockhash, fee)
|
||||||
&[],
|
|
||||||
loader,
|
|
||||||
&instruction,
|
|
||||||
recent_blockhash,
|
|
||||||
fee,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -218,6 +218,18 @@ impl Transaction {
|
||||||
Script::new(instructions).compile()
|
Script::new(instructions).compile()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn new_signed_instructions<T: KeypairUtil>(
|
||||||
|
from_keypairs: &[&T],
|
||||||
|
instructions: Vec<Instruction>,
|
||||||
|
recent_blockhash: Hash,
|
||||||
|
fee: u64,
|
||||||
|
) -> Transaction {
|
||||||
|
let mut tx = Self::new(instructions);
|
||||||
|
tx.fee = fee;
|
||||||
|
tx.sign(from_keypairs, recent_blockhash);
|
||||||
|
tx
|
||||||
|
}
|
||||||
|
|
||||||
pub fn new_with_blockhash_and_fee<T: Serialize>(
|
pub fn new_with_blockhash_and_fee<T: Serialize>(
|
||||||
from_pubkey: &Pubkey,
|
from_pubkey: &Pubkey,
|
||||||
transaction_keys: &[Pubkey],
|
transaction_keys: &[Pubkey],
|
||||||
|
|
Loading…
Reference in New Issue