Created
April 12, 2022 06:15
-
-
Save CJ42/03661dde43f6d782fd2d393f2229522d to your computer and use it in GitHub Desktop.
Practical Example from Sablier to explain return inside if statement
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity =0.5.17; | |
// import statements (omitted for brievity) ... | |
/** | |
* @title Sablier | |
* @author Sablier | |
* @notice Money streaming. | |
*/ | |
contract Sablier is ISablier, ReentrancyGuard, CarefulMath { | |
// functions and events (omitted for brievity) .... | |
/** | |
* @notice Returns either the delta in seconds between `block.timestamp` and `startTime` or | |
* between `stopTime` and `startTime, whichever is smaller. If `block.timestamp` is before | |
* `startTime`, it returns 0. | |
* @dev Throws if the id does not point to a valid stream. | |
* @param streamId The id of the stream for which to query the delta. | |
* @return The time delta in seconds. | |
*/ | |
function deltaOf(uint256 streamId) public view streamExists(streamId) returns (uint256 delta) { | |
Types.Stream memory stream = streams[streamId]; | |
if (block.timestamp <= stream.startTime) return 0; | |
if (block.timestamp < stream.stopTime) return block.timestamp - stream.startTime; | |
return stream.stopTime - stream.startTime; | |
} | |
// more functions definitions ... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment