9

I have an error whenever I add the event line for EVM logging and to notify anyone listening that this transfer took place.

function sendCoin(address receiver, uint amount) returns (bool sufficient) {     //receiver 160 bit public address of receipient 
                                                                                     //amount # of coins to be sent

            if (balances[msg.sender] < amount || balances[receiver] + amount < balances[receiver]) throw;             //Check current balance of msg.sender is less than amount to send if yes, then dont execute 

            balances[msg.sender] -= amount;         

                 //If msg.sender is greater than amount then subtract the # of coins from sender account
        balances[receiver] += amount;                                           // and add the # of coins to receiver account
        return true;

    //event sendCoin(address indexed sender, address indexed receiver, uint256 amount); 
    //sendCoin(msg.sender, receiver, amount)

How do I fix this?

niksmac
  • 9,673
  • 2
  • 40
  • 72
Kizito
  • 779
  • 1
  • 8
  • 17

2 Answers2

14

Events are convenience interfaces with the EVM logging facilities.

Define an Event

event HighestBidIncreased(address bidder, uint amount); // Event

Triggering an Event

emit HighestBidIncreased(msg.sender, msg.value); // Triggering event using emit

You can avoid the params if you don't want them.

source

Shane Fontaine
  • 18,036
  • 20
  • 54
  • 82
niksmac
  • 9,673
  • 2
  • 40
  • 72
3

Here's an example that fixes the question:

contract C {
    event EventSendCoin(address indexed sender, address indexed receiver, uint256 amount);

    function sendCoin(address receiver, uint amount) returns (bool sufficient) {
        // ...
        EventSendCoin(msg.sender, receiver, amount);
        return true;
    }
}

Some things to note:

  • events are declared outside of the function
  • event names must be different from function names
  • event names are CapWords per Solidity styleguide
  • an event name that is more than 1 character/case different than a function name leads to less confusion
eth
  • 85,679
  • 53
  • 285
  • 406