|
| 1 | +--- |
| 2 | +title: "Orderless Transactions" |
| 3 | +--- |
| 4 | + |
| 5 | +import { Callout } from 'nextra/components' |
| 6 | + |
| 7 | +# Orderless Transactions |
| 8 | + |
| 9 | +Orderless transactions allow you to create transactions that do not specify a |
| 10 | +order of execution between them. This is particularly useful |
| 11 | +in scenarios where multiple machines need to sign a transaction, but the order |
| 12 | +in which they sign does not affect the outcome of the transaction or matter to |
| 13 | +the creator. |
| 14 | + |
| 15 | +## Building Orderless Transactions |
| 16 | + |
| 17 | +Creating and executing a multi-agent transaction follows a similar flow to the |
| 18 | +[simple transaction flow](../building-transactions.mdx), and the |
| 19 | +[multi-agent transaction flow](./multi-agent-transactions.mdx). |
| 20 | + |
| 21 | +<Callout type="info"> |
| 22 | + Instead of providing a `sequenceNumber` (or no sequence number at all), a |
| 23 | + `Replay Protection Nonce` is used to ensure that the transaction is unique and |
| 24 | + cannot be replayed (i.e., executed multiple times with the same nonce). |
| 25 | +</Callout> |
| 26 | + |
| 27 | +For example, to create a single signer transaction that uses orderless transactions, |
| 28 | +specify the `nonce` in the `build.simple` method like so: |
| 29 | + |
| 30 | +```ts filename="build-a-transaction.ts" |
| 31 | +const transaction = await aptos.transaction.build.simple({ |
| 32 | + sender: sender.accountAddress, |
| 33 | + data: { |
| 34 | + // All transactions on Aptos are implemented via smart contracts. |
| 35 | + function: "0x1::aptos_account::transfer", |
| 36 | + functionArguments: [destination.accountAddress, 100], |
| 37 | + }, |
| 38 | + options: { |
| 39 | + replayProtectionNonce: 12345, // This is the nonce that will be used to ensure the transaction is unique. |
| 40 | + } |
| 41 | +}); |
| 42 | +``` |
| 43 | + |
| 44 | +Similarly, if you are building a multi-agent transaction, you can specify the |
| 45 | +`replayProtectionNonce` in the `build.multiAgent` method: |
| 46 | + |
| 47 | +```ts filename="build-a-transaction.ts" |
| 48 | +const transaction = await aptos.transaction.build.multiAgent({ |
| 49 | + sender: sender.accountAddress, |
| 50 | + secondarySignerAddresses: [bob.accountAddress], // List of secondary signers |
| 51 | + data: { |
| 52 | + // All transactions on Aptos are implemented via smart contracts. |
| 53 | + function: "0x1::aptos_account::transfer", |
| 54 | + functionArguments: [destination.accountAddress, 100], |
| 55 | + }, |
| 56 | + options: { |
| 57 | + replayProtectionNonce: 12345, // This is the nonce that will be used to ensure the transaction is unique. |
| 58 | + } |
| 59 | +}); |
| 60 | +``` |
| 61 | + |
| 62 | +And the same if you are building a sponsored transaction, you can specify the |
| 63 | +`replayProtectionNonce` in the `build.multiAgent` method: |
| 64 | +```ts filename="build-a-transaction.ts" |
| 65 | +const transaction = await aptos.transaction.build.multiAgent({ |
| 66 | + sender: sender.accountAddress, |
| 67 | + withFeePayer: true, // This indicates that the transaction will be sponsored. |
| 68 | + data: { |
| 69 | + // All transactions on Aptos are implemented via smart contracts. |
| 70 | + function: "0x1::aptos_account::transfer", |
| 71 | + functionArguments: [destination.accountAddress, 100], |
| 72 | + }, |
| 73 | + options: { |
| 74 | + replayProtectionNonce: 12345, // This is the nonce that will be used to ensure the transaction is unique. |
| 75 | + } |
| 76 | +}); |
| 77 | +``` |
| 78 | + |
| 79 | +<Callout type="info"> |
| 80 | + For orderless transactions, the `replayProtectionNonce` must be unique for |
| 81 | + each transaction. Additionally, the expiration time of the transaction is |
| 82 | + maximum 60 seconds from the time it is submitted. If the transaction is not |
| 83 | + executed within that time, it will be considered expired and will not be |
| 84 | + executed. |
| 85 | +</Callout> |
| 86 | + |
| 87 | +After that, simply follow the same steps as you would for a simple transaction: |
| 88 | +1. [**Simulate** the transaction (optional)](./simulating-transactions). |
| 89 | +2. **Sign** the transaction. |
| 90 | +3. **Submit** the transaction to the network. |
| 91 | +4. **Wait** for the transaction to be executed. |
| 92 | + |
| 93 | +### Examples |
| 94 | + |
| 95 | +* [TS SDK Example](https://github.com/aptos-labs/aptos-ts-sdk/blob/main/examples/typescript/simple_orderless_transfer.ts) |
0 commit comments