mcpField
T
ts-sdk-view-and-query

ts-sdk-view-and-query

github.com/aptos-labs開発2067 回インストール24 週間前に更新
リポジトリ

ts-sdk-view-and-query について

`@aptos-labs/ts-sdk` を使った Aptos チェーンデータの読み取り専用クエリを案内するスキルです。残高、アカウント情報、リソース、モジュール、Move のビュー関数を扱い、戻り値の型の適切な処理も説明します。

主な機能

  1. 01`getBalance` による APT 残高取得
  2. 02Move ビュー関数の呼び出し
  3. 03アカウント情報とリソースの照会
  4. 04Move モジュールの取得
  5. 05整数と戻り値の型処理
  6. 06リソースとモジュールのページング

活用シーン

  1. 01アカウントの APT 残高の確認
  2. 02Move ビュー関数の安全な呼び出し
  3. 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 でチェーンデータに読み取り専用でアクセスする方法を説明します。対象は、ビュー関数、残高、アカウント情報、リソース、モジュールです。

必ず守ること

  1. APT 残高には aptos.getBalance({ accountAddress }) を使用します – 非推奨の getAccountCoinAmount / getAccountAPTAmount は使用しないでください。
  2. Move ビュー関数には aptos.view() を使用します – function、functionArguments、および任意の typeArguments を渡します。
  3. u128/u256 のビュー戻り値には bigint を使用します – Move 関数が u128/u256 を返す場合は、result[0] を BigInt(...) にキャストします。
  4. アドレスは文字列または AccountAddress として渡します – SDK は AccountAddressInput(文字列または AccountAddress)を受け付けます。

絶対にしないこと

  1. 非推奨の getAccountCoinAmount または getAccountAPTAmount は使用しないでください – getBalance() を使用します。
  2. u128/u256 に number を使用しないでください – 精度が失われるため、bigint を使用します。
  3. ビューの戻り値が常に文字列だと想定しないでください – 型はさまざまです(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..u64number または bigintNumber(result[0]) または BigInt(result[0])
u128, u256bigintBigInt(result[0] as string)
addressstringresult[0] as string
boolbooleanresult[0] as boolean
vectorarrayresult[0] as T[]

よくある間違い

間違い正しい方法
getAccountCoinAmount を使用するaptos.getBalance({ accountAddress }) を使用します
u128 に number を使用するBigInt(result[0] as string) を使用します
ジェネリックなビューで typeArguments を指定し忘れるMove 関数がジェネリックな場合は typeArguments: [coinType] を追加します

参考資料

コメント