coral-xyz / anchor

⚓ Solana Sealevel Framework
https://anchor-lang.com
Apache License 2.0
3.36k stars 1.25k forks source link

`idl-build`/`declare_program!()` issue with `zero_copy` accounts using Structs #2974

Closed CanardMandarin closed 1 month ago

CanardMandarin commented 1 month ago

Hello,

I recently wanted to add a custom struct inside a zero_copy account. However, importing the generated IDL with declare_program!() macro throws an error. It seems that declare_program!() does not implement Zeroable + Pod for the custom struct.

I created a small reproducible example to make testing easier:

https://github.com/CanardMandarin/anchor/blob/9fffb883ea3224c81d1fc92eca8bb792b589f054/tests/declare-program/programs/external/src/lib.rs#L56-L70

Here is the generated IDL:

https://github.com/CanardMandarin/anchor/blob/9fffb883ea3224c81d1fc92eca8bb792b589f054/tests/declare-program/idls/external.json

I am not very familiar with the new IDL generation, but I will try to look into it!

acheroncrypto commented 1 month ago

IDL generation does not check custom impls, and using AnchorSerialize currently means using borsh.

It should work if you use #[zero_copy] here.

This is not related to declare_program! because it just defaults to using borsh when there is no serialization field specified in the IDL here.

CanardMandarin commented 1 month ago

Thanks!