Created
April 12, 2022 05:13
-
-
Save CJ42/d23350de854ae863f87eb6aca0775900 to your computer and use it in GitHub Desktop.
Brief example from Sablier protocol to show one line if statements in Solidity
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 ... | |
/** | |
* @title Sablier | |
* @author Sablier | |
* @notice Money streaming. | |
*/ | |
contract Sablier is ISablier, ReentrancyGuard, CarefulMath { | |
// functions and events (omitted for brievity) .... | |
/** | |
* @notice Withdraws from the contract to the recipient's account. | |
* @dev Throws if the id does not point to a valid stream. | |
* Throws if the caller is not the sender or the recipient of the stream. | |
* Throws if the amount exceeds the available balance. | |
* Throws if there is a token transfer failure. | |
* @param streamId The id of the stream to withdraw tokens from. | |
* @param amount The amount of tokens to withdraw. | |
*/ | |
function withdrawFromStream(uint256 streamId, uint256 amount) | |
external | |
nonReentrant | |
streamExists(streamId) | |
onlySenderOrRecipient(streamId) | |
returns (bool) | |
{ | |
// omitted for brievity | |
// - check that the amount is not zero | |
// - retrieve the stream recipient balance | |
// update the balance in the stream by removing the amount withdrawn | |
MathError mathErr; | |
(mathErr, streams[streamId].remainingBalance) = subUInt(stream.remainingBalance, amount); | |
/** | |
* `subUInt` can only return MathError.INTEGER_UNDERFLOW but we know that `remainingBalance` is at least | |
* as big as `amount`. | |
*/ | |
assert(mathErr == MathError.NO_ERROR); | |
if (streams[streamId].remainingBalance == 0) delete streams[streamId]; | |
IERC20(stream.tokenAddress).safeTransfer(stream.recipient, amount); | |
emit WithdrawFromStream(streamId, stream.recipient, amount); | |
return true; | |
} | |
// more functions (omitted for brievity) ... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment