0

Below are few lines from the contract in which I am basically trying to send some ether to an array of addresses. Now, when can run this code in remix solidity by replacing "value" to "msg.value". But I am trying to achieve this from backend.

pragma solidity ^0.4.16;

contract init {
    address owner;
    address[] public userAddresses;
    function init() {
        owner = msg.sender;
    }

    function getaddresses(address rec) public {
        userAddresses.push(rec);
    } 

    function seneth(uint value) public {
        for (uint i = 0; i < userAddresses.length; i++) {
            userAddresses[i].transfer(value);
        }
    }
}

Below is the code that I am trying

var txOptions = {
    nonce: web3.toHex(web3.eth.getTransactionCount(address)),
    gasLimit: web3.toHex(800000),
    gasPrice: web3.toHex(20000000000),
    to: contractAddress
}

var rawTx = txutils.functionTx(interface, 'seneth', [100000000000000], txOptions);
sendRaw(rawTx);

I am running the code in my console but I am getting the output below

attached is the output in etherscan

0

Why don't you pass the values on array instead of single address in transfer method. It will save the Gas Amount.

  • you mean changing the line from "userAddresses[i].transfer(value);" to "userAddresses.transfer(value);" and removing the for loop?? i have tried but didn't worked out. i have updated the question with the complete code – Suman Raj Jan 8 at 11:18
0

Just remove uint value from seneth function as below because your code says that you are transferring just uint numbers to all addresses instead of sending msg.value(ethers).

Changes in smartcontract :

   function seneth () public{
        for (uint i=0; i<userAddresses.length; i++) {

                 userAddresses[i].transfer(msg.value);

      }
    }

Changes in web3.js code just add value part:

var txOptions = {
    nonce: web3.toHex(web3.eth.getTransactionCount(address)),
    gasLimit: web3.toHex(800000),
    gasPrice: web3.toHex(20000000000),
    to: contractAddress
    value: web3.toHex(web3.toBigNumber(10000000000000000))
}

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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