13

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.

rahul_eth
  • 556
  • 1
  • 4
  • 11

4 Answers4

13

Here is Dapp to make it easier for you: https://multisender.app

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

contract MultisenderApp {

function multisendEther(address[] calldata _contributors, uint256[] calldata _balances) external payable {
    uint256 total = msg.value;
    uint256 i = 0;
    for (i; i < _contributors.length; i++) {
        require(total >= _balances[i]);
        assert(total - _balances[i] > 0);
        total = total - _balances[i];
        (bool success, ) = _contributors[i].call.value(_balances[i])("");
        require(success, "Transfer failed.");
    }
}    

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

rstormsf
  • 4,337
  • 2
  • 25
  • 42
10

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.

Lucas Hendren
  • 241
  • 1
  • 8
  • i just want to send ether. why do i need to include contract abi? – rahul_eth May 08 '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 08 '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_eth May 08 '18 at 14:23
  • I added in an example for sending ether – Lucas Hendren May 08 '18 at 14:35
  • 1
    will try and reach you soon – rahul_eth May 08 '18 at 14:44
  • hi @Legmam everything working fine but still have to pay gas fee equals to number of transactions. what i want to do just make a single batch transaction with single fee (21000 gas)? – rahul_eth May 09 '18 at 06:06
  • I use batch sending at : https://rstormsf.github.io/multisender/#/

    I use recursive function to call sendTransaction and metamask generates it as stack of txs

    – rstormsf Sep 01 '18 at 23:18
  • Just to clarify, would it require less gas to batch together 100 transactions vs sending 100 transactions individually? And if so, approximately how much gas would be saved? Or in other words, approximately how much would it cost in gas to send 1 batched transaction with 100 transaction batched inside it? – MikeyE Oct 31 '21 at 17:59
2
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.

Zenos Pavlakou
  • 443
  • 3
  • 9
0

if you want batch send Ether or Token,you can use: https://algocryptodapp.github.io/Bee-Sender/

or send ether use solidity:

 for (uint i=0; i < address.length; i++) {
            address[i].transfer(amounts[i]);
        }
}
xexchange
  • 43
  • 4