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

Render a union type for parsed instructions in js-experimental #162

Merged

Conversation

mcintyre94
Copy link
Contributor

This PR adds a new union type to each program, where each variant is an instructionType from its instruction types enum together with the parsed type. Eg:

export type ParsedMplTokenAuthRulesInstruction =
  | ({
      instructionType: MplTokenAuthRulesInstruction.CreateRuleSet;
    } & ParsedCreateRuleSetInstruction)
  | ({
      instructionType: MplTokenAuthRulesInstruction.Validate;
    } & ParsedValidateInstruction)
  | ({
      instructionType: MplTokenAuthRulesInstruction.CreateFrequencyRule;
    } & ParsedCreateFrequencyRuleInstruction);

This allows consumers to parse instructions and then pass them around as eg ParsedMplTokenAuthRulesInstruction. They can then use instructionType to distinguish the variants, and then access the known parsed type. Passing around ParsedCreateRuleSetInstruction is insufficient because it doesn't have an instruction type field that we can discriminate on.

This gives more flexibility, particularly to consumers that are dealing with parsed instructions from multiple programs.

Copy link
Collaborator

@lorisleiva lorisleiva left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great, thank you! Just a few comments and LGTM.

You might want to write a small test on the programsPage.test.ts file for that union type. It should be fairly similar to that one:

test('it renders an enum of all available instructions for a program', (t) => {
// Given the following program.
const node = programNode({
name: 'splToken',
publicKey: 'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA',
instructions: [
instructionNode({ name: 'mintTokens' }),
instructionNode({ name: 'transferTokens' }),
instructionNode({ name: 'updateAuthority' }),
],
});
// When we render it.
const renderMap = visit(node, getRenderMapVisitor());
// Then we expect the following program instruction enum.
renderMapContains(t, renderMap, 'programs/splToken.ts', [
'export enum SplTokenInstruction { MintTokens, TransferTokens, UpdateAuthority };',
]);
});

Finally, don't forget to mark this PR as a patch update by running changeset and providing a description of the change (you can just copy the PR title).

Copy link

changeset-bot bot commented Feb 28, 2024

🦋 Changeset detected

Latest commit: 3856336

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@metaplex-foundation/kinobi Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@mcintyre94
Copy link
Contributor Author

Thanks Loris! Updated those, and added a test and changeset :)

Copy link
Collaborator

@lorisleiva lorisleiva left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfection 🫶

@lorisleiva lorisleiva merged commit c52bea0 into metaplex-foundation:main Feb 28, 2024
6 checks passed
@github-actions github-actions bot mentioned this pull request Feb 28, 2024
@mcintyre94 mcintyre94 deleted the cm-parsed-instruction-union branch February 28, 2024 18:14
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

Successfully merging this pull request may close these issues.

2 participants