mcpField
T
ts-sdk-types

ts-sdk-types

github.com/aptos-labs开发2068 次安装24 周前更新
代码仓库

关于 ts-sdk-types

这项参考 Skill 面向使用 `@aptos-labs/ts-sdk` 的 TypeScript 开发者,说明 Move 类型映射和函数载荷构造。内容侧重于用 bigint 保持大整数精度、地址字符串、泛型类型参数及 TypeTag 解析。

主要功能

  1. 01Move 到 TypeScript 的类型映射
  2. 02大整数的 bigint 使用指引
  3. 03地址与函数参数格式
  4. 04泛型 typeArguments 用法
  5. 05TypeTag 解析示例

适用场景

  1. 01构造 Aptos 交易载荷
  2. 02安全处理 u128 或 u256 值
  3. 03调用泛型 Move 函数
  4. 04解读 view 结果和类型标签

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/aptos-labs/aptos-agent-skills --skill ts-sdk-types

安装后在 Claude Code 中输入/ts-sdk-types确认已加载。

详细介绍

TypeScript SDK:类型(Move ↔ TypeScript)

目的

使用 @aptos-labs/ts-sdk 时,指导如何在 Move 和 TypeScript 之间映射类型:数值类型(尤其是 u128/u256)、地址、TypeTag,以及 functionArguments / typeArguments。

务必遵守

  1. u128 和 u256 使用 bigint ——在视图结果和 functionArguments 中都如此;JavaScript number 在超过 2^53 时会丢失精度。
  2. payload 中的地址使用 string ——例如 "0x1" 或 accountAddress.toString();SDK 接受 AccountAddressInput(string 或 AccountAddress)。
  3. 泛型 Move 函数使用 typeArguments ——例如,coin::balance 或 coin::transfer 的币种类型为 ["0x1::aptos_coin::AptosCoin"]。
  4. 明确转换视图结果的类型,前提是你知道 Move 返回类型 ——例如,对 u128 使用 BigInt(result[0] as string)。

切勿

  1. 不要对 u128/u256 使用 number ——会丢失精度;应使用 bigint。
  2. 不要在 entry/view 中为较大的 u64 传入原始 number ——如果值可能超过 Number.MAX_SAFE_INTEGER,请使用 bigint。
  3. Move 函数有类型参数时,不要省略 typeArguments(例如 balance<CoinType>)。

Move → TypeScript(摘要)

Move 类型TypeScript 类型示例
u8、u16、u32number255、65535
u64number | bigint较大的值优先使用 bigint
u128、u256bigintBigInt("340282366920938463463374607431768211455")
i8..i64(Move 2.3+)number | biginti64 的值较大时使用 bigint
boolbooleantrue
addressstring"0x1"
signer—不从 TS 传入;signer 是交易发送方
vectorUint8Array | string (hex)new Uint8Array([1,2,3]) 或十六进制
vectorT[][1, 2, 3]
Stringstring"hello"
Objectstring(对象地址)objectAddress.toString()
OptionT | null值或 null

functionArguments

顺序和类型必须与 Move entry/view 函数参数一致:

// Move: public fun transfer<CoinType>(to: address, amount: u64)
await aptos.transaction.build.simple({
  sender: account.accountAddress,
  data: {
    function: "0x1::coin::transfer",
    typeArguments: ["0x1::aptos_coin::AptosCoin"],
    functionArguments: [
      "0xrecipient...", // address as string
      1000n // u64 as bigint (or number if small)
    ]
  }
});

typeArguments

对于泛型 Move 函数,传入完整类型字符串(address::module::StructName):

// Move: balance<CoinType>(addr): u64
typeArguments: ["0x1::aptos_coin::AptosCoin"];

// Move: transfer<CoinType>(to, amount)
typeArguments: ["0x1::aptos_coin::AptosCoin"];

视图返回类型

const result = await aptos.view({
  payload: {
    function: "0x1::coin::balance",
    typeArguments: ["0x1::aptos_coin::AptosCoin"],
    functionArguments: [accountAddress]
  }
});
// result is an array; u128 often returned as string in JSON
const balance = BigInt(result[0] as string);

TypeTag(高级)

以编程方式构建 payload 或解析类型字符串时:

import { TypeTag } from "@aptos-labs/ts-sdk";

// Parser for type tag strings
import { parseTypeTag } from "@aptos-labs/ts-sdk";
const tag = parseTypeTag("0x1::aptos_coin::AptosCoin");

简单场景中使用字符串数组作为 typeArguments;SDK API 要求 TypeTag 时则使用 TypeTag。


参数中的对象 / 资源地址

将对象地址作为字符串传入(按 AIP-40 使用 LONG 或 SHORT):

functionArguments: [
  nftObjectAddress.toString(), // or "0x..."
  price
];

常见错误

错误正确做法
为 u128 数量传入 number使用 1000000n 或 BigInt("...")
为 coin::balance 省略 typeArguments添加 typeArguments: ["0x1::aptos_coin::AptosCoin"]
将 u128 的 result[0] 当作 number 使用使用 BigInt(result[0] as string)
functionArguments 顺序错误严格匹配 Move 参数顺序

参考资料

评论

支持 Markdown