Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sorted withdrawals #15

Merged
merged 7 commits into from
Feb 17, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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