diff --git a/src/PublicAllocator.sol b/src/PublicAllocator.sol index 404983a..08b6814 100644 --- a/src/PublicAllocator.sol +++ b/src/PublicAllocator.sol @@ -89,16 +89,13 @@ contract PublicAllocator is IPublicAllocatorStaticTyping { Id depositMarketId = depositMarketParams.id(); uint128 totalWithdrawn; + Id id; + Id oldId; for (uint256 i = 0; i < withdrawals.length; i++) { - Id id = withdrawals[i].marketParams.id(); - - // Revert if the market is elsewhere in the list, or is the deposit market. - for (uint256 j = i + 1; j < withdrawals.length; j++) { - if (Id.unwrap(id) == Id.unwrap(withdrawals[j].marketParams.id())) { - revert ErrorsLib.InconsistentWithdrawTo(); - } - } - if (Id.unwrap(id) == Id.unwrap(depositMarketId)) revert ErrorsLib.InconsistentWithdrawTo(); + oldId = id; + id = withdrawals[i].marketParams.id(); + if (Id.unwrap(id) <= Id.unwrap(oldId)) revert ErrorsLib.InconsistentWithdrawals(); + if (Id.unwrap(id) == Id.unwrap(depositMarketId)) revert ErrorsLib.DepositMarketInWithdrawals(); uint128 withdrawnAssets = withdrawals[i].amount; totalWithdrawn += withdrawnAssets; diff --git a/src/libraries/ErrorsLib.sol b/src/libraries/ErrorsLib.sol index f8a3b9c..e6462b1 100644 --- a/src/libraries/ErrorsLib.sol +++ b/src/libraries/ErrorsLib.sol @@ -39,8 +39,11 @@ library ErrorsLib { /// @notice Thrown when the value is already set. error AlreadySet(); - /// @notice Thrown when there are duplicates with nonzero assets in `withdrawTo` arguments. - error InconsistentWithdrawTo(); + /// @notice Thrown when `withdrawals` contains a duplicate or is not sorted. + error InconsistentWithdrawals(); + + /// @notice Thrown when the deposit market is in `withdrawals`. + error DepositMarketInWithdrawals(); /// @notice Thrown when attempting to set max inflow/outflow above the MAX_SETTABLE_FLOW_CAP. error MaxSettableFlowCapExceeded(); diff --git a/test/PublicAllocatorTest.sol b/test/PublicAllocatorTest.sol index a9a957d..2e6282b 100644 --- a/test/PublicAllocatorTest.sol +++ b/test/PublicAllocatorTest.sol @@ -448,7 +448,7 @@ contract PublicAllocatorTest is IntegrationTest { // _setCap(allMarkets[1], CAP2); withdrawals.push(Withdrawal(idleParams, 1e18)); withdrawals.push(Withdrawal(idleParams, 1e18)); - vm.expectRevert(ErrorsLib.InconsistentWithdrawTo.selector); + vm.expectRevert(ErrorsLib.InconsistentWithdrawals.selector); publicAllocator.withdrawTo(withdrawals, allMarkets[0]); } @@ -460,7 +460,7 @@ contract PublicAllocatorTest is IntegrationTest { publicAllocator.setFlowCaps(flowCaps); withdrawals.push(Withdrawal(idleParams, 1e18)); - vm.expectRevert(ErrorsLib.InconsistentWithdrawTo.selector); + vm.expectRevert(ErrorsLib.DepositMarketInWithdrawals.selector); publicAllocator.withdrawTo(withdrawals, idleParams); }