如何在 @aptos-labs/ts-sdk 中读取链上数据:view()、getBalance()、getAccountInfo()、getAccountResources()、getAccountModules()、getResource() 等方法的使用。触发...
TypeScript SDK: View and Query. uspose. 指南只读访问在 @aptos-labs/ts-sdk中的链条数据:视图函数,平衡是一项面向实际任务的技能,主要用于AlWAYS.;Use aptos.getBalance({ ac>).。
实际使用前应先确认任务范围、数据来源、运行环境、必要权限和关键参数,再依据技能说明逐步执行;若输入条件不完整,应先补齐信息或采用保守配置,避免因错误假设导致结果偏离需求。执行过程中需要关注工具调用是否成功、接口或依赖是否可用、输出格式是否符合预期,并对异常提示、缺失字段和边界情况进行处理;
涉及批量任务时,还应保存进度,避免中断后重复操作。该技能适合用于一次性任务,也可以接入自动化工作流,与其他技能或上层代理配合完成更完整的业务链路;在组合使用时,应明确每一步的输入输出关系,并避免不同步骤之间出现参数冲突。
指导如何在 @aptos-labs/ts-sdk 中进行只读链上数据访问:调用 Move 视图函数、查询 APT 余额、账户信息、资源(resources)和模块(modules)。
aptos.getBalance({ accountAddress }) 查询 APT 余额 —— 不要使用已弃用的 getAccountCoinAmount / getAccountAPTAmount。aptos.view() 调用 Move 视图函数 —— 传入 function、functionArguments,以及可选的 typeArguments。bigint 类型接收结果 —— 当 Move 函数返回 u128/u256 时,请将 result[0] 显式转换为 BigInt(...)。AccountAddress 实例 —— SDK 接受 AccountAddressInput 类型(即字符串或 AccountAddress 对象)。getAccountCoinAmount 或 getAccountAPTAmount —— 请统一使用 getBalance()。number 类型 —— 将导致精度丢失;必须使用 bigint。number、bigint、string、boolean、array 等)。const balance = await aptos.getBalance({
accountAddress: account.accountAddress
});
// balance 是以 octas 为单位的 bigint(1 APT = 100_000_000 octas)
const apt = balance / 100_000_000n;
const remainder = balance % 100_000_000n;
console.log(`${apt}.${remainder.toString().padStart(8, "0")} APT`);
const accountInfo = await aptos.getAccountInfo({
accountAddress: "0x1"
});
// accountInfo: { sequence_number, authentication_key, ... }
// 无类型参数(type arguments)
const result = await aptos.view({
payload: {
function: `${MODULE_ADDRESS}::counter::get_count`,
functionArguments: [accountAddress]
}
});
const count = Number(result[0]);
// 带类型参数(例如代币类型)
const balanceResult = await aptos.view({
payload: {
function: "0x1::coin::balance",
typeArguments: ["0x1::aptos_coin::AptosCoin"],
functionArguments: [accountAddress]
}
});
const coinBalance = BigInt(balanceResult[0] as string);
// 多返回值示例
// Move 函数定义:public fun get_listing(addr): (address, u64, bool)
const [seller, price, isActive] = await aptos.view({
payload: {
function: `${MODULE_ADDRESS}::marketplace::get_listing`,
functionArguments: [listingAddress]
}
});
const listing = {
seller: seller as string,
price: BigInt(price as string),
isActive: isActive as boolean
};
const resources = await aptos.getAccountResources({
accountAddress: account.accountAddress
});
// resources: Array
const counterResource = resources.find((r) => r.type === `${MODULE_ADDRESS}::counter::Counter`);
const resource = await aptos.getAccountResource({
accountAddress: account.accountAddress,
resourceType: `${MODULE_ADDRESS}::counter::Counter`
});
// resource.data 包含结构体字段
const value = (resource?.data as { value: number })?.value;
const modules = await aptos.getAccountModules({
accountAddress: modulePublisherAddress
});
// modules: MoveModuleBytecode[](含 ABI 和字节码)
const module = await aptos.getModule({
accountAddress: modulePublisherAddress,
moduleName: "counter"
});
当接口支持时,请使用基于游标的分页选项:
const { resources, cursor } = await aptos.getAccountResourcesPage({
accountAddress: account.accountAddress,
options: { limit: 10, cursor: nextCursor }
});
| Move 返回类型 | TypeScript 类型 | 示例 |
|---|---|---|
| u8..u64 | number 或 bigint |
Number(result[0]) 或 BigInt(result[0]) |
| u128、u256 | bigint |
BigInt(result[0] as string) |
| address | string |
result[0] as string |
| bool | boolean |
result[0] as boolean |
| vector |
array |
result[0] as T[] |
| 错误做法 | 正确做法 |
|---|---|
使用 getAccountCoinAmount |
使用 aptos.getBalance({ accountAddress }) |
对 u128 使用 number |
使用 BigInt(result[0] as string) |
调用泛型视图函数时遗漏 typeArguments |
当 Move 函数为泛型时,需显式传入 typeArguments: [coinType] |
src/internal/view.ts、src/api/account.ts,以及内部实现中的 balance/getBalanceTYPESCRIPT_SDK.mdts-sdk-client、ts-sdk-types、use-ts-sdk相关专题
热门下载
相关下载
精品课程
共6课时 | 54.7万人学习
共89课时 | 134.1万人学习
共49课时 | 82.6万人学习