Relevant GitHub Links
https://github.com/Cyfrin/2024-07-templegold/blob/main/protocol/contracts/templegold/DaiGoldAuction.sol#L118
https://github.com/Cyfrin/2024-07-templegold/blob/main/protocol/contracts/templegold/DaiGoldAuction.sol#L246
https://github.com/Cyfrin/2024-07-templegold/blob/main/protocol/contracts/templegold/SpiceAuction.sol#L189
https://github.com/Cyfrin/2024-07-templegold/blob/main/protocol/contracts/templegold/SpiceAuction.sol#L328
https://github.com/Cyfrin/2024-07-templegold/blob/main/protocol/contracts/templegold/SpiceAuction.sol#L47
https://github.com/Cyfrin/2024-07-templegold/blob/main/protocol/contracts/templegold/DaiGoldAuction.sol#L141
https://github.com/Cyfrin/2024-07-templegold/blob/main/protocol/contracts/templegold/TempleGoldStaking.sol#L344
Summary
The protocol can reduce gas consumption and improve efficiency by:
Pre-checking conditions in if statements.
Removing unnecessary variables.
Optimizing storage layout.
Vulnerability Details
Please refer to the Recommendations section for detailed suggestions.
Impact
Reduce gas costs and imporve protocol efficiency.
Tools Used
Manual review.
Recommendations
L118
uint256 totalGoldAmount = nextAuctionGoldAmount;
+ if (totalGoldAmount < config.auctionMinimumDistributedGold) { revert LowGoldDistributed(totalGoldAmount); }
nextAuctionGoldAmount = 0;
uint256 epochId = _currentEpochId = _currentEpochId + 1;
- if (totalGoldAmount < config.auctionMinimumDistributedGold) { revert LowGoldDistributed(totalGoldAmount); }
L246
function getClaimableAtEpoch(address depositor, uint256 epochId) public override view returns (uint256) {
+ if (epochId > _currentEpochId) {return 0;}
uint256 bidTokenAmount = depositors[depositor][epochId];
- if (bidTokenAmount == 0 || epochId > _currentEpochId) { return 0; }
+ if (bidTokenAmount == 0) {return 0;}
EpochInfo memory info = epochs[epochId];
return bidTokenAmount.mulDivRound(info.totalAuctionTokenAmount, info.totalBidTokenAmount, false);
}
L189
function bid(uint256 amount) external virtual override {
+ if (amount == 0) { revert CommonEventsAndErrors.ExpectedNonZero(); }
/// @dev Cache, gas savings
uint256 epochId = _currentEpochId;
EpochInfo storage info = epochs[epochId];
if(!info.isActive()) { revert CannotDeposit(); }
- if (amount == 0) { revert CommonEventsAndErrors.ExpectedNonZero(); }
L328
function getClaimableForEpoch(address depositor, uint256 epochId) external override view returns (uint256) {
+ if (epochId > _currentEpochId) {return 0;}
uint256 bidTokenAmount = depositors[depositor][epochId];
- if (bidTokenAmount == 0 || epochId > _currentEpochId) { return 0; }
+ if(bidTokenAmount == 0) {return 0;}
EpochInfo memory info = epochs[epochId];
return bidTokenAmount.mulDivRound(info.totalAuctionTokenAmount, info.totalBidTokenAmount, false);
}
L344
function stakeBalanceOf(address account, uint256 stakeIndex) external override view returns (uint256) {
- StakeInfo storage stakeInfo = _stakeInfos[account][stakeIndex];
- return stakeInfo.amount;
+ return _stakeInfos[account][stakeIndex].amount;
}
L141
function bid(uint256 amount) external virtual override onlyWhenLive {
if (amount == 0) { revert CommonEventsAndErrors.ExpectedNonZero(); }
bidToken.safeTransferFrom(msg.sender, treasury, amount);
uint256 epochIdCache = _currentEpochId;
depositors[msg.sender][epochIdCache] += amount;
- EpochInfo storage info = epochs[epochIdCache];
- info.totalBidTokenAmount += amount;
+ epochs[epochIdCache].totalBidTokenAmount += amount;
emit Deposit(msg.sender, epochIdCache, amount);
}
L47
Modify _deployTimestamp
to uint32
to share a storage slot with the above uint32
variables, saving storage costs.
/// @notice Auctions run for minimum 1 week
uint32 public constant MINIMUM_AUCTION_PERIOD = 1 weeks;
/// @notice Maximum wait period between last and next auctions
uint32 public constant MAXIMUM_AUCTION_WAIT_PERIOD = 90 days;
/// @notice Maximum auction duration
uint32 public constant MAXIMUM_AUCTION_DURATION = 30 days;
+ /// @notice Last time auction was started. For zero auctions, it is the contract deploy timestamp
+ uint32 private immutable _deployTimestamp;
/// @notice Name of this Spice Bazaar auction
string public override name;
- /// @notice Last time auction was started. For zero auctions, it is the contract deploy timestamp
- uint256 private immutable _deployTimestamp;