Using Typescript SDK to pass Optional object of type fungible metadata #549
-
Discord user IDDescribe your question in detail.I want to call a public script function using typescript sdk. public entry fun test(&signer, Optional<0x1::object::Object<0x1::fungible_asset::Metadata>>, u64) what typescript object would be a or in case i dont want it an empty optional. I saw these are the script function argument types: Saw MoveOption is missing the serializeForScriptFunction method. What error, if any, are you getting?No response What have you tried or looked at? Or how can we reproduce the error? let ty_args: Array<TypeTag> = [];
let args: Array<TransactionArgument> = [new U64(1000)];
ty_args.push(new TypeTagU8())
args.push(new U64(BigInt(27)))
args.push(new Bool(false))
args.push(AccountAddress.fromStringStrict("0x1"))
/* Here I want to get an optional object which is a meta data of a fungible asset. and no idea how. */
args.push(new MoveString(""))
const payload = new TransactionPayloadScript(
new Script(
Hex.fromHexString(script.parsed).toUint8Array(),
ty_args ,args )); Which operating system are you using?Linux (Ubuntu, Fedora, Windows WSL, etc.) Which SDK or tool are you using? (if any)TypeScript SDK Describe your environment or tooling in detailNo response |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
To pass an Optional object in the TypeScript SDK when calling a Move script function, you need to use MoveOption. While it's true that MoveOption is missing the serializeForScriptFunction method, you can work around this limitation by serializing the optional value directly using a custom approach. |
Beta Was this translation helpful? Give feedback.
-
You can check out the Example below to pass an Optional argument of type 0x1::object::Object<0x1::fungible_asset::Metadata> or an empty Optional: `import { MoveOption, TransactionArgument, TransactionPayloadScript, TypeTagStruct, TypeTagAddress, TypeTagVector, U64, AccountAddress } from "@aptos-labs/aptos-ts-sdk"; // Helper to create a MoveOption for Optional function serializeMetadata(metadata: any): Uint8Array {
} // Example script function call // Signer const optionalMetadata = createOptionalMetadata(null); args.push(new U64(1000)); // Create transaction payload ` |
Beta Was this translation helpful? Give feedback.
-
@ranlavanet |
Beta Was this translation helpful? Give feedback.
You can check out the Example below to pass an Optional argument of type 0x1::object::Object<0x1::fungible_asset::Metadata> or an empty Optional:
`import { MoveOption, TransactionArgument, TransactionPayloadScript, TypeTagStruct, TypeTagAddress, TypeTagVector, U64, AccountAddress } from "@aptos-labs/aptos-ts-sdk";
import { HexString } from "@aptos-labs/hex-string";
// Helper to create a MoveOption for Optional
function createOptionalMetadata(metadata: any | null): MoveOption {
if (metadata === null) {
return new MoveOption(null); // Represents an empty Optional
} else {
// Serialize the metadata object manually
const serializedMetadata = serializeMetadata(metadata);
return new MoveOption(…