From 0d0576a83ce854f95c15cd01b03405b093ba604b Mon Sep 17 00:00:00 2001 From: andreivladbrg Date: Mon, 22 Apr 2024 17:40:10 +0300 Subject: [PATCH] refactor: change order of array counts in custom error test: createMultiple function --- src/SablierV2OpenEnded.sol | 4 +- src/libraries/Errors.sol | 2 +- .../create-multiple/createMultiple.t.sol | 138 ++++++++++++++++++ .../create-multiple/createMultiple.tree | 15 ++ 4 files changed, 156 insertions(+), 3 deletions(-) create mode 100644 test/integration/create-multiple/createMultiple.t.sol create mode 100644 test/integration/create-multiple/createMultiple.tree diff --git a/src/SablierV2OpenEnded.sol b/src/SablierV2OpenEnded.sol index f51e955f..508cae7b 100644 --- a/src/SablierV2OpenEnded.sol +++ b/src/SablierV2OpenEnded.sol @@ -181,8 +181,8 @@ contract SablierV2OpenEnded is ISablierV2OpenEnded, NoDelegateCall, SablierV2Ope // Check: count of `senders`, `recipients`, `ratesPerSecond` matches. if (recipientsCount != sendersCount || recipientsCount != ratesPerSecondCount) { - revert Errors.SablierV2OpenEnded_CreateArrayCountsNotEqual( - sendersCount, recipientsCount, ratesPerSecondCount + revert Errors.SablierV2OpenEnded_CreateMultipleArrayCountsNotEqual( + recipientsCount, sendersCount, ratesPerSecondCount ); } diff --git a/src/libraries/Errors.sol b/src/libraries/Errors.sol index 71aa1eaa..27cb9e5c 100644 --- a/src/libraries/Errors.sol +++ b/src/libraries/Errors.sol @@ -19,7 +19,7 @@ library Errors { /// @notice Thrown when trying to create multiple streams and the number of senders, recipients and rates per second /// does not match. - error SablierV2OpenEnded_CreateArrayCountsNotEqual( + error SablierV2OpenEnded_CreateMultipleArrayCountsNotEqual( uint256 recipientsCount, uint256 sendersCount, uint256 ratesPerSecondCount ); diff --git a/test/integration/create-multiple/createMultiple.t.sol b/test/integration/create-multiple/createMultiple.t.sol new file mode 100644 index 00000000..6b5eec34 --- /dev/null +++ b/test/integration/create-multiple/createMultiple.t.sol @@ -0,0 +1,138 @@ +// SPDX-License-Identifier: UNLICENSED +pragma solidity >=0.8.22; + +import { ISablierV2OpenEnded } from "src/interfaces/ISablierV2OpenEnded.sol"; +import { Errors } from "src/libraries/Errors.sol"; +import { OpenEnded } from "src/types/DataTypes.sol"; + +import { Integration_Test } from "../Integration.t.sol"; + +contract CreateMultiple_Integration_Test is Integration_Test { + address[] internal defaultRecipients; + address[] internal defaultSenders; + uint128[] internal defaultRatesPerSecond; + + function setUp() public override { + Integration_Test.setUp(); + + defaultRecipients.push(users.recipient); + defaultRecipients.push(users.recipient); + defaultSenders.push(users.sender); + defaultSenders.push(users.sender); + defaultRatesPerSecond.push(RATE_PER_SECOND); + defaultRatesPerSecond.push(RATE_PER_SECOND); + } + + function test_RevertWhen_DelegateCall() external { + bytes memory callData = abi.encodeCall( + ISablierV2OpenEnded.createMultiple, (defaultRecipients, defaultSenders, defaultRatesPerSecond, dai) + ); + _test_RevertWhen_DelegateCall(callData); + } + + modifier whenArrayCountsNotEqual() { + _; + } + + function test_RevertWhen_RecipientsCountNotEqual() external whenNotDelegateCalled whenArrayCountsNotEqual { + address[] memory recipients = new address[](1); + + vm.expectRevert( + abi.encodeWithSelector( + Errors.SablierV2OpenEnded_CreateMultipleArrayCountsNotEqual.selector, + recipients.length, + defaultSenders.length, + defaultRatesPerSecond.length + ) + ); + openEnded.createMultiple(recipients, defaultSenders, defaultRatesPerSecond, dai); + } + + function test_RevertWhen_SendersCountNotEqual() external whenNotDelegateCalled whenArrayCountsNotEqual { + address[] memory senders = new address[](1); + + vm.expectRevert( + abi.encodeWithSelector( + Errors.SablierV2OpenEnded_CreateMultipleArrayCountsNotEqual.selector, + defaultRecipients.length, + senders.length, + defaultRatesPerSecond.length + ) + ); + openEnded.createMultiple(defaultRecipients, senders, defaultRatesPerSecond, dai); + } + + function test_RevertWhen_RatesPerSecondCountNotEqual() external whenNotDelegateCalled whenArrayCountsNotEqual { + uint128[] memory ratesPerSecond = new uint128[](1); + + vm.expectRevert( + abi.encodeWithSelector( + Errors.SablierV2OpenEnded_CreateMultipleArrayCountsNotEqual.selector, + defaultRecipients.length, + defaultSenders.length, + ratesPerSecond.length + ) + ); + openEnded.createMultiple(defaultRecipients, defaultSenders, ratesPerSecond, dai); + } + + modifier whenArrayCountsEqual() { + _; + } + + function test_CreateMultiple() external whenNotDelegateCalled whenArrayCountsEqual { + uint256 beforeNextStreamId = openEnded.nextStreamId(); + + vm.expectEmit({ emitter: address(openEnded) }); + emit CreateOpenEndedStream({ + streamId: beforeNextStreamId, + sender: users.sender, + recipient: users.recipient, + ratePerSecond: RATE_PER_SECOND, + asset: dai, + lastTimeUpdate: uint40(block.timestamp) + }); + vm.expectEmit({ emitter: address(openEnded) }); + emit CreateOpenEndedStream({ + streamId: beforeNextStreamId + 1, + sender: users.sender, + recipient: users.recipient, + ratePerSecond: RATE_PER_SECOND, + asset: dai, + lastTimeUpdate: uint40(block.timestamp) + }); + + uint256[] memory streamIds = + openEnded.createMultiple(defaultRecipients, defaultSenders, defaultRatesPerSecond, dai); + + uint256 afterNextStreamId = openEnded.nextStreamId(); + + assertEq(streamIds[0], beforeNextStreamId, "streamIds[0] != beforeNextStreamId"); + assertEq(streamIds[1], beforeNextStreamId + 1, "streamIds[1] != beforeNextStreamId + 1"); + + assertEq(streamIds.length, defaultRecipients.length, "streamIds.length != defaultRecipients.length"); + assertEq( + beforeNextStreamId + defaultRecipients.length, + afterNextStreamId, + "afterNextStreamId != beforeNextStreamId + defaultRecipients.length" + ); + + OpenEnded.Stream memory expectedStream = OpenEnded.Stream({ + ratePerSecond: RATE_PER_SECOND, + asset: dai, + assetDecimals: 18, + balance: 0, + lastTimeUpdate: uint40(block.timestamp), + isCanceled: false, + isStream: true, + recipient: users.recipient, + sender: users.sender + }); + + OpenEnded.Stream memory actualStream = openEnded.getStream(streamIds[0]); + assertEq(actualStream, expectedStream); + + actualStream = openEnded.getStream(streamIds[1]); + assertEq(actualStream, expectedStream); + } +} diff --git a/test/integration/create-multiple/createMultiple.tree b/test/integration/create-multiple/createMultiple.tree new file mode 100644 index 00000000..fda1e5ac --- /dev/null +++ b/test/integration/create-multiple/createMultiple.tree @@ -0,0 +1,15 @@ +createMultiple.t.sol +├── when delegate called +│ └── it should revert +└── when not delegate called + ├── when arrays counts are not equal + │ ├── when the recipients array is not equal + │ │ └── it should revert + │ ├── when the senders array is not equal + │ │ └── it should revert + │ └── when the rates per second array is not equal + │ └── it should revert + └── when arrays counts are equal + ├── it should create multiple streams + ├── it should bump the next stream id multiple times + └── it should emit multiple {CreateOpenEndedStream} events \ No newline at end of file