For the arithmetic operations that will never over/underflow, using the unchecked directive (Solidity v0.8 has default overflow/underflow checks) can save some gas from the unnecessary internal over/underflow checks.
function oracleVersionToSettle(PrePosition storage self, IOracle oracle) internal view returns (uint256) {
uint256 current = oracle.currentVersion();
uint256 next = self.oracleVersion + 1;
if (next == 1) return current; // no pre position
if (next > current) return current; // pre in future
return next; // settle pre
}
Handle
WatchPug
Vulnerability details
For the arithmetic operations that will never over/underflow, using the unchecked directive (Solidity v0.8 has default overflow/underflow checks) can save some gas from the unnecessary internal over/underflow checks.
For example:
https://github.com/code-423n4/2021-12-perennial/blob/fd7c38823833a51ae0c6ae3856a3d93a7309c0e4/protocol/contracts/oracle/ChainlinkOracle.sol#L50-L60
timestampAtVersion[currentVersion()] + minDelay
won't overflow.https://github.com/code-423n4/2021-12-perennial/blob/fd7c38823833a51ae0c6ae3856a3d93a7309c0e4/protocol/contracts/product/types/position/PrePosition.sol#L169-L176
self.oracleVersion + 1
won't overflow.