0

I'm currently doing a smart contracts short course and we got the code below but I've swapped out the event for emit. Now I am getting the following error:

ParseError: Expected ';' but got '(' emit sent(_from, _to, _value);^

Why would there be a semi-colon before the open bracket after sent? My code is as follows:

pragma solidity ^0.4.0;

contract Coin{

  address public minter;
  mapping (address => uint) public balances;

  emit sent(_from, _to, _value);

  constructor () public{
      minter = msg.sender;
  }

  function mint(address receiver, uint amount) public {
      if(msg.sender != minter) return;
      balances[receiver] += amount;
  }

  function send(address receiver, uint amount) public {
      if (balances[msg.sender] < amount) return;
      balances[msg.sender] -=amount;
      balances[receiver] +=amount;
      sent(msg.sender,receiver,amount);
  }
} 

Thanks so much, I really appreciate any help.

1

You are creating and calling the event the wrong way. Keyword event is used when you initialize the event and keyword emit is used when you're firing the event, look at this example:

pragma solidity ^0.4.0;

contract Coin {
    address public minter;
    mapping (address => uint) public balances;

    event sent(_from, _to, _value);

    constructor () public{
        minter = msg.sender;
    }

    function mint(address receiver, uint amount) public {
        if(msg.sender != minter) return;
        balances[receiver] += amount;
        
    }

    function send(address receiver, uint amount) public {   
        if (balances[msg.sender] < amount) return;
        balances[msg.sender] -=amount;
        balances[receiver] +=amount;
        emit sent(msg.sender,receiver,amount);
    }
}
1

You are emitting the event at the very beginning of your code, whereas you should be defining it by using the event keyword (that's why you get an error).

And then, you are just calling the event without the emit keyword. I think this is OK for old versions of Solidity (<0.4.21), like in your case, but all recent versions require the keyword emit when you want to store the event.

I suggest you use a higher version of Solidity (at least 0,6). In this case, there are two steps when it comes to events: definition and usage.

As per your example, you should first define the types you want to include in your event, and the indexed fields in case you want to search by some specific fields (you can use up to 3 indexes):

event Sent(address indexed _from, address indexed _to, uint256 _value);

Afterwards, you can emit the event in the corresponding function:

function send(address receiver, uint amount) public {
   //...
   emit Sent(msg.sender,receiver,amount);
}

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.