How to build, sign, submit, and simulate transactions in @aptos-labs/ts-sdk. Covers build.simple(),
技能名称: ts-sdk-transactions
详细描述:
指导使用 @aptos-labs/ts-sdk 进行交易的构建、签名、提交和模拟。遵循 构建 → 签名 → 提交 → 等待 模式;可选择在提交前进行模拟。
typescript
import { Aptos, AptosConfig, Network } from @aptos-labs/ts-sdk;
const aptos = new Aptos(new AptosConfig({ network: Network.TESTNET }));
const MODULE_ADDRESS = 0x...;
// 1. 构建
const transaction = await aptos.transaction.build.simple({
sender: account.accountAddress,
data: {
function: ${MODULE_ADDRESS}::counter::increment,
functionArguments: []
}
});
// 2. 签名并提交
const pendingTx = await aptos.signAndSubmitTransaction({
signer: account,
transaction
});
// 3. 等待提交
const committedTx = await aptos.waitForTransaction({
transactionHash: pendingTx.hash
});
if (!committedTx.success) {
throw new Error(交易失败: ${committedTx.vm_status});
}
typescript
// 带类型参数(例如代币类型)
const transaction = await aptos.transaction.build.simple({
sender: account.accountAddress,
data: {
function: 0x1::coin::transfer,
typeArguments: [0x1::aptos_coin::AptosCoin],
functionArguments: [recipientAddress, amount]
}
});
// 可选:最大 gas、过期时间等(完整选项见 SDK 类型)
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)
}
});
typescript
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(模拟失败: ${simResult.vm_status});
}
console.log(使用的 Gas:, simResult.gas_used);
typescript
// 1. 使用费用支付者构建
const transaction = await aptos.transaction.build.simple({
sender: sender.accountAddress,
withFeePayer: true,
data: {
function: ${MODULE_ADDRESS}::counter::increment,
functionArguments: []
}
});
// 2. 发送者签名
const senderAuth = aptos.transaction.sign({
signer: sender,
transaction
});
// 3. 费用支付者签名(不同方法)
const feePayerAuth = aptos.transaction.signAsFeePayer({
signer: feePayer,
transaction
});
// 4. 使用两个认证器提交
const pendingTx = await aptos.transaction.submit.simple({
transaction,
senderAuthenticator: senderAuth,
feePayerAuthenticator: feePayerAuth
});
await aptos.waitForTransaction({ transactionHash: pendingTx.hash });
typescript
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 });
typescript
const committed = await aptos.waitForTransaction({
transactionHash: pendingTx.hash,
options: {
timeoutSecs: 60,
checkSuccess: true // 如果交易失败则抛出异常
}
});
typescript
const gasProfile = await aptos.gasProfile({
sender: account.accountAddress,
data: {
function: ${MODULEADDRESS}::module::functionname,
functionArguments: []
}
});
console.log(Gas 分析:, gasProfile);
| 错误 | 正确方法 |
|---|---|
| 未调用 waitForTransaction | 始终等待并检查 committedTx.success |
| 对大额使用 number |
该技能支持在以下平台通过对话安装:
帮我安装 SkillHub 和 ts-sdk-transactions-1776168242 技能
设置 SkillHub 为我的优先技能安装源,然后帮我安装 ts-sdk-transactions-1776168242 技能
skillhub install ts-sdk-transactions-1776168242
文件大小: 2.81 KB | 发布时间: 2026-4-17 16:24