connext / connext/chaindata

Use Local Memory Type Variable Instead of Global Storage Type Variable in Event to Save Gas

Open
#59 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Solidity
Stars
166
Forks
175
PR merge metrics
No merged PRs in 30d

Description

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:

- [AnyswapV5ERC20.sol](https://github.com/connext/chaindata/blob/main/AnyswapV5ERC20.sol)
**function name:** *changeVault*
**event name:**  *LogChangeVault*
**variable:**    *pendingVault->newVault*
```Solidity
function changeVault(address newVault) external onlyVault returns (bool) {
require(newVault != address(0), "AnyswapV3ERC20: address(0x0)");
pendingVault = newVault;
delayVault = block.timestamp + delay;
emit LogChangeVault(vault, pendingVault, delayVault);
return true;
}
```
  **function name:** *changeMPCOwner*
  **event name:**  *LogChangeMPCOwner*
  **variable:**    *pendingVault->newVault*
```Solidity
function changeMPCOwner(address newVault) public onlyVault returns (bool) {
require(newVault != address(0), "AnyswapV3ERC20: address(0x0)");
pendingVault = newVault;
delayVault = block.timestamp + delay;
emit LogChangeMPCOwner(vault, pendingVault, delayVault);
return true;
}
```
- [AnyswapV4ERC20.sol](https://github.com/connext/chaindata/blob/main/AnyswapV4ERC20.sol)
**function name:** *changeVault*
**event name:**  *LogChangeVault*
**variable:**    *pendingVault->newVault*
```Solidity
function changeVault(address newVault) external onlyVault returns (bool) {
require(newVault != address(0), "AnyswapV3ERC20: address(0x0)");
pendingVault = newVault;
delayVault = block.timestamp + delay;
emit LogChangeVault(vault, pendingVault, delayVault);
return true;
}
```
  **function name:** *changeMPCOwner*
  **event name:**  *LogChangeMPCOwner*
  **variable:**    *pendingVault->newVault*
```Solidity
function changeMPCOwner(address newVault) public onlyVault returns (bool) {
require(newVault != address(0), "AnyswapV3ERC20: address(0x0)");
pendingVault = newVault;
delayVault = block.timestamp + delay;
emit LogChangeMPCOwner(vault, pendingVault, delayVault);
return true;
}
```

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!

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.