Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose coder for user defined types as program.coder.types. #1930

Closed
vovacodes opened this issue May 29, 2022 · 3 comments · Fixed by #1931
Closed

Expose coder for user defined types as program.coder.types. #1930

vovacodes opened this issue May 29, 2022 · 3 comments · Fixed by #1931

Comments

@vovacodes
Copy link
Contributor

I've run into a situation recently when I needed to manually deserialize a slice of account data into a type that I have in my IDL, but which is not an account type. Here's like my Rust part looks like:

#[account]
pub struct Mintlist {
    pub mints: Vec<MintInfo>
}

#[derive(AnchorSerialize, AnchorDeserialize, Clone, PartialEq)]
pub struct MintInfo {
    pub minted: bool,           // 1
    pub metadata_url: [u8; 96], // 96
}

On the client I would like to use something like this:

const mintInfo = program.coder.types.decode("MintInfo", dataSlice)

Looking at the codebase it seems like a trivial thing to add and I'm happy to submit a PR if you think it makes sense.

@kklas
Copy link
Contributor

kklas commented May 29, 2022

You can also do this with anchor-client-gen btw. This is how you'd decode a single type from encoded data https://github.com/kklas/anchor-client-gen/blob/master/examples/tic-tac-toe/generated-client/accounts/Game.ts#L89.

@vovacodes
Copy link
Contributor Author

@kklas thanks for pointing out, this is great. That said, I also believe anchor will benefit from exposing this functionality directly as it already has largely implemented internally, just not exposed publicly.

@kklas
Copy link
Contributor

kklas commented May 29, 2022

Of course!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants