0

I just want to distribute a "eth" values to multiple public addresses. Is it possible with current version of "web3.js"? So that metamask will ask only a single time to approve a transaction.

Edit:-

initContract: function() {    
    $.getJSON('PayContract.json', function(data) {
    var payContract = data;
    App.contracts.PayContract = TruffleContract(payContract);    
    App.contracts.PayContract.setProvider(App.web3Provider);    
      });
  },



refundButtonOnClick : function (event) {
            console.log('refund button pressed');
            var mmPayToClients;
            var payees = [0x****BEf57,0x***a8BEf57,0x**7];
            var values = [0.5,1.0,0.5];
        App.contracts.PayContract.deployed().then(function(instance)
            {
                mmPayToClients = instance;
                return mmPayToClients.pay(payees,values);
            }).then(function(result) {
                console.log(result.message);

            }).catch(function(err) {
                console.log(err.message);
            });
        }

1 Answer 1

1

If you create and deploy a smart contract with a payable function, which takes an array of addresses and an array of values as arguments, and use this data to transfer the paid Ether to the relevant accounts.

An example of such a contract is as follows, but do not use this contract. I wrote it quickly to demonstrate the working principle, but your contract should include safety checks such as making sure the value paid equals the total of the values, and other checks to prevent things such as re-entrancy attacks.

pragma solidity ^0.4.21;

contract PayMultiple {
    function pay(address[] payees, uint[] values) public payable{
        for(uint i = 0; i < payees.length; i++){
            payees[i].transfer(values[i]);
        }
    }   
}

EDIT: In case you aren't familiar with Solidity, the version below should be safe to use. You will want to use the SafeMath library to prevent clever people from doing tricks with a fallback function overflowing the total counter. The contract below first iterates though all values and checks they are equal to the amount sent, then it iterates through again and sends it. This, combined with SafeMath should prevent Re-entrancy attacks. You can use this contract from any address, so once it's deployed it's completely re-usable.

pragma solidity ^0.4.21;

contract PayMultiple {
    using SafeMath for uint;

    function pay(address[] payees, uint[] values) public payable{
         //Make sure both arrays have matching lengths
         require(payees.length == values.length);

         //Make sure total of the values is the amount sent
        uint total = 0;
        for(uint i = 0; i < payees.length; i++){
            total = total.add(values[i]);
        }
        require(total == msg.value);

        //Actually send to the payees
        for(i = 0; i < payees.length; i++){
            payees[i].transfer(values[i]);
        }
    }
}
7
  • Thanks alot @AnAllergyToAnalogy, I'll try and let you know if it works. Commented Apr 16, 2018 at 8:37
  • @DavinderSingh i've updated the answer, the new contract should be fine to use Commented Apr 16, 2018 at 12:07
  • Hi @AnAllergyToAnalogy, actually i am very new to "ethereum", can you help me with this i am geeting exception i.e "base fee exceed at gas limit at rucall". here is my code snippet:- "var payees = [0x6******57,0x****Ef57,0x****57]; var values = [0.5,1.0,0.5]; App.contracts.MPayContract.deployed().then(function(instance) { mmPayContract = instance; return mmPayContract.pay(payees,values); }); Commented Apr 17, 2018 at 7:01
  • is above call is right to call solidity from JS? Commented Apr 17, 2018 at 7:11
  • could you update the question and include the complete js file? it's a bit hard to follow out of context. Commented Apr 17, 2018 at 7:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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