diff --git a/src/PublicAllocator.sol b/src/PublicAllocator.sol index 10cb299..5325c0d 100644 --- a/src/PublicAllocator.sol +++ b/src/PublicAllocator.sol @@ -99,24 +99,26 @@ 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; - flowCap[id].maxIn += withdrawnAssets; + if (flowCap[id].maxOut < withdrawnAssets) revert ErrorsLib.MaxOutflowExceeded(id); + + flowCap[id].maxIn += withdrawnAssets; flowCap[id].maxOut -= withdrawnAssets; - if (assets < withdrawnAssets) revert ErrorsLib.NotEnoughSupply(id); allocations[i].assets = assets - withdrawnAssets; allocations[i].marketParams = withdrawals[i].marketParams; + 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); diff --git a/src/libraries/ErrorsLib.sol b/src/libraries/ErrorsLib.sol index 274a0e1..62b86a8 100644 --- a/src/libraries/ErrorsLib.sol +++ b/src/libraries/ErrorsLib.sol @@ -32,9 +32,6 @@ 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); diff --git a/test/PublicAllocatorTest.sol b/test/PublicAllocatorTest.sol index 4f23a64..1111de8 100644 --- a/test/PublicAllocatorTest.sol +++ b/test/PublicAllocatorTest.sol @@ -485,24 +485,6 @@ 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