0

I have created a contract which store the address of the users and i want to send 1 ether all the users stored in array.

Here is my code

contract project{

    address public user;
    bytes32[10] name;
    address[10] customer;
    uint public i=0;
    function project(){
        user=msg.sender;
    }



    function add(bytes32 _name){
        name[i]=_name;
        customer[i]=msg.sender;
        i++;
    }

    function get_address() constant returns(address[10]){
        return customer;
    }

    function reward() {
        for(uint i=0;i<10;i++)
        {
            customer[i].send(1);
        }

    }

}

When i call the method reward metamask executed the transaction but ether is not transferred.

Please help me.

1

You're trying to make the contract send funds it doesn't have. It needs ether to draw from. The balance would start at 0, so you need to add a function() payable{} and send some ether to work with.

I've marked a section of the code *** unsafe *** to avoid misunderstanding. There are deeper concerns about handling money this way. A good next step is to check the success/fail of the send() operation and then respond differently. I have it emitting a failure message or a success message.

I added some event emitters so you can see what's going on.

Hope it helps.

contract project{

  address public user;
  bytes32[10] name;
  address[10] customer;
  uint public i=0;

  event LogDep (address sender,    uint amount, uint balance);
  event LogSent(address recipient, uint amount, uint balance);
  event LogErr (address recipient, uint amount, uint balance);

  function project(){
    user=msg.sender;
  }

  function depositFunds() public payable returns(bool success) {
    LogDep(msg.sender, msg.value, this.balance); 
    return true;
  }

  function add(bytes32 _name){
    name[i]=_name;
    customer[i]=msg.sender;
    i++;
  }

  function get_address() constant returns(address[10]){
    return customer;
  }

  function reward() {
    for(uint i=0;i<10;i++)
    {

        // *** unsafe pattern ***

        if(customer[i].send(1)) {
            LogSent(customer[i], 1, this.balance);
        } else {
            LogErr(customer[i], 1, this.balance);
        }
    }
  }
}

Here it is in Remix to show it working.

If you haven't seen Remix before, go here https://ethereum.github.io/browser-solidity and just paste the code into the compiler. You can interact with the contract functions.

Step 1: Create the contract

enter image description here

Step 2: Send the contract some funds

10 wei is just enough to send 1 to all 10 customers. If you send less, you'll start seeing failed sends in Step 3.

enter image description here

enter image description here

Step 3: send txn to reward()

Be sure to NOT send wei/eth this time, because this function isn't payable.

enter image description here

  • It is showing LogErr when I call reward. Can you tell me how can I send ether from one address to another? – Anurag Verma Mar 30 '17 at 8:08
  • Added some screen caps to my original answer. Not sure you've used Remix before. It's worth it. – Rob Hitchens - B9lab Mar 30 '17 at 10:08
  • You have the options of voting or accepting (or both) if the answer is helpful. ;-) Thanks. – Rob Hitchens - B9lab Mar 30 '17 at 23:00
  • So what you saying that if we want to send ether to other account we have to send ether to the contract. And then use send function. – Anurag Verma Mar 31 '17 at 1:43
  • Yes. The contract can't spend any funds except its own. So, you can make a convincingly honest contract with an obvious function. Then, users can trust the contract to perform its function with their funds. It's not possible to make a contract that spends from another party's account. – Rob Hitchens - B9lab Mar 31 '17 at 1:57

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.