kinobi-so / kinobi

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

Generate generic instruction codecs for programs. #87

Open lithdew opened 2 weeks ago

lithdew commented 2 weeks ago

The JavaScript renderer currently renders the following fields and types given a program:

export const PASS_PROGRAM_ADDRESS =
  'nabZn3LTGJpknPtSDiqLXZq98VwfYVicy6zDfPu82Cs' as Address<'nabZn3LTGJpknPtSDiqLXZq98VwfYVicy6zDfPu82Cs'>;

export enum PassInstruction {
  CreatePass,
  UpdatePass,
  MintPass,
  MintPass2,
}

export function identifyPassInstruction(
  instruction: { data: Uint8Array } | Uint8Array
): PassInstruction {
  const data =
    instruction instanceof Uint8Array ? instruction : instruction.data;
  if (containsBytes(data, getU8Encoder().encode(0), 0)) {
    return PassInstruction.CreatePass;
  }
  if (containsBytes(data, getU8Encoder().encode(1), 1)) {
    return PassInstruction.UpdatePass;
  }
  if (containsBytes(data, getU8Encoder().encode(2), 2)) {
    return PassInstruction.MintPass;
  }
  if (containsBytes(data, getU8Encoder().encode(3), 3)) {
    return PassInstruction.MintPass2;
  }
  throw new Error(
    'The provided instruction could not be identified as a pass instruction.'
  );
}

export type ParsedPassInstruction<
  TProgram extends string = 'nabZn3LTGJpknPtSDiqLXZq98VwfYVicy6zDfPu82Cs',
> =
  | ({
      instructionType: PassInstruction.CreatePass;
    } & ParsedCreatePassInstruction<TProgram>)
  | ({
      instructionType: PassInstruction.UpdatePass;
    } & ParsedUpdatePassInstruction<TProgram>)
  | ({
      instructionType: PassInstruction.MintPass;
    } & ParsedMintPassInstruction<TProgram>)
  | ({
      instructionType: PassInstruction.MintPass2;
    } & ParsedMintPass2Instruction<TProgram>);

It would be useful to additionally generate a generic instruction codec to decode/encode any of the programs instructions which may work on top of identifyInstruction.

lorisleiva commented 2 weeks ago

That's a great feature request, thanks! What do you think about generating a parseMyProgramInstruction helper that identifies the instruction first and then switch/cases into the appropriate parse function for that particular instruction? This would only fill the "decode" aspect of the requested codec but would be more idiomatic to the way things are currently generated.