I am trying to create a Python code to analyze Solana transactions. Currently I am receiving transactions data in Json formats. In a certain part I think I need to decode a phrase. For example, a part of a transaction data is this:
{'accounts': [0, 1], 'data': '3Bxs43ZMjSRQLs6o', 'programIdIndex': 2, 'stackHeight': None}
I guess the "data" key value: "3Bxs43ZMjSRQLs6o" must be decoded somehow and I think it must be something indicating a transfer or something like that. I tried using the base58 and base64 libraries, but I was unsuccessful. I would be grateful if anyone could help.
Solana encodes the instruction data in Base58 string format, the layout is defined as 1 byte for instructions followed up by 8 bytes of little-endian unsigned integer (amt of lamports).
You can use the following code sample to decode the Solana transaction data:
import base58
# Your transaction instruction data (Base58 encoded)
encoded_data = "3Bxs43ZMjSRQLs6o"
# Decode from Base58 to bytes
decoded_bytes = base58.b58decode(encoded_data)
# For a System Program transfer, the layout is:
# byte 0: instruction index (should be 2 for transfer)
# bytes 1-8: amount in lamports (u64 little-endian)
instruction_type = decoded_bytes[0]
lamports = int.from_bytes(decoded_bytes[1:9], byteorder="little")
print("Instruction Type:", instruction_type)
print("Lamports:", lamports)