跳转到内容

无序交易

无序交易让你创建彼此之间不指定执行顺序的交易。这在多台机器需要签署交易、但签署顺序不影响交易结果或创建者的场景中特别有用。

创建和执行多代理交易的流程与简单交易流程多代理交易流程类似。

例如,要创建使用无序交易的单签名者交易,可在 build.simple 方法中按如下方式指定 nonce

const transaction = await aptos.transaction.build.simple({
sender: sender.accountAddress,
data: {
// All transactions on Aptos are implemented via smart contracts.
function: "0x1::aptos_account::transfer",
functionArguments: [destination.accountAddress, 100],
},
options: {
replayProtectionNonce: 12345n, // This is the nonce that will be used to ensure the transaction is unique.
}
});

类似地,如果构建多代理交易,可在 build.multiAgent 方法中指定 replayProtectionNonce

const transaction = await aptos.transaction.build.multiAgent({
sender: sender.accountAddress,
secondarySignerAddresses: [bob.accountAddress], // List of secondary signers
data: {
// All transactions on Aptos are implemented via smart contracts.
function: "0x1::aptos_account::transfer",
functionArguments: [destination.accountAddress, 100],
},
options: {
replayProtectionNonce: 12345n, // This is the nonce that will be used to ensure the transaction is unique.
}
});

构建赞助交易时也是如此;可在 build.multiAgent 方法中指定 replayProtectionNonce

const transaction = await aptos.transaction.build.multiAgent({
sender: sender.accountAddress,
withFeePayer: true, // This indicates that the transaction will be sponsored.
data: {
// All transactions on Aptos are implemented via smart contracts.
function: "0x1::aptos_account::transfer",
functionArguments: [destination.accountAddress, 100],
},
options: {
replayProtectionNonce: 12345n, // This is the nonce that will be used to ensure the transaction is unique.
}
});

之后只需遵循与简单交易相同的步骤:

  1. 模拟交易(可选)
  2. 签署交易。
  3. 交易提交到网络。
  4. 等待交易执行。