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

Conversation

Akk525
Copy link

@Akk525 Akk525 commented Nov 19, 2024

Description

This pull request adds a new example, batchTransactions.js, to the examples/javascript directory. The example demonstrates how to perform batch transactions using the Aptos SDK, showcasing the process of sending multiple transactions sequentially in a script. This example is designed to help developers optimize workflows that involve multiple on-chain operations.

Test Plan

  1. Run the script using the following command:
   node examples/javascript/batchTransactions.js
   
2. Verify the following:
   - The script successfully submits multiple transactions to the Aptos Devnet.
   - Transaction hashes are displayed in the console.
   - Each transaction hash can be validated on the [Aptos Devnet Explorer](https://explorer.devnet.aptos.dev/) by searching     for the transaction hash.

### Related Links
No related issues or pull requests.

### Checklist
- [x] Have you ran `pnpm fmt`?  
  **Yes**, the code has been formatted using `pnpm fmt`.
- [ ] Have you updated the `CHANGELOG.md`?  
  **No**, as this is an addition to the examples directory and does not modify core functionality.

@Akk525 Akk525 requested a review from a team as a code owner November 19, 2024 21:26
Copy link
Collaborator

@gregnazario gregnazario left a comment

Choose a reason for hiding this comment

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

We generally have avoided the JavaScript examples, just because we already are building them in TypeScript, which tends to have better practices / showing how to manage types (where JavaScript you can omit them).

I'm curious have you seen this example in TypeScript?

It uses a transaction worker thread to manage the batch send, which might not be exactly what you want.

I'd love feedback as well if JavaScript examples are important rather than just TypeScript.

cc @0xmaayan

Comment on lines +66 to +71
// 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
}
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
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants