metaplex-foundation / solita

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

feat: make optional accounts specification more generic #95

Open thlorenz opened 1 year ago

thlorenz commented 1 year ago

Summary

Even though https://github.com/metaplex-foundation/solita/pull/94 solved part of the optional accounts requirement (allowing to set optional accounts out of order), it doesn't cover all needs.

What is Covered

What Else do devs Need

Solution

Best explained with examples:

#[account(optional)]

#[account(optional(program_id))]

#[account(optional(system_account))]

#[account(optional(spl_token))]

#[account(optional("my-custom-pubkey"))]

#[account(set_to(system_account))]

The above should cover all the cases that Sam needs (which are more generic).

Additionally at this point the existing solution can simply be re-implemented on top of the above and becomes something akin to syntactic sugar as the below:

#[default_optional_accounts]
#[account(6, optional, ...)]
#[account(7, optional, ...)]
#[account(8, optional, ...)]

simply expands to:

#[account(6, optional(program_id), ...)]
#[account(7, optional(program_id), ...)]
#[account(8, optional(program_id), ...)]

Also anchor's isOptional at this point is equivalent to optional(program_id) and we write a simple preprocessor to solita that treats it as such.

To be clear the defaultOptionalAccounts property would never appear in the IDL as shank pre-expands it to have a detailed optional property per instruction account instead.

Questions

Not all of these have to be answered for the first implementation as we can default to the one we believe is most common, but going forward we may extend the solution to provide more flexibility.

Do we allow devs to turn off that check all together via a solita config property? Do we allow mixing optional with optional(default) and/or set_to(some_key)? Do we add a config option to .solitarc.js to allow users to decide what is allowed and how missing optionals are treated?