ts-sdk-transactions について
`@aptos-labs/ts-sdk` を使う TypeScript 開発者向けのスキルガイドです。Aptos 取引の構築、署名、送信、確定確認に加え、シミュレーションや注意点を説明します。
主な機能
- 01シンプルおよびマルチエージェント取引の構築
- 02送信前の取引シミュレーション
- 03対応するアカウントで署名・送信
- 04取引の確定待ち
- 05大きな引数に bigint を使用
活用シーン
- 01Aptos コントラクト呼び出しの実装
- 02高額取引の送信前チェック
- 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 のパターンを使用し、必要に応じて送信前にシミュレーションします。
必ず行うこと
- 送信後に
aptos.waitForTransaction({ transactionHash })を呼び出します –signAndSubmitTransactionの後、トランザクションがコミット済みだと想定しないでください。 - エントリ関数ペイロードには
aptos.transaction.build.simple()を使用します –senderとdata: { function, functionArguments, typeArguments? }を渡します。 - 重要なフローや高額なフローでは送信前にシミュレーションします –
aptos.transaction.simulate.simple()を使用し、successとvm_statusを確認します。 - 構築時に
senderアドレスとして使用するものと同じ Account インスタンスを署名者として使用します(例:account.accountAddressを sender として、accountを署名者として使用します)。
絶対に行わないこと
waitForTransactionを省略しないでください – 送信が返るのはトランザクションが受理された時点であり、コミットされた時点ではありません。- 非推奨の
scriptComposerを使用しないでください(v6 で削除)– 個別のトランザクションまたはバッチパターンを使用します。 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 で削除されました |
参考資料
- SDK:
src/api/transaction.ts、src/internal/transactionSubmission.ts、src/internal/transaction.ts - パターン:TYPESCRIPT_SDK.md
- 関連項目:ts-sdk-account、ts-sdk-client、 ts-sdk-wallet-adapter、use-ts-sdk
コメント
Markdown 対応