Skip to content

Instantly share code, notes, and snippets.

@CJ42
Created April 12, 2022 06:13
Show Gist options
  • Save CJ42/5e58b4e7448a0e99bb1fcaec1f94f1f8 to your computer and use it in GitHub Desktop.
Save CJ42/5e58b4e7448a0e99bb1fcaec1f94f1f8 to your computer and use it in GitHub Desktop.
Example from Sablier to explain external calls with if statements
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 Cancels the stream and transfers the tokens back on a pro rata basis.
* @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 there is a token transfer failure.
* @param streamId The id of the stream to cancel.
* @return bool true=success, otherwise false.
*/
function cancelStream(uint256 streamId)
external
nonReentrant
streamExists(streamId)
onlySenderOrRecipient(streamId)
returns (bool)
{
Types.Stream memory stream = streams[streamId];
uint256 senderBalance = balanceOf(streamId, stream.sender);
uint256 recipientBalance = balanceOf(streamId, stream.recipient);
delete streams[streamId];
IERC20 token = IERC20(stream.tokenAddress);
if (recipientBalance > 0) token.safeTransfer(stream.recipient, recipientBalance);
if (senderBalance > 0) token.safeTransfer(stream.sender, senderBalance);
emit CancelStream(streamId, stream.sender, stream.recipient, senderBalance, recipientBalance);
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment