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

3 Answers 3

0

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

1
  • 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, 2019 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))
}
0

as far as i concern you are trying to fire multiple transaction called "seneth" which has functionality to give ether. but you must be careful when you're trying to firing multiple at once there is following exception such as: 1. a fired transaction should be waiting for 30 seconds or less to be completed receive by blockchain network. 2. you can only send another transaction after the current transaction is fully completed if it's not, the interface would recognize the latest transaction as replacement for advance transaction and throw an error "error replacement transaction is underpriced " 3. or it will throw an error caused by nonce too low. it occurs because previous transaction is not yet confirmed and there is already another transaction sent by the same address

Hopefully it helps you!

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.