帖子
分享您的知识。
如何使用 SUI TS SDK 从 node js 应用程序中调用?
这是 Sui 动作代码库的片段
public enum Category has copy, drop, store {
A,
B,
C
}
public entry fun process_categories(categories: vector<Category>, ctx: &mut TxContext)
如何使用 SUI TS SDK 从节点 js 应用程序中调用 process_categories 函数,特别是将类别作为参数发送?
- Sui
答案
4process_categories
要categories
使用 Sui TypeScript 软件开发工具包从 Node.js 应用程序中调用该函数,请[0, 1]
将参数作为与枚举变体(例如 f A
or B
、)对应的数字数组传递. Move vector<Category>
会自动将数值反序列化为正确的枚举变体. 使用 S transaction
DK 中的对象来生成调用:
const tx = new Transaction();
tx.moveCall({
target: `${packageId}::module_name::process_categories`,
arguments: [tx.pure([0, 1])], // e.g., Category::A, Category::B
});
确保在 Move 模块中定义了枚举并且正确引用了该包.
要process_categories
使用 Sui TypeScript SDK 从 Node.js 应用程序中调用该函数,您需要执行以下操作:
1. 安装 Sui SDK
首先,确保你的项目中已经安装了 Sui SDK:
npm install @mysten/sui.js
2. 设置连接
接下来,你需要连接到 Sui 网络. 这样做是JsonRpcProvider
为了获得对区块链的访问权限.
import { JsonRpcProvider, RawSigner, Ed25519Keypair } from '@mysten/sui.js';
const provider = new JsonRpcProvider('https://fullnode.devnet.sui.io:5001'); // Change the URL if you're using Testnet/Mainnet
const keypair = Ed25519Keypair.fromFile('path/to/your/secret.key'); // Path to your private key
const signer = new RawSigner(keypair, provider);
3. 创建类别矢量
你正在调用的函数需要一个Category
值向量,所以你需要将其作为参数传递. 由于要传递诸如A``B
、和之类的枚举值C
,因此需要准备类别数组.
const categories = ["A", "B", "C"]; // These are the categories you want to pass
4. 创建并发送交易
现在,你将创建一个调用process_categories
函数的交易,将类别作为参数传递. 您将使用TransactionBlock
进行设置.
import { TransactionBlock } from '@mysten/sui.js';
const tx = new TransactionBlock();
// Create the vector of categories as an argument
const categoriesArg = tx.makeMoveVec(categories.map(category => tx.pure(category)));
// Call the Move function, passing the categories
tx.moveCall({
target: '0xYourPackageAddress::YourModule::process_categories', // Replace with your actual package and module address
arguments: [categoriesArg],
});
// Sign and execute the transaction
const response = await signer.signAndExecuteTransactionBlock({ transactionBlock: tx });
console.log('Transaction response:', response);
发生了什么:
*创建类别:您正在创建类别的矢量 A``B
,并将其C
作为一个简单的数组并将其转换为 Sui 可以理解的格式.
*调用移动函数:你是在让 Sui 使用类别向process_categories
量进行调用.
*签署交易:对RawSigner
交易进行签名,然后您在网络上执行交易.
关键注意事项:
*包裹地址:0xYourPackageAddress::YourModule::process_categories
替换为 Move 模块的实际地址.
*交易确认:这将发送交易,您将在控制台中得到响应. 如果出现问题,回复通常会告诉你出了什么问题.
要process_categories
使用 Sui TS SDK 从 Node.js 调用,您需要:
- 使用 BCS 序列化
Category
枚举向量. - 创建并执行交易.
####示例代码 (Node.js):
import { Transaction } from '@mysten/sui.js/transactions';
import { bcs } from '@mysten/sui.js/bcs';
// Define BCS type for the enum (must match Move definition)
bcs.registerEnumType('Category', {
A: null,
B: null,
C: null,
});
const tx = new Transaction();
tx.moveCall({
target: '0xPACKAGE_ID::module_name::process_categories',
arguments: [
tx.pure(bcs.vector(bcs.Category).serialize(['A', 'B', 'C']), // Categories vector
tx.pure.address('0xYourAddress'), // TxContext (if needed)
],
});
// Sign and execute
const signedTx = await wallet.signTransaction(tx);
const result = await client.executeTransaction(signedTx);
###关键步骤:
A``B``C
注册 BCS 枚举:匹配移动枚举 (,,).bcs.vector()
序列化向量:用于参数.
3.调用移动功能:指定软件包、模块和函数.
###注意事项:
-将0xPACKAGE_ID
和module_name
替换为您的实际值.
-确保钱包 (wallet
) 和客户端 (client
) 已初始化.
要process_categories
使用 Sui TypeScript 软件开发工具包从 Node.js 应用程序中调用该函数并将vector<Category>
作为参数传递,你需要将枚举值序列化为 Sui Move 在交易执行期间所期望的格式. 在你的例子中,Category
是一个enum
带有变体A``B
、和的移动C
,在 BCS 编码中用数字判别值(0、1、2)表示. vector<Category>
由于 Move 需要 avector<u8>
,因此您需要将枚举值序列化为 a,TransactionBlock
并使用将其正确传递给函数.
您可以通过以下方式在 Node.js 中使用以下方法执行此操作@mysten/sui.js
:
import { TransactionBlock, SuiClient, getFullnodeUrl } from '@mysten/sui.js';
import { bcs } from '@mysten/bcs';
// Enum mapping: A = 0, B = 1, C = 2
const CategoryEnum = {
A: 0,
B: 1,
C: 2,
};
// Example categories you want to send
const selectedCategories = [CategoryEnum.A, CategoryEnum.B];
// Serialize the enum values into a BCS vector<u8>
const serializedCategories = bcs.vector(bcs.u8()).serialize(selectedCategories).toBytes();
// Build the transaction
const tx = new TransactionBlock();
tx.moveCall({
target: '0xYourPackage::your_module::process_categories',
arguments: [tx.pure(serializedCategories, 'vector<u8>')],
});
// Execute the transaction
const client = new SuiClient({ url: getFullnodeUrl('testnet') });
const result = await client.signAndExecuteTransactionBlock({
signer: yourWalletOrKeypair,
transactionBlock: tx,
});
console.log('Transaction result:', result);
这种模式可确保您的枚举值以正确编码的形式vector<Category>
从 JavaScript 传递到 Move. 只要确保你的Move模块在链上发布并且你引用了正确target
的地址和模块名称即可.
有关使用 BCS 进行移动参数和枚举序列化的参考,请参见: https://docs.sui.io/build/programmable-transactions
你知道答案吗?
请登录并分享。
Sui is a Layer 1 protocol blockchain designed as the first internet-scale programmable blockchain platform.