0

I've recently started with Solidity and like to know about the emit event as I wasn't sure of why it is being used in the program.

contract MyToken {

    string public name;
    string public symbol;
    uint8 public decimals;

    mapping (address => uint256) public balanceOf;

    event Transfer(address indexed from, address indexed to, uint256 value);

    /* Send coins */
    function transfer(address _to, uint256 _value) public {
        require(balanceOf[msg.sender] >= _value && balanceOf[_to] + _value >= balanceOf[_to]);
        balanceOf[msg.sender] -= _value;
        balanceOf[_to] += _value;
        emit Transfer(msg.sender, _to, _value);
    }   

   constructor(uint256 initialSupply, string memory tokenName, string memory tokenSymbol, uint8 decimalUnits) public {
        balanceOf[msg.sender] = initialSupply;              
        name = tokenName;                                   
        symbol = tokenSymbol;                               
        decimals = decimalUnits; 

    }

}
0

emit is used to emit any events. Events are the way to notify the transaction initiator about the actions performed by the called function. It stores its emitted parameters in a certain log history and they can accessed outside the contract with some filter parameters. As per the solidity documentation:

Events are inheritable members of contracts. When you call them, they cause the arguments to be stored in the transaction’s log - a special data structure in the blockchain. These logs are associated with the address of the contract, are incorporated into the blockchain, and stay there as long as a block is accessible.

For more: What is an Event?

  • Do you know where is the best place for the emit? Should it be stayed at the end of the function or after an operation has been triggered? – Ender Dec 3 '19 at 11:34
  • Depends on the event type. If event is something which signifies the completion of step, then its good to put it at the function end. OR if event is there for notifying a step completion, then it should be just after that operation – Aniket Dec 3 '19 at 13:14

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