2

can someone tell me where can i find an example to decode metadata in rust & anchor ?

i was using Metadata::from_account_info, but this is no available anymore in recent mpl_token_metadata versions, i tried with Metadata = Metadata::from_bytes(&ctx.accounts.pda_metadata.to_account_info().data).unwrap() but it's not working since ctx.accounts.pda_metadata.to_account_info().data is not &[u8] but &Rc<RefCell<&mut [u8]>>

may be someone can tell me how to go from &Rc<RefCell<&mut [u8]>> to &[u8] ?

1 Answer 1

3

With Anchor since account data is deserialized with the instruction call

let metadata = ctx.accounts.metadata_accounts
// yields https://docs.rs/mpl-token-metadata/latest/mpl_token_metadata/accounts/struct.Metadata.html#fields

If you have an AccountInfo, you can also do the deserialization on its data.

use mpl_token_metadata::accounts::Metadata as MPLMetadata;

let data = ctx.accounts.mtadata_accpunt.data.try_borrow_mut().unwrap();
let metadata_acc = MPLMetadata::deserialize(&mut data.as_ref());

Note: this assumes that you have confirmed that the account is a metadata account as the unwrap() will cause the program to panic if the data is something else.

3
  • 1
    thank you ! i'm trying the 2nd option, but i think it misses something ? i have this error now : no field primary_sale_happened on type std::result::Result<mpl_token_metadata::accounts::Metadata, std::io::Error>, any idea ?
    – Effe2
    Commented Jun 20 at 13:38
  • Updated. Is this still occurring?
    – Jimii
    Commented Jun 20 at 16:05
  • i have added a "?" like this : deserialize(&mut data.as_ref())?; and it works well, thank you, you gave me the right path !
    – Effe2
    Commented Jun 27 at 8:45

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.