Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gas Optimizations #175

Open
code423n4 opened this issue May 14, 2022 · 0 comments
Open

Gas Optimizations #175

code423n4 opened this issue May 14, 2022 · 0 comments
Labels
bug Something isn't working G (Gas Optimization)

Comments

@code423n4
Copy link
Contributor

Custom Error Should Be Used For Gas-optimization

Permalinks

Description

Starting from Solidity v0.8.4, there is a convenient and gas-efficient way to explain to users why an operation failed through the use of custom errors. Until now, you could already use strings to give more information about failures (e.g., revert("Insufficient funds.");), but they are rather expensive, especially when it comes to deploy cost, and it is difficult to use dynamic information in them.
Source: https://blog.soliditylang.org/2021/04/21/custom-errors/

Mitigation

Consider using custom errors instead if the contract uses solidity version 0.8.4 or above.


Local Variable Used Only Once

Permalinks

https://github.com/code-423n4/2022-05-cally/blob/1849f9ee12434038aa80753266ce6a2f2b082c59/contracts/src/Cally.sol#L227-L228

Description

It is assumed that auctionStartTimestamp is a locally cached variable in order to reduce gas cost, however, it is only used once after being defined.

Mitigation

auctionStartTimestamp can be replaced at this line to save more gas.


Update Only Certain Variable in Struct Can Save More Gas

Permalinks

https://github.com/code-423n4/2022-05-cally/blob/1849f9ee12434038aa80753266ce6a2f2b082c59/contracts/src/Cally.sol#L241

Description

As the fact that EVM is a 256-bits stack machine, contract storage will be read and written in a 256 bits stack. All stacks will be read and written on utilizing for the struct variable that used multiple stacks (total size > 256 bits). Therefore, if a contract want to update certain variables which located in the same storage slot or less than the total slot, it is better to read/write only the particular variables rather than the entire struct.

Mitigation

The LoC 231-241 can be replaced as following:

// set new currentStrike
_vaults[vaultId].currentStrike = getDutchAuctionStrike(
    strikeOptions[vault.dutchAuctionStartingStrikeIndex],
    vault.currentExpiration + AUCTION_DURATION,
    vault.dutchAuctionReserveStrike
);

// set new expiration
_vaults[vaultId].currentExpiration = uint32(block.timestamp) + (vault.durationDays * 1 days);

The LoC 278-279 can be replaced as following:

_vaults[vaultId].isExercised = true;

The gas report test from Foundry:
│ Function Name ┆ min ┆ avg ┆ median ┆ max ┆ # calls │
│ buyOption ┆ 1788 ┆ 75271 ┆ 79045 ┆ 99845 ┆ 41 │
│ buyOption (optimized) ┆ 1788 ┆ 74517 ┆ 78186 ┆ 98986 ┆ 41 |
│ exercise ┆ 470 ┆ 50035 ┆ 64223 ┆ 87933 ┆ 18 │
│ exercise (optimized) ┆ 470 ┆ 49258 ┆ 63163 ┆ 86873 ┆ 18 │

@code423n4 code423n4 added bug Something isn't working G (Gas Optimization) labels May 14, 2022
code423n4 added a commit that referenced this issue May 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working G (Gas Optimization)
Projects
None yet
Development

No branches or pull requests

1 participant