Skip to content

Commit

Permalink
refactor: use unchecked and casting in calculateStreamedPercentage
Browse files Browse the repository at this point in the history
docs: last polishes
  • Loading branch information
andreivladbrg committed Jan 7, 2025
1 parent 51dfa58 commit e502409
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
5 changes: 4 additions & 1 deletion src/LockupNFTDescriptor.sol
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,10 @@ contract LockupNFTDescriptor is ILockupNFTDescriptor {
pure
returns (uint256)
{
return streamedAmount * 10_000 / depositedAmount;
// This cannot overflow because both inputs are uint128s, and zero deposit amounts are not allowed in Sablier.
unchecked {
return uint256(streamedAmount) * 10_000 / depositedAmount;
}
}

/// @notice Generates a pseudo-random HSL color by hashing together the `chainid`, the `sablier` address,
Expand Down
8 changes: 4 additions & 4 deletions src/SablierLockup.sol
Original file line number Diff line number Diff line change
Expand Up @@ -297,15 +297,15 @@ contract SablierLockup is ISablierLockup, SablierLockupBase {

/// @inheritdoc SablierLockupBase
function _calculateStreamedAmount(uint256 streamId) internal view override returns (uint128) {
// Load the stream's parameters in memory.
// Load in memory the parameters used in {VestingMath}.
uint40 blockTimestamp = uint40(block.timestamp);
uint128 depositedAmount = _streams[streamId].amounts.deposited;
Lockup.Model lockupModel = _streams[streamId].lockupModel;
uint128 streamedAmount;
Lockup.Timestamps memory timestamps =
Lockup.Timestamps({ start: _streams[streamId].startTime, end: _streams[streamId].endTime });

// Calculate streamed amount for the Lockup Dynamic model.
// Calculate the streamed amount for the Lockup Dynamic model.
if (lockupModel == Lockup.Model.LOCKUP_DYNAMIC) {
streamedAmount = VestingMath.calculateLockupDynamicStreamedAmount({
depositedAmount: depositedAmount,
Expand All @@ -315,7 +315,7 @@ contract SablierLockup is ISablierLockup, SablierLockupBase {
withdrawnAmount: _streams[streamId].amounts.withdrawn
});
}
// Calculate streamed amount for the Lockup Linear model.
// Calculate the streamed amount for the Lockup Linear model.
else if (lockupModel == Lockup.Model.LOCKUP_LINEAR) {
streamedAmount = VestingMath.calculateLockupLinearStreamedAmount({
depositedAmount: depositedAmount,
Expand All @@ -326,7 +326,7 @@ contract SablierLockup is ISablierLockup, SablierLockupBase {
withdrawnAmount: _streams[streamId].amounts.withdrawn
});
}
// Calculate streamed amount for the Lockup Tranched model.
// Calculate the streamed amount for the Lockup Tranched model.
else if (lockupModel == Lockup.Model.LOCKUP_TRANCHED) {
streamedAmount = VestingMath.calculateLockupTranchedStreamedAmount({
depositedAmount: depositedAmount,
Expand Down
4 changes: 2 additions & 2 deletions src/libraries/VestingMath.sol
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,9 @@ library VestingMath {
/// @dev Lockup linear model uses the following distribution function:
///
/// $$
/// ( x * sa + s, cliffTime > blockTimestamp
/// ( x * sa + s, cliff time > block timestamp
/// f(x) = (
/// ( x * sa + s + c, cliffTime <= blockTimestamp
/// ( x * sa + s + c, cliff time <= block timestamp
/// $$
///
/// Where:
Expand Down

0 comments on commit e502409

Please sign in to comment.