3

Is it possible sending ether to multiple addresses in one go, better called batch sending? I want to make an API to transfer ether to 100 address at once so that I do not need to make 100 transactions.

3

This can be kinda of delicate at times, but web3 does support batch requests https://github.com/ethereum/wiki/wiki/JavaScript-API#batch-requests Here is their example on their site. I can include an example for sending ether if this is not clear

var batch = web3.createBatch();
batch.add(web3.eth.getBalance.request('0x0000000000000000000000000000000000000000', 'latest', callback));
batch.add(web3.eth.Contract(abi).at(address).balance.request(address, callback2));
batch.execute();

The Specific example would be something akin to eth.sendTransaction({from:sender, to:receiver, value: amount})

var batch = web3.createBatch();
batch.add(web3.eth.sendTransaction({from:sender, to:receiver, value: amount}));
batch.add(web3.eth.sendTransaction({from:sender, to:receiver2, value: amount2}));
batch.execute();

Just an FYI Batching requests do not make them faster! Making many requests at once will sometimes be faster, as requests are processed asynchronously. Batch requests are mainly used to ensure events happen in serial.

  • i just want to send ether. why do i need to include contract abi? – rahul saini May 8 '18 at 14:19
  • This was the example on their site for generic batch requests which didn't show sending but would be very similar, I can edit it for specifically sending ether if that will help. – Lucas Hendren May 8 '18 at 14:20
  • I want to make 10 ether transaction to 10 different addresses in the single transaction fee. does it possible through batch transaction? – rahul saini May 8 '18 at 14:23
  • I added in an example for sending ether – Lucas Hendren May 8 '18 at 14:35
  • 1
    will try and reach you soon – rahul saini May 8 '18 at 14:44
3
function sendBatch(address[] _addrs) public payable {
    for(uint i = 0; i < _addrs.length; i++) {
        _addrs[i].transfer(msg.value.div(_addrs.length));
    }
}

This simple solidity function would do exactly what you're asking about. You would just need to provide an array of addresses when executing the function and it will split the ETH you send to all those addresses evenly.

2

Here is Dapp to make it easier for you: https://rstormsf.github.io/multisender/#/

If you want to do it yourself, here is some code for you

function multisendEther(address[] _contributors, uint256[] _balances) public payable {
    uint256 total = msg.value;
    require(_contributors.length <= 150); // limit to 150 addresses per 1 tx to make sure tx will be mined
    uint256 i = 0;
    for (i; i < _contributors.length; i++) {
        require(total >= _balances[i]);
        total = total.sub(_balances[i]);
        _contributors[i].transfer(_balances[i]);
    }
}

this way, you can be sure that whoever interacts with this function, won't take more eth than he/she should.

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.