Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate generic instruction codecs for programs. #87

Open
lithdew opened this issue Jun 20, 2024 · 1 comment
Open

Generate generic instruction codecs for programs. #87

lithdew opened this issue Jun 20, 2024 · 1 comment

Comments

@lithdew
Copy link
Contributor

lithdew commented Jun 20, 2024

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
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants