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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/mighty-bats-relax.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@metaplex-foundation/kinobi': patch
---

Render a union type for parsed instructions in js-experimental
1 change: 1 addition & 0 deletions src/renderers/js-experimental/ImportMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const DEFAULT_MODULE_MAP: Record<string, string> = {
generatedAccounts: '../accounts',
generatedErrors: '../errors',
generatedTypes: '../types',
generatedInstructions: '../instructions',
};

export class ImportMap {
Expand Down
39 changes: 38 additions & 1 deletion src/renderers/js-experimental/fragments/programInstructions.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
getAllInstructionsWithSubs,
InstructionNode,
ProgramNode,
getAllInstructionsWithSubs,
structTypeNodeFromInstructionArgumentNodes,
} from '../../../nodes';
import type { GlobalFragmentScope } from '../getRenderMapVisitor';
Expand Down Expand Up @@ -29,6 +29,7 @@ export function getProgramInstructionsFragment(
[
getProgramInstructionsEnumFragment(scopeWithInstructions),
getProgramInstructionsIdentifierFunctionFragment(scopeWithInstructions),
getProgramInstructionsParsedUnionTypeFragment(scopeWithInstructions),
],
(r) => `${r.join('\n\n')}\n`
);
Expand Down Expand Up @@ -103,3 +104,39 @@ function getProgramInstructionsIdentifierFunctionFragment(
`}`
);
}

function getProgramInstructionsParsedUnionTypeFragment(
scope: Pick<GlobalFragmentScope, 'nameApi'> & {
programNode: ProgramNode;
allInstructions: InstructionNode[];
}
): Fragment {
const { programNode, allInstructions, nameApi } = scope;

const programInstructionsType = nameApi.programInstructionsParsedUnionType(
programNode.name
);

const programInstructionsEnum = nameApi.programInstructionsEnum(
programNode.name
);

const typeVariants = allInstructions.map((instruction): Fragment => {
const instructionEnumVariant = nameApi.programInstructionsEnumVariant(
instruction.name
);

const parsedInstructionType = nameApi.instructionParsedType(
instruction.name
);

return fragment(
`| { instructionType: ${programInstructionsEnum}.${instructionEnumVariant} } & ${parsedInstructionType}`
).addImports('generatedInstructions', parsedInstructionType);
});

return mergeFragments(
[fragment(`export type ${programInstructionsType} =`), ...typeVariants],
(r) => r.join('\n')
);
}
3 changes: 3 additions & 0 deletions src/renderers/js-experimental/nameTransformers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export type NameTransformerKey =
| 'programInstructionsEnum'
| 'programInstructionsEnumVariant'
| 'programInstructionsIdentifierFunction'
| 'programInstructionsParsedUnionType'
| 'programErrorClass'
| 'programErrorCodeEnum'
| 'programErrorCodeMap'
Expand Down Expand Up @@ -147,6 +148,8 @@ export const DEFAULT_NAME_TRANSFORMERS: NameTransformers = {
programInstructionsEnumVariant: (name) => `${pascalCase(name)}`,
programInstructionsIdentifierFunction: (name) =>
`identify${pascalCase(name)}Instruction`,
programInstructionsParsedUnionType: (name) =>
`Parsed${pascalCase(name)}Instruction`,
programErrorClass: (name) => `${pascalCase(name)}ProgramError`,
programErrorCodeEnum: (name) => `${pascalCase(name)}ProgramErrorCode`,
programErrorCodeMap: (name) => `${camelCase(name)}ProgramErrorCodeMap`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,17 @@ import {
MplCandyMachineCoreProgramErrorCode,
getMplCandyMachineCoreProgramErrorFromCode,
} from '../errors';
import {
ParsedAddConfigLinesInstruction,
ParsedDummyInstruction,
ParsedInitializeInstruction,
ParsedMintFromCandyMachineInstruction,
ParsedSetAuthorityInstruction,
ParsedSetCollectionInstruction,
ParsedSetMintAuthorityInstruction,
ParsedUpdateCandyMachineInstruction,
ParsedWithdrawInstruction,
} from '../instructions';
import { memcmp } from '../shared';

export const MPL_CANDY_MACHINE_CORE_PROGRAM_ADDRESS =
Expand Down Expand Up @@ -101,3 +112,32 @@ export function identifyMplCandyMachineCoreInstruction(
'The provided instruction could not be identified as a mplCandyMachineCore instruction.'
);
}

export type ParsedMplCandyMachineCoreInstruction =
| ({
instructionType: MplCandyMachineCoreInstruction.Dummy;
} & ParsedDummyInstruction)
| ({
instructionType: MplCandyMachineCoreInstruction.AddConfigLines;
} & ParsedAddConfigLinesInstruction)
| ({
instructionType: MplCandyMachineCoreInstruction.Initialize;
} & ParsedInitializeInstruction)
| ({
instructionType: MplCandyMachineCoreInstruction.MintFromCandyMachine;
} & ParsedMintFromCandyMachineInstruction)
| ({
instructionType: MplCandyMachineCoreInstruction.SetAuthority;
} & ParsedSetAuthorityInstruction)
| ({
instructionType: MplCandyMachineCoreInstruction.SetCollection;
} & ParsedSetCollectionInstruction)
| ({
instructionType: MplCandyMachineCoreInstruction.SetMintAuthority;
} & ParsedSetMintAuthorityInstruction)
| ({
instructionType: MplCandyMachineCoreInstruction.UpdateCandyMachine;
} & ParsedUpdateCandyMachineInstruction)
| ({
instructionType: MplCandyMachineCoreInstruction.Withdraw;
} & ParsedWithdrawInstruction);
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ import {
MplTokenAuthRulesProgramErrorCode,
getMplTokenAuthRulesProgramErrorFromCode,
} from '../errors';
import {
ParsedCreateFrequencyRuleInstruction,
ParsedCreateRuleSetInstruction,
ParsedValidateInstruction,
} from '../instructions';
import { memcmp } from '../shared';
import { TaKey } from '../types';

Expand Down Expand Up @@ -77,3 +82,14 @@ export function identifyMplTokenAuthRulesInstruction(
'The provided instruction could not be identified as a mplTokenAuthRules instruction.'
);
}

export type ParsedMplTokenAuthRulesInstruction =
| ({
instructionType: MplTokenAuthRulesInstruction.CreateRuleSet;
} & ParsedCreateRuleSetInstruction)
| ({
instructionType: MplTokenAuthRulesInstruction.Validate;
} & ParsedValidateInstruction)
| ({
instructionType: MplTokenAuthRulesInstruction.CreateFrequencyRule;
} & ParsedCreateFrequencyRuleInstruction);
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,60 @@ import {
MplTokenMetadataProgramErrorCode,
getMplTokenMetadataProgramErrorFromCode,
} from '../errors';
import {
ParsedApproveCollectionAuthorityInstruction,
ParsedApproveUseAuthorityInstruction,
ParsedBubblegumSetCollectionSizeInstruction,
ParsedBurnEditionNftInstruction,
ParsedBurnInstruction,
ParsedBurnNftInstruction,
ParsedCloseEscrowAccountInstruction,
ParsedConvertMasterEditionV1ToV2Instruction,
ParsedCreateEscrowAccountInstruction,
ParsedCreateMasterEditionInstruction,
ParsedCreateMasterEditionV3Instruction,
ParsedCreateMetadataAccountInstruction,
ParsedCreateMetadataAccountV2Instruction,
ParsedCreateMetadataAccountV3Instruction,
ParsedCreateReservationListInstruction,
ParsedCreateV1Instruction,
ParsedCreateV2Instruction,
ParsedDelegateInstruction,
ParsedDeprecatedCreateMasterEditionInstruction,
ParsedDeprecatedMintNewEditionFromMasterEditionViaPrintingTokenInstruction,
ParsedDeprecatedMintPrintingTokensInstruction,
ParsedDeprecatedMintPrintingTokensViaTokenInstruction,
ParsedDeprecatedSetReservationListInstruction,
ParsedFreezeDelegatedAccountInstruction,
ParsedMigrateInstruction,
ParsedMintInstruction,
ParsedMintNewEditionFromMasterEditionViaTokenInstruction,
ParsedMintNewEditionFromMasterEditionViaVaultProxyInstruction,
ParsedPuffMetadataInstruction,
ParsedRemoveCreatorVerificationInstruction,
ParsedRevokeCollectionAuthorityInstruction,
ParsedRevokeInstruction,
ParsedRevokeUseAuthorityInstruction,
ParsedSetAndVerifyCollectionInstruction,
ParsedSetAndVerifySizedCollectionItemInstruction,
ParsedSetCollectionSizeInstruction,
ParsedSetTokenStandardInstruction,
ParsedSignMetadataInstruction,
ParsedThawDelegatedAccountInstruction,
ParsedTransferInstruction,
ParsedTransferOutOfEscrowInstruction,
ParsedUnverifyCollectionInstruction,
ParsedUnverifySizedCollectionItemInstruction,
ParsedUpdateMetadataAccountInstruction,
ParsedUpdateMetadataAccountV2Instruction,
ParsedUpdatePrimarySaleHappenedViaTokenInstruction,
ParsedUpdateV1Instruction,
ParsedUseAssetInstruction,
ParsedUtilizeInstruction,
ParsedVerifyCollectionInstruction,
ParsedVerifyInstruction,
ParsedVerifySizedCollectionItemInstruction,
} from '../instructions';
import { memcmp } from '../shared';
import { TmKey, getTmKeyEncoder } from '../types';

Expand Down Expand Up @@ -315,3 +369,161 @@ export function identifyMplTokenMetadataInstruction(
'The provided instruction could not be identified as a mplTokenMetadata instruction.'
);
}

export type ParsedMplTokenMetadataInstruction =
| ({
instructionType: MplTokenMetadataInstruction.CreateMetadataAccount;
} & ParsedCreateMetadataAccountInstruction)
| ({
instructionType: MplTokenMetadataInstruction.UpdateMetadataAccount;
} & ParsedUpdateMetadataAccountInstruction)
| ({
instructionType: MplTokenMetadataInstruction.DeprecatedCreateMasterEdition;
} & ParsedDeprecatedCreateMasterEditionInstruction)
| ({
instructionType: MplTokenMetadataInstruction.DeprecatedMintNewEditionFromMasterEditionViaPrintingToken;
} & ParsedDeprecatedMintNewEditionFromMasterEditionViaPrintingTokenInstruction)
| ({
instructionType: MplTokenMetadataInstruction.UpdatePrimarySaleHappenedViaToken;
} & ParsedUpdatePrimarySaleHappenedViaTokenInstruction)
| ({
instructionType: MplTokenMetadataInstruction.DeprecatedSetReservationList;
} & ParsedDeprecatedSetReservationListInstruction)
| ({
instructionType: MplTokenMetadataInstruction.CreateReservationList;
} & ParsedCreateReservationListInstruction)
| ({
instructionType: MplTokenMetadataInstruction.SignMetadata;
} & ParsedSignMetadataInstruction)
| ({
instructionType: MplTokenMetadataInstruction.DeprecatedMintPrintingTokensViaToken;
} & ParsedDeprecatedMintPrintingTokensViaTokenInstruction)
| ({
instructionType: MplTokenMetadataInstruction.DeprecatedMintPrintingTokens;
} & ParsedDeprecatedMintPrintingTokensInstruction)
| ({
instructionType: MplTokenMetadataInstruction.CreateMasterEdition;
} & ParsedCreateMasterEditionInstruction)
| ({
instructionType: MplTokenMetadataInstruction.MintNewEditionFromMasterEditionViaToken;
} & ParsedMintNewEditionFromMasterEditionViaTokenInstruction)
| ({
instructionType: MplTokenMetadataInstruction.ConvertMasterEditionV1ToV2;
} & ParsedConvertMasterEditionV1ToV2Instruction)
| ({
instructionType: MplTokenMetadataInstruction.MintNewEditionFromMasterEditionViaVaultProxy;
} & ParsedMintNewEditionFromMasterEditionViaVaultProxyInstruction)
| ({
instructionType: MplTokenMetadataInstruction.PuffMetadata;
} & ParsedPuffMetadataInstruction)
| ({
instructionType: MplTokenMetadataInstruction.UpdateMetadataAccountV2;
} & ParsedUpdateMetadataAccountV2Instruction)
| ({
instructionType: MplTokenMetadataInstruction.CreateMetadataAccountV2;
} & ParsedCreateMetadataAccountV2Instruction)
| ({
instructionType: MplTokenMetadataInstruction.CreateMasterEditionV3;
} & ParsedCreateMasterEditionV3Instruction)
| ({
instructionType: MplTokenMetadataInstruction.VerifyCollection;
} & ParsedVerifyCollectionInstruction)
| ({
instructionType: MplTokenMetadataInstruction.Utilize;
} & ParsedUtilizeInstruction)
| ({
instructionType: MplTokenMetadataInstruction.ApproveUseAuthority;
} & ParsedApproveUseAuthorityInstruction)
| ({
instructionType: MplTokenMetadataInstruction.RevokeUseAuthority;
} & ParsedRevokeUseAuthorityInstruction)
| ({
instructionType: MplTokenMetadataInstruction.UnverifyCollection;
} & ParsedUnverifyCollectionInstruction)
| ({
instructionType: MplTokenMetadataInstruction.ApproveCollectionAuthority;
} & ParsedApproveCollectionAuthorityInstruction)
| ({
instructionType: MplTokenMetadataInstruction.RevokeCollectionAuthority;
} & ParsedRevokeCollectionAuthorityInstruction)
| ({
instructionType: MplTokenMetadataInstruction.SetAndVerifyCollection;
} & ParsedSetAndVerifyCollectionInstruction)
| ({
instructionType: MplTokenMetadataInstruction.FreezeDelegatedAccount;
} & ParsedFreezeDelegatedAccountInstruction)
| ({
instructionType: MplTokenMetadataInstruction.ThawDelegatedAccount;
} & ParsedThawDelegatedAccountInstruction)
| ({
instructionType: MplTokenMetadataInstruction.RemoveCreatorVerification;
} & ParsedRemoveCreatorVerificationInstruction)
| ({
instructionType: MplTokenMetadataInstruction.BurnNft;
} & ParsedBurnNftInstruction)
| ({
instructionType: MplTokenMetadataInstruction.VerifySizedCollectionItem;
} & ParsedVerifySizedCollectionItemInstruction)
| ({
instructionType: MplTokenMetadataInstruction.UnverifySizedCollectionItem;
} & ParsedUnverifySizedCollectionItemInstruction)
| ({
instructionType: MplTokenMetadataInstruction.SetAndVerifySizedCollectionItem;
} & ParsedSetAndVerifySizedCollectionItemInstruction)
| ({
instructionType: MplTokenMetadataInstruction.CreateMetadataAccountV3;
} & ParsedCreateMetadataAccountV3Instruction)
| ({
instructionType: MplTokenMetadataInstruction.SetCollectionSize;
} & ParsedSetCollectionSizeInstruction)
| ({
instructionType: MplTokenMetadataInstruction.SetTokenStandard;
} & ParsedSetTokenStandardInstruction)
| ({
instructionType: MplTokenMetadataInstruction.BubblegumSetCollectionSize;
} & ParsedBubblegumSetCollectionSizeInstruction)
| ({
instructionType: MplTokenMetadataInstruction.BurnEditionNft;
} & ParsedBurnEditionNftInstruction)
| ({
instructionType: MplTokenMetadataInstruction.CreateEscrowAccount;
} & ParsedCreateEscrowAccountInstruction)
| ({
instructionType: MplTokenMetadataInstruction.CloseEscrowAccount;
} & ParsedCloseEscrowAccountInstruction)
| ({
instructionType: MplTokenMetadataInstruction.TransferOutOfEscrow;
} & ParsedTransferOutOfEscrowInstruction)
| ({
instructionType: MplTokenMetadataInstruction.CreateV1;
} & ParsedCreateV1Instruction)
| ({
instructionType: MplTokenMetadataInstruction.CreateV2;
} & ParsedCreateV2Instruction)
| ({
instructionType: MplTokenMetadataInstruction.Mint;
} & ParsedMintInstruction)
| ({
instructionType: MplTokenMetadataInstruction.UpdateV1;
} & ParsedUpdateV1Instruction)
| ({
instructionType: MplTokenMetadataInstruction.Burn;
} & ParsedBurnInstruction)
| ({
instructionType: MplTokenMetadataInstruction.UseAsset;
} & ParsedUseAssetInstruction)
| ({
instructionType: MplTokenMetadataInstruction.Transfer;
} & ParsedTransferInstruction)
| ({
instructionType: MplTokenMetadataInstruction.Verify;
} & ParsedVerifyInstruction)
| ({
instructionType: MplTokenMetadataInstruction.Delegate;
} & ParsedDelegateInstruction)
| ({
instructionType: MplTokenMetadataInstruction.Revoke;
} & ParsedRevokeInstruction)
| ({
instructionType: MplTokenMetadataInstruction.Migrate;
} & ParsedMigrateInstruction);
Loading
Loading