Omniscia Maverick Protocol Audit
Cast Code Style Findings
Cast Code Style Findings
CTS-01C: Optimization of Statements
| Type | Severity | Location |
|---|---|---|
| Gas Optimization | ![]() | Cast.sol:L8, L12, L16, L20, L24 |
Description:
A miniscule optimization of all Cast::to-prefixed functions can be applied to the codebase by restructuring their statements, increasing their legibility in the process as well.
Example:
4library Cast {5 error CastValueExceedsBits(uint256 amount, uint256 bits);6
7 function toUint8(uint256 x) internal pure returns (uint8 y) {8 if ((y = uint8(x)) != x) revert CastValueExceedsBits(x, 8);9 }10
11 function toUint16(uint256 x) internal pure returns (uint16 y) {12 if ((y = uint16(x)) != x) revert CastValueExceedsBits(x, 16);13 }14
15 function toUint64(uint256 x) internal pure returns (uint64 y) {16 if ((y = uint64(x)) != x) revert CastValueExceedsBits(x, 64);17 }18
19 function toUint88(uint256 x) internal pure returns (uint88 y) {20 if ((y = uint88(x)) != x) revert CastValueExceedsBits(x, 88);21 }22
23 function toUint128(uint256 x) internal pure returns (uint128 y) {24 if ((y = uint128(x)) != x) revert CastValueExceedsBits(x, 128);25 }26}Recommendation:
We advise the return argument's explicit name to be omitted to prevent memory reservation. As a next step, we advise the if conditional to evaluate whether the input argument (x) is greater-than the limitation of the desired type (i.e. for Cast::toUint8 the conditional would be x > type(uint8).max).
After the conditional, the function should simply return the value cast directly. These adjustments will result in a 6 unit gas optimization for each instance with optimizations turned on.
Alleviation (175f8c39b19df69134add3aa8a2a042ce3047763):
All import statements of the Cast library have been replaced by its OpenZeppelin counterpart which already integrates these optimizations, rendering the exhibit addressed.
