metaplex-foundation / solita

Genrates an SDK API from solana contract IDL.
Apache License 2.0
142 stars 33 forks source link

[feat]: Include byteSize for custom types #83

Open febo opened 2 years ago

febo commented 2 years ago

Solita generates a BeetArgsStruct that exposes a type's attributes using beet types. We could also include the information of how many bytes the struct would take at most. This would help to create custom serialization/deserialization patterns. This could be either an autogenerated function or the facility to query beet with the attributes of the generated type.

thlorenz commented 2 years ago

This would only work for types that have a clear max defined, i.e. Options Some vs None is an example.

However once you have a string or an array then the max size is potentially infinite. Do you have any ideas on how to handle those?

febo commented 2 years ago

Would it make sense to annotate the Rust struct with the max size? Even when we use strings and arrays in programs, we know what would be the max size since we need to allocate the account space. Then solita could get the information from the annotation and be able to determine the size of the data type.

thlorenz commented 2 years ago

Yes that is an option. So combining these ideas we could do the following:

For the second option we need to extend shank and the types with a maxSize need to derive a ShankType(maxSize=x) (right now they just need to be BorshSerialize/BorshDeserialize.

Does that make sense?

febo commented 2 years ago

Sounds good!