The following will provide a process for sending bulk transactions programmatically. The focus is on Ethereum-based blockchains, but the principles can be adapted for
other platforms with minor adjustments. Using tools like ethers.js, smart contracts, or batching servers, you can streamline the process of handling multiple transactions efficiently.
Prerequisites
- You have Node.js environment set up.
- You possess wallet credentials (private key or mnemonic for signing transactions).
- Access to a peaq RPC endpoints
- Basic familiarity with blockchain SDKs (e.g., ethers.js or web3.js)
Instructions
1. Install Required Dependencies
Install ethers.js to handle blockchain interactions programmatically.
2. Define Bulk Transactions
Create and structure your transactions in an array to represent the recipient addresses and amounts to send.
const { ethers } = require("ethers");
require("dotenv").config(); // For environment variables
const RPC_URL = process.env.PEAQ_RPC_URL || "https://peaq.api.onfinality.io/public"; // Replace with your preferred peaq RPC URL
const ETH_PRIVATE = process.env.ETH_PRIVATE;
// Setup provider and wallet
const provider = new ethers.JsonRpcProvider(RPC_URL);
const wallet = new ethers.Wallet(ETH_PRIVATE, provider);
const createTransactions = async () => {
// Define the bulk transactions
const transactions = [
{
to: "0xRecipientAddress1",
value: ethers.parseEther("0.1"),
},
{
to: "0xRecipientAddress2",
value: ethers.parseEther("0.2"),
},
{
to: "0xRecipientAddress3",
value: ethers.parseEther("0.3"),
},
];
}
3. Putting it all together
You can use the following JavaScript code to send batch transactions
const { ethers } = require("ethers");
require("dotenv").config(); // For environment variables
const RPC_URL = process.env.PEAQ_RPC_URL || "https://peaq.api.onfinality.io/public"; // Replace with your preferred peaq RPC URL
const ETH_PRIVATE = process.env.ETH_PRIVATE;
const createTransactions = async () => {
// Define the bulk transactions
const transactions = [
{
to: "0xRecipientAddress1",
value: ethers.parseEther("0.1"),
},
{
to: "0xRecipientAddress2",
value: ethers.parseEther("0.2"),
},
{
to: "0xRecipientAddress3",
value: ethers.parseEther("0.3"),
},
];
}
const sendBulkTransactions = async (txs) => {
// connect to provider
const provider = new ethers.JsonRpcProvider("https://peaq.api.onfinality.io/public");
const wallet = new ethers.Wallet(ETH_PRIVATE, provider);
// send transactions
for (let tx of txs) {
try {
const transaction = await wallet.sendTransaction(tx);
console.log(`Transaction sent: ${transaction.hash}`);
await transaction.wait(); // Wait for confirmation
console.log(`Transaction confirmed: ${transaction.hash}`);
} catch (error) {
console.error(`Failed to send transaction: ${error.message}`);
}
}
};
const main = async () => {
try {
const txs = createTransactions();
sendBulkTransactions(txs);
} catch (error) {
console.error("Error in event listener:", error);
process.exit(1);
}
};
main();
Tips
- Test with a small batch before sending large-scale transactions.
- Monitor gas prices and set appropriate limits to avoid failed transactions.
- Secure your private keys—never expose them in code or logs.
- Implement error handling to retry failed transactions where applicable.