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)
1 Like
Indeed events donβt affect storage and you can change the order across updates. Good question though!
2 Likes