TS SDK Transaction Builder
The SDK provides a simplified and meaningful transaction builder flow to handles the transaction creation lifecycle.
The transaction builder is separated to different namespaces for each transaction step in the transaction submission flow. Each namespace/step can be accessed by initiating the Aptos class
- build - Build a raw transaction that can be signed and then submitted to chain
- simulate - Simulate a transaction before signing and submitting to chain
- sign - Sign a raw transaction to later submit to chain
- submit - Submit a transaction to chain
Each step provides supports to all the different transaction types Aptos supports -
- simple transaction - Single signer
- complex transaction - Sponsor and multi agent
Submit transaction
Simple transaction
// build a transaction
const transaction = await aptos.transaction.build.simple({
sender: alice.accountAddress,
data: {
function: "0x1::coin::transfer",
typeArguments: ["0x1::aptos_coin::AptosCoin"],
functionArguments: [bobAddress, 100],
},
});
// using sign and submit separately
const senderAuthenticator = aptos.transaction.sign({
signer: alice,
transaction,
});
const committedTransaction = await aptos.transaction.submit.simple({
transaction,
senderAuthenticator,
});
// using signAndSubmit combined
const committedTransaction = await aptos.signAndSubmitTransaction({
signer: alice,
transaction,
});
Complex transaction - Multi agent
// build a transaction
const transaction = await aptos.transaction.build.multiAgent({
sender: alice.accountAddress,
secondarySignerAddresses: [secondarySignerAccount.accountAddress],
data: {
function: "0x1::coin::transfer",
typeArguments: ["0x1::aptos_coin::AptosCoin"],
functionArguments: [bobAddress, 100],
},
});
// sign transaction
const senderAuthenticator = aptos.transaction.sign({
signer: alice,
transaction,
});
const secondarySignerAuthenticator = aptos.transaction.sign({
signer: secondarySignerAccount,
transaction,
});
// submit transaction
const committedTransaction = await aptos.transaction.submit.multiAgent({
transaction,
senderAuthenticator,
additionalSignersAuthenticators: [secondarySignerAuthenticator],
});
Complex transaction - Simple transaction with Sponsor transaction
// build a transaction
const transaction = await aptos.transaction.build.simple({
sender: alice.accountAddress,
withFeePayer: true,
data: {
function: "0x1::coin::transfer",
typeArguments: ["0x1::aptos_coin::AptosCoin"],
functionArguments: [bobAddress, 100],
},
});
// sign transaction
const senderAuthenticator = aptos.transaction.sign({
signer: alice,
transaction,
});
const feePayerSignerAuthenticator = aptos.transaction.signAsFeePayer({
signer: feePayerAccount,
transaction,
});
// submit transaction
const committedTransaction = await aptos.transaction.submit.simple({
transaction,
senderAuthenticator,
feePayerAuthenticator: feePayerSignerAuthenticator,
});