Skip to content

Commit 43aca39

Browse files
acheroncryptoochaloup
authored andcommitted
Remove Instruction duplication and add a common print function
1 parent 830edd7 commit 43aca39

4 files changed

Lines changed: 38 additions & 101 deletions

File tree

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cli/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ default = []
1919
[dependencies]
2020
clap = { version = "4.2.4", features = ["derive"] }
2121
anyhow = "1.0.32"
22+
base64 = "0.13.1"
23+
bincode = "1.3.3"
2224
syn = { version = "1.0.60", features = ["full", "extra-traits"] }
2325
anchor-lang = { path = "../lang", version = "0.27.0" }
2426
anchor-client = { path = "../client", version = "0.27.0" }

cli/src/lib.rs

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use crate::config::{
33
ProgramDeployment, ProgramWorkspace, ScriptsConfig, TestValidator, WithPath, SHUTDOWN_WAIT,
44
STARTUP_WAIT,
55
};
6-
use crate::transaction_model::TransactionInstruction;
76
use anchor_client::Cluster;
87
use anchor_lang::idl::{IdlAccount, IdlInstruction, ERASED_AUTHORITY};
98
use anchor_lang::{AccountDeserialize, AnchorDeserialize, AnchorSerialize};
@@ -49,7 +48,6 @@ pub mod config;
4948
mod path;
5049
pub mod rust_template;
5150
pub mod solidity_template;
52-
pub mod transaction_model;
5351

5452
// Version of the docker image.
5553
pub const VERSION: &str = env!("CARGO_PKG_VERSION");
@@ -1945,7 +1943,7 @@ fn idl_set_buffer(
19451943
keypair.pubkey()
19461944
};
19471945
// Instruction to set the buffer onto the IdlAccount.
1948-
let set_buffer_ix = {
1946+
let ix = {
19491947
let accounts = vec![
19501948
AccountMeta::new(buffer, false),
19511949
AccountMeta::new(idl_address, false),
@@ -1961,21 +1959,12 @@ fn idl_set_buffer(
19611959
};
19621960

19631961
if print_only {
1964-
let instruction: TransactionInstruction = set_buffer_ix.into();
1965-
println!("Print only mode. No execution!");
1966-
println!(
1967-
"base64 set-buffer to idl account {} of program {}:",
1968-
idl_address, instruction.program_id
1969-
);
1970-
println!(
1971-
" {}",
1972-
anchor_lang::__private::base64::encode(&instruction.try_to_vec()?)
1973-
);
1962+
print_idl_instruction("SetBuffer", &ix, &idl_address)?;
19741963
} else {
19751964
// Build the transaction.
19761965
let latest_hash = client.get_latest_blockhash()?;
19771966
let tx = Transaction::new_signed_with_payer(
1978-
&[set_buffer_ix],
1967+
&[ix],
19791968
Some(&keypair.pubkey()),
19801969
&[&keypair],
19811970
latest_hash,
@@ -2071,16 +2060,7 @@ fn idl_set_authority(
20712060
};
20722061

20732062
if print_only {
2074-
let instruction: TransactionInstruction = ix.into();
2075-
println!("Print only mode. No execution!");
2076-
println!(
2077-
"base64 set-authority to idl account {} of program {}:",
2078-
idl_address, instruction.program_id
2079-
);
2080-
println!(
2081-
" {}",
2082-
anchor_lang::__private::base64::encode(&instruction.try_to_vec()?)
2083-
);
2063+
print_idl_instruction("SetAuthority", &ix, &idl_address)?;
20842064
} else {
20852065
// Send transaction.
20862066
let latest_hash = client.get_latest_blockhash()?;
@@ -2152,16 +2132,7 @@ fn idl_close_account(
21522132
};
21532133

21542134
if print_only {
2155-
let instruction: TransactionInstruction = ix.into();
2156-
println!("Print only mode. No execution!");
2157-
println!(
2158-
"base64 close idl account {} of program {}:",
2159-
idl_address, instruction.program_id
2160-
);
2161-
println!(
2162-
" {}",
2163-
anchor_lang::__private::base64::encode(&instruction.try_to_vec()?)
2164-
);
2135+
print_idl_instruction("Close", &ix, &idl_address)?;
21652136
} else {
21662137
// Send transaction.
21672138
let latest_hash = client.get_latest_blockhash()?;
@@ -2291,6 +2262,35 @@ fn write_idl(idl: &Idl, out: OutFile) -> Result<()> {
22912262
Ok(())
22922263
}
22932264

2265+
/// Print `base64+borsh` encoded IDL instruction.
2266+
fn print_idl_instruction(ix_name: &str, ix: &Instruction, idl_address: &Pubkey) -> Result<()> {
2267+
println!("Print only mode. No execution!");
2268+
println!("Instruction: {ix_name}");
2269+
println!("IDL address: {idl_address}");
2270+
println!("Program: {}", ix.program_id);
2271+
2272+
// Serialize with `bincode` because `Instruction` does not implement `BorshSerialize`
2273+
let mut serialized_ix = bincode::serialize(ix)?;
2274+
2275+
// Remove extra bytes in order to make the serialized instruction `borsh` compatible
2276+
// `bincode` uses 8 bytes(LE) for length meanwhile `borsh` uses 4 bytes(LE)
2277+
let mut remove_extra_vec_bytes = |index: usize| {
2278+
serialized_ix.drain((index + 4)..(index + 8));
2279+
};
2280+
2281+
let accounts_index = std::mem::size_of_val(&ix.program_id);
2282+
remove_extra_vec_bytes(accounts_index);
2283+
let data_index = accounts_index + 4 + std::mem::size_of_val(&*ix.accounts);
2284+
remove_extra_vec_bytes(data_index);
2285+
2286+
println!(
2287+
"Base64 encoded instruction: {}",
2288+
base64::encode(serialized_ix)
2289+
);
2290+
2291+
Ok(())
2292+
}
2293+
22942294
fn account(
22952295
cfg_override: &ConfigOverride,
22962296
account_type: String,

cli/src/transaction_model.rs

Lines changed: 0 additions & 67 deletions
This file was deleted.

0 commit comments

Comments
 (0)