From bad662ddc5008b006a6eed2381b45ee5edf09bc3 Mon Sep 17 00:00:00 2001 From: Paul Razvan Berg Date: Sat, 29 Apr 2023 21:42:13 +0300 Subject: [PATCH 1/2] test: use "expectCall" overload with "count" param build: bump prb-test --- lib/prb-test | 2 +- test/Base.t.sol | 103 +++++++++++------- .../batchCancelMultiple.t.sol | 12 +- .../batchCreateWithDeltas.t.sol | 3 +- .../batchCreateWithDurations.t.sol | 3 +- .../batchCreateWithMilestones.t.sol | 6 +- .../batchCreateWithRange.t.sol | 3 +- .../unit/cancel-multiple/cancelMultiple.t.sol | 6 +- 8 files changed, 90 insertions(+), 48 deletions(-) diff --git a/lib/prb-test b/lib/prb-test index 94b5ef9b..96972422 160000 --- a/lib/prb-test +++ b/lib/prb-test @@ -1 +1 @@ -Subproject commit 94b5ef9b2878833b2371034b46095d02e2198842 +Subproject commit 969724221ab27e7221a1fa195e0d5b36a2e224d4 diff --git a/test/Base.t.sol b/test/Base.t.sol index b82e8c07..bfd78681 100644 --- a/test/Base.t.sol +++ b/test/Base.t.sol @@ -55,7 +55,6 @@ abstract contract Base_Test is Assertions, StdCheats { //////////////////////////////////////////////////////////////////////////*/ function setUp() public virtual { - // Create users for testing. users.admin = createUser("Admin"); users.broker = createUser("Broker"); users.recipient = createUser("Recipient"); @@ -250,62 +249,86 @@ abstract contract Base_Test is Assertions, StdCheats { vm.expectCall({ callee: address(linear), data: abi.encodeCall(ISablierV2LockupLinear.createWithRange, (params)) }); } - /// @dev Expects multiple calls to {SablierV2LockupDynamic.createWithMilestones}. - function expectMultipleCallsToCreateWithDeltas(LockupDynamic.CreateWithDeltas memory params) internal { - for (uint256 i = 0; i < defaults.BATCH_SIZE(); ++i) { - expectCallToCreateWithDeltas(params); - } + /// @dev Expects a call to {IERC20.transfer}. + function expectCallToTransfer(address to, uint256 amount) internal { + vm.expectCall({ callee: address(dai), data: abi.encodeCall(IERC20.transfer, (to, amount)) }); } - /// @dev Expects multiple calls to {ISablierV2LockupLinear.createWithDurations}. - function expectMultipleCallsToCreateWithDurations(LockupLinear.CreateWithDurations memory params) internal { - for (uint256 i = 0; i < defaults.BATCH_SIZE(); ++i) { - expectCallToCreateWithDurations(params); - } + /// @dev Expects a call to {IERC20.transferFrom}. + function expectCallToTransferFrom(address from, address to, uint256 amount) internal { + vm.expectCall({ callee: address(dai), data: abi.encodeCall(IERC20.transferFrom, (from, to, amount)) }); } - /// @dev Expects multiple calls to {SablierV2LockupDynamic.createWithMilestones}. - function expectMultipleCallsToCreateWithMilestones(LockupDynamic.CreateWithMilestones memory params) internal { - for (uint256 i = 0; i < defaults.BATCH_SIZE(); ++i) { - expectCallToCreateWithMilestones(params); - } + /// @dev Expects a call to {IERC20.transferFrom}. + function expectCallToTransferFrom(address asset, address from, address to, uint256 amount) internal { + vm.expectCall({ callee: asset, data: abi.encodeCall(IERC20.transferFrom, (from, to, amount)) }); } - /// @dev Expects multiple calls to {SablierV2LockupLinear.createWithRange}. - function expectMultipleCallsToCreateWithRange(LockupLinear.CreateWithRange memory params) internal { - for (uint256 i = 0; i < defaults.BATCH_SIZE(); ++i) { - expectCallToCreateWithRange(params); - } + /// @dev Expects multiple calls to {ISablierV2LockupDynamic.createWithMilestones}. + function expectMultipleCallsToCreateWithDeltas( + uint256 count, + LockupDynamic.CreateWithDeltas memory params + ) + internal + { + vm.expectCall({ + callee: address(dynamic), + count: uint64(count), + data: abi.encodeCall(ISablierV2LockupDynamic.createWithDeltas, (params)) + }); } - /// @dev Expects multiple calls to the `transfer` function of the default ERC-20 contract. - function expectMultipleCallsToTransfer(address to, uint256 amount) internal { - for (uint256 i = 0; i < defaults.BATCH_SIZE(); ++i) { - expectCallToTransfer(to, amount); - } + /// @dev Expects multiple calls to {ISablierV2LockupLinear.createWithDurations}. + function expectMultipleCallsToCreateWithDurations( + uint256 count, + LockupLinear.CreateWithDurations memory params + ) + internal + { + vm.expectCall({ + callee: address(linear), + count: uint64(count), + data: abi.encodeCall(ISablierV2LockupLinear.createWithDurations, (params)) + }); } - /// @dev Expects multiple calls to the `transferFrom` function of the default ERC-20 contract. - function expectMultipleCallsToTransferFrom(address from, address to, uint256 amount) internal { - for (uint256 i = 0; i < defaults.BATCH_SIZE(); ++i) { - expectCallToTransferFrom(from, to, amount); - } + /// @dev Expects multiple calls to {ISablierV2LockupDynamic.createWithMilestones}. + function expectMultipleCallsToCreateWithMilestones( + uint256 count, + LockupDynamic.CreateWithMilestones memory params + ) + internal + { + vm.expectCall({ + callee: address(dynamic), + count: uint64(count), + data: abi.encodeCall(ISablierV2LockupDynamic.createWithMilestones, (params)) + }); } - /// @dev Expects a call to the `transfer` function of the default ERC-20 contract. - function expectCallToTransfer(address to, uint256 amount) internal { - vm.expectCall({ callee: address(dai), data: abi.encodeCall(IERC20.transfer, (to, amount)) }); + /// @dev Expects multiple calls to {ISablierV2LockupLinear.createWithRange}. + function expectMultipleCallsToCreateWithRange(uint256 count, LockupLinear.CreateWithRange memory params) internal { + vm.expectCall({ + callee: address(linear), + count: uint64(count), + data: abi.encodeCall(ISablierV2LockupLinear.createWithRange, (params)) + }); } - /// @dev Expects a call to the `transferFrom` function of the default ERC-20 contract. - function expectCallToTransferFrom(address from, address to, uint256 amount) internal { - vm.expectCall({ callee: address(dai), data: abi.encodeCall(IERC20.transferFrom, (from, to, amount)) }); + /// @dev Expects multiple calls to {IERC20.transfer}. + function expectMultipleCallsToTransfer(uint256 count, address to, uint256 amount) internal { + vm.expectCall({ callee: address(dai), count: uint64(count), data: abi.encodeCall(IERC20.transfer, (to, amount)) }); } - /// @dev Expects a call to the `transferFrom` function of the provided ERC-20 contract. - function expectCallToTransferFrom(address asset, address from, address to, uint256 amount) internal { - vm.expectCall({ callee: asset, data: abi.encodeCall(IERC20.transferFrom, (from, to, amount)) }); + /// @dev Expects multiple calls to {IERC20.transferFrom}. + function expectMultipleCallsToTransferFrom(uint256 count, address from, address to, uint256 amount) internal { + vm.expectCall({ + callee: address(dai), + count: uint64(count), + data: abi.encodeCall(IERC20.transferFrom, (from, to, amount)) + }); } + /*////////////////////////////////////////////////////////////////////////// PERMIT2 //////////////////////////////////////////////////////////////////////////*/ diff --git a/test/unit/batch-cancel-multiple/batchCancelMultiple.t.sol b/test/unit/batch-cancel-multiple/batchCancelMultiple.t.sol index 207cdffa..a9c57de0 100644 --- a/test/unit/batch-cancel-multiple/batchCancelMultiple.t.sol +++ b/test/unit/batch-cancel-multiple/batchCancelMultiple.t.sol @@ -36,8 +36,16 @@ contract BatchCancelMultiple_Unit_Test is Unit_Test { // Asset flow: Sablier → proxy → proxy owner // Expects transfers from the Sablier contracts to the proxy, and then from the proxy to the proxy owner. - expectMultipleCallsToTransfer({ to: address(proxy), amount: defaults.REFUND_AMOUNT() }); - expectMultipleCallsToTransfer({ to: address(proxy), amount: defaults.REFUND_AMOUNT() }); + expectMultipleCallsToTransfer({ + count: defaults.BATCH_SIZE(), + to: address(proxy), + amount: defaults.REFUND_AMOUNT() + }); + expectMultipleCallsToTransfer({ + count: defaults.BATCH_SIZE(), + to: address(proxy), + amount: defaults.REFUND_AMOUNT() + }); expectCallToTransfer({ to: users.sender.addr, amount: 2 * defaults.BATCH_SIZE() * defaults.REFUND_AMOUNT() }); // ABI encode the parameters and call the function via the proxy. diff --git a/test/unit/batch-create-with-deltas/batchCreateWithDeltas.t.sol b/test/unit/batch-create-with-deltas/batchCreateWithDeltas.t.sol index 65e6ed88..382ee25e 100644 --- a/test/unit/batch-create-with-deltas/batchCreateWithDeltas.t.sol +++ b/test/unit/batch-create-with-deltas/batchCreateWithDeltas.t.sol @@ -25,8 +25,9 @@ contract BatchCreateWithDeltas_Unit_Test is Unit_Test { // Asset flow: proxy owner → proxy → Sablier // Expect transfers from the proxy owner to the proxy, and then from the proxy to the Sablier contract. expectCallToTransferFrom({ from: users.sender.addr, to: address(proxy), amount: defaults.TRANSFER_AMOUNT() }); - expectMultipleCallsToCreateWithDeltas({ params: defaults.createWithDeltas() }); + expectMultipleCallsToCreateWithDeltas({ count: defaults.BATCH_SIZE(), params: defaults.createWithDeltas() }); expectMultipleCallsToTransferFrom({ + count: defaults.BATCH_SIZE(), from: address(proxy), to: address(dynamic), amount: defaults.PER_STREAM_AMOUNT() diff --git a/test/unit/batch-create-with-durations/batchCreateWithDurations.t.sol b/test/unit/batch-create-with-durations/batchCreateWithDurations.t.sol index cec0c4ed..7267db6b 100644 --- a/test/unit/batch-create-with-durations/batchCreateWithDurations.t.sol +++ b/test/unit/batch-create-with-durations/batchCreateWithDurations.t.sol @@ -25,8 +25,9 @@ contract BatchCreateWithDurations_Unit_Test is Unit_Test { // Asset flow: proxy owner → proxy → Sablier // Expect transfers from the proxy owner to the proxy, and then from the proxy to the Sablier contract. expectCallToTransferFrom({ from: users.sender.addr, to: address(proxy), amount: defaults.TRANSFER_AMOUNT() }); - expectMultipleCallsToCreateWithDurations({ params: defaults.createWithDurations() }); + expectMultipleCallsToCreateWithDurations({ count: defaults.BATCH_SIZE(), params: defaults.createWithDurations() }); expectMultipleCallsToTransferFrom({ + count: defaults.BATCH_SIZE(), from: address(proxy), to: address(linear), amount: defaults.PER_STREAM_AMOUNT() diff --git a/test/unit/batch-create-with-milestones/batchCreateWithMilestones.t.sol b/test/unit/batch-create-with-milestones/batchCreateWithMilestones.t.sol index e7279cd5..428f2ec8 100644 --- a/test/unit/batch-create-with-milestones/batchCreateWithMilestones.t.sol +++ b/test/unit/batch-create-with-milestones/batchCreateWithMilestones.t.sol @@ -25,8 +25,12 @@ contract BatchCreateWithMilestones_Unit_Test is Unit_Test { // Asset flow: proxy owner → proxy → Sablier // Expect transfers from the proxy owner to the proxy, and then from the proxy to the Sablier contract. expectCallToTransferFrom({ from: users.sender.addr, to: address(proxy), amount: defaults.TRANSFER_AMOUNT() }); - expectMultipleCallsToCreateWithMilestones({ params: defaults.createWithMilestones() }); + expectMultipleCallsToCreateWithMilestones({ + count: defaults.BATCH_SIZE(), + params: defaults.createWithMilestones() + }); expectMultipleCallsToTransferFrom({ + count: defaults.BATCH_SIZE(), from: address(proxy), to: address(dynamic), amount: defaults.PER_STREAM_AMOUNT() diff --git a/test/unit/batch-create-with-range/batchCreateWithRange.t.sol b/test/unit/batch-create-with-range/batchCreateWithRange.t.sol index de89bcc1..97423a3a 100644 --- a/test/unit/batch-create-with-range/batchCreateWithRange.t.sol +++ b/test/unit/batch-create-with-range/batchCreateWithRange.t.sol @@ -24,8 +24,9 @@ contract BatchCreateWithRange_Unit_Test is Unit_Test { // Asset flow: proxy owner → proxy → Sablier // Expect transfers from the proxy owner to the proxy, and then from the proxy to the Sablier contract. expectCallToTransferFrom({ from: users.sender.addr, to: address(proxy), amount: defaults.TRANSFER_AMOUNT() }); - expectMultipleCallsToCreateWithRange({ params: defaults.createWithRange() }); + expectMultipleCallsToCreateWithRange({ count: defaults.BATCH_SIZE(), params: defaults.createWithRange() }); expectMultipleCallsToTransferFrom({ + count: defaults.BATCH_SIZE(), from: address(proxy), to: address(linear), amount: defaults.PER_STREAM_AMOUNT() diff --git a/test/unit/cancel-multiple/cancelMultiple.t.sol b/test/unit/cancel-multiple/cancelMultiple.t.sol index ab93eaf7..3d7d57ad 100644 --- a/test/unit/cancel-multiple/cancelMultiple.t.sol +++ b/test/unit/cancel-multiple/cancelMultiple.t.sol @@ -31,7 +31,11 @@ contract CancelMultiple_Unit_Test is Unit_Test { vm.warp(defaults.WARP_26_PERCENT()); // Asset flow: proxy owner → proxy → sender - expectMultipleCallsToTransfer({ to: address(proxy), amount: defaults.REFUND_AMOUNT() }); + expectMultipleCallsToTransfer({ + count: defaults.BATCH_SIZE(), + to: address(proxy), + amount: defaults.REFUND_AMOUNT() + }); expectCallToTransfer({ to: users.sender.addr, amount: defaults.REFUND_AMOUNT() * defaults.BATCH_SIZE() }); bytes memory data = abi.encodeCall(target.cancelMultiple, (lockup, defaults.assets(), streamIds)); From 0bbdc4c9b91904ba69a1d7a22f4cc9f69713c8ba Mon Sep 17 00:00:00 2001 From: Paul Razvan Berg Date: Mon, 1 May 2023 15:15:00 +0300 Subject: [PATCH 2/2] test: change param type to "uint64" test: change "BATCH_SIZE" type to "uint64" --- test/Base.t.sol | 12 ++++++------ test/utils/Defaults.sol | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/test/Base.t.sol b/test/Base.t.sol index bfd78681..973d3b22 100644 --- a/test/Base.t.sol +++ b/test/Base.t.sol @@ -266,7 +266,7 @@ abstract contract Base_Test is Assertions, StdCheats { /// @dev Expects multiple calls to {ISablierV2LockupDynamic.createWithMilestones}. function expectMultipleCallsToCreateWithDeltas( - uint256 count, + uint64 count, LockupDynamic.CreateWithDeltas memory params ) internal @@ -280,7 +280,7 @@ abstract contract Base_Test is Assertions, StdCheats { /// @dev Expects multiple calls to {ISablierV2LockupLinear.createWithDurations}. function expectMultipleCallsToCreateWithDurations( - uint256 count, + uint64 count, LockupLinear.CreateWithDurations memory params ) internal @@ -294,7 +294,7 @@ abstract contract Base_Test is Assertions, StdCheats { /// @dev Expects multiple calls to {ISablierV2LockupDynamic.createWithMilestones}. function expectMultipleCallsToCreateWithMilestones( - uint256 count, + uint64 count, LockupDynamic.CreateWithMilestones memory params ) internal @@ -307,7 +307,7 @@ abstract contract Base_Test is Assertions, StdCheats { } /// @dev Expects multiple calls to {ISablierV2LockupLinear.createWithRange}. - function expectMultipleCallsToCreateWithRange(uint256 count, LockupLinear.CreateWithRange memory params) internal { + function expectMultipleCallsToCreateWithRange(uint64 count, LockupLinear.CreateWithRange memory params) internal { vm.expectCall({ callee: address(linear), count: uint64(count), @@ -316,12 +316,12 @@ abstract contract Base_Test is Assertions, StdCheats { } /// @dev Expects multiple calls to {IERC20.transfer}. - function expectMultipleCallsToTransfer(uint256 count, address to, uint256 amount) internal { + function expectMultipleCallsToTransfer(uint64 count, address to, uint256 amount) internal { vm.expectCall({ callee: address(dai), count: uint64(count), data: abi.encodeCall(IERC20.transfer, (to, amount)) }); } /// @dev Expects multiple calls to {IERC20.transferFrom}. - function expectMultipleCallsToTransferFrom(uint256 count, address from, address to, uint256 amount) internal { + function expectMultipleCallsToTransferFrom(uint64 count, address from, address to, uint256 amount) internal { vm.expectCall({ callee: address(dai), count: uint64(count), diff --git a/test/utils/Defaults.sol b/test/utils/Defaults.sol index ba8c66bb..021771ab 100644 --- a/test/utils/Defaults.sol +++ b/test/utils/Defaults.sol @@ -17,7 +17,7 @@ contract Defaults { GENERIC CONSTANTS //////////////////////////////////////////////////////////////////////////*/ - uint256 public constant BATCH_SIZE = 10; + uint64 public constant BATCH_SIZE = 10; Broker public BROKER; UD60x18 public constant BROKER_FEE = UD60x18.wrap(0); uint40 public constant CLIFF_DURATION = 2500 seconds;