Skip to main content

You're trying to make the contract send funds it doesn't have. It'sIt needs to Ethether to draw from. BalanceThe 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

You're trying to make the contract send funds it doesn't have. It's needs to Eth to draw from. 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

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

added 882 characters in body
Source Link
Rob Hitchens
  • 55.6k
  • 11
  • 91
  • 146

You're trying to make the contract send funds it doesn't have. It's needs to Eth to draw from. 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

You're trying to make the contract send funds it doesn't have. It's needs to Eth to draw from. 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);
        }
    }
  }
}

You're trying to make the contract send funds it doesn't have. It's needs to Eth to draw from. 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

Source Link
Rob Hitchens
  • 55.6k
  • 11
  • 91
  • 146

You're trying to make the contract send funds it doesn't have. It's needs to Eth to draw from. 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);
        }
    }
  }
}