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?

2 Answers 2

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

1
  • 1
    Compilers above ^0.4.21 needs emit keyword to call the event. Jul 4, 2019 at 13:25
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.