Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ solana-sdk = "2.0.3"
solana-program = "2.0.3"
bincode = "1.3.3"
hex = "0.4.3"
base64 = "0.13.0"
77 changes: 53 additions & 24 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,40 +7,60 @@ use crate::solana::structs::SolanaParsedTransactionPayload;

fn main() {
let args: Vec<String> = env::args().collect();
if args.len() != 4 {
println!("Usage: `cargo run parse <unsigned transaction> --message OR cargo run parse <unsinged transaction> --transaction`");

if !(args.len() == 4 || args.len() == 6) {
println!("Usage: `cargo run parse <unsigned transaction> --message OR cargo run parse <unsigned transaction> --transaction`. You can optionally include the format flag (base64 or hex) after the flag.");
return;
}

let command = &args[1];
match command.as_str() {
"parse" => {
let unsigned_tx = &args[3];
let flag = if args.len() > 3 { Some(&args[2]) } else { None };
match flag {
Some(flag) if flag == "--message" || flag == "--transaction" => {
let is_transaction = flag == "--transaction";
let result = parse_transaction(unsigned_tx.to_string(), is_transaction);
match result {
Ok(response) => {
print_parsed_transaction(response.solana_parsed_transaction.payload.unwrap());
},
Err(e) => println!("Error: {}", e),
match command.as_str() {
"parse" => {
let unsigned_tx = &args[3];
let flag = if args.len() > 3 { Some(&args[2]) } else { None };
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now with the introduction of format flag, I'd like to rename this flag to be a bit more specific.

As I was thinking about it, I was thinking that --message and --transaction are both formats, and --hex and --base64 are types of encodings. So maybe we can rename these variables as such? format_flag and encoding_flag respectively

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any reason not to use clap for more consistent parsing?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't a message a component of a transaction? I'd call something a format if it's representation of same thing. I agree on encoding

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you feel strongly about not adding dependency or have a lighterweight suggestion I can use that, but this is much cleaner to read with clap than manually accounting for indices b6117dc

println!("Parsing transaction: {}", unsigned_tx);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can remove some of these prints!

println!("Flag: {:?}", flag);
let format_flag = if args.len() > 4 {
Some(args[5].as_str())
} else {
Some("hex")
}; // Default to "hex"
println!("Format Flag: {:?}", format_flag);
match (flag, format_flag) {
(Some(flag), Some(format_flag))
if (flag == "--message" || flag == "--transaction")
&& (format_flag == "base64" || format_flag == "hex") =>
{
let is_transaction = flag == "--transaction";
let result = parse_transaction(
unsigned_tx.to_string(),
is_transaction,
format_flag.to_string(),
);
match result {
Ok(response) => {
print_parsed_transaction(
response.solana_parsed_transaction.payload.unwrap(),
);
}
}
_ => {
println!("Invalid or missing flag. Use either --message or --transaction.");
Err(e) => println!("Error: {}", e),
}
}
_ => {
println!("Invalid or missing flag. Use either --message or --transaction followed by format flag (base64 or hex).");
}
}
_ => println!("Unknown command: {}", command),
}
_ => println!("Unknown command: {}", command),
}
}

fn print_parsed_transaction(transaction_payload: SolanaParsedTransactionPayload) {
println!("Solana Parsed Transaction Payload:");
println!(" Unsigned Payload: {}", transaction_payload.unsigned_payload);
println!(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like these are some inadvertent formatting changes. Mind reverting these?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ran cargo fmt but seems more things changed?

" Unsigned Payload: {}",
transaction_payload.unsigned_payload
);
if let Some(metadata) = transaction_payload.transaction_metadata {
println!(" Transaction Metadata:");
println!(" Signatures: {:?}", metadata.signatures);
Expand All @@ -52,8 +72,14 @@ fn print_parsed_transaction(transaction_payload: SolanaParsedTransactionPayload)
println!(" Instruction {}:", i + 1);
println!(" Program Key: {}", instruction.program_key);
println!(" Accounts: {:?}", instruction.accounts);
println!(" Instruction Data (hex): {}", instruction.instruction_data_hex);
println!(" Address Table Lookups: {:?}", instruction.address_table_lookups);
println!(
" Instruction Data (hex): {}",
instruction.instruction_data_hex
);
println!(
" Address Table Lookups: {:?}",
instruction.address_table_lookups
);
}
println!(" Transfers:");
for (i, transfer) in metadata.transfers.iter().enumerate() {
Expand Down Expand Up @@ -82,6 +108,9 @@ fn print_parsed_transaction(transaction_payload: SolanaParsedTransactionPayload)
println!(" Fee: {}", fee);
}
}
println!(" Address Table Lookups: {:?}", metadata.address_table_lookups);
println!(
" Address Table Lookups: {:?}",
metadata.address_table_lookups
);
}
}
Loading