mcpField
T
ts-sdk-transactions

ts-sdk-transactions

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

ts-sdk-transactions について

`@aptos-labs/ts-sdk` を使う TypeScript 開発者向けのスキルガイドです。Aptos 取引の構築、署名、送信、確定確認に加え、シミュレーションや注意点を説明します。

主な機能

  1. 01シンプルおよびマルチエージェント取引の構築
  2. 02送信前の取引シミュレーション
  3. 03対応するアカウントで署名・送信
  4. 04取引の確定待ち
  5. 05大きな引数に bigint を使用

活用シーン

  1. 01Aptos コントラクト呼び出しの実装
  2. 02高額取引の送信前チェック
  3. 03スポンサー付き・マルチエージェント取引の送信

インストール

コマンドをコピーしてターミナルで実行します。

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

インストール後、Claude Code で/ts-sdk-transactionsを入力して読み込まれたか確認します。

詳細

TypeScript SDK:トランザクション

目的

@aptos-labs/ts-sdk を使用して、トランザクションの構築、署名、送信、シミュレーションを行う方法を説明します。build → sign → submit → wait のパターンを使用し、必要に応じて送信前にシミュレーションします。

必ず行うこと

  1. 送信後に aptos.waitForTransaction({ transactionHash }) を呼び出します – signAndSubmitTransaction の後、トランザクションがコミット済みだと想定しないでください。
  2. エントリ関数ペイロードには aptos.transaction.build.simple() を使用します – sender と data: { function, functionArguments, typeArguments? } を渡します。
  3. 重要なフローや高額なフローでは送信前にシミュレーションします – aptos.transaction.simulate.simple() を使用し、 success と vm_status を確認します。
  4. 構築時に sender アドレスとして使用するものと同じ Account インスタンスを署名者として使用します(例: account.accountAddress を sender として、account を署名者として使用します)。

絶対に行わないこと

  1. waitForTransaction を省略しないでください – 送信が返るのはトランザクションが受理された時点であり、コミットされた時点ではありません。
  2. 非推奨の scriptComposer を使用しないでください(v6 で削除)– 個別のトランザクションまたはバッチパターンを使用します。
  3. functionArguments 内の u64/u128/u256 に number を使用しないでください – 精度の損失を防ぐため、必要に応じて bigint を使用します。

標準的なフロー(シンプルなトランザクション)

import { Aptos, AptosConfig, Network } from "@aptos-labs/ts-sdk";

const aptos = new Aptos(new AptosConfig({ network: Network.TESTNET }));
const MODULE_ADDRESS = "0x...";

// 1. Build
const transaction = await aptos.transaction.build.simple({
  sender: account.accountAddress,
  data: {
    function: `${MODULE_ADDRESS}::counter::increment`,
    functionArguments: []
  }
});

// 2. Sign and submit
const pendingTx = await aptos.signAndSubmitTransaction({
  signer: account,
  transaction
});

// 3. Wait for commitment
const committedTx = await aptos.waitForTransaction({
  transactionHash: pendingTx.hash
});

if (!committedTx.success) {
  throw new Error(`Tx failed: ${committedTx.vm_status}`);
}

構築オプション

// With type arguments (e.g. coin type)
const transaction = await aptos.transaction.build.simple({
  sender: account.accountAddress,
  data: {
    function: "0x1::coin::transfer",
    typeArguments: ["0x1::aptos_coin::AptosCoin"],
    functionArguments: [recipientAddress, amount]
  }
});

// Optional: max gas, expiry, etc. (see SDK types for full options)
const transactionWithOptions = await aptos.transaction.build.simple({
  sender: account.accountAddress,
  data: { function: "...", functionArguments: [] },
  options: {
    maxGasAmount: 2000n,
    gasUnitPrice: 100n,
    expireTimestamp: BigInt(Math.floor(Date.now() / 1000) + 600)
  }
});

シミュレーション(送信前)

const transaction = await aptos.transaction.build.simple({
  sender: account.accountAddress,
  data: {
    function: `${MODULE_ADDRESS}::counter::increment`,
    functionArguments: []
  }
});

const [simResult] = await aptos.transaction.simulate.simple({
  signerPublicKey: account.publicKey,
  transaction
});

if (!simResult.success) {
  throw new Error(`Simulation failed: ${simResult.vm_status}`);
}
console.log("Gas used:", simResult.gas_used);

スポンサー付きトランザクション(手数料支払者)

// 1. Build with fee payer
const transaction = await aptos.transaction.build.simple({
  sender: sender.accountAddress,
  withFeePayer: true,
  data: {
    function: `${MODULE_ADDRESS}::counter::increment`,
    functionArguments: []
  }
});

// 2. Sender signs
const senderAuth = aptos.transaction.sign({
  signer: sender,
  transaction
});

// 3. Fee payer signs (different method)
const feePayerAuth = aptos.transaction.signAsFeePayer({
  signer: feePayer,
  transaction
});

// 4. Submit with both authenticators
const pendingTx = await aptos.transaction.submit.simple({
  transaction,
  senderAuthenticator: senderAuth,
  feePayerAuthenticator: feePayerAuth
});

await aptos.waitForTransaction({ transactionHash: pendingTx.hash });

マルチエージェントトランザクション

const transaction = await aptos.transaction.build.multiAgent({
  sender: alice.accountAddress,
  secondarySignerAddresses: [bob.accountAddress],
  data: {
    function: `${MODULE_ADDRESS}::escrow::exchange`,
    functionArguments: [itemAddress, amount]
  }
});

const aliceAuth = aptos.transaction.sign({ signer: alice, transaction });
const bobAuth = aptos.transaction.sign({ signer: bob, transaction });

const pendingTx = await aptos.transaction.submit.multiAgent({
  transaction,
  senderAuthenticator: aliceAuth,
  additionalSignersAuthenticators: [bobAuth]
});

await aptos.waitForTransaction({ transactionHash: pendingTx.hash });

waitForTransaction のオプション

const committed = await aptos.waitForTransaction({
  transactionHash: pendingTx.hash,
  options: {
    timeoutSecs: 60,
    checkSuccess: true // throw if tx failed
  }
});

ガスのプロファイリング

const gasProfile = await aptos.gasProfile({
  sender: account.accountAddress,
  data: {
    function: `${MODULE_ADDRESS}::module::function_name`,
    functionArguments: []
  }
});
console.log("Gas profile:", gasProfile);

よくある間違い

間違い正しい方法
waitForTransaction を呼び出さない常に待機し、committedTx.success を確認します
大きな値に number を使用するfunctionArguments 内の u64/u128/u256 には bigint を使用します
送信時に署名者を間違える送信者(または適切な場合は手数料支払者/追加署名者)のアドレスを持つ Account を使用します
scriptComposer が存在すると想定する個別のトランザクションまたはバッチを使用します。scriptComposer は v6 で削除されました

参考資料

コメント