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

Custom overflow errors #16

Merged
merged 4 commits into from
Feb 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
16 changes: 11 additions & 5 deletions src/PublicAllocator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -96,21 +96,27 @@ contract PublicAllocator is IPublicAllocatorStaticTyping {

MORPHO.accrueInterest(withdrawals[i].marketParams);
uint256 assets = MORPHO.expectedSupplyAssets(withdrawals[i].marketParams, address(VAULT));

uint128 withdrawnAssets = withdrawals[i].amount;
totalWithdrawn += withdrawnAssets;

if (flowCap[id].maxOut < withdrawnAssets) revert ErrorsLib.MaxOutflowExceeded(id);
if (assets < withdrawnAssets) revert ErrorsLib.NotEnoughSupply(id);

flowCap[id].maxIn += withdrawnAssets;
flowCap[id].maxOut -= withdrawnAssets;
allocations[i].assets = assets - withdrawnAssets;
allocations[i].marketParams = withdrawals[i].marketParams;
allocations[i].assets = assets - withdrawnAssets;

totalWithdrawn += withdrawnAssets;

emit EventsLib.PublicWithdrawal(id, withdrawnAssets);
}

allocations[withdrawals.length].marketParams = supplyMarketParams;
allocations[withdrawals.length].assets = type(uint256).max;
if (flowCap[supplyMarketId].maxIn < totalWithdrawn) revert ErrorsLib.MaxInflowExceeded(supplyMarketId);

flowCap[supplyMarketId].maxIn -= totalWithdrawn;
flowCap[supplyMarketId].maxOut += totalWithdrawn;
allocations[withdrawals.length].marketParams = supplyMarketParams;
allocations[withdrawals.length].assets = type(uint256).max;

VAULT.reallocate(allocations);

Expand Down
9 changes: 9 additions & 0 deletions src/libraries/ErrorsLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,13 @@ library ErrorsLib {

/// @notice Thrown when the PublicAllocatorFactory is called with a vault not made by the MetaMorphoFactory.
error NotMetaMorpho();

/// @notice Thrown when attempting to withdraw more than the available supply of a market.
error NotEnoughSupply(Id id);

/// @notice Thrown when attempting to withdraw more than the max outflow of a market.
error MaxOutflowExceeded(Id id);

/// @notice Thrown when attempting to supply more than the max inflow of a market.
error MaxInflowExceeded(Id id);
}
48 changes: 46 additions & 2 deletions test/PublicAllocatorTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ contract PublicAllocatorTest is IntegrationTest {
vm.prank(OWNER);
publicAllocator.setFlowCaps(flowCaps);
withdrawals.push(Withdrawal(idleParams, flow));
vm.expectRevert(stdError.arithmeticError);
vm.expectRevert(abi.encodeWithSelector(ErrorsLib.MaxOutflowExceeded.selector, idleParams.id()));
publicAllocator.reallocateTo(withdrawals, allMarkets[0]);
}

Expand All @@ -110,7 +110,7 @@ contract PublicAllocatorTest is IntegrationTest {
vm.prank(OWNER);
publicAllocator.setFlowCaps(flowCaps);
withdrawals.push(Withdrawal(idleParams, flow));
vm.expectRevert(stdError.arithmeticError);
vm.expectRevert(abi.encodeWithSelector(ErrorsLib.MaxInflowExceeded.selector, allMarkets[0].id()));
publicAllocator.reallocateTo(withdrawals, allMarkets[0]);
}

Expand Down Expand Up @@ -507,6 +507,50 @@ contract PublicAllocatorTest is IntegrationTest {
publicAllocator.setFlowCaps(flowCaps);
}

function testNotEnoughSupply() public {
uint128 flow = 1e18;
// Set flow limits with withdraw market's maxIn to max
flowCaps.push(FlowConfig(idleParams.id(), FlowCap(MAX_SETTABLE_FLOW_CAP, MAX_SETTABLE_FLOW_CAP)));
flowCaps.push(FlowConfig(allMarkets[0].id(), FlowCap(MAX_SETTABLE_FLOW_CAP, MAX_SETTABLE_FLOW_CAP)));
vm.prank(OWNER);
publicAllocator.setFlowCaps(flowCaps);

withdrawals.push(Withdrawal(idleParams, flow));
publicAllocator.reallocateTo(withdrawals, allMarkets[0]);

delete withdrawals;

withdrawals.push(Withdrawal(allMarkets[0], flow + 1));
vm.expectRevert(abi.encodeWithSelector(ErrorsLib.NotEnoughSupply.selector, allMarkets[0].id()));
publicAllocator.reallocateTo(withdrawals, idleParams);
}

function testMaxOutflowExceeded() public {
uint128 cap = 1e18;
// Set flow limits with withdraw market's maxIn to max
flowCaps.push(FlowConfig(idleParams.id(), FlowCap(MAX_SETTABLE_FLOW_CAP, cap)));
flowCaps.push(FlowConfig(allMarkets[0].id(), FlowCap(MAX_SETTABLE_FLOW_CAP, MAX_SETTABLE_FLOW_CAP)));
vm.prank(OWNER);
publicAllocator.setFlowCaps(flowCaps);

withdrawals.push(Withdrawal(idleParams, cap + 1));
vm.expectRevert(abi.encodeWithSelector(ErrorsLib.MaxOutflowExceeded.selector, idleParams.id()));
publicAllocator.reallocateTo(withdrawals, allMarkets[0]);
}

function testMaxInflowExceeded() public {
uint128 cap = 1e18;
// Set flow limits with withdraw market's maxIn to max
flowCaps.push(FlowConfig(idleParams.id(), FlowCap(MAX_SETTABLE_FLOW_CAP, MAX_SETTABLE_FLOW_CAP)));
flowCaps.push(FlowConfig(allMarkets[0].id(), FlowCap(cap, MAX_SETTABLE_FLOW_CAP)));
vm.prank(OWNER);
publicAllocator.setFlowCaps(flowCaps);

withdrawals.push(Withdrawal(idleParams, cap + 1));
vm.expectRevert(abi.encodeWithSelector(ErrorsLib.MaxInflowExceeded.selector, allMarkets[0].id()));
publicAllocator.reallocateTo(withdrawals, allMarkets[0]);
}

function testReallocateToNotSorted() public {
// Prepare public reallocation from 2 markets to 1
_setCap(allMarkets[1], CAP2);
Expand Down