Use 1e36 instead of 1e18 * 1e18 in the getDutchAuctionStrike function (uint256 auctionStrike = (progress * progress * startingStrike) / (1e18 * 1e18);)
Optimize the getDutchAuctionStrike by splitting it into if statements
These three optimizations gives us these changes:
old code:
Put mapping(uint256 => address) private _vaultBeneficiaries; inside the vault struct (it won't change the size of the struct because of the 256 bits padding)
Use auctionStartTimestamp instead of accessing vault.currentExpiration again in the buyOption function
uint32 auctionStartTimestamp = vault.currentExpiration;
require(block.timestamp >= auctionStartTimestamp, "Auction not started");
// set new currentStrike
vault.currentStrike = getDutchAuctionStrike(
strikeOptions[vault.dutchAuctionStartingStrikeIndex],
vault.currentExpiration + AUCTION_DURATION,
vault.dutchAuctionReserveStrike
);
Shortening revert strings to fit in 32 bytes will decrease deployment time gas and will decrease runtime gas when the revert condition is met. Revert strings that are longer than 32 bytes require at least one additional mstore, along with additional overhead for computing memory offset, etc.
An even better and gas efficient approach will be to use Solidity Custom Errors instead of revert strings.
Cache feeRate to save an SLOAD + redundant initialization (variables in solidity are automatically initialized to their default value)
Gas Optimizations
getDutchAuctionStrike
(line 417) - it won't underflow because we know thatauctionEndTimestamp > block.timestamp
.1e36
instead of1e18 * 1e18
in thegetDutchAuctionStrike
function (uint256 auctionStrike = (progress * progress * startingStrike) / (1e18 * 1e18);
)getDutchAuctionStrike
by splitting it into if statementsThese three optimizations gives us these changes: old code:
getPremium
function to save the gas spent of function callnum & 1
instead ofnum % 2
)== false
(this can be done twice in thebuyOption
function and also in thewithdraw
function)old:
new:
mapping(uint256 => address) private _vaultBeneficiaries;
inside the vault struct (it won't change the size of the struct because of the 256 bits padding)Use
auctionStartTimestamp
instead of accessingvault.currentExpiration
again in thebuyOption
functionCache feeRate to save an SLOAD + redundant initialization (variables in solidity are automatically initialized to their default value)
old:
new: