-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathbatchTransactions.js
More file actions
78 lines (60 loc) · 3.47 KB
/
batchTransactions.js
File metadata and controls
78 lines (60 loc) · 3.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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);