-
Notifications
You must be signed in to change notification settings - Fork 84
Add support for public struct/enum transaction arguments #824
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,13 +8,24 @@ All notable changes to the Aptos TypeScript SDK will be captured in this file. T | |
|
|
||
| ## Added | ||
|
|
||
| - [Transactions] Add async variants of argument conversion functions (`convertArgumentWithABI`, `checkOrConvertArgumentWithABI`, `parseArgAsync`) to support fetching module ABIs for struct/enum argument encoding. Original synchronous functions remain unchanged for backwards compatibility. | ||
|
|
||
| - Add JWK caching for keyless authentication with 5-minute TTL to improve performance | ||
| - Add `clearMemoizeCache()` utility function for clearing the memoization cache | ||
| - Add Bun runtime detection with `isBun()` utility function | ||
| - Add warning at `AptosConfig` construction time when running in Bun without explicitly disabling HTTP/2 (Bun does not fully support HTTP/2, which is enabled by default) | ||
| - Add Bun runtime CI tests to verify SDK compatibility with Bun | ||
| - Add Deno runtime CI tests to verify SDK compatibility with Deno | ||
| - Add web environment CI tests using Vitest + jsdom to verify browser compatibility | ||
| - [Transactions] Add support for public copy structs and enums as transaction arguments via `MoveStructArgument`, `MoveEnumArgument`, and `StructEnumArgumentParser` classes | ||
| - Automatic type inference from function ABI | ||
| - Nested structs/enums support (up to 7 levels deep) | ||
| - Generic type parameter substitution (T0, T1, etc.) | ||
| - Support for all Move primitive types (bool, u8-u256, i8-i256, address) | ||
| - Special framework types (String, Object<T>, Option<T>) | ||
| - Option<T> dual format support (vector and enum formats) | ||
| - Module ABI caching for performance | ||
| - Comprehensive validation and error messages | ||
|
Comment on lines
+20
to
+28
|
||
|
|
||
| ## Changed | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -222,6 +222,88 @@ async function example() { | |||||||||||||||||||||||||||||||
| example(); | ||||||||||||||||||||||||||||||||
| ``` | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| ### Using Struct and Enum Arguments | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| --- | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| The SDK supports passing public copy structs and enums as transaction arguments. You must encode them using the `StructEnumArgumentParser` before passing to transaction building functions: | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| ```ts | ||||||||||||||||||||||||||||||||
| import { | ||||||||||||||||||||||||||||||||
| Aptos, | ||||||||||||||||||||||||||||||||
| AptosConfig, | ||||||||||||||||||||||||||||||||
| Network, | ||||||||||||||||||||||||||||||||
| StructEnumArgumentParser, | ||||||||||||||||||||||||||||||||
| parseTypeTag, | ||||||||||||||||||||||||||||||||
| TypeTagStruct | ||||||||||||||||||||||||||||||||
| } from "@aptos-labs/ts-sdk"; | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| const config = new AptosConfig({ network: Network.TESTNET }); | ||||||||||||||||||||||||||||||||
| const aptos = new Aptos(config); | ||||||||||||||||||||||||||||||||
| const parser = new StructEnumArgumentParser(config); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| // Example 1: Simple struct argument | ||||||||||||||||||||||||||||||||
| const pointType = parseTypeTag("0x1::shapes::Point") as TypeTagStruct; | ||||||||||||||||||||||||||||||||
| const pointArg = await parser.encodeStructArgument(pointType, { x: "10", y: "20" }); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| const transaction = await aptos.transaction.build.simple({ | ||||||||||||||||||||||||||||||||
| sender: alice.accountAddress, | ||||||||||||||||||||||||||||||||
| data: { | ||||||||||||||||||||||||||||||||
| function: "0x1::shapes::draw_point", | ||||||||||||||||||||||||||||||||
| functionArguments: [pointArg], | ||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| // Example 2: Nested structs | ||||||||||||||||||||||||||||||||
| const lineType = parseTypeTag("0x1::shapes::Line") as TypeTagStruct; | ||||||||||||||||||||||||||||||||
| const lineArg = await parser.encodeStructArgument(lineType, { | ||||||||||||||||||||||||||||||||
| start: { x: "0", y: "0" }, | ||||||||||||||||||||||||||||||||
| end: { x: "10", y: "10" } | ||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| const transaction2 = await aptos.transaction.build.simple({ | ||||||||||||||||||||||||||||||||
| sender: alice.accountAddress, | ||||||||||||||||||||||||||||||||
| data: { | ||||||||||||||||||||||||||||||||
| function: "0x1::shapes::draw_line", | ||||||||||||||||||||||||||||||||
| functionArguments: [lineArg], | ||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| // Example 3: Enum variants | ||||||||||||||||||||||||||||||||
| const colorType = parseTypeTag("0x1::game::Color") as TypeTagStruct; | ||||||||||||||||||||||||||||||||
| const colorArg = await parser.encodeEnumArgument(colorType, { Red: {} }); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| const transaction3 = await aptos.transaction.build.simple({ | ||||||||||||||||||||||||||||||||
| sender: alice.accountAddress, | ||||||||||||||||||||||||||||||||
| data: { | ||||||||||||||||||||||||||||||||
| function: "0x1::game::set_color", | ||||||||||||||||||||||||||||||||
| functionArguments: [colorArg], | ||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| // Example 4: Enum with fields | ||||||||||||||||||||||||||||||||
| const accountTypeTag = parseTypeTag("0x1::game::AccountType") as TypeTagStruct; | ||||||||||||||||||||||||||||||||
| const accountTypeArg = await parser.encodeEnumArgument(accountTypeTag, { | ||||||||||||||||||||||||||||||||
| Premium: { "0": "100" } | ||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| const transaction4 = await aptos.transaction.build.simple({ | ||||||||||||||||||||||||||||||||
| sender: alice.accountAddress, | ||||||||||||||||||||||||||||||||
| data: { | ||||||||||||||||||||||||||||||||
| function: "0x1::game::create_player", | ||||||||||||||||||||||||||||||||
| functionArguments: [accountTypeArg], | ||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
| }); | |
| }); | |
| // Example 5: Automatic type inference from function ABI (no explicit encoding) | |
| // Assuming 0x1::game::set_position takes a struct { x: u64, y: u64 } as its argument. | |
| const positionTypeTag = parseTypeTag("0x1::game::Position") as TypeTagStruct; | |
| const transaction5 = await aptos.transaction.build.simple({ | |
| sender: alice.accountAddress, | |
| data: { | |
| function: "0x1::game::set_position", | |
| // Plain object is automatically encoded based on the function ABI | |
| functionArguments: [{ x: "10", y: "20" }], | |
| }, | |
| }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The CHANGELOG states "Add async variants of argument conversion functions" and mentions "Original synchronous functions remain unchanged for backwards compatibility." However, this is misleading - the code shows that the original
checkOrConvertArgumentandparseArgfunctions are SYNCHRONOUS and a NEW async variantparseArgAsyncandcheckOrConvertArgumentWithABIwere added. The synchronouscheckOrConvertArgumentfunction remains unchanged, which is correct for backwards compatibility. The CHANGELOG should clarify this more accurately to avoid confusion.