Skip to content

Commit

Permalink
feat: allow to put shares or assets
Browse files Browse the repository at this point in the history
  • Loading branch information
MerlinEgalite committed Sep 25, 2023
1 parent 11ff4e6 commit ab9be38
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 17 deletions.
4 changes: 2 additions & 2 deletions src/MetaMorpho.sol
Original file line number Diff line number Diff line change
Expand Up @@ -290,15 +290,15 @@ contract MetaMorpho is ERC4626, ERC20Permit, Ownable2Step, Multicall, IMetaMorph
for (uint256 i; i < nbWithdrawn; ++i) {
MarketAllocation memory allocation = withdrawn[i];

MORPHO.withdraw(allocation.marketParams, 0, allocation.shares, address(this), address(this));
MORPHO.withdraw(allocation.marketParams, allocation.assets, allocation.shares, address(this), address(this));
}

uint256 nbSupplied = supplied.length;

for (uint256 i; i < nbSupplied; ++i) {
MarketAllocation memory allocation = supplied[i];

MORPHO.supply(allocation.marketParams, 0, allocation.shares, address(this), hex"");
MORPHO.supply(allocation.marketParams, allocation.assets, allocation.shares, address(this), hex"");

require(
_supplyBalance(allocation.marketParams) <= config[allocation.marketParams.id()].cap,
Expand Down
1 change: 1 addition & 0 deletions src/interfaces/IMetaMorpho.sol
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ struct PendingAddress {

struct MarketAllocation {
MarketParams marketParams;
uint256 assets;
uint256 shares;
}

Expand Down
30 changes: 15 additions & 15 deletions test/forge/ReallocateTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ contract ReallocateTest is BaseTest {
withdraw2 = bound(withdraw2, 1, sharesBefore2);

MarketAllocation[] memory withdrawn = new MarketAllocation[](3);
withdrawn[0] = MarketAllocation(allMarkets[0], withdraw0);
withdrawn[1] = MarketAllocation(allMarkets[1], withdraw1);
withdrawn[2] = MarketAllocation(allMarkets[2], withdraw2);
withdrawn[0] = MarketAllocation(allMarkets[0], 0, withdraw0);
withdrawn[1] = MarketAllocation(allMarkets[1], 0, withdraw1);
withdrawn[2] = MarketAllocation(allMarkets[2], 0, withdraw2);

MarketAllocation[] memory supplied;

Expand Down Expand Up @@ -87,9 +87,9 @@ contract ReallocateTest is BaseTest {
uint256 withdraw2 = morpho.supplyShares(allMarkets[2].id(), address(vault));

MarketAllocation[] memory withdrawn = new MarketAllocation[](3);
withdrawn[0] = MarketAllocation(allMarkets[0], withdraw0);
withdrawn[1] = MarketAllocation(allMarkets[1], withdraw1);
withdrawn[2] = MarketAllocation(allMarkets[2], withdraw2);
withdrawn[0] = MarketAllocation(allMarkets[0], 0, withdraw0);
withdrawn[1] = MarketAllocation(allMarkets[1], 0, withdraw1);
withdrawn[2] = MarketAllocation(allMarkets[2], 0, withdraw2);

MarketAllocation[] memory supplied;

Expand Down Expand Up @@ -118,9 +118,9 @@ contract ReallocateTest is BaseTest {
supplied2 = bound(supplied2, VIRTUAL_SHARES, CAP2 * VIRTUAL_SHARES);

MarketAllocation[] memory supplied = new MarketAllocation[](3);
supplied[0] = MarketAllocation(allMarkets[0], supplied0);
supplied[1] = MarketAllocation(allMarkets[1], supplied1);
supplied[2] = MarketAllocation(allMarkets[2], supplied2);
supplied[0] = MarketAllocation(allMarkets[0], 0, supplied0);
supplied[1] = MarketAllocation(allMarkets[1], 0, supplied1);
supplied[2] = MarketAllocation(allMarkets[2], 0, supplied2);

MarketAllocation[] memory withdrawn;

Expand Down Expand Up @@ -163,17 +163,17 @@ contract ReallocateTest is BaseTest {
withdraw.val2 = bound(withdraw.val2, VIRTUAL_SHARES, sharesBefore.val2);

MarketAllocation[] memory withdrawn = new MarketAllocation[](3);
withdrawn[0] = MarketAllocation(allMarkets[0], withdraw.val0);
withdrawn[1] = MarketAllocation(allMarkets[1], withdraw.val1);
withdrawn[2] = MarketAllocation(allMarkets[2], withdraw.val2);
withdrawn[0] = MarketAllocation(allMarkets[0], 0, withdraw.val0);
withdrawn[1] = MarketAllocation(allMarkets[1], 0, withdraw.val1);
withdrawn[2] = MarketAllocation(allMarkets[2], 0, withdraw.val2);

uint256 expectedIdle;
(supply, expectedIdle) = _boundSupply(withdraw, supply);

MarketAllocation[] memory supplied = new MarketAllocation[](3);
supplied[0] = MarketAllocation(allMarkets[0], supply.val0);
supplied[1] = MarketAllocation(allMarkets[1], supply.val1);
supplied[2] = MarketAllocation(allMarkets[2], supply.val2);
supplied[0] = MarketAllocation(allMarkets[0], 0, supply.val0);
supplied[1] = MarketAllocation(allMarkets[1], 0, supply.val1);
supplied[2] = MarketAllocation(allMarkets[2], 0, supply.val2);

vm.prank(ALLOCATOR);
vault.reallocate(withdrawn, supplied);
Expand Down

0 comments on commit ab9be38

Please sign in to comment.