Setting uint256 variables to 0 is redundant as they default to 0.
[M-5] Avoid unnecessary storage reads can save gas
For the storage variables that will be accessed multiple times, cache and read from the stack can save ~100 gas from each extra read (SLOAD after Berlin).
In Cally.sol#createVault(), vaultIndex is read twice.
// check option associated with the vault has expired
uint32 auctionStartTimestamp = vault.currentExpiration;
require(block.timestamp >= auctionStartTimestamp, "Auction not started");
auctionStartTimestamp is unnecessary as it's being used only once.
Recommendation
Change to:
// check option associated with the vault has expired
require(block.timestamp >= vault.currentExpiration, "Auction not started");
[S]: Suggested optimation, save a decent amount of gas without compromising readability;
[M]: Minor optimation, the amount of gas saved is minor, change when you see fit;
[N]: Non-preferred, the amount of gas saved is at cost of readability, only apply when gas saving is a top priority.
[S-1]
Cally.sol#getPremium()
Implementation can be simpler and save some gashttps://github.com/code-423n4/2022-05-cally/blob/1849f9ee12434038aa80753266ce6a2f2b082c59/contracts/src/Cally.sol#L394-L397
vault
is redundant as it's only been used once;premiumIndex
is used, no need to SLOAD and copy the wholeVault
to memorypremium
Recommendation
Change to:
[M-2] Cache storage varibales can save gas
https://github.com/code-423n4/2022-05-cally/blob/1849f9ee12434038aa80753266ce6a2f2b082c59/contracts/src/Cally.sol#L90-L92
https://github.com/code-423n4/2022-05-cally/blob/1849f9ee12434038aa80753266ce6a2f2b082c59/contracts/src/Cally.sol#L167
https://github.com/code-423n4/2022-05-cally/blob/1849f9ee12434038aa80753266ce6a2f2b082c59/contracts/src/Cally.sol#L168-L169
strikeOptions
andpremiumOptions
can be cached to save gas from unnecessary SLOAD.[S-3]
Cally.sol#getDutchAuctionStrike()
Implementation can be simpler and save some gashttps://github.com/code-423n4/2022-05-cally/blob/1849f9ee12434038aa80753266ce6a2f2b082c59/contracts/src/Cally.sol#L417-L422
Recommendation
Change to:
When
delta == 0
, lots of arithmetic operations can be avoided.[M-4] Setting
uint256
variables to0
is redundanthttps://github.com/code-423n4/2022-05-cally/blob/1849f9ee12434038aa80753266ce6a2f2b082c59/contracts/src/Cally.sol#L94-L95
https://github.com/code-423n4/2022-05-cally/blob/1849f9ee12434038aa80753266ce6a2f2b082c59/contracts/src/Cally.sol#L282
Setting
uint256
variables to0
is redundant as they default to0
.[M-5] Avoid unnecessary storage reads can save gas
For the storage variables that will be accessed multiple times, cache and read from the stack can save ~100 gas from each extra read (
SLOAD
after Berlin).In
Cally.sol#createVault()
,vaultIndex
is read twice.https://github.com/code-423n4/2022-05-cally/blob/1849f9ee12434038aa80753266ce6a2f2b082c59/contracts/src/Cally.sol#L187-L190
Recommendation
Change to:
[M-6] Remove unnecessary variable can make the code simpler and save some gas
https://github.com/code-423n4/2022-05-cally/blob/1849f9ee12434038aa80753266ce6a2f2b082c59/contracts/src/Cally.sol#L226-L228
auctionStartTimestamp
is unnecessary as it's being used only once.Recommendation
Change to: