Skip to content

Commit

Permalink
test: polishes
Browse files Browse the repository at this point in the history
  • Loading branch information
andreivladbrg authored and smol-ninja committed Feb 4, 2025
1 parent ae7079b commit 8236b66
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,6 @@ contract CreateWithTimestampsLD_Integration_Fuzz_Test is Lockup_Dynamic_Integrat
Lockup.Status expectedStatus;
bool isCancelable;
bool isSettled;
uint128 depositAmount;
}

/// @dev Given enough fuzz runs, all of the following scenarios will be fuzzed:
Expand Down Expand Up @@ -181,23 +180,21 @@ contract CreateWithTimestampsLD_Integration_Fuzz_Test is Lockup_Dynamic_Integrat

// Fuzz the segment amounts and calculate the deposit amount
Vars memory vars;
vars.depositAmount = fuzzDynamicStreamAmounts({ upperBound: MAX_UINT128, segments: segments });

params.depositAmount = vars.depositAmount;
params.depositAmount = fuzzDynamicStreamAmounts({ upperBound: MAX_UINT128, segments: segments });

// Make the fuzzed funder the caller in the rest of this test.
resetPrank(funder);

uint256 expectedStreamId = lockup.nextStreamId();

// Mint enough tokens to the fuzzed funder.
deal({ token: address(dai), to: funder, give: vars.depositAmount });
deal({ token: address(dai), to: funder, give: params.depositAmount });

// Approve {SablierLockup} to transfer the tokens from the fuzzed funder.
dai.approve({ spender: address(lockup), value: MAX_UINT256 });

// Expect the tokens to be transferred from the funder to {SablierLockup}.
expectCallToTransferFrom({ from: funder, to: address(lockup), value: vars.depositAmount });
expectCallToTransferFrom({ from: funder, to: address(lockup), value: params.depositAmount });

// Expect the relevant event to be emitted.
vm.expectEmit({ emitter: address(lockup) });
Expand All @@ -207,7 +204,7 @@ contract CreateWithTimestampsLD_Integration_Fuzz_Test is Lockup_Dynamic_Integrat
funder: funder,
sender: params.sender,
recipient: params.recipient,
depositAmount: vars.depositAmount,
depositAmount: params.depositAmount,
token: dai,
cancelable: params.cancelable,
transferable: params.transferable,
Expand All @@ -226,7 +223,7 @@ contract CreateWithTimestampsLD_Integration_Fuzz_Test is Lockup_Dynamic_Integrat
vars.isCancelable = vars.isSettled ? false : params.cancelable;

// It should create the stream.
assertEq(lockup.getDepositedAmount(streamId), vars.depositAmount, "depositedAmount");
assertEq(lockup.getDepositedAmount(streamId), params.depositAmount, "depositedAmount");
assertEq(lockup.getEndTime(streamId), params.timestamps.end, "endTime");
assertEq(lockup.isCancelable(streamId), vars.isCancelable, "isCancelable");
assertFalse(lockup.isDepleted(streamId), "isDepleted");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@ contract CreateWithTimestampsLT_Integration_Fuzz_Test is Lockup_Tranched_Integra
Lockup.Status expectedStatus;
bool isCancelable;
bool isSettled;
uint128 depositAmount;
}

/// @dev Given enough fuzz runs, all of the following scenarios will be fuzzed:
Expand Down Expand Up @@ -186,23 +185,19 @@ contract CreateWithTimestampsLT_Integration_Fuzz_Test is Lockup_Tranched_Integra
fuzzTrancheTimestamps(tranches, params.timestamps.start);
params.timestamps.end = tranches[tranches.length - 1].timestamp;

// Fuzz the tranche amounts and calculate the deposit amount.
Vars memory vars;
vars.depositAmount = fuzzTranchedStreamAmounts({ upperBound: MAX_UINT128, tranches: tranches });

params.depositAmount = vars.depositAmount;
params.depositAmount = fuzzTranchedStreamAmounts({ upperBound: MAX_UINT128, tranches: tranches });

// Make the fuzzed funder the caller in the rest of this test.
resetPrank(funder);

// Mint enough tokens to the fuzzed funder.
deal({ token: address(dai), to: funder, give: vars.depositAmount });
deal({ token: address(dai), to: funder, give: params.depositAmount });

// Approve {SablierLockup} to transfer the tokens from the fuzzed funder.
dai.approve({ spender: address(lockup), value: MAX_UINT256 });

// Expect the tokens to be transferred from the funder to {SablierLockup}.
expectCallToTransferFrom({ from: funder, to: address(lockup), value: vars.depositAmount });
expectCallToTransferFrom({ from: funder, to: address(lockup), value: params.depositAmount });

uint256 expectedStreamId = lockup.nextStreamId();

Expand All @@ -214,7 +209,7 @@ contract CreateWithTimestampsLT_Integration_Fuzz_Test is Lockup_Tranched_Integra
funder: funder,
sender: params.sender,
recipient: params.recipient,
depositAmount: vars.depositAmount,
depositAmount: params.depositAmount,
token: dai,
cancelable: params.cancelable,
transferable: params.transferable,
Expand All @@ -227,13 +222,16 @@ contract CreateWithTimestampsLT_Integration_Fuzz_Test is Lockup_Tranched_Integra
// Create the stream.
uint256 streamId = lockup.createWithTimestampsLT(params, tranches);

// Fuzz the tranche amounts and calculate the deposit amount.
Vars memory vars;

// Check if the stream is settled. It is possible for a Lockup Tranched stream to settle at the time of creation
// because some tranche amounts can be zero.
vars.isSettled = (lockup.getDepositedAmount(streamId) - lockup.streamedAmountOf(streamId)) == 0;
vars.isCancelable = vars.isSettled ? false : params.cancelable;

// It should create the stream.
assertEq(lockup.getDepositedAmount(streamId), vars.depositAmount, "depositedAmount");
assertEq(lockup.getDepositedAmount(streamId), params.depositAmount, "depositedAmount");
assertEq(lockup.getEndTime(streamId), params.timestamps.end, "endTime");
assertEq(lockup.isCancelable(streamId), vars.isCancelable, "isCancelable");
assertFalse(lockup.isDepleted(streamId), "isDepleted");
Expand Down
11 changes: 0 additions & 11 deletions tests/utils/Defaults.sol
Original file line number Diff line number Diff line change
Expand Up @@ -105,17 +105,6 @@ contract Defaults is Constants {
return lockupCreateEvent(depositAmount, token, timestamps);
}

function lockupCreateEvent(
IERC20 token_,
Lockup.Timestamps memory timestamps
)
public
view
returns (Lockup.CreateEventCommon memory)
{
return lockupCreateEvent(DEPOSIT_AMOUNT, token_, timestamps);
}

function lockupCreateEvent(
uint128 depositAmount,
IERC20 token_,
Expand Down
18 changes: 4 additions & 14 deletions tests/utils/Fuzzers.sol
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,15 @@ abstract contract Fuzzers is Constants, Utils {

// Precompute the first segment amount to prevent zero deposit amounts.
segments[0].amount = boundUint128(segments[0].amount, 100, maxSegmentAmount);
uint128 estimatedDepositAmount = segments[0].amount;
depositAmount = segments[0].amount;

// Fuzz the other segment amounts by bounding from 0.
unchecked {
for (uint256 i = 1; i < segmentCount; ++i) {
segments[i].amount = boundUint128(segments[i].amount, 0, maxSegmentAmount);
estimatedDepositAmount += segments[i].amount;
depositAmount += segments[i].amount;
}
}

depositAmount = estimatedDepositAmount;

// Here, we account for rounding errors and adjust the estimated deposit amount and the segments.
segments[segments.length - 1].amount += (depositAmount - estimatedDepositAmount);
}

/// @dev Fuzzes the segment durations.
Expand Down Expand Up @@ -219,19 +214,14 @@ abstract contract Fuzzers is Constants, Utils {

// Precompute the first tranche amount to prevent zero deposit amounts.
tranches[0].amount = boundUint128(tranches[0].amount, 100, maxTrancheAmount);
uint128 estimatedDepositAmount = tranches[0].amount;
depositAmount = tranches[0].amount;

// Fuzz the other tranche amounts by bounding from 0.
unchecked {
for (uint256 i = 1; i < trancheCount; ++i) {
tranches[i].amount = boundUint128(tranches[i].amount, 0, maxTrancheAmount);
estimatedDepositAmount += tranches[i].amount;
depositAmount += tranches[i].amount;
}
}

depositAmount = estimatedDepositAmount;

// Here, we account for rounding errors and adjust the estimated deposit amount and the tranches.
tranches[tranches.length - 1].amount += (depositAmount - estimatedDepositAmount);
}
}

0 comments on commit 8236b66

Please sign in to comment.