Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add example for batch transactions using Aptos SDK #585

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions examples/javascript/batchTransactions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Import required modules and classes from the 'aptos' library
import { AptosClient, AptosAccount, FaucetClient, TransactionBuilderEd25519 } from 'aptos';

// Define the URL for the Aptos blockchain node (Devnet) and the Faucet service
const NODE_URL = 'https://fullnode.devnet.aptoslabs.com';
const FAUCET_URL = 'https://faucet.devnet.aptoslabs.com';

// Create an Aptos client to interact with the blockchain node
const client = new AptosClient(NODE_URL);

// Create a Faucet client to fund accounts using the Devnet faucet
const faucet = new FaucetClient(NODE_URL, FAUCET_URL);

// Create a new Aptos account (this generates a private/public key pair and a new address)
const account = new AptosAccount();

// Fund the new account using the faucet, adding 1,000,000 micro-coins (1 Aptos coin)
// Note: `account.address()` retrieves the address of the account
await faucet.fundAccount(account.address(), 1000000);

// Function to send a transaction to the Aptos blockchain
async function sendTransaction(transactionPayload) {
// Generate a raw transaction request for the account, using the provided payload
const txnRequest = await client.generateTransaction(account.address(), transactionPayload);

// Sign the transaction with the account's private key
const signedTxn = await client.signTransaction(account, txnRequest);

// Submit the signed transaction to the blockchain
const transactionRes = await client.submitTransaction(signedTxn);

// Wait for the transaction to be processed on the blockchain
await client.waitForTransaction(transactionRes.hash);

// Return the transaction hash (a unique identifier for the transaction)
return transactionRes.hash;
}

// Function to send a batch of transactions sequentially
async function sendBatchTransactions() {
// Create an array to store the transaction hashes for tracking purposes
const transactionHashes = [];

// Define the recipient's address (replace '0xRECEIVER_ADDRESS_HERE' with the actual address)
const receiverAddress = '0xRECEIVER_ADDRESS_HERE';

// Define the first transaction payload for transferring 10 micro-coins
const transferPayload1 = {
type: 'script_function_payload', // Type of transaction (script function)
function: '0x1::coin::transfer', // The function being called in the Aptos framework
arguments: [receiverAddress, 10], // Arguments: recipient address and amount to transfer
type_arguments: ['0x1::aptos_coin::AptosCoin'], // Coin type for the transaction
};

// Define the second transaction payload for transferring 20 micro-coins
const transferPayload2 = {
type: 'script_function_payload',
function: '0x1::coin::transfer',
arguments: [receiverAddress, 20],
type_arguments: ['0x1::aptos_coin::AptosCoin'],
};

// Combine the two transactions into an array for batch processing
const transactions = [transferPayload1, transferPayload2];

// Loop through each transaction payload, send the transaction, and track its hash
for (const txPayload of transactions) {
const txHash = await sendTransaction(txPayload); // Send the transaction and get its hash
transactionHashes.push(txHash); // Store the transaction hash in the array
console.log(`Transaction submitted with hash: ${txHash}`); // Log the transaction hash
}
Comment on lines +66 to +71
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you're looking to send multiple transactions at once, should probably do these in parallel and handle the sequence number appropriately.

Suggested change
// Loop through each transaction payload, send the transaction, and track its hash
for (const txPayload of transactions) {
const txHash = await sendTransaction(txPayload); // Send the transaction and get its hash
transactionHashes.push(txHash); // Store the transaction hash in the array
console.log(`Transaction submitted with hash: ${txHash}`); // Log the transaction hash
}
// Loop through each transaction payload, send the transaction, and track its hash
const promises = transactions.map((payload) => sendTransaction(payload)); // Handle errors here
const transactionHashes = Promise.all(promises);
console.log(`Transactions submitted with hashes: ${JSON.stringify(transactionHashes)}`); // Log the transaction hashes
}


// Log all transaction hashes once the batch is complete
console.log('Batch transactions complete:', transactionHashes);
}

// Call the batch transaction function and handle any errors
sendBatchTransactions().catch(console.error);
Loading