← 返回
未分类 中文

Ts Sdk Types

Move to TypeScript type mapping in @aptos-labs/ts-sdk: u64/u128/u256 as bigint, address as string, TypeTag, functionArguments and typeArguments. Triggers on:...
在 @aptos-labs/ts-sdk 中迁移至 TypeScript 类型映射:u64/u128/u256 对应 bigint,address 对应 string,以及 TypeTag、functionArguments 和 typeArguments 的类型处理。触发条件:…
iskysun96 iskysun96 来源
未分类 clawhub v1.0.0 1 版本 99754.9 Key: 无需
★ 0
Stars
📥 407
下载
💾 0
安装
1
版本
#latest

概述

TypeScript SDK: Types (Move ↔ TypeScript)

Purpose

Guide type mapping between Move and TypeScript when using @aptos-labs/ts-sdk: numeric types (especially

u128/u256), address, TypeTag, and functionArguments / typeArguments.

ALWAYS

  1. Use bigint for u128 and u256 – in both view results and functionArguments; JavaScript number loses

precision above 2^53.

  1. Use string for address in payloads – e.g. "0x1" or accountAddress.toString(); SDK accepts

AccountAddressInput (string or AccountAddress).

  1. Use typeArguments for generic Move functions – e.g. coin type ["0x1::aptos_coin::AptosCoin"] for

coin::balance or coin::transfer.

  1. Cast view results explicitly when you know the Move return type – e.g. BigInt(result[0] as string) for u128.

NEVER

  1. Do not use number for u128/u256 – precision loss; use bigint.
  2. Do not pass raw number for large u64 in entry/view – use bigint if value can exceed Number.MAX_SAFE_INTEGER.
  3. Do not omit typeArguments when the Move function has type parameters (e.g. balance).

Move → TypeScript (summary)

Move typeTypeScript typeExample
-------------------------------------------------------------------------------------------------
u8, u16, u32number255, 65535
u64number \bigintPrefer bigint for large values
u128, u256bigintBigInt("340282366920938463463374607431768211455")
i8..i64 (Move 2.3+)number \bigintUse bigint for i64 when large
i128, i256bigintBigInt("-...")
boolbooleantrue
addressstring"0x1"
signerNot passed from TS; signer is the transaction sender
vectorUint8Array \string (hex)new Uint8Array([1,2,3]) or hex
vectorT[][1, 2, 3]
Stringstring"hello"
Objectstring (object address)objectAddress.toString()
OptionT \nullValue or null

functionArguments

Order and types must match the Move entry/view function parameters:

// 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

For generic Move functions, pass full type strings (address::module::StructName):

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

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

View return types

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 (advanced)

When building payloads programmatically or parsing type strings:

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");

Use typeArguments as string array in simple cases; use TypeTag when the SDK API expects it.


Object / resource address in arguments

Pass object address as string (LONG or SHORT per AIP-40):

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

Common mistakes

MistakeCorrect approach
-------------------------------------------------------------------------------------------
Passing number for u128 amountUse 1000000n or BigInt("...")
Omitting typeArguments for coin::balanceAdd typeArguments: ["0x1::aptos_coin::AptosCoin"]
Using result[0] as number for u128Use BigInt(result[0] as string)
Wrong order of functionArgumentsMatch Move parameter order exactly

References

ts-sdk-transactions, use-ts-sdk

版本历史

共 1 个版本

  • v1.0.0 当前
    2026-03-30 22:53 安全 安全

安全检测

腾讯云安全 (Keen)

安全,无风险
查看报告

腾讯云安全 (Sanbu)

安全,无风险
查看报告

🔗 相关推荐

dev-programming

Mcporter

steipete
使用 mcporter CLI 直接列出、配置、认证及调用 MCP 服务器/工具(支持 HTTP 或 stdio),涵盖临时服务器、配置编辑及 CLI/类型生成功能。
★ 195 📥 67,535
dev-programming

Github

steipete
使用 `gh` CLI 与 GitHub 交互,通过 `gh issue`、`gh pr`、`gh run` 和 `gh api` 管理议题、PR、CI 运行及高级查询。
★ 677 📥 326,789
it-ops-security

Security Audit

iskysun96
在部署前使用7类安全清单对Move合约进行安全漏洞审计。触发词:'审计合约'、'安全检查'、'安全审查'等。
★ 0 📥 556