ts-sdk-view-and-query について
`@aptos-labs/ts-sdk` を使った Aptos チェーンデータの読み取り専用クエリを案内するスキルです。残高、アカウント情報、リソース、モジュール、Move のビュー関数を扱い、戻り値の型の適切な処理も説明します。
主な機能
- 01`getBalance` による APT 残高取得
- 02Move ビュー関数の呼び出し
- 03アカウント情報とリソースの照会
- 04Move モジュールの取得
- 05整数と戻り値の型処理
- 06リソースとモジュールのページング
活用シーン
- 01アカウントの APT 残高の確認
- 02Move ビュー関数の安全な呼び出し
- 03アカウントのリソースやモジュールの確認
インストール
コマンドをコピーしてターミナルで実行します。
$
npx skills add https:/ / github.com/ aptos-labs/ aptos-agent-skills --skill ts-sdk-view-and-queryインストール後、Claude Code で/ts-sdk-view-and-queryを入力して読み込まれたか確認します。
詳細
TypeScript SDK:表示とクエリ
目的
@aptos-labs/ts-sdk でチェーンデータに読み取り専用でアクセスする方法を説明します。対象は、ビュー関数、残高、アカウント情報、リソース、モジュールです。
必ず守ること
- APT 残高には
aptos.getBalance({ accountAddress })を使用します – 非推奨のgetAccountCoinAmount/getAccountAPTAmountは使用しないでください。 - Move ビュー関数には
aptos.view()を使用します –function、functionArguments、および任意のtypeArgumentsを渡します。 - u128/u256 のビュー戻り値には
bigintを使用します – Move 関数が u128/u256 を返す場合は、result[0]をBigInt(...)にキャストします。 - アドレスは文字列または AccountAddress として渡します – SDK は
AccountAddressInput(文字列またはAccountAddress)を受け付けます。
絶対にしないこと
- 非推奨の
getAccountCoinAmountまたはgetAccountAPTAmountは使用しないでください –getBalance()を使用します。 - u128/u256 に
numberを使用しないでください – 精度が失われるため、bigintを使用します。 - ビューの戻り値が常に文字列だと想定しないでください – 型はさまざまです(number、bigint、string、boolean、array)。
getBalance(APT)
const balance = await aptos.getBalance({
accountAddress: account.accountAddress
});
// balance is bigint in octas (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`);
getAccountInfo
const accountInfo = await aptos.getAccountInfo({
accountAddress: "0x1"
});
// accountInfo: { sequence_number, authentication_key, ... }
view() – Move ビュー関数
// No type arguments
const result = await aptos.view({
payload: {
function: `${MODULE_ADDRESS}::counter::get_count`,
functionArguments: [accountAddress]
}
});
const count = Number(result[0]);
// With type arguments (e.g. coin type)
const balanceResult = await aptos.view({
payload: {
function: "0x1::coin::balance",
typeArguments: ["0x1::aptos_coin::AptosCoin"],
functionArguments: [accountAddress]
}
});
const coinBalance = BigInt(balanceResult[0] as string);
// Multiple return values
// 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
};
getAccountResources
const resources = await aptos.getAccountResources({
accountAddress: account.accountAddress
});
// resources: Array<MoveResource>
const counterResource = resources.find((r) => r.type === `${MODULE_ADDRESS}::counter::Counter`);
getAccountResource(単一の型)
const resource = await aptos.getAccountResource({
accountAddress: account.accountAddress,
resourceType: `${MODULE_ADDRESS}::counter::Counter`
});
// resource.data has the struct fields
const value = (resource?.data as { value: number })?.value;
getAccountModules
const modules = await aptos.getAccountModules({
accountAddress: modulePublisherAddress
});
// modules: MoveModuleBytecode[] (ABI, bytecode)
getModule(名前で単一のモジュールを取得)
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] を追加します |
参考資料
- SDK:
src/internal/view.ts、src/api/account.ts、internal の balance/getBalance - パターン:TYPESCRIPT_SDK.md
- 関連項目:ts-sdk-client、ts-sdk-types、 use-ts-sdk
コメント
Markdown 対応