Post
Share your knowledge.
How can I call from a node js app using SUI TS SDK
Here is a snippet of a Sui move
public enum Category has copy, drop, store {
A,
B,
C
}
public entry fun process_categories(categories: vector<Category>, ctx: &mut TxContext)
How can I call from a node js app using SUI TS SDK, to call the process_categories function, specifically send categories as an argument?
- Sui
Answers
1To call your process_categories
function from a Node.js app using the Sui TypeScript SDK, you need to pass the categories
argument as a vector of enum values in a Move-compliant format. Here's how to do that properly using the SDK:
✅ Full Example Using Sui TS SDK
import { SuiClient, TransactionBlock } from "@mysten/sui.js/client";
import { Ed25519Keypair } from "@mysten/sui.js/keypairs";
import { fromB64 } from "@mysten/bcs";
// Setup your client and keypair
const keypair = Ed25519Keypair.deriveKeypair("your mnemonic or private key");
const suiClient = new SuiClient({ url: "https://fullnode.devnet.sui.io" });
// Build transaction
const tx = new TransactionBlock();
// Define enum variants
const categories = ["A", "B", "C"]; // example Move enum values
// Encode the enum values using bcs
const bcs = tx.bcs;
const CategoryEnum = bcs.enum("Category", {
A: null,
B: null,
C: null,
});
// Register Category type
bcs.registerEnumType("Category", {
A: null,
B: null,
C: null,
});
// Encode enum vector
const encoded = bcs.vector(CategoryEnum).serialize(categories.map((cat) => ({ [cat]: null }))).toBase64();
// Call your move function with serialized vector
tx.moveCall({
target: "0xYourPackage::YourModule::process_categories",
arguments: [
tx.pure(encoded, "vector<Category>"),
],
});
// Sign and execute
(async () => {
const result = await suiClient.signAndExecuteTransactionBlock({
signer: keypair,
transactionBlock: tx,
options: { showEffects: true },
});
console.log("Transaction Result:", result);
})();
🔎 Real-World Tip
Since enums are custom structured types, you need to encode them using Sui's BCS (Binary Canonical Serialization) format. You can't just pass ["A", "B"]
directly — you have to serialize them properly so Move can interpret the values.
📘 Read More
For more on BCS encoding in the Sui SDK: https://docs.sui.io/build/typescript-sdk/advanced-usage#bcs-serialization
And on Move types and function calling: https://docs.sui.io/build/typescript-sdk/dev-guide
Do you know the answer?
Please log in and share it.
Sui is a Layer 1 protocol blockchain designed as the first internet-scale programmable blockchain platform.
- Why does BCS require exact field order for deserialization when Move structs have named fields?53
- Multiple Source Verification Errors" in Sui Move Module Publications - Automated Error Resolution43
- Sui Transaction Failing: Objects Reserved for Another Transaction25
- How do ability constraints interact with dynamic fields in heterogeneous collections?05