Skip to content

Commit

Permalink
perf: sorted withdrawals
Browse files Browse the repository at this point in the history
  • Loading branch information
MathisGD committed Feb 16, 2024
1 parent 17f4b84 commit deea123
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
15 changes: 6 additions & 9 deletions src/PublicAllocator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 5 additions & 2 deletions src/libraries/ErrorsLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions test/PublicAllocatorTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}

Expand All @@ -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);
}

Expand Down

0 comments on commit deea123

Please sign in to comment.