here is my on-chain code to create the PDA account

pub struct Initialize<'info>  {
    #[account(init, payer = user, space = 20 , seeds = [user.key().as_ref()], bump)] 
    pub pda: Account<'info, MyData>,
    pub user: Signer<'info>,
    pub system_program: Program<'info, System>,

the data inside:

#[derive(Clone, AnchorSerialize, AnchorDeserialize)]
pub struct MyData {
    pub userkey = Pubkey,

In my rust client code, can I get the data for "userkey" without deserializing? right now I'm doing

 let account = connection.get_account(*PDA ADDRESS*).unwrap();
 let data = &account.data;
 let stored_key: String = Pubkey::new_from_array(data[10..42].try_into().unwrap()).to_string();

which is honestly super tedious and if I were to add more fields, id have to find the exact spot of the byte array again :(

I know that there is a .fetch() in javascript that lets me do this: let acc_info = await program.account.myData.fetch(PDA); and then I can just get acc_info.userkey;. is there a similar function in anchor_client?

1 Answer 1


You should be able to get any account and deserialise it using the client:

let program = client.program(*Program Address*);
let my_data: MyData = program.account(*PDA ADDRESS*)?;

You can see similar examples in the Anchor repo.

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.