Skip to content

Commit b99bece

Browse files
acheroncryptoochaloup
authored andcommitted
Remove Instruction duplication and add a common print function
1 parent ff4d9f9 commit b99bece

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};
@@ -48,7 +47,6 @@ pub mod config;
4847
mod path;
4948
pub mod rust_template;
5049
pub mod solidity_template;
51-
pub mod transaction_model;
5250

5351
// Version of the docker image.
5452
pub const VERSION: &str = env!("CARGO_PKG_VERSION");
@@ -1944,7 +1942,7 @@ fn idl_set_buffer(
19441942
keypair.pubkey()
19451943
};
19461944
// Instruction to set the buffer onto the IdlAccount.
1947-
let set_buffer_ix = {
1945+
let ix = {
19481946
let accounts = vec![
19491947
AccountMeta::new(buffer, false),
19501948
AccountMeta::new(idl_address, false),
@@ -1960,21 +1958,12 @@ fn idl_set_buffer(
19601958
};
19611959

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

20682057
if print_only {
2069-
let instruction: TransactionInstruction = ix.into();
2070-
println!("Print only mode. No execution!");
2071-
println!(
2072-
"base64 set-authority to idl account {} of program {}:",
2073-
idl_address, instruction.program_id
2074-
);
2075-
println!(
2076-
" {}",
2077-
anchor_lang::__private::base64::encode(&instruction.try_to_vec()?)
2078-
);
2058+
print_idl_instruction("SetAuthority", &ix, &idl_address)?;
20792059
} else {
20802060
// Send transaction.
20812061
let latest_hash = client.get_latest_blockhash()?;
@@ -2143,16 +2123,7 @@ fn idl_close_account(
21432123
};
21442124

21452125
if print_only {
2146-
let instruction: TransactionInstruction = ix.into();
2147-
println!("Print only mode. No execution!");
2148-
println!(
2149-
"base64 close idl account {} of program {}:",
2150-
idl_address, instruction.program_id
2151-
);
2152-
println!(
2153-
" {}",
2154-
anchor_lang::__private::base64::encode(&instruction.try_to_vec()?)
2155-
);
2126+
print_idl_instruction("Close", &ix, &idl_address)?;
21562127
} else {
21572128
// Send transaction.
21582129
let latest_hash = client.get_latest_blockhash()?;
@@ -2274,6 +2245,35 @@ fn write_idl(idl: &Idl, out: OutFile) -> Result<()> {
22742245
Ok(())
22752246
}
22762247

2248+
/// Print `base64+borsh` encoded IDL instruction.
2249+
fn print_idl_instruction(ix_name: &str, ix: &Instruction, idl_address: &Pubkey) -> Result<()> {
2250+
println!("Print only mode. No execution!");
2251+
println!("Instruction: {ix_name}");
2252+
println!("IDL address: {idl_address}");
2253+
println!("Program: {}", ix.program_id);
2254+
2255+
// Serialize with `bincode` because `Instruction` does not implement `BorshSerialize`
2256+
let mut serialized_ix = bincode::serialize(ix)?;
2257+
2258+
// Remove extra bytes in order to make the serialized instruction `borsh` compatible
2259+
// `bincode` uses 8 bytes(LE) for length meanwhile `borsh` uses 4 bytes(LE)
2260+
let mut remove_extra_vec_bytes = |index: usize| {
2261+
serialized_ix.drain((index + 4)..(index + 8));
2262+
};
2263+
2264+
let accounts_index = std::mem::size_of_val(&ix.program_id);
2265+
remove_extra_vec_bytes(accounts_index);
2266+
let data_index = accounts_index + 4 + std::mem::size_of_val(&*ix.accounts);
2267+
remove_extra_vec_bytes(data_index);
2268+
2269+
println!(
2270+
"Base64 encoded instruction: {}",
2271+
base64::encode(serialized_ix)
2272+
);
2273+
2274+
Ok(())
2275+
}
2276+
22772277
fn account(
22782278
cfg_override: &ConfigOverride,
22792279
account_type: String,

cli/src/transaction_model.rs

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

0 commit comments

Comments
 (0)