Using events in upgradeable contracts?

jagdeep sidhu asked on Telegram

hey guys do events qualify as variables so when im using proxy and updating it, if im adding event and another field, should i put the event at the bottom of all fields or does it matter? ie field a, event a, event b, event c… if i add field b it would be below all events and event d as well?

As far as I am aware events don’t impact contract storage.

https://solidity.readthedocs.io/en/v0.6.3/contracts.html#events
Solidity events give an abstraction on top of the EVM’s logging functionality.

Box.sol

// contracts/Box.sol
pragma solidity ^0.5.0;

contract Box {
    uint256 private value;

    // Emitted when the stored value changes
    event ValueChanged(uint256 newValue);

    // Stores a new value in the contract
    function store(uint256 newValue) public {
        value = newValue;
        emit ValueChanged(newValue);
    }

    // Reads the last stored value
    function retrieve() public view returns (uint256) {
        return value;
    }
}

Deploy and interact

$ npx oz deploy
✓ Compiled contracts with solc 0.5.16 (commit.9c3226ce)
? Choose the kind of deployment upgradeable
? Pick a network development
? Pick a contract to deploy Box
✓ Added contract Box
✓ Contract Box deployed
All implementations have been deployed
? Call a function to initialize the instance after creating it? No
✓ Setting everything up to create contract instances
✓ Instance created at 0x0290FB167208Af455bB137780163b7B7a9a10C16
0x0290FB167208Af455bB137780163b7B7a9a10C16

$ npx oz send-tx
? Pick a network development
? Pick an instance Box at 0x0290FB167208Af455bB137780163b7B7a9a10C16
? Select which function store(newValue: uint256)
? newValue: uint256: 42
✓ Transaction successful. Transaction hash: 0x2af50ea3b69df983749f72af7917634aec13de79eebcffbfa03966410ef86685
Events emitted:
 - ValueChanged(42)

Box.sol (upgrade)

event added before state variable

// contracts/Box.sol
pragma solidity ^0.5.0;

contract Box {
    event OldValue(uint256 oldValue);

    uint256 private value;

    // Emitted when the stored value changes
    event ValueChanged(uint256 newValue);

    // Stores a new value in the contract
    function store(uint256 newValue) public {
        emit OldValue(value);
        value = newValue;
        emit ValueChanged(newValue);
    }

    // Reads the last stored value
    function retrieve() public view returns (uint256) {
        return value;
    }
}

Upgrade and interact

$ npx oz upgrade
? Pick a network development
✓ Compiled contracts with solc 0.5.16 (commit.9c3226ce)
✓ Contract Box deployed
All implementations have been deployed
? Which instances would you like to upgrade? Choose by address
? Pick an instance to upgrade Box at 0x0290FB167208Af455bB137780163b7B7a9a10C16
? Call a function on the instance after upgrading it? No
✓ Instance upgraded at 0x0290FB167208Af455bB137780163b7B7a9a10C16. Transaction receipt: 0xa5945b0dd6550f1bd6a3042531736573f2d7383395b30ece08ca1931196a1975
✓ Instance at 0x0290FB167208Af455bB137780163b7B7a9a10C16 upgraded

$ npx oz send-tx
? Pick a network development
? Pick an instance Box at 0x0290FB167208Af455bB137780163b7B7a9a10C16
? Select which function store(newValue: uint256)
? newValue: uint256: 23
✓ Transaction successful. Transaction hash: 0x3a77ad230c2fddee77f296783a4a98eac517285f99cd85f53b1c149c7aba2f7c
Events emitted:
 - OldValue(42)
 - ValueChanged(23)

Indeed events don’t affect storage and you can change the order across updates. Good question though!

1 Like