Skip to content

Commit bf8279b

Browse files
committed
Add support for reading enum variants in ABI
1 parent d081f1a commit bf8279b

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ All notable changes to the Aptos TypeScript SDK will be captured in this file. T
44

55
# Unreleased
66

7+
- Add `variants` field to `MoveStruct` now that the ABI fully describes enums.
8+
79
## Added
810

911
- Add JWK caching for keyless authentication with 5-minute TTL to improve performance

src/transactions/transactionBuilder/remoteAbi.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -637,10 +637,10 @@ function parseArg(
637637
);
638638
}
639639

640-
// We are assuming that fieldless structs are enums, and therefore we cannot typecheck any further due
641-
// to limited information from the ABI. This does not work for structs on other modules.
640+
// Enums cannot be further type-checked from the ABI alone, so we pass through the raw bytes.
641+
// TODO: The enum information is now present in the ABI, we could use that.
642642
const structDefinition = moduleAbi?.structs.find((s) => s.name === param.value.name.identifier);
643-
if (structDefinition?.fields.length === 0 && arg instanceof Uint8Array) {
643+
if (structDefinition?.is_enum && arg instanceof Uint8Array) {
644644
return new FixedBytes(arg);
645645
}
646646

src/types/types.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1564,6 +1564,18 @@ export type MoveStructField = {
15641564
type: string;
15651565
};
15661566

1567+
/**
1568+
* A variant of a Move enum, containing a name and its associated fields.
1569+
*/
1570+
export type MoveStructVariant = {
1571+
name: string;
1572+
/**
1573+
* Fields belonging to this variant. This will be empty if the enum variant has no
1574+
* fields. e.g. if enum MyEnum { A { field: u64 }, B }, then B will have no fields.
1575+
*/
1576+
fields: Array<MoveStructField>;
1577+
};
1578+
15671579
/**
15681580
* A Move module
15691581
*/
@@ -1613,9 +1625,14 @@ export type MoveStruct = {
16131625
*/
16141626
generic_type_params: Array<MoveFunctionGenericTypeParam>;
16151627
/**
1616-
* Fields associated with the struct
1628+
* Fields associated with the struct. Populated for regular structs, empty for enums.
16171629
*/
16181630
fields: Array<MoveStructField>;
1631+
/**
1632+
* Variants of the enum. Populated when `is_enum` is true, empty for regular structs.
1633+
* Each variant has a name and can have its own set of fields.
1634+
*/
1635+
variants: Array<MoveStructVariant>;
16191636
};
16201637

16211638
/**

0 commit comments

Comments
 (0)