unlock
unlock copied to clipboard
Use Local Memory Type Variable Instead of Global Storage Type Variable in Event to Save Gas
Hi, we recently have conducted a systematic study about Solidity event usage, evolution, and impact, and we are attempting to build a tool to improve the practice of Solidity event use based on our findings. We have tried our prototype tool on some of the most popular GitHub Solidity repositories, and for your repository, we find a potential optimization of gas consumption arisen from event use.
The point is that when we use emit operation to store the value of a certain variable, local memory type variable would be preferable to global storage type (state) variable if they hold the same value. The reason is that an extra SLOAD operation would be needed to access the variable if it is storage type, and the SLOAD operation costs 800 gas.
For your repository, we find that the following event use can be improved:
- PublicLockV4.sol function name: initialize event name: OwnershipTransferred variable: _owner->sender
function initialize(address sender) public initializer {
_owner = sender;
emit OwnershipTransferred(address(0), _owner);
}
function name: updateKeyPrice event name: PriceChanged variable: keyPrice->_keyPrice
function updateKeyPrice(
uint _keyPrice
)
external
onlyOwner
onlyIfAlive
{
uint oldKeyPrice = keyPrice;
keyPrice = _keyPrice;
emit PriceChanged(oldKeyPrice, keyPrice);
}
- PublicLockV0.sol function name: updateKeyPrice event name: PriceChanged variable: keyPrice->_keyPrice
function updateKeyPrice(
uint _keyPrice
)
external
onlyOwner
{
uint oldKeyPrice = keyPrice;
keyPrice = _keyPrice;
emit PriceChanged(oldKeyPrice, keyPrice);
}
- PublicLockV7.sol function name: updateKeyPricing event name: PricingChanged variable: keyPrice->_keyPrice
function updateKeyPricing(
uint _keyPrice,
address _tokenAddress
)
external
onlyLockManager
onlyIfAlive
{
uint oldKeyPrice = keyPrice;
address oldTokenAddress = tokenAddress;
require(
_tokenAddress == address(0) || IERC20(_tokenAddress).totalSupply() > 0,
'INVALID_TOKEN'
);
keyPrice = _keyPrice;
tokenAddress = _tokenAddress;
emit PricingChanged(oldKeyPrice, keyPrice, oldTokenAddress, tokenAddress);
}
-
PublicLockV8.sol function name: updateKeyPricing event name: PricingChanged variable: keyPrice->_keyPrice
-
PublicLockV3.sol function name: initialize event name: OwnershipTransferred variable: _owner->sender
function name: updateKeyPrice event name: PriceChanged variable: keyPrice->_keyPrice
- PublicLockV6.sol function name: initialize event name: OwnershipTransferred variable: _owner->sender
function name: updateKeyPricing event name: PricingChanged variable: keyPrice->_keyPrice
- PublicLockV1.sol function name: initialize event name: OwnershipTransferred variable: _owner->sender
function name: updateKeyPrice event name: PriceChanged variable: keyPrice->_keyPrice
-
UnlockV0.sol function name: updateKeyPrice event name: PriceChanged variable: keyPrice->_keyPrice
-
UnlockV6.sol function name: initialize event name: OwnershipTransferred variable: _owner->sender
-
UnlockV4.sol function name: initialize event name: OwnershipTransferred variable: _owner->sender
function name: updateKeyPrice event name: PriceChanged variable: keyPrice->_keyPrice
- UnlockV1.sol function name: initialize event name: OwnershipTransferred variable: _owner->sender
function name: updateKeyPrice event name: PriceChanged variable: keyPrice->_keyPrice
- UnlockV3.sol function name: initialize event name: OwnershipTransferred variable: _owner->sender
function name: updateKeyPrice event name: PriceChanged variable: keyPrice->_keyPrice
-
UnlockV9.sol function name: initialize event name: OwnershipTransferred variable: _owner->sender
-
UnlockV7.sol function name: initialize event name: OwnershipTransferred variable: _owner->sender
-
UnlockV8.sol function name: initialize event name: OwnershipTransferred variable: _owner->sender
-
MixinLockCore.sol function name: updateKeyPricing event name: PricingChanged variable: keyPrice->_keyPrice
Do you find our results useful? Your reply and invaluable suggestions would be greatly appreciated, and are vital for improving our tool. Thanks a lot for your time!