Sablier

Sablier
DeFiFoundry
53,440 USDC
View results
Submission Details
Severity: low
Invalid

optimize isCold and isWarm functions to reduce gas usage

Recommendations

Here’s the optimized version of the isCold and isWarm functions with reduced gas usage:

function isWarm(uint256 streamId) public view override notNull(streamId) returns (bool result) {
result = _isWarm(streamId);
}
function _isWarm(uint256 streamId) internal view returns (bool result) {
Lockup.Status status = _statusOf(streamId);
result = status == Lockup.Status.PENDING || status == Lockup.Status.STREAMING;
}
function isCold(uint256 streamId) external view override notNull(streamId) returns (bool result) {
result = !_isWarm(streamId);
}

Explanation

  • Combine isCold and isWarm: Since isCold is the opposite of isWarm, we can calculate one and get the other. This reduces the number of calculations and gas usage.

  • Move isWarm implementation to an internal function: By moving the isWarm implementation to an internal function_isWarm, we can reuse it in the isWarm function and also in other parts of the contract where we need to check if a stream is warm.

  • Simplify isCold function: The isCold function now simply returns the negation of the _isWarm function, which is more gas-efficient than duplicating the same logic.

  • Eliminate redundant checks: The 7 lines of code from 287 to 293 in SablierV2Lockup.sol can be replaced by a single check:

function renounce(uint256 streamId) external override noDelegateCall notNull(streamId) updateMetadata(streamId) {
// Check: the stream is not cold.
Lockup.Status status = _statusOf(streamId);
- if (status == Lockup.Status.DEPLETED) {
- revert Errors.SablierV2Lockup_StreamDepleted(streamId);
- } else if (status == Lockup.Status.CANCELED) {
- revert Errors.SablierV2Lockup_StreamCanceled(streamId);
- } else if (status == Lockup.Status.SETTLED) {
- revert Errors.SablierV2Lockup_StreamSettled(streamId);
- }
+ if (!_isWarm(streamId)) {
+ revert Errors.SablierV2Lockup_StreamCold(streamId); //this error must be added to Errors.sol
+}
// the rest of the code

This reduces the gas usage and simplifies the code.

The optimized version of the functions should result in lower gas usage, as it avoids redundant calculations and leverages the complementary nature of the Cold and Warm states.

Updates

Lead Judging Commences

inallhonesty Lead Judge 12 months ago
Submission Judgement Published
Invalidated
Reason: Non-acceptable severity
Assigned finding tags:

Info/Gas/Invalid as per Docs

https://docs.codehawks.com/hawks-auditors/how-to-determine-a-finding-validity

Support

FAQs

Can't find an answer? Chat with us on Discord, Twitter or Linkedin.