-
Notifications
You must be signed in to change notification settings - Fork 27
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
Conversation
@@ -1,8 +1,7 @@ | |||
// SPDX-License-Identifier: GPL-2.0-or-later | |||
pragma solidity 0.8.21; | |||
pragma solidity 0.8.19; |
There was a problem hiding this comment.
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
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); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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); | |
} |
There was a problem hiding this comment.
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).
if (cap > 0) { | ||
uint256 currentSupply = _supplyBalance(marketParams); | ||
|
||
toDeposit = UtilsLib.min(cap.zeroFloorSub(currentSupply), assets); | ||
} |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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).
There was a problem hiding this comment.
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
|
||
string internal constant CONFIG_UDPATE_FAILED = "config update failed"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
string internal constant CONFIG_UDPATE_FAILED = "config update failed"; |
This error is not usefull then
bytes memory encodedCall = | ||
abi.encodeCall(_MORPHO.withdraw, (marketParams, toWithdraw, 0, address(this), address(this))); | ||
(bool success,) = address(_MORPHO).staticcall(encodedCall); | ||
|
||
if (success) assets -= toWithdraw; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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; |
bytes memory encodedCall = | ||
abi.encodeCall(_MORPHO.withdraw, (marketParams, toWithdraw, 0, address(this), address(this))); | ||
(bool success,) = address(_MORPHO).call(encodedCall); | ||
|
||
if (success) assets -= toWithdraw; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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; |
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.