$npx -y skills add aptos-labs/aptos-agent-skills --skill ts-sdk-transactionsHow to build, sign, submit, and simulate transactions in @aptos-labs/ts-sdk. Covers build.simple(), signAndSubmitTransaction(), waitForTransaction(), simulate, sponsored (fee payer), and multi-agent. Triggers on: 'build.simple', 'signAndSubmitTransaction', 'transaction.build', 'w
| 1 | # TypeScript SDK: Transactions |
| 2 | |
| 3 | ## Purpose |
| 4 | |
| 5 | Guide **building, signing, submitting, and simulating** transactions with `@aptos-labs/ts-sdk`. Use the build → sign → |
| 6 | submit → wait pattern; optionally simulate before submit. |
| 7 | |
| 8 | ## ALWAYS |
| 9 | |
| 10 | 1. **Call `aptos.waitForTransaction({ transactionHash })` after submit** – do not assume transaction is committed after |
| 11 | `signAndSubmitTransaction`. |
| 12 | 2. **Use `aptos.transaction.build.simple()` for entry function payloads** – pass `sender` and |
| 13 | `data: { function, functionArguments, typeArguments? }`. |
| 14 | 3. **Simulate before submit for critical/high-value flows** – use `aptos.transaction.simulate.simple()` and check |
| 15 | `success` and `vm_status`. |
| 16 | 4. **Use the same Account instance for signer** that you use for `sender` address when building (e.g. |
| 17 | `account.accountAddress` as sender, `account` as signer). |
| 18 | |
| 19 | ## NEVER |
| 20 | |
| 21 | 1. **Do not skip `waitForTransaction`** – submission returns when the tx is accepted, not when it is committed. |
| 22 | 2. **Do not use deprecated `scriptComposer`** (removed in v6) – use separate transactions or batch patterns. |
| 23 | 3. **Do not use `number` for u64/u128/u256 in `functionArguments`** – use `bigint` where required to avoid precision |
| 24 | loss. |
| 25 | |
| 26 | --- |
| 27 | |
| 28 | ## Standard flow (simple transaction) |
| 29 | |
| 30 | ```typescript |
| 31 | import { Aptos, AptosConfig, Network } from "@aptos-labs/ts-sdk"; |
| 32 | |
| 33 | const aptos = new Aptos(new AptosConfig({ network: Network.TESTNET })); |
| 34 | const MODULE_ADDRESS = "0x..."; |
| 35 | |
| 36 | // 1. Build |
| 37 | const transaction = await aptos.transaction.build.simple({ |
| 38 | sender: account.accountAddress, |
| 39 | data: { |
| 40 | function: `${MODULE_ADDRESS}::counter::increment`, |
| 41 | functionArguments: [] |
| 42 | } |
| 43 | }); |
| 44 | |
| 45 | // 2. Sign and submit |
| 46 | const pendingTx = await aptos.signAndSubmitTransaction({ |
| 47 | signer: account, |
| 48 | transaction |
| 49 | }); |
| 50 | |
| 51 | // 3. Wait for commitment |
| 52 | const committedTx = await aptos.waitForTransaction({ |
| 53 | transactionHash: pendingTx.hash |
| 54 | }); |
| 55 | |
| 56 | if (!committedTx.success) { |
| 57 | throw new Error(`Tx failed: ${committedTx.vm_status}`); |
| 58 | } |
| 59 | ``` |
| 60 | |
| 61 | --- |
| 62 | |
| 63 | ## Build options |
| 64 | |
| 65 | ```typescript |
| 66 | // With type arguments (e.g. coin type) |
| 67 | const transaction = await aptos.transaction.build.simple({ |
| 68 | sender: account.accountAddress, |
| 69 | data: { |
| 70 | function: "0x1::coin::transfer", |
| 71 | typeArguments: ["0x1::aptos_coin::AptosCoin"], |
| 72 | functionArguments: [recipientAddress, amount] |
| 73 | } |
| 74 | }); |
| 75 | |
| 76 | // Optional: max gas, expiry, etc. (see SDK types for full options) |
| 77 | const transactionWithOptions = await aptos.transaction.build.simple({ |
| 78 | sender: account.accountAddress, |
| 79 | data: { function: "...", functionArguments: [] }, |
| 80 | options: { |
| 81 | maxGasAmount: 2000n, |
| 82 | gasUnitPrice: 100n, |
| 83 | expireTimestamp: BigInt(Math.floor(Date.now() / 1000) + 600) |
| 84 | } |
| 85 | }); |
| 86 | ``` |
| 87 | |
| 88 | --- |
| 89 | |
| 90 | ## Simulation (before submit) |
| 91 | |
| 92 | ```typescript |
| 93 | const transaction = await aptos.transaction.build.simple({ |
| 94 | sender: account.accountAddress, |
| 95 | data: { |
| 96 | function: `${MODULE_ADDRESS}::counter::increment`, |
| 97 | functionArguments: [] |
| 98 | } |
| 99 | }); |
| 100 | |
| 101 | const [simResult] = await aptos.transaction.simulate.simple({ |
| 102 | signerPublicKey: account.publicKey, |
| 103 | transaction |
| 104 | }); |
| 105 | |
| 106 | if (!simResult.success) { |
| 107 | throw new Error(`Simulation failed: ${simResult.vm_status}`); |
| 108 | } |
| 109 | console.log("Gas used:", simResult.gas_used); |
| 110 | ``` |
| 111 | |
| 112 | --- |
| 113 | |
| 114 | ## Sponsored transactions (fee payer) |
| 115 | |
| 116 | ```typescript |
| 117 | // 1. Build with fee payer |
| 118 | const transaction = await aptos.transaction.build.simple({ |
| 119 | sender: sender.accountAddress, |
| 120 | withFeePayer: true, |
| 121 | data: { |
| 122 | function: `${MODULE_ADDRESS}::counter::increment`, |
| 123 | functionArguments: [] |
| 124 | } |
| 125 | }); |
| 126 | |
| 127 | // 2. Sender signs |
| 128 | const senderAuth = aptos.transaction.sign({ |
| 129 | signer: sender, |
| 130 | transaction |
| 131 | }); |
| 132 | |
| 133 | // 3. Fee payer signs (different method) |
| 134 | const feePayerAuth = aptos.transaction.signAsFeePayer({ |
| 135 | signer: feePayer, |
| 136 | transaction |
| 137 | }); |
| 138 | |
| 139 | // 4. Submit with both authenticators |
| 140 | const pendingTx = await aptos.transaction.submit.simple({ |
| 141 | transaction, |
| 142 | senderAuthenticator: senderAuth, |
| 143 | feePayerAuthenticator: feePayerAuth |
| 144 | }); |
| 145 | |
| 146 | await aptos.waitForTransaction({ transactionHash: pendingTx.hash }); |
| 147 | ``` |
| 148 | |
| 149 | --- |
| 150 | |
| 151 | ## Multi-agent transactions |
| 152 | |
| 153 | ```typescript |
| 154 | const transaction = await aptos.transaction.build.multiAgent({ |
| 155 | sender: alice.accountAddress, |
| 156 | secondarySignerAddresses: [bob.accountAddress], |
| 157 | data: { |
| 158 | function: `${MODULE_ADDRESS}::escrow::exchange`, |
| 159 | functionArguments: [itemAddress, amount] |
| 160 | } |
| 161 | }); |
| 162 | |
| 163 | const aliceAuth = aptos.transaction.sign({ signer: alice, transaction }); |
| 164 | const bobAuth = apt |