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.
Best Answer
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 thesend()
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.
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
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.
Step 3: send txn to reward()
Be sure to NOT send wei/eth this time, because this function isn't payable.