From bf8279ba8992806c00627ae8b8c35ea2f96319db Mon Sep 17 00:00:00 2001 From: Daniel Porteous Date: Fri, 20 Feb 2026 16:51:08 +0000 Subject: [PATCH] Add support for reading enum variants in ABI --- CHANGELOG.md | 2 ++ .../transactionBuilder/remoteAbi.ts | 6 +++--- src/types/types.ts | 19 ++++++++++++++++++- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 97ba40c14..bbf6de8fd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ All notable changes to the Aptos TypeScript SDK will be captured in this file. T # Unreleased +- Add `variants` field to `MoveStruct` now that the ABI fully describes enums. + ## Added - Add JWK caching for keyless authentication with 5-minute TTL to improve performance diff --git a/src/transactions/transactionBuilder/remoteAbi.ts b/src/transactions/transactionBuilder/remoteAbi.ts index 4c5dae7a1..aa38fc9b1 100644 --- a/src/transactions/transactionBuilder/remoteAbi.ts +++ b/src/transactions/transactionBuilder/remoteAbi.ts @@ -637,10 +637,10 @@ function parseArg( ); } - // We are assuming that fieldless structs are enums, and therefore we cannot typecheck any further due - // to limited information from the ABI. This does not work for structs on other modules. + // Enums cannot be further type-checked from the ABI alone, so we pass through the raw bytes. + // TODO: The enum information is now present in the ABI, we could use that. const structDefinition = moduleAbi?.structs.find((s) => s.name === param.value.name.identifier); - if (structDefinition?.fields.length === 0 && arg instanceof Uint8Array) { + if (structDefinition?.is_enum && arg instanceof Uint8Array) { return new FixedBytes(arg); } diff --git a/src/types/types.ts b/src/types/types.ts index b089f0909..733502c79 100644 --- a/src/types/types.ts +++ b/src/types/types.ts @@ -1564,6 +1564,18 @@ export type MoveStructField = { type: string; }; +/** + * A variant of a Move enum, containing a name and its associated fields. + */ +export type MoveStructVariant = { + name: string; + /** + * Fields belonging to this variant. This will be empty if the enum variant has no + * fields. e.g. if enum MyEnum { A { field: u64 }, B }, then B will have no fields. + */ + fields: Array; +}; + /** * A Move module */ @@ -1613,9 +1625,14 @@ export type MoveStruct = { */ generic_type_params: Array; /** - * Fields associated with the struct + * Fields associated with the struct. Populated for regular structs, empty for enums. */ fields: Array; + /** + * Variants of the enum. Populated when `is_enum` is true, empty for regular structs. + * Each variant has a name and can have its own set of fields. + */ + variants: Array; }; /**