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

refactor(reallocate): use sum of assets #114

Merged
merged 3 commits into from
Sep 29, 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
25 changes: 18 additions & 7 deletions src/MetaMorpho.sol
Original file line number Diff line number Diff line change
Expand Up @@ -283,33 +283,44 @@ contract MetaMorpho is ERC4626, ERC20Permit, Ownable2Step, Multicall, IMetaMorph
external
onlyAllocator
{
uint256 balanceBefore = ERC20(asset()).balanceOf(address(this));

uint256 totalWithdrawn;
uint256 nbWithdrawn = withdrawn.length;

for (uint256 i; i < nbWithdrawn; ++i) {
MarketAllocation memory allocation = withdrawn[i];

MORPHO.withdraw(allocation.marketParams, allocation.assets, allocation.shares, address(this), address(this));
(uint256 withdrawnAssets,) = MORPHO.withdraw(
allocation.marketParams, allocation.assets, allocation.shares, address(this), address(this)
);

totalWithdrawn += withdrawnAssets;
}

uint256 totalSupplied;
uint256 nbSupplied = supplied.length;

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

MORPHO.supply(allocation.marketParams, allocation.assets, allocation.shares, address(this), hex"");
(uint256 suppliedAssets,) =
MORPHO.supply(allocation.marketParams, allocation.assets, allocation.shares, address(this), hex"");

totalSupplied += suppliedAssets;

require(
_supplyBalance(allocation.marketParams) <= config[allocation.marketParams.id()].cap,
ErrorsLib.SUPPLY_CAP_EXCEEDED
);
}

uint256 balanceAfter = ERC20(asset()).balanceOf(address(this));
if (totalWithdrawn > totalSupplied) {
idle += totalWithdrawn - totalSupplied;
} else {
uint256 idleSupplied = totalSupplied - totalWithdrawn;
require(idle >= idleSupplied, ErrorsLib.INSUFFICIENT_IDLE);

if (balanceAfter > balanceBefore) idle += balanceAfter - balanceBefore;
else idle -= balanceBefore - balanceAfter;
idle -= idleSupplied;
}
}

/* EXTERNAL */
Expand Down
2 changes: 2 additions & 0 deletions src/libraries/ErrorsLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,6 @@ library ErrorsLib {
string internal constant MAX_QUEUE_SIZE_EXCEEDED = "max queue size exceeded";

string internal constant ZERO_FEE_RECIPIENT = "fee recipient is zero";

string internal constant INSUFFICIENT_IDLE = "insufficient idle liquidity";
}
Loading