diff --git a/.changeset/poor-lions-promise.md b/.changeset/poor-lions-promise.md new file mode 100644 index 000000000..f415e80cc --- /dev/null +++ b/.changeset/poor-lions-promise.md @@ -0,0 +1,5 @@ +--- +"@metaplex-foundation/kinobi": minor +--- + +Update enums codecs from latest Web3.js version on the JS experimental renderer diff --git a/src/renderers/js-experimental/fragments/index.ts b/src/renderers/js-experimental/fragments/index.ts index fdcf2c36c..a08c7aa84 100644 --- a/src/renderers/js-experimental/fragments/index.ts +++ b/src/renderers/js-experimental/fragments/index.ts @@ -23,7 +23,7 @@ export * from './programErrors'; export * from './programInstructions'; export * from './type'; export * from './typeCodec'; -export * from './typeDataEnumHelpers'; export * from './typeDecoder'; +export * from './typeDiscriminatedUnionHelpers'; export * from './typeEncoder'; export * from './typeWithCodec'; diff --git a/src/renderers/js-experimental/fragments/typeDataEnumHelpers.njk b/src/renderers/js-experimental/fragments/typeDataEnumHelpers.njk deleted file mode 100644 index 62c32f509..000000000 --- a/src/renderers/js-experimental/fragments/typeDataEnumHelpers.njk +++ /dev/null @@ -1,23 +0,0 @@ -// Data Enum Helpers. -{% for variant in typeNode.variants %} - {% if variant.kind === 'enumStructVariantTypeNode' %} - 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: '{{ getVariant(variant.name) }}', data: GetDataEnumKindContent<{{ looseName }}, '{{ getVariant(variant.name) }}'>['fields']): GetDataEnumKind<{{ looseName }}, '{{ getVariant(variant.name) }}'>; - {% else %} - export function {{ dataEnumFunction }}(kind: '{{ getVariant(variant.name) }}'): GetDataEnumKind<{{ looseName }}, '{{ getVariant(variant.name) }}'>; - {% endif %} -{% endfor %} -export function {{ dataEnumFunction }}( - kind: K, - data?: Data, -) { - return Array.isArray(data) ? { {{ dataEnumDiscriminator }}: kind, fields: data } : { {{ dataEnumDiscriminator }}: kind, ...(data ?? {}) }; -} - -export function {{ isDataEnumFunction }}( - kind: K, - value: {{ strictName }}, -): value is {{ strictName }} & { {{ dataEnumDiscriminator }}: K } { - return value.{{ dataEnumDiscriminator }} === kind; -}; diff --git a/src/renderers/js-experimental/fragments/typeDiscriminatedUnionHelpers.njk b/src/renderers/js-experimental/fragments/typeDiscriminatedUnionHelpers.njk new file mode 100644 index 000000000..cc1a038d1 --- /dev/null +++ b/src/renderers/js-experimental/fragments/typeDiscriminatedUnionHelpers.njk @@ -0,0 +1,23 @@ +// Data Enum Helpers. +{% for variant in typeNode.variants %} + {% if variant.kind === 'enumStructVariantTypeNode' %} + export function {{ discriminatedUnionFunction }}(kind: '{{ getVariant(variant.name) }}', data: GetDiscriminatedUnionVariantContent<{{ looseName }}, '__kind', '{{ getVariant(variant.name) }}'>): GetDiscriminatedUnionVariant<{{ looseName }}, '__kind', '{{ getVariant(variant.name) }}'>; + {% elif variant.kind === 'enumTupleVariantTypeNode' %} + export function {{ discriminatedUnionFunction }}(kind: '{{ getVariant(variant.name) }}', data: GetDiscriminatedUnionVariantContent<{{ looseName }}, '__kind', '{{ getVariant(variant.name) }}'>['fields']): GetDiscriminatedUnionVariant<{{ looseName }}, '__kind', '{{ getVariant(variant.name) }}'>; + {% else %} + export function {{ discriminatedUnionFunction }}(kind: '{{ getVariant(variant.name) }}'): GetDiscriminatedUnionVariant<{{ looseName }}, '__kind', '{{ getVariant(variant.name) }}'>; + {% endif %} +{% endfor %} +export function {{ discriminatedUnionFunction }}( + kind: K, + data?: Data, +) { + return Array.isArray(data) ? { {{ discriminatedUnionDiscriminator }}: kind, fields: data } : { {{ discriminatedUnionDiscriminator }}: kind, ...(data ?? {}) }; +} + +export function {{ isDiscriminatedUnionFunction }}( + kind: K, + value: {{ strictName }}, +): value is {{ strictName }} & { {{ discriminatedUnionDiscriminator }}: K } { + return value.{{ discriminatedUnionDiscriminator }} === kind; +}; diff --git a/src/renderers/js-experimental/fragments/typeDataEnumHelpers.ts b/src/renderers/js-experimental/fragments/typeDiscriminatedUnionHelpers.ts similarity index 50% rename from src/renderers/js-experimental/fragments/typeDataEnumHelpers.ts rename to src/renderers/js-experimental/fragments/typeDiscriminatedUnionHelpers.ts index d8bbb7a04..2cb2913e1 100644 --- a/src/renderers/js-experimental/fragments/typeDataEnumHelpers.ts +++ b/src/renderers/js-experimental/fragments/typeDiscriminatedUnionHelpers.ts @@ -2,30 +2,31 @@ import { TypeNode, isDataEnum, isNode } from '../../../nodes'; import type { GlobalFragmentScope } from '../getRenderMapVisitor'; import { Fragment, fragment, fragmentFromTemplate } from './common'; -export function getTypeDataEnumHelpersFragment( +export function getTypeDiscriminatedUnionHelpersFragment( scope: Pick & { name: string; typeNode: TypeNode; } ): Fragment { const { name, typeNode, nameApi } = scope; - const isDataEnumNode = + const isDiscriminatedUnion = isNode(typeNode, 'enumTypeNode') && isDataEnum(typeNode); - if (!isDataEnumNode) { + if (!isDiscriminatedUnion) { return fragment(''); } - return fragmentFromTemplate('typeDataEnumHelpers.njk', { + return fragmentFromTemplate('typeDiscriminatedUnionHelpers.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), + discriminatedUnionDiscriminator: + nameApi.discriminatedUnionDiscriminator(name), + getVariant: (variant: string) => nameApi.discriminatedUnionVariant(variant), + discriminatedUnionFunction: nameApi.discriminatedUnionFunction(name), + isDiscriminatedUnionFunction: nameApi.isDiscriminatedUnionFunction(name), typeNode, }).addImports('solanaCodecsDataStructures', [ - 'GetDataEnumKindContent', - 'GetDataEnumKind', + 'GetDiscriminatedUnionVariantContent', + 'GetDiscriminatedUnionVariant', ]); } diff --git a/src/renderers/js-experimental/getRenderMapVisitor.ts b/src/renderers/js-experimental/getRenderMapVisitor.ts index 1a5d2d5bf..4c5b112c7 100644 --- a/src/renderers/js-experimental/getRenderMapVisitor.ts +++ b/src/renderers/js-experimental/getRenderMapVisitor.ts @@ -49,7 +49,7 @@ import { getProgramErrorsFragment, getProgramFragment, getProgramInstructionsFragment, - getTypeDataEnumHelpersFragment, + getTypeDiscriminatedUnionHelpersFragment, getTypeWithCodecFragment, } from './fragments'; import { @@ -447,10 +447,13 @@ export function getRenderMapVisitor(options: GetRenderMapOptions = {}) { }; const typeWithCodecFragment = getTypeWithCodecFragment(scope); - const typeDataEnumHelpersFragment = - getTypeDataEnumHelpersFragment(scope); + const typeDiscriminatedUnionHelpersFragment = + getTypeDiscriminatedUnionHelpersFragment(scope); const imports = new ImportMap() - .mergeWith(typeWithCodecFragment, typeDataEnumHelpersFragment) + .mergeWith( + typeWithCodecFragment, + typeDiscriminatedUnionHelpersFragment + ) .remove('generatedTypes', [ nameApi.dataType(node.name), nameApi.dataArgsType(node.name), @@ -467,7 +470,7 @@ export function getRenderMapVisitor(options: GetRenderMapOptions = {}) { generatedTypes: '.', }), typeWithCodecFragment, - typeDataEnumHelpersFragment, + typeDiscriminatedUnionHelpersFragment, }) ); }, diff --git a/src/renderers/js-experimental/getTypeManifestVisitor.ts b/src/renderers/js-experimental/getTypeManifestVisitor.ts index 742deb194..700620e13 100644 --- a/src/renderers/js-experimental/getTypeManifestVisitor.ts +++ b/src/renderers/js-experimental/getTypeManifestVisitor.ts @@ -171,28 +171,28 @@ export function getTypeManifestVisitor(input: { ); } const variantNames = enumType.variants.map(({ name }) => - nameApi.scalarEnumVariant(name) + nameApi.enumVariant(name) ); return { isEnum: true, strictType: fragment(`{ ${variantNames.join(', ')} }`), looseType: fragment(`{ ${variantNames.join(', ')} }`), encoder: fragment( - `getScalarEnumEncoder(${ + `getEnumEncoder(${ currentParentName.strict + encoderOptionsAsString })`, encoderImports.add( 'solanaCodecsDataStructures', - 'getScalarEnumEncoder' + 'getEnumEncoder' ) ), decoder: fragment( - `getScalarEnumDecoder(${ + `getEnumDecoder(${ currentParentName.strict + decoderOptionsAsString })`, decoderImports.add( 'solanaCodecsDataStructures', - 'getScalarEnumDecoder' + 'getEnumDecoder' ) ), }; @@ -205,24 +205,32 @@ export function getTypeManifestVisitor(input: { ); mergedManifest.encoder .mapRender( - (r) => `getDataEnumEncoder([${r}]${encoderOptionsAsString})` + (r) => + `getDiscriminatedUnionEncoder([${r}]${encoderOptionsAsString})` ) .mergeImportsWith(encoderImports) - .addImports('solanaCodecsDataStructures', ['getDataEnumEncoder']); + .addImports('solanaCodecsDataStructures', [ + 'getDiscriminatedUnionEncoder', + ]); mergedManifest.decoder .mapRender( - (r) => `getDataEnumDecoder([${r}]${decoderOptionsAsString})` + (r) => + `getDiscriminatedUnionDecoder([${r}]${decoderOptionsAsString})` ) .mergeImportsWith(decoderImports) - .addImports('solanaCodecsDataStructures', ['getDataEnumDecoder']); + .addImports('solanaCodecsDataStructures', [ + 'getDiscriminatedUnionDecoder', + ]); return mergedManifest; }, visitEnumEmptyVariantType(enumEmptyVariantType) { - const discriminator = nameApi.dataEnumDiscriminator( + const discriminator = nameApi.discriminatedUnionDiscriminator( + enumEmptyVariantType.name + ); + const name = nameApi.discriminatedUnionVariant( enumEmptyVariantType.name ); - const name = nameApi.dataEnumVariant(enumEmptyVariantType.name); const kindAttribute = `${discriminator}: "${name}"`; return { isEnum: false, @@ -240,10 +248,12 @@ export function getTypeManifestVisitor(input: { }, visitEnumStructVariantType(enumStructVariantType, { self }) { - const discriminator = nameApi.dataEnumDiscriminator( + const discriminator = nameApi.discriminatedUnionDiscriminator( + enumStructVariantType.name + ); + const name = nameApi.discriminatedUnionVariant( enumStructVariantType.name ); - const name = nameApi.dataEnumVariant(enumStructVariantType.name); const kindAttribute = `${discriminator}: "${name}"`; const structManifest = visit(enumStructVariantType.struct, self); structManifest.strictType.mapRender( @@ -258,10 +268,12 @@ export function getTypeManifestVisitor(input: { }, visitEnumTupleVariantType(enumTupleVariantType, { self }) { - const discriminator = nameApi.dataEnumDiscriminator( + const discriminator = nameApi.discriminatedUnionDiscriminator( + enumTupleVariantType.name + ); + const name = nameApi.discriminatedUnionVariant( enumTupleVariantType.name ); - const name = nameApi.dataEnumVariant(enumTupleVariantType.name); const kindAttribute = `${discriminator}: "${name}"`; const struct = structTypeNode([ structFieldTypeNode({ diff --git a/src/renderers/js-experimental/nameTransformers.ts b/src/renderers/js-experimental/nameTransformers.ts index a8679a318..2f1a193c5 100644 --- a/src/renderers/js-experimental/nameTransformers.ts +++ b/src/renderers/js-experimental/nameTransformers.ts @@ -40,11 +40,11 @@ export type NameTransformerKey = | 'accountFetchFromSeedsFunction' | 'accountFetchMaybeFromSeedsFunction' | 'accountGetSizeFunction' - | 'scalarEnumVariant' - | 'dataEnumDiscriminator' - | 'dataEnumVariant' - | 'dataEnumFunction' - | 'isDataEnumFunction' + | 'enumVariant' + | 'discriminatedUnionDiscriminator' + | 'discriminatedUnionVariant' + | 'discriminatedUnionFunction' + | 'isDiscriminatedUnionFunction' | 'instructionAsyncInputType' | 'instructionSyncInputType' | 'instructionType' @@ -111,11 +111,11 @@ export const DEFAULT_NAME_TRANSFORMERS: NameTransformers = { accountFetchMaybeFromSeedsFunction: (name) => `fetchMaybe${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)}`, + enumVariant: (name) => `${pascalCase(name)}`, + discriminatedUnionDiscriminator: () => '__kind', + discriminatedUnionVariant: (name) => `${pascalCase(name)}`, + discriminatedUnionFunction: (name) => `${camelCase(name)}`, + isDiscriminatedUnionFunction: (name) => `is${pascalCase(name)}`, instructionAsyncInputType: (name) => `${pascalCase(name)}AsyncInput`, instructionSyncInputType: (name) => `${pascalCase(name)}Input`, instructionType: (name) => `${pascalCase(name)}Instruction`, diff --git a/src/renderers/js-experimental/renderValueNodeVisitor.ts b/src/renderers/js-experimental/renderValueNodeVisitor.ts index 4dc2ac466..3daa586f5 100644 --- a/src/renderers/js-experimental/renderValueNodeVisitor.ts +++ b/src/renderers/js-experimental/renderValueNodeVisitor.ts @@ -24,7 +24,7 @@ export function renderValueNodeVisitor(input: { }, visitEnumValue(node) { const enumName = nameApi.dataType(node.enum.name); - const enumFunction = nameApi.dataEnumFunction(node.enum.name); + const enumFunction = nameApi.discriminatedUnionFunction(node.enum.name); const importFrom = node.enum.importFrom ?? 'generatedTypes'; const enumNode = linkables.get(node.enum)?.type; @@ -34,14 +34,14 @@ export function renderValueNodeVisitor(input: { : !nonScalarEnums.includes(node.enum.name); if (!node.value && isScalar) { - const variantName = nameApi.scalarEnumVariant(node.variant); + const variantName = nameApi.enumVariant(node.variant); return fragment(`${enumName}.${variantName}`).addImports( importFrom, enumName ); } - const variantName = nameApi.dataEnumVariant(node.variant); + const variantName = nameApi.discriminatedUnionVariant(node.variant); if (!node.value) { return fragment(`${enumFunction}('${variantName}')`).addImports( importFrom, diff --git a/src/renderers/js-experimental/templates/definedTypesPage.njk b/src/renderers/js-experimental/templates/definedTypesPage.njk index 688ab32b4..5849cad81 100644 --- a/src/renderers/js-experimental/templates/definedTypesPage.njk +++ b/src/renderers/js-experimental/templates/definedTypesPage.njk @@ -5,5 +5,5 @@ {{ imports }} {{ typeWithCodecFragment }} -{{ typeDataEnumHelpersFragment }} +{{ typeDiscriminatedUnionHelpersFragment }} {% endblock %} diff --git a/test/packages/js-experimental/src/generated/types/authorityType.ts b/test/packages/js-experimental/src/generated/types/authorityType.ts index 782cd376b..09b9a7933 100644 --- a/test/packages/js-experimental/src/generated/types/authorityType.ts +++ b/test/packages/js-experimental/src/generated/types/authorityType.ts @@ -11,8 +11,8 @@ import { Decoder, Encoder, combineCodec, - getScalarEnumDecoder, - getScalarEnumEncoder, + getEnumDecoder, + getEnumEncoder, } from '@solana/codecs'; export enum AuthorityType { @@ -25,11 +25,11 @@ export enum AuthorityType { export type AuthorityTypeArgs = AuthorityType; export function getAuthorityTypeEncoder(): Encoder { - return getScalarEnumEncoder(AuthorityType); + return getEnumEncoder(AuthorityType); } export function getAuthorityTypeDecoder(): Decoder { - return getScalarEnumDecoder(AuthorityType); + return getEnumDecoder(AuthorityType); } export function getAuthorityTypeCodec(): Codec< diff --git a/test/packages/js-experimental/src/generated/types/burnArgs.ts b/test/packages/js-experimental/src/generated/types/burnArgs.ts index 03c97fd45..0c99ac532 100644 --- a/test/packages/js-experimental/src/generated/types/burnArgs.ts +++ b/test/packages/js-experimental/src/generated/types/burnArgs.ts @@ -11,8 +11,8 @@ import { Decoder, Encoder, combineCodec, - getScalarEnumDecoder, - getScalarEnumEncoder, + getEnumDecoder, + getEnumEncoder, getU64Decoder, getU64Encoder, } from '@solana/codecs'; @@ -24,11 +24,11 @@ export enum BurnArgs { export type BurnArgsArgs = BurnArgs; export function getBurnArgsEncoder(): Encoder { - return getScalarEnumEncoder(BurnArgs, { size: getU64Encoder() }); + return getEnumEncoder(BurnArgs, { size: getU64Encoder() }); } export function getBurnArgsDecoder(): Decoder { - return getScalarEnumDecoder(BurnArgs, { size: getU64Decoder() }); + return getEnumDecoder(BurnArgs, { size: getU64Decoder() }); } export function getBurnArgsCodec(): Codec { diff --git a/test/packages/js-experimental/src/generated/types/collectionDetails.ts b/test/packages/js-experimental/src/generated/types/collectionDetails.ts index 914fca35d..5800fff4e 100644 --- a/test/packages/js-experimental/src/generated/types/collectionDetails.ts +++ b/test/packages/js-experimental/src/generated/types/collectionDetails.ts @@ -10,11 +10,11 @@ import { Codec, Decoder, Encoder, - GetDataEnumKind, - GetDataEnumKindContent, + GetDiscriminatedUnionVariant, + GetDiscriminatedUnionVariantContent, combineCodec, - getDataEnumDecoder, - getDataEnumEncoder, + getDiscriminatedUnionDecoder, + getDiscriminatedUnionEncoder, getStructDecoder, getStructEncoder, getU64Decoder, @@ -26,13 +26,13 @@ export type CollectionDetails = { __kind: 'V1'; size: bigint }; export type CollectionDetailsArgs = { __kind: 'V1'; size: number | bigint }; export function getCollectionDetailsEncoder(): Encoder { - return getDataEnumEncoder([ + return getDiscriminatedUnionEncoder([ ['V1', getStructEncoder([['size', getU64Encoder()]])], ]); } export function getCollectionDetailsDecoder(): Decoder { - return getDataEnumDecoder([ + return getDiscriminatedUnionDecoder([ ['V1', getStructDecoder([['size', getU64Decoder()]])], ]); } @@ -50,8 +50,12 @@ export function getCollectionDetailsCodec(): Codec< // Data Enum Helpers. export function collectionDetails( kind: 'V1', - data: GetDataEnumKindContent -): GetDataEnumKind; + data: GetDiscriminatedUnionVariantContent< + CollectionDetailsArgs, + '__kind', + 'V1' + > +): GetDiscriminatedUnionVariant; export function collectionDetails< K extends CollectionDetailsArgs['__kind'], Data, diff --git a/test/packages/js-experimental/src/generated/types/delegateArgs.ts b/test/packages/js-experimental/src/generated/types/delegateArgs.ts index c36d04be2..29b10544a 100644 --- a/test/packages/js-experimental/src/generated/types/delegateArgs.ts +++ b/test/packages/js-experimental/src/generated/types/delegateArgs.ts @@ -10,11 +10,11 @@ import { Codec, Decoder, Encoder, - GetDataEnumKind, - GetDataEnumKindContent, + GetDiscriminatedUnionVariant, + GetDiscriminatedUnionVariantContent, combineCodec, - getDataEnumDecoder, - getDataEnumEncoder, + getDiscriminatedUnionDecoder, + getDiscriminatedUnionEncoder, getStructDecoder, getStructEncoder, getU16Decoder, @@ -36,7 +36,7 @@ export type DelegateArgsArgs = | { __kind: 'TransferV1'; amount: number | bigint }; export function getDelegateArgsEncoder(): Encoder { - return getDataEnumEncoder( + return getDiscriminatedUnionEncoder( [ ['CollectionV1', getUnitEncoder()], ['SaleV1', getStructEncoder([['amount', getU64Encoder()]])], @@ -47,7 +47,7 @@ export function getDelegateArgsEncoder(): Encoder { } export function getDelegateArgsDecoder(): Decoder { - return getDataEnumDecoder( + return getDiscriminatedUnionDecoder( [ ['CollectionV1', getUnitDecoder()], ['SaleV1', getStructDecoder([['amount', getU64Decoder()]])], @@ -64,15 +64,23 @@ export function getDelegateArgsCodec(): Codec { // Data Enum Helpers. export function delegateArgs( kind: 'CollectionV1' -): GetDataEnumKind; +): GetDiscriminatedUnionVariant; export function delegateArgs( kind: 'SaleV1', - data: GetDataEnumKindContent -): GetDataEnumKind; + data: GetDiscriminatedUnionVariantContent< + DelegateArgsArgs, + '__kind', + 'SaleV1' + > +): GetDiscriminatedUnionVariant; export function delegateArgs( kind: 'TransferV1', - data: GetDataEnumKindContent -): GetDataEnumKind; + data: GetDiscriminatedUnionVariantContent< + DelegateArgsArgs, + '__kind', + 'TransferV1' + > +): GetDiscriminatedUnionVariant; export function delegateArgs( kind: K, data?: Data diff --git a/test/packages/js-experimental/src/generated/types/delegateRole.ts b/test/packages/js-experimental/src/generated/types/delegateRole.ts index 93d937fef..978365de7 100644 --- a/test/packages/js-experimental/src/generated/types/delegateRole.ts +++ b/test/packages/js-experimental/src/generated/types/delegateRole.ts @@ -11,8 +11,8 @@ import { Decoder, Encoder, combineCodec, - getScalarEnumDecoder, - getScalarEnumEncoder, + getEnumDecoder, + getEnumEncoder, } from '@solana/codecs'; export enum DelegateRole { @@ -28,11 +28,11 @@ export enum DelegateRole { export type DelegateRoleArgs = DelegateRole; export function getDelegateRoleEncoder(): Encoder { - return getScalarEnumEncoder(DelegateRole); + return getEnumEncoder(DelegateRole); } export function getDelegateRoleDecoder(): Decoder { - return getScalarEnumDecoder(DelegateRole); + return getEnumDecoder(DelegateRole); } export function getDelegateRoleCodec(): Codec { diff --git a/test/packages/js-experimental/src/generated/types/escrowAuthority.ts b/test/packages/js-experimental/src/generated/types/escrowAuthority.ts index 89b832bcb..2b23134e4 100644 --- a/test/packages/js-experimental/src/generated/types/escrowAuthority.ts +++ b/test/packages/js-experimental/src/generated/types/escrowAuthority.ts @@ -15,11 +15,11 @@ import { Codec, Decoder, Encoder, - GetDataEnumKind, - GetDataEnumKindContent, + GetDiscriminatedUnionVariant, + GetDiscriminatedUnionVariantContent, combineCodec, - getDataEnumDecoder, - getDataEnumEncoder, + getDiscriminatedUnionDecoder, + getDiscriminatedUnionEncoder, getStructDecoder, getStructEncoder, getTupleDecoder, @@ -35,7 +35,7 @@ export type EscrowAuthority = export type EscrowAuthorityArgs = EscrowAuthority; export function getEscrowAuthorityEncoder(): Encoder { - return getDataEnumEncoder([ + return getDiscriminatedUnionEncoder([ ['TokenOwner', getUnitEncoder()], [ 'Creator', @@ -45,7 +45,7 @@ export function getEscrowAuthorityEncoder(): Encoder { } export function getEscrowAuthorityDecoder(): Decoder { - return getDataEnumDecoder([ + return getDiscriminatedUnionDecoder([ ['TokenOwner', getUnitDecoder()], [ 'Creator', @@ -64,11 +64,15 @@ export function getEscrowAuthorityCodec(): Codec< // Data Enum Helpers. export function escrowAuthority( kind: 'TokenOwner' -): GetDataEnumKind; +): GetDiscriminatedUnionVariant; export function escrowAuthority( kind: 'Creator', - data: GetDataEnumKindContent['fields'] -): GetDataEnumKind; + data: GetDiscriminatedUnionVariantContent< + EscrowAuthorityArgs, + '__kind', + 'Creator' + >['fields'] +): GetDiscriminatedUnionVariant; export function escrowAuthority( kind: K, data?: Data diff --git a/test/packages/js-experimental/src/generated/types/migrateArgs.ts b/test/packages/js-experimental/src/generated/types/migrateArgs.ts index fc66e515b..4597594c8 100644 --- a/test/packages/js-experimental/src/generated/types/migrateArgs.ts +++ b/test/packages/js-experimental/src/generated/types/migrateArgs.ts @@ -11,8 +11,8 @@ import { Decoder, Encoder, combineCodec, - getScalarEnumDecoder, - getScalarEnumEncoder, + getEnumDecoder, + getEnumEncoder, } from '@solana/codecs'; export enum MigrateArgs { @@ -22,11 +22,11 @@ export enum MigrateArgs { export type MigrateArgsArgs = MigrateArgs; export function getMigrateArgsEncoder(): Encoder { - return getScalarEnumEncoder(MigrateArgs); + return getEnumEncoder(MigrateArgs); } export function getMigrateArgsDecoder(): Decoder { - return getScalarEnumDecoder(MigrateArgs); + return getEnumDecoder(MigrateArgs); } export function getMigrateArgsCodec(): Codec { diff --git a/test/packages/js-experimental/src/generated/types/mintArgs.ts b/test/packages/js-experimental/src/generated/types/mintArgs.ts index 34ae076c7..a0d520fdb 100644 --- a/test/packages/js-experimental/src/generated/types/mintArgs.ts +++ b/test/packages/js-experimental/src/generated/types/mintArgs.ts @@ -10,13 +10,13 @@ import { Codec, Decoder, Encoder, - GetDataEnumKind, - GetDataEnumKindContent, + GetDiscriminatedUnionVariant, + GetDiscriminatedUnionVariantContent, Option, OptionOrNullable, combineCodec, - getDataEnumDecoder, - getDataEnumEncoder, + getDiscriminatedUnionDecoder, + getDiscriminatedUnionEncoder, getOptionDecoder, getOptionEncoder, getStructDecoder, @@ -44,7 +44,7 @@ export type MintArgsArgs = { }; export function getMintArgsEncoder(): Encoder { - return getDataEnumEncoder([ + return getDiscriminatedUnionEncoder([ [ 'V1', getStructEncoder([ @@ -56,7 +56,7 @@ export function getMintArgsEncoder(): Encoder { } export function getMintArgsDecoder(): Decoder { - return getDataEnumDecoder([ + return getDiscriminatedUnionDecoder([ [ 'V1', getStructDecoder([ @@ -74,8 +74,8 @@ export function getMintArgsCodec(): Codec { // Data Enum Helpers. export function mintArgs( kind: 'V1', - data: GetDataEnumKindContent -): GetDataEnumKind; + data: GetDiscriminatedUnionVariantContent +): GetDiscriminatedUnionVariant; export function mintArgs( kind: K, data?: Data diff --git a/test/packages/js-experimental/src/generated/types/operation.ts b/test/packages/js-experimental/src/generated/types/operation.ts index 037b6f1b6..23d442dc8 100644 --- a/test/packages/js-experimental/src/generated/types/operation.ts +++ b/test/packages/js-experimental/src/generated/types/operation.ts @@ -11,8 +11,8 @@ import { Decoder, Encoder, combineCodec, - getScalarEnumDecoder, - getScalarEnumEncoder, + getEnumDecoder, + getEnumEncoder, } from '@solana/codecs'; export enum Operation { @@ -25,11 +25,11 @@ export enum Operation { export type OperationArgs = Operation; export function getOperationEncoder(): Encoder { - return getScalarEnumEncoder(Operation); + return getEnumEncoder(Operation); } export function getOperationDecoder(): Decoder { - return getScalarEnumDecoder(Operation); + return getEnumDecoder(Operation); } export function getOperationCodec(): Codec { diff --git a/test/packages/js-experimental/src/generated/types/payloadKey.ts b/test/packages/js-experimental/src/generated/types/payloadKey.ts index c6e0a44fa..421ba02b2 100644 --- a/test/packages/js-experimental/src/generated/types/payloadKey.ts +++ b/test/packages/js-experimental/src/generated/types/payloadKey.ts @@ -11,8 +11,8 @@ import { Decoder, Encoder, combineCodec, - getScalarEnumDecoder, - getScalarEnumEncoder, + getEnumDecoder, + getEnumEncoder, } from '@solana/codecs'; export enum PayloadKey { @@ -25,11 +25,11 @@ export enum PayloadKey { export type PayloadKeyArgs = PayloadKey; export function getPayloadKeyEncoder(): Encoder { - return getScalarEnumEncoder(PayloadKey); + return getEnumEncoder(PayloadKey); } export function getPayloadKeyDecoder(): Decoder { - return getScalarEnumDecoder(PayloadKey); + return getEnumDecoder(PayloadKey); } export function getPayloadKeyCodec(): Codec { diff --git a/test/packages/js-experimental/src/generated/types/payloadType.ts b/test/packages/js-experimental/src/generated/types/payloadType.ts index 48721fff5..f52b9d623 100644 --- a/test/packages/js-experimental/src/generated/types/payloadType.ts +++ b/test/packages/js-experimental/src/generated/types/payloadType.ts @@ -15,15 +15,15 @@ import { Codec, Decoder, Encoder, - GetDataEnumKind, - GetDataEnumKindContent, + GetDiscriminatedUnionVariant, + GetDiscriminatedUnionVariantContent, combineCodec, getArrayDecoder, getArrayEncoder, getBytesDecoder, getBytesEncoder, - getDataEnumDecoder, - getDataEnumEncoder, + getDiscriminatedUnionDecoder, + getDiscriminatedUnionEncoder, getStructDecoder, getStructEncoder, getTupleDecoder, @@ -48,7 +48,7 @@ export type PayloadTypeArgs = | { __kind: 'Number'; fields: [number | bigint] }; export function getPayloadTypeEncoder(): Encoder { - return getDataEnumEncoder([ + return getDiscriminatedUnionEncoder([ [ 'Pubkey', getStructEncoder([['fields', getTupleEncoder([getAddressEncoder()])]]), @@ -74,7 +74,7 @@ export function getPayloadTypeEncoder(): Encoder { } export function getPayloadTypeDecoder(): Decoder { - return getDataEnumDecoder([ + return getDiscriminatedUnionDecoder([ [ 'Pubkey', getStructDecoder([['fields', getTupleDecoder([getAddressDecoder()])]]), @@ -106,20 +106,32 @@ export function getPayloadTypeCodec(): Codec { // Data Enum Helpers. export function payloadType( kind: 'Pubkey', - data: GetDataEnumKindContent['fields'] -): GetDataEnumKind; + data: GetDiscriminatedUnionVariantContent< + PayloadTypeArgs, + '__kind', + 'Pubkey' + >['fields'] +): GetDiscriminatedUnionVariant; export function payloadType( kind: 'Seeds', - data: GetDataEnumKindContent -): GetDataEnumKind; + data: GetDiscriminatedUnionVariantContent +): GetDiscriminatedUnionVariant; export function payloadType( kind: 'MerkleProof', - data: GetDataEnumKindContent -): GetDataEnumKind; + data: GetDiscriminatedUnionVariantContent< + PayloadTypeArgs, + '__kind', + 'MerkleProof' + > +): GetDiscriminatedUnionVariant; export function payloadType( kind: 'Number', - data: GetDataEnumKindContent['fields'] -): GetDataEnumKind; + data: GetDiscriminatedUnionVariantContent< + PayloadTypeArgs, + '__kind', + 'Number' + >['fields'] +): GetDiscriminatedUnionVariant; export function payloadType( kind: K, data?: Data diff --git a/test/packages/js-experimental/src/generated/types/revokeArgs.ts b/test/packages/js-experimental/src/generated/types/revokeArgs.ts index 157d9a2a8..0e191e532 100644 --- a/test/packages/js-experimental/src/generated/types/revokeArgs.ts +++ b/test/packages/js-experimental/src/generated/types/revokeArgs.ts @@ -11,8 +11,8 @@ import { Decoder, Encoder, combineCodec, - getScalarEnumDecoder, - getScalarEnumEncoder, + getEnumDecoder, + getEnumEncoder, } from '@solana/codecs'; export enum RevokeArgs { @@ -24,11 +24,11 @@ export enum RevokeArgs { export type RevokeArgsArgs = RevokeArgs; export function getRevokeArgsEncoder(): Encoder { - return getScalarEnumEncoder(RevokeArgs); + return getEnumEncoder(RevokeArgs); } export function getRevokeArgsDecoder(): Decoder { - return getScalarEnumDecoder(RevokeArgs); + return getEnumDecoder(RevokeArgs); } export function getRevokeArgsCodec(): Codec { diff --git a/test/packages/js-experimental/src/generated/types/taKey.ts b/test/packages/js-experimental/src/generated/types/taKey.ts index 4f225b1fb..0eca299f1 100644 --- a/test/packages/js-experimental/src/generated/types/taKey.ts +++ b/test/packages/js-experimental/src/generated/types/taKey.ts @@ -11,8 +11,8 @@ import { Decoder, Encoder, combineCodec, - getScalarEnumDecoder, - getScalarEnumEncoder, + getEnumDecoder, + getEnumEncoder, } from '@solana/codecs'; export enum TaKey { @@ -23,11 +23,11 @@ export enum TaKey { export type TaKeyArgs = TaKey; export function getTaKeyEncoder(): Encoder { - return getScalarEnumEncoder(TaKey); + return getEnumEncoder(TaKey); } export function getTaKeyDecoder(): Decoder { - return getScalarEnumDecoder(TaKey); + return getEnumDecoder(TaKey); } export function getTaKeyCodec(): Codec { diff --git a/test/packages/js-experimental/src/generated/types/tmCreateArgs.ts b/test/packages/js-experimental/src/generated/types/tmCreateArgs.ts index 6018807d2..e013644b5 100644 --- a/test/packages/js-experimental/src/generated/types/tmCreateArgs.ts +++ b/test/packages/js-experimental/src/generated/types/tmCreateArgs.ts @@ -10,13 +10,13 @@ import { Codec, Decoder, Encoder, - GetDataEnumKind, - GetDataEnumKindContent, + GetDiscriminatedUnionVariant, + GetDiscriminatedUnionVariantContent, Option, OptionOrNullable, combineCodec, - getDataEnumDecoder, - getDataEnumEncoder, + getDiscriminatedUnionDecoder, + getDiscriminatedUnionEncoder, getOptionDecoder, getOptionEncoder, getStructDecoder, @@ -56,7 +56,7 @@ export type TmCreateArgsArgs = }; export function getTmCreateArgsEncoder(): Encoder { - return getDataEnumEncoder([ + return getDiscriminatedUnionEncoder([ [ 'V1', getStructEncoder([ @@ -76,7 +76,7 @@ export function getTmCreateArgsEncoder(): Encoder { } export function getTmCreateArgsDecoder(): Decoder { - return getDataEnumDecoder([ + return getDiscriminatedUnionDecoder([ [ 'V1', getStructDecoder([ @@ -102,12 +102,12 @@ export function getTmCreateArgsCodec(): Codec { // Data Enum Helpers. export function tmCreateArgs( kind: 'V1', - data: GetDataEnumKindContent -): GetDataEnumKind; + data: GetDiscriminatedUnionVariantContent +): GetDiscriminatedUnionVariant; export function tmCreateArgs( kind: 'V2', - data: GetDataEnumKindContent -): GetDataEnumKind; + data: GetDiscriminatedUnionVariantContent +): GetDiscriminatedUnionVariant; export function tmCreateArgs( kind: K, data?: Data diff --git a/test/packages/js-experimental/src/generated/types/tmKey.ts b/test/packages/js-experimental/src/generated/types/tmKey.ts index 6f79e47bd..14314a8da 100644 --- a/test/packages/js-experimental/src/generated/types/tmKey.ts +++ b/test/packages/js-experimental/src/generated/types/tmKey.ts @@ -11,8 +11,8 @@ import { Decoder, Encoder, combineCodec, - getScalarEnumDecoder, - getScalarEnumEncoder, + getEnumDecoder, + getEnumEncoder, } from '@solana/codecs'; export enum TmKey { @@ -33,11 +33,11 @@ export enum TmKey { export type TmKeyArgs = TmKey; export function getTmKeyEncoder(): Encoder { - return getScalarEnumEncoder(TmKey); + return getEnumEncoder(TmKey); } export function getTmKeyDecoder(): Decoder { - return getScalarEnumDecoder(TmKey); + return getEnumDecoder(TmKey); } export function getTmKeyCodec(): Codec { diff --git a/test/packages/js-experimental/src/generated/types/tokenStandard.ts b/test/packages/js-experimental/src/generated/types/tokenStandard.ts index 83dd95abe..f3b634673 100644 --- a/test/packages/js-experimental/src/generated/types/tokenStandard.ts +++ b/test/packages/js-experimental/src/generated/types/tokenStandard.ts @@ -11,8 +11,8 @@ import { Decoder, Encoder, combineCodec, - getScalarEnumDecoder, - getScalarEnumEncoder, + getEnumDecoder, + getEnumEncoder, } from '@solana/codecs'; export enum TokenStandard { @@ -26,11 +26,11 @@ export enum TokenStandard { export type TokenStandardArgs = TokenStandard; export function getTokenStandardEncoder(): Encoder { - return getScalarEnumEncoder(TokenStandard); + return getEnumEncoder(TokenStandard); } export function getTokenStandardDecoder(): Decoder { - return getScalarEnumDecoder(TokenStandard); + return getEnumDecoder(TokenStandard); } export function getTokenStandardCodec(): Codec< diff --git a/test/packages/js-experimental/src/generated/types/transferArgs.ts b/test/packages/js-experimental/src/generated/types/transferArgs.ts index d4bed8fe7..3274b6ec6 100644 --- a/test/packages/js-experimental/src/generated/types/transferArgs.ts +++ b/test/packages/js-experimental/src/generated/types/transferArgs.ts @@ -10,13 +10,13 @@ import { Codec, Decoder, Encoder, - GetDataEnumKind, - GetDataEnumKindContent, + GetDiscriminatedUnionVariant, + GetDiscriminatedUnionVariantContent, Option, OptionOrNullable, combineCodec, - getDataEnumDecoder, - getDataEnumEncoder, + getDiscriminatedUnionDecoder, + getDiscriminatedUnionEncoder, getOptionDecoder, getOptionEncoder, getStructDecoder, @@ -44,7 +44,7 @@ export type TransferArgsArgs = { }; export function getTransferArgsEncoder(): Encoder { - return getDataEnumEncoder([ + return getDiscriminatedUnionEncoder([ [ 'V1', getStructEncoder([ @@ -56,7 +56,7 @@ export function getTransferArgsEncoder(): Encoder { } export function getTransferArgsDecoder(): Decoder { - return getDataEnumDecoder([ + return getDiscriminatedUnionDecoder([ [ 'V1', getStructDecoder([ @@ -74,8 +74,8 @@ export function getTransferArgsCodec(): Codec { // Data Enum Helpers. export function transferArgs( kind: 'V1', - data: GetDataEnumKindContent -): GetDataEnumKind; + data: GetDiscriminatedUnionVariantContent +): GetDiscriminatedUnionVariant; export function transferArgs( kind: K, data?: Data diff --git a/test/packages/js-experimental/src/generated/types/updateArgs.ts b/test/packages/js-experimental/src/generated/types/updateArgs.ts index 22c2dddcb..40a6711dd 100644 --- a/test/packages/js-experimental/src/generated/types/updateArgs.ts +++ b/test/packages/js-experimental/src/generated/types/updateArgs.ts @@ -15,8 +15,8 @@ import { Codec, Decoder, Encoder, - GetDataEnumKind, - GetDataEnumKindContent, + GetDiscriminatedUnionVariant, + GetDiscriminatedUnionVariantContent, Option, OptionOrNullable, combineCodec, @@ -24,8 +24,8 @@ import { getArrayEncoder, getBooleanDecoder, getBooleanEncoder, - getDataEnumDecoder, - getDataEnumEncoder, + getDiscriminatedUnionDecoder, + getDiscriminatedUnionEncoder, getOptionDecoder, getOptionEncoder, getStringDecoder, @@ -121,7 +121,7 @@ export type UpdateArgsArgs = { }; export function getUpdateArgsEncoder(): Encoder { - return getDataEnumEncoder([ + return getDiscriminatedUnionEncoder([ [ 'V1', mapEncoder( @@ -172,7 +172,7 @@ export function getUpdateArgsEncoder(): Encoder { } export function getUpdateArgsDecoder(): Decoder { - return getDataEnumDecoder([ + return getDiscriminatedUnionDecoder([ [ 'V1', getStructDecoder([ @@ -217,8 +217,8 @@ export function getUpdateArgsCodec(): Codec { // Data Enum Helpers. export function updateArgs( kind: 'V1', - data: GetDataEnumKindContent -): GetDataEnumKind; + data: GetDiscriminatedUnionVariantContent +): GetDiscriminatedUnionVariant; export function updateArgs( kind: K, data?: Data diff --git a/test/packages/js-experimental/src/generated/types/useAssetArgs.ts b/test/packages/js-experimental/src/generated/types/useAssetArgs.ts index a2137ceef..6c66ec648 100644 --- a/test/packages/js-experimental/src/generated/types/useAssetArgs.ts +++ b/test/packages/js-experimental/src/generated/types/useAssetArgs.ts @@ -10,11 +10,11 @@ import { Codec, Decoder, Encoder, - GetDataEnumKind, - GetDataEnumKindContent, + GetDiscriminatedUnionVariant, + GetDiscriminatedUnionVariantContent, combineCodec, - getDataEnumDecoder, - getDataEnumEncoder, + getDiscriminatedUnionDecoder, + getDiscriminatedUnionEncoder, getStructDecoder, getStructEncoder, getU64Decoder, @@ -26,13 +26,13 @@ export type UseAssetArgs = { __kind: 'V1'; useCount: bigint }; export type UseAssetArgsArgs = { __kind: 'V1'; useCount: number | bigint }; export function getUseAssetArgsEncoder(): Encoder { - return getDataEnumEncoder([ + return getDiscriminatedUnionEncoder([ ['V1', getStructEncoder([['useCount', getU64Encoder()]])], ]); } export function getUseAssetArgsDecoder(): Decoder { - return getDataEnumDecoder([ + return getDiscriminatedUnionDecoder([ ['V1', getStructDecoder([['useCount', getU64Decoder()]])], ]); } @@ -44,8 +44,8 @@ export function getUseAssetArgsCodec(): Codec { // Data Enum Helpers. export function useAssetArgs( kind: 'V1', - data: GetDataEnumKindContent -): GetDataEnumKind; + data: GetDiscriminatedUnionVariantContent +): GetDiscriminatedUnionVariant; export function useAssetArgs( kind: K, data?: Data diff --git a/test/packages/js-experimental/src/generated/types/useMethod.ts b/test/packages/js-experimental/src/generated/types/useMethod.ts index a206c846e..77771f0cb 100644 --- a/test/packages/js-experimental/src/generated/types/useMethod.ts +++ b/test/packages/js-experimental/src/generated/types/useMethod.ts @@ -11,8 +11,8 @@ import { Decoder, Encoder, combineCodec, - getScalarEnumDecoder, - getScalarEnumEncoder, + getEnumDecoder, + getEnumEncoder, } from '@solana/codecs'; export enum UseMethod { @@ -24,11 +24,11 @@ export enum UseMethod { export type UseMethodArgs = UseMethod; export function getUseMethodEncoder(): Encoder { - return getScalarEnumEncoder(UseMethod); + return getEnumEncoder(UseMethod); } export function getUseMethodDecoder(): Decoder { - return getScalarEnumDecoder(UseMethod); + return getEnumDecoder(UseMethod); } export function getUseMethodCodec(): Codec { diff --git a/test/packages/js-experimental/src/generated/types/verifyArgs.ts b/test/packages/js-experimental/src/generated/types/verifyArgs.ts index de2e670d1..3525b0df9 100644 --- a/test/packages/js-experimental/src/generated/types/verifyArgs.ts +++ b/test/packages/js-experimental/src/generated/types/verifyArgs.ts @@ -11,8 +11,8 @@ import { Decoder, Encoder, combineCodec, - getScalarEnumDecoder, - getScalarEnumEncoder, + getEnumDecoder, + getEnumEncoder, } from '@solana/codecs'; export enum VerifyArgs { @@ -22,11 +22,11 @@ export enum VerifyArgs { export type VerifyArgsArgs = VerifyArgs; export function getVerifyArgsEncoder(): Encoder { - return getScalarEnumEncoder(VerifyArgs); + return getEnumEncoder(VerifyArgs); } export function getVerifyArgsDecoder(): Decoder { - return getScalarEnumDecoder(VerifyArgs); + return getEnumDecoder(VerifyArgs); } export function getVerifyArgsCodec(): Codec {