In JS/TS I can decode an account from bytes using the coder:

const program = new Program(IDL, new PublicKey(""), anchorProvider)

const decoded = program.coder.accounts.decode("accountName", buffer)

How can I do something similar in a Rust client, assuming I have access to the Anchor IDL?

1 Answer 1


Anchor will handle this if the struct is from your crate, pub my_acc: Account<'info, SomeStruct> will decode for you. If you are using a struct from another crate, you can use:

pub fn get_my_struct_from_bytes(v: &[u8]) -> &MyStruct {

Note that if it's an Anchor struct, you need to trim the 8-byte discriminator when you the load the account:

    /// CHECK: no checks
    pub my_acc: AccountInfo<'info>,

let some_data = &ctx.accounts.my_acc.try_borrow_data()?[..][8..];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.