1

When you go to a deployment contract on Etherscan, which are 0x addresses just like wallets, they have a tab called "Events". What are Events and how come only these deployment contracts have Events, but not personal wallets? How does one cake Events into a smart contract?

2 Answers 2

2

Events are a way to inform the outside world that something has happened in the smart contract, that some action has taken place, etc. So others can subscribe to these events and act accordingly.

For example:

// SPDX-License-Identifier: GPL-3.0

pragma solidity ^0.8.17;

contract Event {

    event Deposit(address from, uint256 amount, uint256 timestamp);
    event Transfer(address from, address to, uint256 amount, uint256 timestamp);
    event Withdraw(address sender, uint256 amount, uint256 timestamp);

    mapping(address => uint256) public balances;

    function deposit() public payable {
        // deposit logic...

        emit Deposit(msg.sender, msg.value, block.timestamp);
    }

    function transfer(address to, uint256 amount) public payable {
        // transfer logic...

        emit Transfer(msg.sender, to, amount, block.timestamp);
    }

    function withdraw(uint256 amount) public payable {
        // withdraw logic...

        emit Withdraw(msg.sender, amount, block.timestamp);
    }

}

This is a simple toy code, short enough just to see how to declare and emit events. No actual logic is involved.

Notice how we can declare an event like:

event Deposit(address from, uint256 amount, uint256 timestamp);

And then we can emit that event in a function like:

emit Deposit(msg.sender, msg.value, block.timestamp);

Events are practically a way of "returning" data from a function that modifies the state in a smart contract. Because you cannot simply return data from a non-pure/non-view function and expect to get the result right away, because transactions execute asynchronously in the blockchain and we don't know when our function will actually run. Thus, we need to rely on events to get notified when our function is executed, to know what happened during execution, etc.

Regular address don't emit events because events are user-defined in smart contracts. You need to manually declare and emit events in a smart contract if you want to see them.

4
  • Can't an interloper engage the smart contract directly to execute the action and event called withdraw nefariously?
    – user610620
    Dec 12, 2022 at 23:43
  • All the functions should have logic to defend themselves against attackers. In this case, a withdraw function should only allowed authorized users to withdraw funds, or their own funds. I did not implement such a logic just to keep the example simple and event focused. Dec 12, 2022 at 23:45
  • understood, and well the above merely declares and logs withdraw events right? Events don't necessarily define actions (what can be done to a smart contract) ex ante, if not never?
    – user610620
    Dec 12, 2022 at 23:46
  • Events should be emitted when something important and useful happens in a smart contract. If you exchange tokens on a smart contract, an event should be emitted with the information of that exchange. If you bought an NFT, an event should be emitted with the details of that purchase. If you authorized another user to spend x amount of your ERC20 tokens, an event with that information should be emitted. This way your wallet or any other client can subscribe to these events and inform you right away of what happened, update a ui, etc. Dec 12, 2022 at 23:51
1

Events are offchain logs to allow third parties, API or bots to listen to a contract and know what's happening with this contract, or contract's storage.

It's good practice to emit an event when funds are moved or things like that.

For example, in the open zeppelin ERC20 smart contract, the function approve()

    function _approve(
        address owner,
        address spender,
        uint256 amount
    ) internal virtual {
        require(owner != address(0), "ERC20: approve from the zero address");
        require(spender != address(0), "ERC20: approve to the zero address");

        _allowances[owner][spender] = amount;
        emit Approval(owner, spender, amount);
    }

emits the 'Approval' event on the last line, with 3 arguments, the address of the owner of the funds, the address allowed to spend the owner's funds and the amount of erc20 the owners allows the spender to spend.

More from the open zepellin's contract:

Additionally, an {Approval} event is emitted on calls to {transferFrom}.
 * This allows applications to reconstruct the allowance for all accounts just
 * by listening to said events. Other implementations of the EIP may not emit
 * these events, as it isn't required by the specification.

Hope this helps!

1
  • Is the function approval always a Solidity event? What are some other functions or arguments that count as Events? Why don't personal wallets have Events. please add these answers to above.
    – user610620
    Dec 12, 2022 at 23:38

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.