You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
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:
```sol
function getDutchAuctionStrike(
uint256 startingStrike,
uint32 auctionEndTimestamp,
uint256 reserveStrike
) public view returns (uint256 strike) {
/*
delta = max(auctionEnd - currentTimestamp, 0)
progress = delta / auctionDuration
auctionStrike = progress^2 * startingStrike
strike = max(auctionStrike, reserveStrike)
*/
uint256 delta = auctionEndTimestamp > block.timestamp ? auctionEndTimestamp - block.timestamp : 0;
uint256 progress = (1e18 * delta) / AUCTION_DURATION;
uint256 auctionStrike = (progress * progress * startingStrike) / (1e18 * 1e18);
Inline the
getPremium
function to save the gas spent of function callUse bitwise operations to check if a number is even or odd (
num & 1
instead ofnum % 2
)use logic not instead of
== false
(this can be done twice in thebuyOption
function and also in thewithdraw
function)save an SLOAD on lines 188-190 by applying this optimization
old:
vaultIndex += 2; vaultId = vaultIndex; _vaults[vaultId] = vault;
new:
vaultId = vaultIndex; vaultId += 2; vaultIndex = vaultId; _vaults[vaultId] = vault;
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 accessingvault.currentExpiration
again in thebuyOption
functionuint32 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)
old:
new:
The text was updated successfully, but these errors were encountered: