6

I have struct MyAccount that I want to deserialize from a list of ctx.remaining_accounts. What's the best way of doing this?

Currently I'm using the following format, copied from jet protocol v2's line here

let account_info = &ctx.remaining_accounts[0];
let account_data = &mut &**account_info.try_borrow_mut_data()?;
let mut my_account = MyAccount::try_deserialize(account_data).unwrap();

Curious what the best practice is.

2 Answers 2

1

Some people prefer the [..] syntax vs the double deref (both are confusing, imo). You might also use the ? operator over unwrap:

    let buf: &[u8] = &ctx.remaining_accounts[0].try_borrow_mut_data()?[..];
    let acc: Whatever = Whatever::try_deserialize(&mut buf)?;
1

You have to setup a rust client.

  1. Create new cargo package inside your program root directory. And make sure to import your program in your client package (Cargo.toml)
  2. Create new solana client
    let payer = Keypair::new();
    let client = Client::new(payer, "http://localhost:8899".into());
  1. get the data slice and deserialize using the program Account
    let mut data: &[u8] = &client.get_account_data(&pubkey).unwrap();
    let some_account = my_program::SomeAccount::try_deserialize(&mut data).unwrap();
    print!("{:?}", some_account);
2
  • This cannot be done inside an Anchor program
    – qubitz
    Commented May 13 at 0:26
  • Maybe so, but it was exactly what I was trying to do when I ran across this question. Thank you Vicyyn!
    – Ahri
    Commented Jun 11 at 20:11

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.