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

fix(reallocate): prevent withdraw inconsistent asset #151

Merged
merged 1 commit into from
Oct 5, 2023
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
9 changes: 6 additions & 3 deletions src/MetaMorpho.sol
Original file line number Diff line number Diff line change
Expand Up @@ -214,9 +214,8 @@ contract MetaMorpho is ERC4626, ERC20Permit, Ownable2Step, Multicall, IMetaMorph
/* ONLY RISK MANAGER FUNCTIONS */

function submitCap(MarketParams memory marketParams, uint256 newMarketCap) external onlyRiskManager {
if (marketParams.loanToken != asset()) revert ErrorsLib.InconsistentAsset();

Id id = marketParams.id();
if (marketParams.loanToken != asset()) revert ErrorsLib.InconsistentAsset(id);
if (MORPHO.lastUpdate(id) == 0) revert ErrorsLib.MarketNotCreated();

uint256 marketCap = config[id].cap;
Expand All @@ -243,7 +242,7 @@ contract MetaMorpho is ERC4626, ERC20Permit, Ownable2Step, Multicall, IMetaMorph
uint256 length = newSupplyQueue.length;

for (uint256 i; i < length; ++i) {
if (config[newSupplyQueue[i]].cap == 0) revert ErrorsLib.UnauthorizedMarket();
if (config[newSupplyQueue[i]].cap == 0) revert ErrorsLib.UnauthorizedMarket(newSupplyQueue[i]);
}

supplyQueue = newSupplyQueue;
Expand Down Expand Up @@ -302,6 +301,10 @@ contract MetaMorpho is ERC4626, ERC20Permit, Ownable2Step, Multicall, IMetaMorph
for (uint256 i; i < nbWithdrawn; ++i) {
MarketAllocation memory allocation = withdrawn[i];

if (allocation.marketParams.loanToken != asset()) {
MerlinEgalite marked this conversation as resolved.
Show resolved Hide resolved
revert ErrorsLib.InconsistentAsset(allocation.marketParams.id());
}

(uint256 withdrawnAssets,) = MORPHO.withdraw(
allocation.marketParams, allocation.assets, allocation.shares, address(this), address(this)
);
Expand Down
4 changes: 2 additions & 2 deletions src/libraries/ErrorsLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ library ErrorsLib {

error NotGuardian();

error UnauthorizedMarket();
error UnauthorizedMarket(Id id);

error InconsistentAsset();
error InconsistentAsset(Id id);

error SupplyCapExceeded(Id id);

Expand Down
2 changes: 1 addition & 1 deletion test/forge/MarketTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ contract MarketTest is BaseTest {
vm.assume(marketParams.loanToken != address(loanToken));

vm.prank(RISK_MANAGER);
vm.expectRevert(ErrorsLib.InconsistentAsset.selector);
vm.expectRevert(abi.encodeWithSelector(ErrorsLib.InconsistentAsset.selector, marketParams.id()));
vault.submitCap(marketParams, 0);
}

Expand Down
10 changes: 10 additions & 0 deletions test/forge/ReallocateWithdrawTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ contract ReallocateWithdrawTest is BaseTest {
assertEq(vault.idle(), INITIAL_DEPOSIT, "vault.idle() 1");
}

function testReallocateWithdrawInconsistentAsset() public {
allMarkets[0].loanToken = address(1);

withdrawn.push(MarketAllocation(allMarkets[0], 0, 0));

vm.prank(ALLOCATOR);
vm.expectRevert(abi.encodeWithSelector(ErrorsLib.InconsistentAsset.selector, allMarkets[0].id()));
vault.reallocate(withdrawn, supplied);
}

function testReallocateWithdrawSupply(uint256[3] memory withdrawnShares, uint256[3] memory suppliedAssets) public {
uint256[3] memory sharesBefore = [
morpho.supplyShares(allMarkets[0].id(), address(vault)),
Expand Down