diff --git a/.changeset/sixty-gorillas-judge.md b/.changeset/sixty-gorillas-judge.md new file mode 100644 index 000000000..90481dc7c --- /dev/null +++ b/.changeset/sixty-gorillas-judge.md @@ -0,0 +1,5 @@ +--- +'@metaplex-foundation/kinobi': patch +--- + +Use name transformers for enum variant and data enum discriminator diff --git a/src/renderers/js-experimental/fragments/typeDataEnumHelpers.njk b/src/renderers/js-experimental/fragments/typeDataEnumHelpers.njk index 1dcb0afe2..fcb2cf718 100644 --- a/src/renderers/js-experimental/fragments/typeDataEnumHelpers.njk +++ b/src/renderers/js-experimental/fragments/typeDataEnumHelpers.njk @@ -1,23 +1,23 @@ // Data Enum Helpers. {% for variant in typeNode.variants %} {% if variant.kind === 'enumStructVariantTypeNode' %} - export function {{ dataEnumFunction }}(kind: '{{ variant.name | pascalCase }}', data: GetDataEnumKindContent<{{ looseName }}, '{{ variant.name | pascalCase }}'>): GetDataEnumKind<{{ looseName }}, '{{ variant.name | pascalCase }}'>; + export function {{ dataEnumFunction }}(kind: '{{ getVariant(variant.name) }}', data: GetDataEnumKindContent<{{ looseName }}, '{{ getVariant(variant.name) }}'>): GetDataEnumKind<{{ looseName }}, '{{ getVariant(variant.name) }}'>; {% elif variant.kind === 'enumTupleVariantTypeNode' %} - export function {{ dataEnumFunction }}(kind: '{{ variant.name | pascalCase }}', data: GetDataEnumKindContent<{{ looseName }}, '{{ variant.name | pascalCase }}'>['fields']): GetDataEnumKind<{{ looseName }}, '{{ variant.name | pascalCase }}'>; + export function {{ dataEnumFunction }}(kind: '{{ getVariant(variant.name) }}', data: GetDataEnumKindContent<{{ looseName }}, '{{ getVariant(variant.name) }}'>['fields']): GetDataEnumKind<{{ looseName }}, '{{ getVariant(variant.name) }}'>; {% else %} - export function {{ dataEnumFunction }}(kind: '{{ variant.name | pascalCase }}'): GetDataEnumKind<{{ looseName }}, '{{ variant.name | pascalCase }}'>; + export function {{ dataEnumFunction }}(kind: '{{ getVariant(variant.name) }}'): GetDataEnumKind<{{ looseName }}, '{{ getVariant(variant.name) }}'>; {% endif %} {% endfor %} -export function {{ dataEnumFunction }}( +export function {{ dataEnumFunction }}( kind: K, data?: any, -): Extract<{{ looseName }}, { __kind: K }> { - return Array.isArray(data) ? { __kind: kind, fields: data } : { __kind: kind, ...(data ?? {}) }; +): Extract<{{ looseName }}, { {{ dataEnumDiscriminator }}: K }> { + return Array.isArray(data) ? { {{ dataEnumDiscriminator }}: kind, fields: data } : { {{ dataEnumDiscriminator }}: kind, ...(data ?? {}) }; } -export function {{ isDataEnumFunction }}( +export function {{ isDataEnumFunction }}( kind: K, value: {{ strictName }}, -): value is {{ strictName }} & { __kind: K } { - return value.__kind === kind; +): value is {{ strictName }} & { {{ dataEnumDiscriminator }}: K } { + return value.{{ dataEnumDiscriminator }} === kind; }; diff --git a/src/renderers/js-experimental/fragments/typeDataEnumHelpers.ts b/src/renderers/js-experimental/fragments/typeDataEnumHelpers.ts index 9981b9101..d8bbb7a04 100644 --- a/src/renderers/js-experimental/fragments/typeDataEnumHelpers.ts +++ b/src/renderers/js-experimental/fragments/typeDataEnumHelpers.ts @@ -19,6 +19,8 @@ export function getTypeDataEnumHelpersFragment( return fragmentFromTemplate('typeDataEnumHelpers.njk', { strictName: nameApi.dataType(name), looseName: nameApi.dataArgsType(name), + dataEnumDiscriminator: nameApi.dataEnumDiscriminator(name), + getVariant: (variant: string) => nameApi.dataEnumVariant(variant), dataEnumFunction: nameApi.dataEnumFunction(name), isDataEnumFunction: nameApi.isDataEnumFunction(name), typeNode, diff --git a/src/renderers/js-experimental/getTypeManifestVisitor.ts b/src/renderers/js-experimental/getTypeManifestVisitor.ts index 0065e7240..ef7faa0e2 100644 --- a/src/renderers/js-experimental/getTypeManifestVisitor.ts +++ b/src/renderers/js-experimental/getTypeManifestVisitor.ts @@ -7,7 +7,7 @@ import { structTypeNode, structTypeNodeFromInstructionArgumentNodes, } from '../../nodes'; -import { camelCase, pascalCase, pipe } from '../../shared'; +import { camelCase, pipe } from '../../shared'; import { Visitor, extendVisitor, staticVisitor, visit } from '../../visitors'; import { ImportMap } from './ImportMap'; import { TypeManifest, mergeManifests } from './TypeManifest'; @@ -140,9 +140,6 @@ export function getTypeManifestVisitor(input: { const currentParentName = parentName; parentName = null; - const variantNames = enumType.variants.map(({ name }) => - pascalCase(name) - ); const encoderImports = new ImportMap(); const decoderImports = new ImportMap(); const encoderOptions: string[] = []; @@ -173,6 +170,9 @@ export function getTypeManifestVisitor(input: { 'defined type that is a scalar enum through a visitor.' ); } + const variantNames = enumType.variants.map(({ name }) => + nameApi.scalarEnumVariant(name) + ); return { isEnum: true, strictType: fragment(`{ ${variantNames.join(', ')} }`), @@ -199,7 +199,7 @@ export function getTypeManifestVisitor(input: { } const variants = enumType.variants.map((variant): TypeManifest => { - const variantName = pascalCase(variant.name); + const variantName = nameApi.dataEnumVariant(variant.name); parentName = currentParentName ? { strict: `GetDataEnumKindContent<${currentParentName.strict}, '${variantName}'>`, @@ -242,8 +242,11 @@ export function getTypeManifestVisitor(input: { }, visitEnumEmptyVariantType(enumEmptyVariantType) { - const name = pascalCase(enumEmptyVariantType.name); - const kindAttribute = `__kind: "${name}"`; + const discriminator = nameApi.dataEnumDiscriminator( + enumEmptyVariantType.name + ); + const name = nameApi.dataEnumVariant(enumEmptyVariantType.name); + const kindAttribute = `${discriminator}: "${name}"`; return { isEnum: false, strictType: fragment(`{ ${kindAttribute} }`), @@ -260,8 +263,11 @@ export function getTypeManifestVisitor(input: { }, visitEnumStructVariantType(enumStructVariantType, { self }) { - const name = pascalCase(enumStructVariantType.name); - const kindAttribute = `__kind: "${name}"`; + const discriminator = nameApi.dataEnumDiscriminator( + enumStructVariantType.name + ); + const name = nameApi.dataEnumVariant(enumStructVariantType.name); + const kindAttribute = `${discriminator}: "${name}"`; const structManifest = visit(enumStructVariantType.struct, self); structManifest.strictType.mapRender( (r) => `{ ${kindAttribute},${r.slice(1, -1)}}` @@ -275,8 +281,11 @@ export function getTypeManifestVisitor(input: { }, visitEnumTupleVariantType(enumTupleVariantType, { self }) { - const name = pascalCase(enumTupleVariantType.name); - const kindAttribute = `__kind: "${name}"`; + const discriminator = nameApi.dataEnumDiscriminator( + enumTupleVariantType.name + ); + const name = nameApi.dataEnumVariant(enumTupleVariantType.name); + const kindAttribute = `${discriminator}: "${name}"`; const struct = structTypeNode([ structFieldTypeNode({ name: 'fields', diff --git a/src/renderers/js-experimental/nameTransformers.ts b/src/renderers/js-experimental/nameTransformers.ts index e7f995a7d..01e8f8e1a 100644 --- a/src/renderers/js-experimental/nameTransformers.ts +++ b/src/renderers/js-experimental/nameTransformers.ts @@ -1,10 +1,10 @@ import { - capitalize, - titleCase, - pascalCase, camelCase, + capitalize, kebabCase, + pascalCase, snakeCase, + titleCase, } from '../../shared'; export type NameTransformerHelpers = { @@ -39,6 +39,9 @@ export type NameTransformerKey = | 'accountFetchFromSeedsFunction' | 'accountSafeFetchFromSeedsFunction' | 'accountGetSizeFunction' + | 'scalarEnumVariant' + | 'dataEnumDiscriminator' + | 'dataEnumVariant' | 'dataEnumFunction' | 'isDataEnumFunction' | 'instructionAsyncInputType' @@ -109,6 +112,9 @@ export const DEFAULT_NAME_TRANSFORMERS: NameTransformers = { accountSafeFetchFromSeedsFunction: (name) => `safeFetch${pascalCase(name)}FromSeeds`, accountGetSizeFunction: (name) => `get${pascalCase(name)}Size`, + scalarEnumVariant: (name) => `${pascalCase(name)}`, + dataEnumDiscriminator: () => '__kind', + dataEnumVariant: (name) => `${pascalCase(name)}`, dataEnumFunction: (name) => `${camelCase(name)}`, isDataEnumFunction: (name) => `is${pascalCase(name)}`, instructionAsyncInputType: (name) => `${pascalCase(name)}AsyncInput`, @@ -132,11 +138,11 @@ export const DEFAULT_NAME_TRANSFORMERS: NameTransformers = { `${snakeCase(name).toUpperCase()}_PROGRAM_ADDRESS`, programCreateFunction: (name) => `create${pascalCase(name)}Program`, programAccountsEnum: (name) => `${pascalCase(name)}Account`, - programAccountsEnumVariant: (name) => `${snakeCase(name).toUpperCase()}`, + programAccountsEnumVariant: (name) => `${pascalCase(name)}`, programAccountsIdentifierFunction: (name) => `identify${pascalCase(name)}Account`, programInstructionsEnum: (name) => `${pascalCase(name)}Instruction`, - programInstructionsEnumVariant: (name) => `${snakeCase(name).toUpperCase()}`, + programInstructionsEnumVariant: (name) => `${pascalCase(name)}`, programInstructionsIdentifierFunction: (name) => `identify${pascalCase(name)}Instruction`, programErrorClass: (name) => `${pascalCase(name)}ProgramError`, diff --git a/src/renderers/js-experimental/renderValueNodeVisitor.ts b/src/renderers/js-experimental/renderValueNodeVisitor.ts index b62835356..be20f4807 100644 --- a/src/renderers/js-experimental/renderValueNodeVisitor.ts +++ b/src/renderers/js-experimental/renderValueNodeVisitor.ts @@ -1,5 +1,5 @@ import { RegisteredValueNodeKind, isNode, isScalarEnum } from '../../nodes'; -import { LinkableDictionary, MainCaseString, pascalCase } from '../../shared'; +import { LinkableDictionary, MainCaseString } from '../../shared'; import { Visitor, visit } from '../../visitors'; import { Fragment, fragment, mergeFragments } from './fragments'; import { NameApi } from './nameTransformers'; @@ -25,7 +25,6 @@ export function renderValueNodeVisitor(input: { visitEnumValue(node) { const enumName = nameApi.dataType(node.enum.name); const enumFunction = nameApi.dataEnumFunction(node.enum.name); - const variantName = pascalCase(node.variant); const importFrom = node.enum.importFrom ?? 'generatedTypes'; const enumNode = linkables.get(node.enum); @@ -35,12 +34,14 @@ export function renderValueNodeVisitor(input: { : !nonScalarEnums.includes(node.enum.name); if (!node.value && isScalar) { + const variantName = nameApi.scalarEnumVariant(node.variant); return fragment(`${enumName}.${variantName}`).addImports( importFrom, enumName ); } + const variantName = nameApi.dataEnumVariant(node.variant); if (!node.value) { return fragment(`${enumFunction}('${variantName}')`).addImports( importFrom, diff --git a/test/packages/js-experimental/src/generated/programs/mplCandyMachineCore.ts b/test/packages/js-experimental/src/generated/programs/mplCandyMachineCore.ts index 21653a13c..40ba9b634 100644 --- a/test/packages/js-experimental/src/generated/programs/mplCandyMachineCore.ts +++ b/test/packages/js-experimental/src/generated/programs/mplCandyMachineCore.ts @@ -35,7 +35,7 @@ export function createMplCandyMachineCoreProgram(): MplCandyMachineCoreProgram { } export enum MplCandyMachineCoreAccount { - CANDY_MACHINE, + CandyMachine, } export function identifyMplCandyMachineCoreAccount( @@ -43,7 +43,7 @@ export function identifyMplCandyMachineCoreAccount( ): MplCandyMachineCoreAccount { const data = account instanceof Uint8Array ? account : account.data; if (memcmp(data, new Uint8Array([51, 173, 177, 113, 25, 241, 109, 189]), 0)) { - return MplCandyMachineCoreAccount.CANDY_MACHINE; + return MplCandyMachineCoreAccount.CandyMachine; } throw new Error( 'The provided account could not be identified as a mplCandyMachineCore account.' @@ -51,15 +51,15 @@ export function identifyMplCandyMachineCoreAccount( } export enum MplCandyMachineCoreInstruction { - DUMMY, - ADD_CONFIG_LINES, - INITIALIZE, - MINT_FROM_CANDY_MACHINE, - SET_AUTHORITY, - SET_COLLECTION, - SET_MINT_AUTHORITY, - UPDATE_CANDY_MACHINE, - WITHDRAW, + Dummy, + AddConfigLines, + Initialize, + MintFromCandyMachine, + SetAuthority, + SetCollection, + SetMintAuthority, + UpdateCandyMachine, + Withdraw, } export function identifyMplCandyMachineCoreInstruction( @@ -68,33 +68,33 @@ export function identifyMplCandyMachineCoreInstruction( const data = instruction instanceof Uint8Array ? instruction : instruction.data; if (memcmp(data, new Uint8Array([167, 117, 211, 79, 251, 254, 47, 135]), 0)) { - return MplCandyMachineCoreInstruction.DUMMY; + return MplCandyMachineCoreInstruction.Dummy; } if (memcmp(data, new Uint8Array([223, 50, 224, 227, 151, 8, 115, 106]), 0)) { - return MplCandyMachineCoreInstruction.ADD_CONFIG_LINES; + return MplCandyMachineCoreInstruction.AddConfigLines; } if (memcmp(data, new Uint8Array([175, 175, 109, 31, 13, 152, 155, 237]), 0)) { - return MplCandyMachineCoreInstruction.INITIALIZE; + return MplCandyMachineCoreInstruction.Initialize; } if (memcmp(data, new Uint8Array([51, 57, 225, 47, 182, 146, 137, 166]), 0)) { - return MplCandyMachineCoreInstruction.MINT_FROM_CANDY_MACHINE; + return MplCandyMachineCoreInstruction.MintFromCandyMachine; } if (memcmp(data, new Uint8Array([133, 250, 37, 21, 110, 163, 26, 121]), 0)) { - return MplCandyMachineCoreInstruction.SET_AUTHORITY; + return MplCandyMachineCoreInstruction.SetAuthority; } if (memcmp(data, new Uint8Array([192, 254, 206, 76, 168, 182, 59, 223]), 0)) { - return MplCandyMachineCoreInstruction.SET_COLLECTION; + return MplCandyMachineCoreInstruction.SetCollection; } if ( memcmp(data, new Uint8Array([67, 127, 155, 187, 100, 174, 103, 121]), 0) ) { - return MplCandyMachineCoreInstruction.SET_MINT_AUTHORITY; + return MplCandyMachineCoreInstruction.SetMintAuthority; } if (memcmp(data, new Uint8Array([219, 200, 88, 176, 158, 63, 253, 127]), 0)) { - return MplCandyMachineCoreInstruction.UPDATE_CANDY_MACHINE; + return MplCandyMachineCoreInstruction.UpdateCandyMachine; } if (memcmp(data, new Uint8Array([183, 18, 70, 156, 148, 109, 161, 34]), 0)) { - return MplCandyMachineCoreInstruction.WITHDRAW; + return MplCandyMachineCoreInstruction.Withdraw; } throw new Error( 'The provided instruction could not be identified as a mplCandyMachineCore instruction.' diff --git a/test/packages/js-experimental/src/generated/programs/mplTokenAuthRules.ts b/test/packages/js-experimental/src/generated/programs/mplTokenAuthRules.ts index 66dabe586..126b98f42 100644 --- a/test/packages/js-experimental/src/generated/programs/mplTokenAuthRules.ts +++ b/test/packages/js-experimental/src/generated/programs/mplTokenAuthRules.ts @@ -37,7 +37,7 @@ export function createMplTokenAuthRulesProgram(): MplTokenAuthRulesProgram { } export enum MplTokenAuthRulesAccount { - FREQUENCY_ACCOUNT, + FrequencyAccount, } export function identifyMplTokenAuthRulesAccount( @@ -45,7 +45,7 @@ export function identifyMplTokenAuthRulesAccount( ): MplTokenAuthRulesAccount { const data = account instanceof Uint8Array ? account : account.data; if (memcmp(data, getU64Encoder().encode(TaKey.Frequency), 0)) { - return MplTokenAuthRulesAccount.FREQUENCY_ACCOUNT; + return MplTokenAuthRulesAccount.FrequencyAccount; } throw new Error( 'The provided account could not be identified as a mplTokenAuthRules account.' @@ -53,9 +53,9 @@ export function identifyMplTokenAuthRulesAccount( } export enum MplTokenAuthRulesInstruction { - CREATE_RULE_SET, - VALIDATE, - CREATE_FREQUENCY_RULE, + CreateRuleSet, + Validate, + CreateFrequencyRule, } export function identifyMplTokenAuthRulesInstruction( @@ -64,13 +64,13 @@ export function identifyMplTokenAuthRulesInstruction( const data = instruction instanceof Uint8Array ? instruction : instruction.data; if (memcmp(data, getU8Encoder().encode(0), 0)) { - return MplTokenAuthRulesInstruction.CREATE_RULE_SET; + return MplTokenAuthRulesInstruction.CreateRuleSet; } if (memcmp(data, getU8Encoder().encode(1), 0)) { - return MplTokenAuthRulesInstruction.VALIDATE; + return MplTokenAuthRulesInstruction.Validate; } if (memcmp(data, getU8Encoder().encode(2), 0)) { - return MplTokenAuthRulesInstruction.CREATE_FREQUENCY_RULE; + return MplTokenAuthRulesInstruction.CreateFrequencyRule; } throw new Error( 'The provided instruction could not be identified as a mplTokenAuthRules instruction.' diff --git a/test/packages/js-experimental/src/generated/programs/mplTokenMetadata.ts b/test/packages/js-experimental/src/generated/programs/mplTokenMetadata.ts index 868db5209..f8ce0d9ae 100644 --- a/test/packages/js-experimental/src/generated/programs/mplTokenMetadata.ts +++ b/test/packages/js-experimental/src/generated/programs/mplTokenMetadata.ts @@ -37,17 +37,17 @@ export function createMplTokenMetadataProgram(): MplTokenMetadataProgram { } export enum MplTokenMetadataAccount { - COLLECTION_AUTHORITY_RECORD, - DELEGATE_RECORD, - EDITION, - EDITION_MARKER, - TOKEN_OWNED_ESCROW, - MASTER_EDITION_V2, - MASTER_EDITION_V1, - METADATA, - RESERVATION_LIST_V2, - RESERVATION_LIST_V1, - USE_AUTHORITY_RECORD, + CollectionAuthorityRecord, + DelegateRecord, + Edition, + EditionMarker, + TokenOwnedEscrow, + MasterEditionV2, + MasterEditionV1, + Metadata, + ReservationListV2, + ReservationListV1, + UseAuthorityRecord, } export function identifyMplTokenMetadataAccount( @@ -57,37 +57,37 @@ export function identifyMplTokenMetadataAccount( if ( memcmp(data, getTmKeyEncoder().encode(TmKey.CollectionAuthorityRecord), 0) ) { - return MplTokenMetadataAccount.COLLECTION_AUTHORITY_RECORD; + return MplTokenMetadataAccount.CollectionAuthorityRecord; } if (memcmp(data, getTmKeyEncoder().encode(TmKey.Delegate), 0)) { - return MplTokenMetadataAccount.DELEGATE_RECORD; + return MplTokenMetadataAccount.DelegateRecord; } if (memcmp(data, getTmKeyEncoder().encode(TmKey.EditionV1), 0)) { - return MplTokenMetadataAccount.EDITION; + return MplTokenMetadataAccount.Edition; } if (memcmp(data, getTmKeyEncoder().encode(TmKey.EditionMarker), 0)) { - return MplTokenMetadataAccount.EDITION_MARKER; + return MplTokenMetadataAccount.EditionMarker; } if (memcmp(data, getTmKeyEncoder().encode(TmKey.TokenOwnedEscrow), 0)) { - return MplTokenMetadataAccount.TOKEN_OWNED_ESCROW; + return MplTokenMetadataAccount.TokenOwnedEscrow; } if (memcmp(data, getTmKeyEncoder().encode(TmKey.MasterEditionV2), 0)) { - return MplTokenMetadataAccount.MASTER_EDITION_V2; + return MplTokenMetadataAccount.MasterEditionV2; } if (memcmp(data, getTmKeyEncoder().encode(TmKey.MasterEditionV1), 0)) { - return MplTokenMetadataAccount.MASTER_EDITION_V1; + return MplTokenMetadataAccount.MasterEditionV1; } if (memcmp(data, getTmKeyEncoder().encode(TmKey.MetadataV1), 0)) { - return MplTokenMetadataAccount.METADATA; + return MplTokenMetadataAccount.Metadata; } if (memcmp(data, getTmKeyEncoder().encode(TmKey.ReservationListV2), 0)) { - return MplTokenMetadataAccount.RESERVATION_LIST_V2; + return MplTokenMetadataAccount.ReservationListV2; } if (memcmp(data, getTmKeyEncoder().encode(TmKey.ReservationListV1), 0)) { - return MplTokenMetadataAccount.RESERVATION_LIST_V1; + return MplTokenMetadataAccount.ReservationListV1; } if (memcmp(data, getTmKeyEncoder().encode(TmKey.UseAuthorityRecord), 0)) { - return MplTokenMetadataAccount.USE_AUTHORITY_RECORD; + return MplTokenMetadataAccount.UseAuthorityRecord; } throw new Error( 'The provided account could not be identified as a mplTokenMetadata account.' @@ -95,58 +95,58 @@ export function identifyMplTokenMetadataAccount( } export enum MplTokenMetadataInstruction { - CREATE_METADATA_ACCOUNT, - UPDATE_METADATA_ACCOUNT, - DEPRECATED_CREATE_MASTER_EDITION, - DEPRECATED_MINT_NEW_EDITION_FROM_MASTER_EDITION_VIA_PRINTING_TOKEN, - UPDATE_PRIMARY_SALE_HAPPENED_VIA_TOKEN, - DEPRECATED_SET_RESERVATION_LIST, - CREATE_RESERVATION_LIST, - SIGN_METADATA, - DEPRECATED_MINT_PRINTING_TOKENS_VIA_TOKEN, - DEPRECATED_MINT_PRINTING_TOKENS, - CREATE_MASTER_EDITION, - MINT_NEW_EDITION_FROM_MASTER_EDITION_VIA_TOKEN, - CONVERT_MASTER_EDITION_V1_TO_V2, - MINT_NEW_EDITION_FROM_MASTER_EDITION_VIA_VAULT_PROXY, - PUFF_METADATA, - UPDATE_METADATA_ACCOUNT_V2, - CREATE_METADATA_ACCOUNT_V2, - CREATE_MASTER_EDITION_V3, - VERIFY_COLLECTION, - UTILIZE, - APPROVE_USE_AUTHORITY, - REVOKE_USE_AUTHORITY, - UNVERIFY_COLLECTION, - APPROVE_COLLECTION_AUTHORITY, - REVOKE_COLLECTION_AUTHORITY, - SET_AND_VERIFY_COLLECTION, - FREEZE_DELEGATED_ACCOUNT, - THAW_DELEGATED_ACCOUNT, - REMOVE_CREATOR_VERIFICATION, - BURN_NFT, - VERIFY_SIZED_COLLECTION_ITEM, - UNVERIFY_SIZED_COLLECTION_ITEM, - SET_AND_VERIFY_SIZED_COLLECTION_ITEM, - CREATE_METADATA_ACCOUNT_V3, - SET_COLLECTION_SIZE, - SET_TOKEN_STANDARD, - BUBBLEGUM_SET_COLLECTION_SIZE, - BURN_EDITION_NFT, - CREATE_ESCROW_ACCOUNT, - CLOSE_ESCROW_ACCOUNT, - TRANSFER_OUT_OF_ESCROW, - CREATE_V1, - CREATE_V2, - MINT, - UPDATE_V1, - BURN, - USE_ASSET, - TRANSFER, - VERIFY, - DELEGATE, - REVOKE, - MIGRATE, + CreateMetadataAccount, + UpdateMetadataAccount, + DeprecatedCreateMasterEdition, + DeprecatedMintNewEditionFromMasterEditionViaPrintingToken, + UpdatePrimarySaleHappenedViaToken, + DeprecatedSetReservationList, + CreateReservationList, + SignMetadata, + DeprecatedMintPrintingTokensViaToken, + DeprecatedMintPrintingTokens, + CreateMasterEdition, + MintNewEditionFromMasterEditionViaToken, + ConvertMasterEditionV1ToV2, + MintNewEditionFromMasterEditionViaVaultProxy, + PuffMetadata, + UpdateMetadataAccountV2, + CreateMetadataAccountV2, + CreateMasterEditionV3, + VerifyCollection, + Utilize, + ApproveUseAuthority, + RevokeUseAuthority, + UnverifyCollection, + ApproveCollectionAuthority, + RevokeCollectionAuthority, + SetAndVerifyCollection, + FreezeDelegatedAccount, + ThawDelegatedAccount, + RemoveCreatorVerification, + BurnNft, + VerifySizedCollectionItem, + UnverifySizedCollectionItem, + SetAndVerifySizedCollectionItem, + CreateMetadataAccountV3, + SetCollectionSize, + SetTokenStandard, + BubblegumSetCollectionSize, + BurnEditionNft, + CreateEscrowAccount, + CloseEscrowAccount, + TransferOutOfEscrow, + CreateV1, + CreateV2, + Mint, + UpdateV1, + Burn, + UseAsset, + Transfer, + Verify, + Delegate, + Revoke, + Migrate, } export function identifyMplTokenMetadataInstruction( @@ -155,160 +155,160 @@ export function identifyMplTokenMetadataInstruction( const data = instruction instanceof Uint8Array ? instruction : instruction.data; if (memcmp(data, getU8Encoder().encode(0), 0)) { - return MplTokenMetadataInstruction.CREATE_METADATA_ACCOUNT; + return MplTokenMetadataInstruction.CreateMetadataAccount; } if (memcmp(data, getU8Encoder().encode(1), 0)) { - return MplTokenMetadataInstruction.UPDATE_METADATA_ACCOUNT; + return MplTokenMetadataInstruction.UpdateMetadataAccount; } if (memcmp(data, getU8Encoder().encode(2), 0)) { - return MplTokenMetadataInstruction.DEPRECATED_CREATE_MASTER_EDITION; + return MplTokenMetadataInstruction.DeprecatedCreateMasterEdition; } if (memcmp(data, getU8Encoder().encode(3), 0)) { - return MplTokenMetadataInstruction.DEPRECATED_MINT_NEW_EDITION_FROM_MASTER_EDITION_VIA_PRINTING_TOKEN; + return MplTokenMetadataInstruction.DeprecatedMintNewEditionFromMasterEditionViaPrintingToken; } if (memcmp(data, getU8Encoder().encode(4), 0)) { - return MplTokenMetadataInstruction.UPDATE_PRIMARY_SALE_HAPPENED_VIA_TOKEN; + return MplTokenMetadataInstruction.UpdatePrimarySaleHappenedViaToken; } if (memcmp(data, getU8Encoder().encode(5), 0)) { - return MplTokenMetadataInstruction.DEPRECATED_SET_RESERVATION_LIST; + return MplTokenMetadataInstruction.DeprecatedSetReservationList; } if (memcmp(data, getU8Encoder().encode(6), 0)) { - return MplTokenMetadataInstruction.CREATE_RESERVATION_LIST; + return MplTokenMetadataInstruction.CreateReservationList; } if (memcmp(data, getU8Encoder().encode(7), 0)) { - return MplTokenMetadataInstruction.SIGN_METADATA; + return MplTokenMetadataInstruction.SignMetadata; } if (memcmp(data, getU8Encoder().encode(8), 0)) { - return MplTokenMetadataInstruction.DEPRECATED_MINT_PRINTING_TOKENS_VIA_TOKEN; + return MplTokenMetadataInstruction.DeprecatedMintPrintingTokensViaToken; } if (memcmp(data, getU8Encoder().encode(9), 0)) { - return MplTokenMetadataInstruction.DEPRECATED_MINT_PRINTING_TOKENS; + return MplTokenMetadataInstruction.DeprecatedMintPrintingTokens; } if (memcmp(data, getU8Encoder().encode(10), 0)) { - return MplTokenMetadataInstruction.CREATE_MASTER_EDITION; + return MplTokenMetadataInstruction.CreateMasterEdition; } if (memcmp(data, getU8Encoder().encode(11), 0)) { - return MplTokenMetadataInstruction.MINT_NEW_EDITION_FROM_MASTER_EDITION_VIA_TOKEN; + return MplTokenMetadataInstruction.MintNewEditionFromMasterEditionViaToken; } if (memcmp(data, getU8Encoder().encode(12), 0)) { - return MplTokenMetadataInstruction.CONVERT_MASTER_EDITION_V1_TO_V2; + return MplTokenMetadataInstruction.ConvertMasterEditionV1ToV2; } if (memcmp(data, getU8Encoder().encode(13), 0)) { - return MplTokenMetadataInstruction.MINT_NEW_EDITION_FROM_MASTER_EDITION_VIA_VAULT_PROXY; + return MplTokenMetadataInstruction.MintNewEditionFromMasterEditionViaVaultProxy; } if (memcmp(data, getU8Encoder().encode(14), 0)) { - return MplTokenMetadataInstruction.PUFF_METADATA; + return MplTokenMetadataInstruction.PuffMetadata; } if (memcmp(data, getU8Encoder().encode(15), 0)) { - return MplTokenMetadataInstruction.UPDATE_METADATA_ACCOUNT_V2; + return MplTokenMetadataInstruction.UpdateMetadataAccountV2; } if (memcmp(data, getU8Encoder().encode(16), 0)) { - return MplTokenMetadataInstruction.CREATE_METADATA_ACCOUNT_V2; + return MplTokenMetadataInstruction.CreateMetadataAccountV2; } if (memcmp(data, getU8Encoder().encode(17), 0)) { - return MplTokenMetadataInstruction.CREATE_MASTER_EDITION_V3; + return MplTokenMetadataInstruction.CreateMasterEditionV3; } if (memcmp(data, getU8Encoder().encode(18), 0)) { - return MplTokenMetadataInstruction.VERIFY_COLLECTION; + return MplTokenMetadataInstruction.VerifyCollection; } if (memcmp(data, getU8Encoder().encode(19), 0)) { - return MplTokenMetadataInstruction.UTILIZE; + return MplTokenMetadataInstruction.Utilize; } if (memcmp(data, getU8Encoder().encode(20), 0)) { - return MplTokenMetadataInstruction.APPROVE_USE_AUTHORITY; + return MplTokenMetadataInstruction.ApproveUseAuthority; } if (memcmp(data, getU8Encoder().encode(21), 0)) { - return MplTokenMetadataInstruction.REVOKE_USE_AUTHORITY; + return MplTokenMetadataInstruction.RevokeUseAuthority; } if (memcmp(data, getU8Encoder().encode(22), 0)) { - return MplTokenMetadataInstruction.UNVERIFY_COLLECTION; + return MplTokenMetadataInstruction.UnverifyCollection; } if (memcmp(data, getU8Encoder().encode(23), 0)) { - return MplTokenMetadataInstruction.APPROVE_COLLECTION_AUTHORITY; + return MplTokenMetadataInstruction.ApproveCollectionAuthority; } if (memcmp(data, getU8Encoder().encode(24), 0)) { - return MplTokenMetadataInstruction.REVOKE_COLLECTION_AUTHORITY; + return MplTokenMetadataInstruction.RevokeCollectionAuthority; } if (memcmp(data, getU8Encoder().encode(25), 0)) { - return MplTokenMetadataInstruction.SET_AND_VERIFY_COLLECTION; + return MplTokenMetadataInstruction.SetAndVerifyCollection; } if (memcmp(data, getU8Encoder().encode(26), 0)) { - return MplTokenMetadataInstruction.FREEZE_DELEGATED_ACCOUNT; + return MplTokenMetadataInstruction.FreezeDelegatedAccount; } if (memcmp(data, getU8Encoder().encode(27), 0)) { - return MplTokenMetadataInstruction.THAW_DELEGATED_ACCOUNT; + return MplTokenMetadataInstruction.ThawDelegatedAccount; } if (memcmp(data, getU8Encoder().encode(28), 0)) { - return MplTokenMetadataInstruction.REMOVE_CREATOR_VERIFICATION; + return MplTokenMetadataInstruction.RemoveCreatorVerification; } if (memcmp(data, getU8Encoder().encode(29), 0)) { - return MplTokenMetadataInstruction.BURN_NFT; + return MplTokenMetadataInstruction.BurnNft; } if (memcmp(data, getU8Encoder().encode(30), 0)) { - return MplTokenMetadataInstruction.VERIFY_SIZED_COLLECTION_ITEM; + return MplTokenMetadataInstruction.VerifySizedCollectionItem; } if (memcmp(data, getU8Encoder().encode(31), 0)) { - return MplTokenMetadataInstruction.UNVERIFY_SIZED_COLLECTION_ITEM; + return MplTokenMetadataInstruction.UnverifySizedCollectionItem; } if (memcmp(data, getU8Encoder().encode(32), 0)) { - return MplTokenMetadataInstruction.SET_AND_VERIFY_SIZED_COLLECTION_ITEM; + return MplTokenMetadataInstruction.SetAndVerifySizedCollectionItem; } if (memcmp(data, getU8Encoder().encode(33), 0)) { - return MplTokenMetadataInstruction.CREATE_METADATA_ACCOUNT_V3; + return MplTokenMetadataInstruction.CreateMetadataAccountV3; } if (memcmp(data, getU8Encoder().encode(34), 0)) { - return MplTokenMetadataInstruction.SET_COLLECTION_SIZE; + return MplTokenMetadataInstruction.SetCollectionSize; } if (memcmp(data, getU8Encoder().encode(35), 0)) { - return MplTokenMetadataInstruction.SET_TOKEN_STANDARD; + return MplTokenMetadataInstruction.SetTokenStandard; } if (memcmp(data, getU8Encoder().encode(36), 0)) { - return MplTokenMetadataInstruction.BUBBLEGUM_SET_COLLECTION_SIZE; + return MplTokenMetadataInstruction.BubblegumSetCollectionSize; } if (memcmp(data, getU8Encoder().encode(37), 0)) { - return MplTokenMetadataInstruction.BURN_EDITION_NFT; + return MplTokenMetadataInstruction.BurnEditionNft; } if (memcmp(data, getU8Encoder().encode(38), 0)) { - return MplTokenMetadataInstruction.CREATE_ESCROW_ACCOUNT; + return MplTokenMetadataInstruction.CreateEscrowAccount; } if (memcmp(data, getU8Encoder().encode(39), 0)) { - return MplTokenMetadataInstruction.CLOSE_ESCROW_ACCOUNT; + return MplTokenMetadataInstruction.CloseEscrowAccount; } if (memcmp(data, getU8Encoder().encode(40), 0)) { - return MplTokenMetadataInstruction.TRANSFER_OUT_OF_ESCROW; + return MplTokenMetadataInstruction.TransferOutOfEscrow; } if (memcmp(data, getU8Encoder().encode(41), 0)) { - return MplTokenMetadataInstruction.CREATE_V1; + return MplTokenMetadataInstruction.CreateV1; } if (memcmp(data, getU8Encoder().encode(41), 0)) { - return MplTokenMetadataInstruction.CREATE_V2; + return MplTokenMetadataInstruction.CreateV2; } if (memcmp(data, getU8Encoder().encode(42), 0)) { - return MplTokenMetadataInstruction.MINT; + return MplTokenMetadataInstruction.Mint; } if (memcmp(data, getU8Encoder().encode(43), 0)) { - return MplTokenMetadataInstruction.UPDATE_V1; + return MplTokenMetadataInstruction.UpdateV1; } if (memcmp(data, getU8Encoder().encode(44), 0)) { - return MplTokenMetadataInstruction.BURN; + return MplTokenMetadataInstruction.Burn; } if (memcmp(data, getU8Encoder().encode(45), 0)) { - return MplTokenMetadataInstruction.USE_ASSET; + return MplTokenMetadataInstruction.UseAsset; } if (memcmp(data, getU8Encoder().encode(46), 0)) { - return MplTokenMetadataInstruction.TRANSFER; + return MplTokenMetadataInstruction.Transfer; } if (memcmp(data, getU8Encoder().encode(47), 0)) { - return MplTokenMetadataInstruction.VERIFY; + return MplTokenMetadataInstruction.Verify; } if (memcmp(data, getU8Encoder().encode(48), 0)) { - return MplTokenMetadataInstruction.DELEGATE; + return MplTokenMetadataInstruction.Delegate; } if (memcmp(data, getU8Encoder().encode(49), 0)) { - return MplTokenMetadataInstruction.REVOKE; + return MplTokenMetadataInstruction.Revoke; } if (memcmp(data, getU8Encoder().encode(50), 0)) { - return MplTokenMetadataInstruction.MIGRATE; + return MplTokenMetadataInstruction.Migrate; } throw new Error( 'The provided instruction could not be identified as a mplTokenMetadata instruction.' diff --git a/test/packages/js-experimental/src/generated/programs/splSystem.ts b/test/packages/js-experimental/src/generated/programs/splSystem.ts index 9666e2947..3aae81739 100644 --- a/test/packages/js-experimental/src/generated/programs/splSystem.ts +++ b/test/packages/js-experimental/src/generated/programs/splSystem.ts @@ -23,8 +23,8 @@ export function createSplSystemProgram(): SplSystemProgram { } export enum SplSystemInstruction { - CREATE_ACCOUNT, - TRANSFER_SOL, + CreateAccount, + TransferSol, } export function identifySplSystemInstruction( @@ -33,10 +33,10 @@ export function identifySplSystemInstruction( const data = instruction instanceof Uint8Array ? instruction : instruction.data; if (memcmp(data, getU32Encoder().encode(0), 0)) { - return SplSystemInstruction.CREATE_ACCOUNT; + return SplSystemInstruction.CreateAccount; } if (memcmp(data, getU32Encoder().encode(2), 0)) { - return SplSystemInstruction.TRANSFER_SOL; + return SplSystemInstruction.TransferSol; } throw new Error( 'The provided instruction could not be identified as a splSystem instruction.' diff --git a/test/renderers/js-experimental/programsPage.test.ts b/test/renderers/js-experimental/programsPage.test.ts index 6f20fffc5..9729d5684 100644 --- a/test/renderers/js-experimental/programsPage.test.ts +++ b/test/renderers/js-experimental/programsPage.test.ts @@ -50,7 +50,7 @@ test('it renders an enum of all available accounts for a program', (t) => { // Then we expect the following program account enum. renderMapContains(t, renderMap, 'programs/splToken.ts', [ - 'export enum SplTokenAccount { MINT, TOKEN };', + 'export enum SplTokenAccount { Mint, Token };', ]); }); @@ -93,8 +93,8 @@ test('it renders an function that identifies accounts in a program', (t) => { renderMapContains(t, renderMap, 'programs/splToken.ts', [ `export function identifySplTokenAccount(account: { data: Uint8Array } | Uint8Array): SplTokenAccount {\n` + `const data = account instanceof Uint8Array ? account : account.data;\n` + - `if (memcmp(data, getU8Encoder().encode(5), 0)) { return SplTokenAccount.METADATA; }\n` + - `if (data.length === 72 && memcmp(data, new Uint8Array([1, 2, 3]), 4)) { return SplTokenAccount.TOKEN; }\n` + + `if (memcmp(data, getU8Encoder().encode(5), 0)) { return SplTokenAccount.Metadata; }\n` + + `if (data.length === 72 && memcmp(data, new Uint8Array([1, 2, 3]), 4)) { return SplTokenAccount.Token; }\n` + `throw new Error('The provided account could not be identified as a splToken account.')\n` + `}`, ]); @@ -122,7 +122,7 @@ test('it renders an enum of all available instructions for a program', (t) => { // Then we expect the following program instruction enum. renderMapContains(t, renderMap, 'programs/splToken.ts', [ - 'export enum SplTokenInstruction { MINT_TOKENS, TRANSFER_TOKENS, UPDATE_AUTHORITY };', + 'export enum SplTokenInstruction { MintTokens, TransferTokens, UpdateAuthority };', ]); }); @@ -165,8 +165,8 @@ test('it renders an function that identifies instructions in a program', (t) => renderMapContains(t, renderMap, 'programs/splToken.ts', [ `export function identifySplTokenInstruction(instruction: { data: Uint8Array } | Uint8Array): SplTokenInstruction {\n` + `const data = instruction instanceof Uint8Array ? instruction : instruction.data;\n` + - `if (memcmp(data, getU8Encoder().encode(1), 0)) { return SplTokenInstruction.MINT_TOKENS; }\n` + - `if (data.length === 72 && memcmp(data, new Uint8Array([1, 2, 3]), 4)) { return SplTokenInstruction.TRANSFER_TOKENS; }\n` + + `if (memcmp(data, getU8Encoder().encode(1), 0)) { return SplTokenInstruction.MintTokens; }\n` + + `if (data.length === 72 && memcmp(data, new Uint8Array([1, 2, 3]), 4)) { return SplTokenInstruction.TransferTokens; }\n` + `throw new Error('The provided instruction could not be identified as a splToken instruction.')\n` + `}`, ]); @@ -234,8 +234,8 @@ test('it checks the discriminator of sub-instructions before their parents.', (t renderMapContains(t, renderMap, 'programs/splToken.ts', [ `export function identifySplTokenInstruction(instruction: { data: Uint8Array } | Uint8Array): SplTokenInstruction {\n` + `const data = instruction instanceof Uint8Array ? instruction : instruction.data;\n` + - `if (memcmp(data, getU8Encoder().encode(1), 0) && memcmp(data, getU32Encoder().encode(1), 1)) { return SplTokenInstruction.MINT_TOKENS_V1; }\n` + - `if (memcmp(data, getU8Encoder().encode(1), 0)) { return SplTokenInstruction.MINT_TOKENS; }\n` + + `if (memcmp(data, getU8Encoder().encode(1), 0) && memcmp(data, getU32Encoder().encode(1), 1)) { return SplTokenInstruction.MintTokensV1; }\n` + + `if (memcmp(data, getU8Encoder().encode(1), 0)) { return SplTokenInstruction.MintTokens; }\n` + `throw new Error('The provided instruction could not be identified as a splToken instruction.')\n` + `}`, ]);