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

Implement new allocation mechanism #17

Merged
merged 11 commits into from
Sep 6, 2023
Merged

Conversation

MerlinEgalite
Copy link
Contributor

@MerlinEgalite MerlinEgalite commented Sep 5, 2023

Fixes #14

For now, _withdrawOrder and _supplyOrder are not tested yet. I will but in a dedicated PR with tests for basic supply and withdraw on top later on.

The PR also adds a basic test setup.

@@ -1,8 +1,7 @@
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity 0.8.21;
pragma solidity 0.8.19;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Else tests can't be run because Morpho must be deployed

@MerlinEgalite MerlinEgalite marked this pull request as ready for review September 5, 2023 15:11
Comment on lines +361 to +370
function _withdrawable(uint256 assets, Id id)
internal
view
returns (MarketParams memory marketParams, uint256 withdrawable)
{
marketParams = _config.at(_config.getMarket(id).rank);
(uint256 totalSupply,, uint256 totalBorrow,) = _MORPHO.expectedMarketBalances(marketParams);
uint256 available = totalBorrow - totalSupply;
withdrawable = UtilsLib.min(available, assets);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
function _withdrawable(uint256 assets, Id id)
internal
view
returns (MarketParams memory marketParams, uint256 withdrawable)
{
marketParams = _config.at(_config.getMarket(id).rank);
(uint256 totalSupply,, uint256 totalBorrow,) = _MORPHO.expectedMarketBalances(marketParams);
uint256 available = totalBorrow - totalSupply;
withdrawable = UtilsLib.min(available, assets);
}
function _withdrawable(uint256 assets, Id id)
internal
view
returns (MarketParams memory marketParams, uint256 withdrawable)
{
marketParams = _config.at(_config.getMarket(id).rank);
(uint256 totalSupply,, uint256 totalBorrow,) = _MORPHO.expectedMarketBalances(marketParams);
uint256 liquidity = totalBorrow - totalSupply;
uint256 withdrawableNoRevert = UtilsLib.min(liquidity, _supplyBalance(marketParams));
withdrawable = UtilsLib.min(withdrawableNoRevert, assets);
}

Copy link
Contributor

@Jean-Grimal Jean-Grimal Sep 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and maybe, if the entire position is withdrawn on a market, the market might be placed at the end of the withdrawAllocationOrder (really not sure about this, but I think it is worth thinking about this).

Comment on lines +299 to +303
if (cap > 0) {
uint256 currentSupply = _supplyBalance(marketParams);

toDeposit = UtilsLib.min(cap.zeroFloorSub(currentSupply), assets);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, if cap is exceeded, we could think about putting the market at the end of supplyAllocationOrder

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure we should modify the list like that


_config.update(marketParams, marketConfig);
require(_config.update(marketParams, marketConfig), ErrorsLib.CONFIG_UDPATE_FAILED);
Copy link
Contributor

@Jean-Grimal Jean-Grimal Sep 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
require(_config.update(marketParams, marketConfig), ErrorsLib.CONFIG_UDPATE_FAILED);
_config.update(marketParams, marketConfig);

Otherwise it is only possible to add a new market to the config (so it is impossible to just update the cap of a market).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will be changed in #22

Comment on lines +20 to +21

string internal constant CONFIG_UDPATE_FAILED = "config update failed";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
string internal constant CONFIG_UDPATE_FAILED = "config update failed";

This error is not usefull then

Comment on lines +327 to +331
bytes memory encodedCall =
abi.encodeCall(_MORPHO.withdraw, (marketParams, toWithdraw, 0, address(this), address(this)));
(bool success,) = address(_MORPHO).staticcall(encodedCall);

if (success) assets -= toWithdraw;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
bytes memory encodedCall =
abi.encodeCall(_MORPHO.withdraw, (marketParams, toWithdraw, 0, address(this), address(this)));
(bool success,) = address(_MORPHO).staticcall(encodedCall);
if (success) assets -= toWithdraw;
bytes memory encodedCall;
if (toWithdraw >= assets) {
uint256 totalShares = _MORPHO.supplyShares(marketParams.id(), address(this));
encodedCall = abi.encodeCall(_MORPHO.withdraw, (marketParams, 0, totalShares, address(this), address(this)));
} else {
encodedCall = abi.encodeCall(_MORPHO.withdraw, (marketParams, toWithdraw, 0, address(this), address(this)));
}
(bool success, bytes memory data) = address(_MORPHO).staticcall(encodedCall);
(uint256 withdrawn,) = abi.decode(data);
if (success) assets -= withdrawn;

Comment on lines +348 to +352
bytes memory encodedCall =
abi.encodeCall(_MORPHO.withdraw, (marketParams, toWithdraw, 0, address(this), address(this)));
(bool success,) = address(_MORPHO).call(encodedCall);

if (success) assets -= toWithdraw;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
bytes memory encodedCall =
abi.encodeCall(_MORPHO.withdraw, (marketParams, toWithdraw, 0, address(this), address(this)));
(bool success,) = address(_MORPHO).call(encodedCall);
if (success) assets -= toWithdraw;
bytes memory encodedCall;
if (toWithdraw >= assets) {
uint256 totalShares = _MORPHO.supplyShares(marketParams.id(), address(this));
encodedCall = abi.encodeCall(_MORPHO.withdraw, (marketParams, 0, totalShares, address(this), address(this)));
} else {
encodedCall = abi.encodeCall(_MORPHO.withdraw, (marketParams, toWithdraw, 0, address(this), address(this)));
}
(bool success, bytes memory data) = address(_MORPHO).call(encodedCall);
(uint256 withdrawn,) = abi.decode(data);
if (success) assets -= withdrawn;

@MerlinEgalite MerlinEgalite merged commit 8da9ae4 into main Sep 6, 2023
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Strategies update
2 participants