2

I get a b64 encoded data for an account from the transaction. I need to decode it into my account data struct called "Entity".

I deserialize the b64 data into bytes then :-

I tried to straight Entity::try_from_slice(bytes) but that throws an unexpected length of input error. This leads me to believe that that data is an AccountInfo struct, but there's no AccountInfo::try_from_slice() as I would have expected and the try_from() fn doesn't take in bytes.

Stuck on trying to figure out how to deserialize raw account data into an AccountInfo struct or better yet directly into my data struct.

This is for deserializing account info data on a rust client! Not the data within an account

2 Answers 2

0

If your struct is POD consider using bytemuck, but also keep in mind if you are using Anchor, you may need to trim the 8-byte discriminator:

    use bytemuck::{Pod, Zeroable};

    // Your struct...
    #[account(zero_copy)] // or POD
    pub struct SomeStruct {
        // whatever....
    }

    // when you're getting the AccountInfo from contexxt...
    let acc_data = &ctx.accounts.acc.try_borrow_data()?[..][8..];
    let acc: &SomeStruct = get_acc_from_bytes(acc_data);


    pub fn get_acc_from_bytes(v: &[u8]) -> &SomeStruct{
        bytemuck::from_bytes(v)
    }
2
  • I need to deserialize the accountinfo, something the runtime does already with context but no examples on how to do with client side rust code.
    – Dev Bharel
    Commented Apr 28, 2023 at 15:07
  • If you're using anchor_client, you can do something like: let program = client.program(Pubkey::try_from("yourkeyblahblahblahblahblah").unwrap()).unwrap(); let some_accounts: Vec<(Pubkey, YourStruct)> = program.accounts(vec![]).await.unwrap();
    – Whiteseal
    Commented Sep 26, 2023 at 18:07
0

I think there's some confusion here.

If you have a transaction encoded in base-64, then that will have just the transaction bytes, which does not contain the account data. When you call getTransaction https://docs.solana.com/api/http#gettransaction you'll notice that the Transaction object https://docs.solana.com/api/http#transaction-structure does not have account data, just the signatures, account keys, and instruction data.

If you want to get an account, you'll need to call getAccountInfo with your client of choice. For example, here's the Rust RPC client version of get_account https://github.com/solana-labs/solana/blob/b790c129e0a0331901401404c9c19e6b82dbf901/rpc-client/src/rpc_client.rs#L3153

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.