I want to extract some user readable data from CompiledInstruction
. Specifically, I want to understand if CompiledInstruction
is native token transfer (SOL), and print sender, recipient, and amount. I looked at sdk docs, and didn't found methods which can extract such high level information from it. Probably, convert CompiledInstruction
to Instruction
can help, but I didn't find functions for this as well.
1 Answer
CompiledInstruction
requires the account_keys
from the overall Message
. Given that, you can decompile the instruction yourself:
fn decompile(ix: &CompiledInstruction, keys: &[Pubkey]) -> Instruction {
Instruction {
program_id: keys[ix.program_id_index],
accounts: ix.accounts.map(|index| AccountMeta::new(keys[index], false)).collect::<Vec<_>>()
data: ix.data,
}
}
Important note: this simplified function doesn't take into account the role of each account in the instruction (ie signer, writable, read-only signer, writable signer). But it should be a good start!
With the instruction, you can use the SDK that goes along with the program to investigate further. In the case of a SOL transfer, you want to see if the program id is 11111111111111111111111111111111
and the instruction data starts with [2, 0, 0, 0]
. The following 8 bytes are the transfer amount, encoded as a little-endian u64.
Reference is at https://github.com/anza-xyz/agave/blob/164d37c526ba46cffed765416344013e2c1807c7/sdk/program/src/system_instruction.rs#L885