Skip to content

Commit

Permalink
Remove Instruction duplication and add a common print function
Browse files Browse the repository at this point in the history
  • Loading branch information
acheroncrypto authored and ochaloup committed May 16, 2023
1 parent 830edd7 commit 43aca39
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 101 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ default = []
[dependencies]
clap = { version = "4.2.4", features = ["derive"] }
anyhow = "1.0.32"
base64 = "0.13.1"
bincode = "1.3.3"
syn = { version = "1.0.60", features = ["full", "extra-traits"] }
anchor-lang = { path = "../lang", version = "0.27.0" }
anchor-client = { path = "../client", version = "0.27.0" }
Expand Down
68 changes: 34 additions & 34 deletions cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use crate::config::{
ProgramDeployment, ProgramWorkspace, ScriptsConfig, TestValidator, WithPath, SHUTDOWN_WAIT,
STARTUP_WAIT,
};
use crate::transaction_model::TransactionInstruction;
use anchor_client::Cluster;
use anchor_lang::idl::{IdlAccount, IdlInstruction, ERASED_AUTHORITY};
use anchor_lang::{AccountDeserialize, AnchorDeserialize, AnchorSerialize};
Expand Down Expand Up @@ -49,7 +48,6 @@ pub mod config;
mod path;
pub mod rust_template;
pub mod solidity_template;
pub mod transaction_model;

// Version of the docker image.
pub const VERSION: &str = env!("CARGO_PKG_VERSION");
Expand Down Expand Up @@ -1945,7 +1943,7 @@ fn idl_set_buffer(
keypair.pubkey()
};
// Instruction to set the buffer onto the IdlAccount.
let set_buffer_ix = {
let ix = {
let accounts = vec![
AccountMeta::new(buffer, false),
AccountMeta::new(idl_address, false),
Expand All @@ -1961,21 +1959,12 @@ fn idl_set_buffer(
};

if print_only {
let instruction: TransactionInstruction = set_buffer_ix.into();
println!("Print only mode. No execution!");
println!(
"base64 set-buffer to idl account {} of program {}:",
idl_address, instruction.program_id
);
println!(
" {}",
anchor_lang::__private::base64::encode(&instruction.try_to_vec()?)
);
print_idl_instruction("SetBuffer", &ix, &idl_address)?;
} else {
// Build the transaction.
let latest_hash = client.get_latest_blockhash()?;
let tx = Transaction::new_signed_with_payer(
&[set_buffer_ix],
&[ix],
Some(&keypair.pubkey()),
&[&keypair],
latest_hash,
Expand Down Expand Up @@ -2071,16 +2060,7 @@ fn idl_set_authority(
};

if print_only {
let instruction: TransactionInstruction = ix.into();
println!("Print only mode. No execution!");
println!(
"base64 set-authority to idl account {} of program {}:",
idl_address, instruction.program_id
);
println!(
" {}",
anchor_lang::__private::base64::encode(&instruction.try_to_vec()?)
);
print_idl_instruction("SetAuthority", &ix, &idl_address)?;
} else {
// Send transaction.
let latest_hash = client.get_latest_blockhash()?;
Expand Down Expand Up @@ -2152,16 +2132,7 @@ fn idl_close_account(
};

if print_only {
let instruction: TransactionInstruction = ix.into();
println!("Print only mode. No execution!");
println!(
"base64 close idl account {} of program {}:",
idl_address, instruction.program_id
);
println!(
" {}",
anchor_lang::__private::base64::encode(&instruction.try_to_vec()?)
);
print_idl_instruction("Close", &ix, &idl_address)?;
} else {
// Send transaction.
let latest_hash = client.get_latest_blockhash()?;
Expand Down Expand Up @@ -2291,6 +2262,35 @@ fn write_idl(idl: &Idl, out: OutFile) -> Result<()> {
Ok(())
}

/// Print `base64+borsh` encoded IDL instruction.
fn print_idl_instruction(ix_name: &str, ix: &Instruction, idl_address: &Pubkey) -> Result<()> {
println!("Print only mode. No execution!");
println!("Instruction: {ix_name}");
println!("IDL address: {idl_address}");
println!("Program: {}", ix.program_id);

// Serialize with `bincode` because `Instruction` does not implement `BorshSerialize`
let mut serialized_ix = bincode::serialize(ix)?;

// Remove extra bytes in order to make the serialized instruction `borsh` compatible
// `bincode` uses 8 bytes(LE) for length meanwhile `borsh` uses 4 bytes(LE)
let mut remove_extra_vec_bytes = |index: usize| {
serialized_ix.drain((index + 4)..(index + 8));
};

let accounts_index = std::mem::size_of_val(&ix.program_id);
remove_extra_vec_bytes(accounts_index);
let data_index = accounts_index + 4 + std::mem::size_of_val(&*ix.accounts);
remove_extra_vec_bytes(data_index);

println!(
"Base64 encoded instruction: {}",
base64::encode(serialized_ix)
);

Ok(())
}

fn account(
cfg_override: &ConfigOverride,
account_type: String,
Expand Down
67 changes: 0 additions & 67 deletions cli/src/transaction_model.rs

This file was deleted.

0 comments on commit 43aca39

Please sign in to comment.