0

I am sending ether to multiple accounts and I want to send them without delay or waiting for confirmation of previous transaction.

const fs   = require("fs");
const os   = require("os");
const Web3 = require("web3");
const net  = require('net');

const NODE_ADDRESS = "IPC";
const SEND_ACCOUNT = "ACC";
const PRIVATE_KEY  = "PRI";
const INPUT_FILE   = "CSV";

async function run() {
    const web3      = new Web3(NODE_ADDRESS, net);
    let nonce_count = await web3.eth.getTransactionCount(SEND_ACCOUNT);

    const lines     = fs.readFileSync(INPUT_FILE, {encoding: "utf8"}).split(os.EOL);
    for (const line of lines) {
        const [id, address, balance] = line.split(",");
        const options = {
            to       : address,
            value    : balance,
            gasPrice : 1000000000,
            gas      : 21000,
            nonce    : nonce_count
        };
        const signed  = await web3.eth.accounts.signTransaction(options, PRIVATE_KEY);
        const receipt = await web3.eth.sendSignedTransaction(signed.rawTransaction);
        console.log("Address:",address,"Balance:",balance,"Nonce:",nonce_count);
        nonce_count++
    }
}

run();

Thanks in advance.

2
  • You mean "Here is my web3.js code"... – goodvibration Feb 15 '20 at 23:52
  • I mean not literally my code :joy: – Chance Han Feb 16 '20 at 0:04
1

You can do it like so:

const promises = [];
for (const line of lines) {
    ...
    promises.push(web3.eth.sendSignedTransaction(signed.rawTransaction));
    nonce_count++;
}
await Promise.all(promises);
0

Fixed code

const fs   = require("fs");
const os   = require("os");
const Web3 = require("web3");
const net  = require('net');

const NODE_ADDRESS = "IPC";
const SEND_ACCOUNT = "ACC";
const PRIVATE_KEY  = "PRI";
const INPUT_FILE   = "CSV";

async function run() {
    const web3      = new Web3(NODE_ADDRESS, net);
    let nonce_count = await web3.eth.getTransactionCount(SEND_ACCOUNT);

    const lines     = fs.readFileSync(INPUT_FILE, {encoding: "utf8"}).split(os.EOL);
    for (const line of lines) {
        const [id, address, balance] = line.split(",");
        const promises = [];
        const options = {
            to       : address,
            value    : balance,
            gasPrice : 1000000000,
            gas      : 21000,
            nonce    : nonce_count
        };
        const signed  = await web3.eth.accounts.signTransaction(options, PRIVATE_KEY);
        const txHash = await web3.utils.sha3(signed.rawTransaction);
        promises.push(web3.eth.sendSignedTransaction(signed.rawTransaction));
        console.log("Address:",address,"Balance:",balance,"Nonce:",nonce_count,"TX Hash:",txHash);
        nonce_count++
    }
    await Promise.all(promises);
}

run();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.