codama-idl / codama

Generate clients, CLIs, documentation and more from your Solana programs
MIT License
73 stars 16 forks source link

Add support for optional `variablePdaSeedNode` #199

Open febo opened 1 month ago

febo commented 1 month ago

It might be interesting to support optional values on PDA seeds, e.g.:

k.pdaNode({
  name: "account",
  seeds: [
    k.variablePdaSeedNode(
      "from",
      k.publicKeyTypeNode(),
      "Funding account"
    ),
    k.variablePdaSeedNode(
      "seed",
      k.fixedSizeTypeNode(k.bytesTypeNode(), 32),
      "Optional seed for the account derivation"
    ),
    k.variablePdaSeedNode(
      "group",
      k.numberTypeNode("u64"),
      "Group ID for the address derivation"
    ),
  ],
})

In this example, the seed value is optional – it can either be present or not. This would result in the following AccountSeeds for the findAccountPda helper:

export type AccountSeeds = {
  /** Funding account */
  from: Address;
  /** Optional seed for the account derivation */
  seed?: ReadonlyUint8Array;
  /** Group ID for the address derivation */
  group: number | bigint;
};

You could also combine this with default value support (#107), so you could have optional seed values with a default value if one is not provided.