-
Notifications
You must be signed in to change notification settings - Fork 52
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
Akk525
wants to merge
1
commit into
aptos-labs:main
Choose a base branch
from
Akk525:batch-transactions-example
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
||
// 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); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.