Rust Decoding A Transaction

Below is a code example for decoding the transactions that are exposed on the websocket.

Base64 Serialized Transaction

Ab+cAUlq2vEnbTnFZi670f8x5toICHnZjK3WyIgGanQGv5wBSWra8SdtOcVmLrvR/zHm2ggIedmMrdbIiAZqdAaAAQAHCztk/sV9Y2IfCczP0IUOcGfeaYIexr0Prv9nXZGWLGLsNeVXE8D7G87m7RKkh3bp9nE94KT4mBQ/f84Oo3ZC2tl/75g+sQ4ECjjMHLTxbAWgg/IEOdDJZaG4JTKl6MX/9FQFvCnwfXGlORA/Qr/dGwRBBFB870mhJMsOUMBd/LFyAwZGb+UhFzL/7K26csOb57yM5bvF9xJrLEObOkAAAACMlyWPTiSJ8bs9ECkUjg2DC1oTmdr/EIQEjnvY2+n4WQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABt324ddloZPZy+FGzut5rBy0he1fWzeROoz1hX7/AKkEedVb8jHAbu50xW7OaBUH/bGy3qP0jlECsc2iVrwTjy+YLCGdWB4j9BDzIO5ZpUPa3anXkl1plaiJguteY/OftD/6J/XX9kp0wJsfKVh53ksJqzbfyd1RSzIap7OM5egXlD6fOwT5X44dRCOrmV0ykvlViyoizjkTFSMXPXWjaQcEAAkDwNQBAAAAAAAEAAUCwFwVAAUGAAEADgYHAQEGAgABDAIAAABAS0wAAAAAAAcBAQERCBsHAAECCAkDCggPBwsQCwwNCwsLCwsLCwsBAgAj5RfLl3rjrSoBAAAAB2QAAUBLTAAAAAAAcRkyAAAAAABkAFEHAwEAAAEJAcdYyZdfmfDp57MLefbtHsunsL12DtrY/SDx/1rC+8VMA65trANGBQc=

src/main.rs

extern crate solana_sdk;

use base64::{engine::general_purpose, Engine as _};
use solana_sdk::transaction::VersionedTransaction;
use bincode;

fn main() {
    // Example base64 encoded transaction string
    let text = "Ab+cAUlq2vEnbTnFZi670f8x5toICHnZjK3WyIgGanQGv5wBSWra8SdtOcVmLrvR/zHm2ggIedmMrdbIiAZqdAaAAQAHCztk/sV9Y2IfCczP0IUOcGfeaYIexr0Prv9nXZGWLGLsNeVXE8D7G87m7RKkh3bp9nE94KT4mBQ/f84Oo3ZC2tl/75g+sQ4ECjjMHLTxbAWgg/IEOdDJZaG4JTKl6MX/9FQFvCnwfXGlORA/Qr/dGwRBBFB870mhJMsOUMBd/LFyAwZGb+UhFzL/7K26csOb57yM5bvF9xJrLEObOkAAAACMlyWPTiSJ8bs9ECkUjg2DC1oTmdr/EIQEjnvY2+n4WQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABt324ddloZPZy+FGzut5rBy0he1fWzeROoz1hX7/AKkEedVb8jHAbu50xW7OaBUH/bGy3qP0jlECsc2iVrwTjy+YLCGdWB4j9BDzIO5ZpUPa3anXkl1plaiJguteY/OftD/6J/XX9kp0wJsfKVh53ksJqzbfyd1RSzIap7OM5egXlD6fOwT5X44dRCOrmV0ykvlViyoizjkTFSMXPXWjaQcEAAkDwNQBAAAAAAAEAAUCwFwVAAUGAAEADgYHAQEGAgABDAIAAABAS0wAAAAAAAcBAQERCBsHAAECCAkDCggPBwsQCwwNCwsLCwsLCwsBAgAj5RfLl3rjrSoBAAAAB2QAAUBLTAAAAAAAcRkyAAAAAABkAFEHAwEAAAEJAcdYyZdfmfDp57MLefbtHsunsL12DtrY/SDx/1rC+8VMA65trANGBQc=";
    let base64_str = text.to_string();

    // Attempt to decode the Base64 string
    let decoded = general_purpose::STANDARD.decode(&base64_str);
    match decoded {
        Ok(decoded_bytes) => {
            // Convert bytes to string -> This is the bulk of the processing time
            let bincode_bytes = bincode::deserialize::<VersionedTransaction>(
                &decoded_bytes,
            );
            match bincode_bytes {
                Ok(deserialized) => {
                    println!("Deserialized transaction: {:?}", deserialized);
                }
                Err(e) => {
                    println!("Failed to deserialize binary data: {}", e)
                }
            }
        }
        Err(e) => println!("Failed to decode Base64: {}", e),
    }
}

Cargo.toml

[package]
name = "solana_tx_deserializer"
version = "0.1.0"
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
solana-sdk = "2.0.3"  # Ensure this matches the version you are using
base64 = "0.22.1"
bincode = "1.3.3"
tokio = { version = "1.0", features = ["full"] }
serde_json = "1.0"
log = "0.4"

Last updated