feat: optimise

This commit is contained in:
Shriya Tyagi 2023-12-11 17:46:59 +05:30
parent d3458ee7e3
commit 001a4777e6
2 changed files with 21 additions and 10 deletions

View File

@ -22,17 +22,28 @@ abstract contract Helpers is Stores, Basic {
/// high precision computations.
uint256 internal constant VIRTUAL_SHARES = 1e6;
enum Mode {
Collateral,
Repay,
Other // Neither collateral nor repay
}
/// @notice Handles Eth to Weth conversion if assets are provided.
function _performEthToWethConversion(
MarketParams memory _marketParams,
uint256 _assets,
address _onBehalf,
uint256 _getId,
bool _isCollateral,
bool _isRepay
Mode _mode
) internal returns (TokenInterface _tokenContract, uint256 _amt) {
_amt = getUint(_getId, _assets);
bool _isEth = _isCollateral ? _marketParams.collateralToken == ethAddr : _marketParams.loanToken == ethAddr;
bool _isEth;
if (_mode == Mode.Collateral) {
_isEth = _marketParams.collateralToken == ethAddr;
} else {
_isEth = _marketParams.loanToken == ethAddr;
}
// Set the correct token contract
_tokenContract = _isEth ? TokenInterface(wethAddr) : TokenInterface(_marketParams.loanToken);
@ -40,7 +51,7 @@ abstract contract Helpers is Stores, Basic {
// Check for max value
if (_assets == type(uint256).max) {
uint256 _maxAvailable = _isEth ? address(this).balance : _tokenContract.balanceOf(address(this));
if (_isRepay) {
if (_mode == Mode.Repay) {
uint256 _amtDebt = getPaybackBalance(_marketParams, _onBehalf);
_amt = min(_maxAvailable, _amtDebt);
} else {

View File

@ -29,7 +29,7 @@ abstract contract MorphoBlue is Helpers, Events {
(
TokenInterface _tokenContract, // Loan token contract
uint256 _amt
) = _performEthToWethConversion(_marketParams, _assets, address(this), _getId, false, false);
) = _performEthToWethConversion(_marketParams, _assets, address(this), _getId, Mode.Other);
// Approving loan token for supplying
approve(_tokenContract, address(MORPHO_BLUE), _amt);
@ -77,7 +77,7 @@ abstract contract MorphoBlue is Helpers, Events {
(
TokenInterface _tokenContract, // Loan token contract
uint256 _amt
) = _performEthToWethConversion(_marketParams, _assets, _onBehalf, _getId, false, false);
) = _performEthToWethConversion(_marketParams, _assets, _onBehalf, _getId, Mode.Other);
// Approving loan token for supplying
approve(_tokenContract, address(MORPHO_BLUE), _amt);
@ -171,7 +171,7 @@ abstract contract MorphoBlue is Helpers, Events {
(
TokenInterface _tokenContract, // Collateral token contract
uint256 _amt
) = _performEthToWethConversion(_marketParams, _assets, address(this), _getId, true, false);
) = _performEthToWethConversion(_marketParams, _assets, address(this), _getId, Mode.Collateral);
// Approving collateral token
approve(_tokenContract, address(MORPHO_BLUE), _amt);
@ -216,7 +216,7 @@ abstract contract MorphoBlue is Helpers, Events {
(
TokenInterface _tokenContract, // Collateral token contract
uint256 _amt
) = _performEthToWethConversion(_marketParams, _assets, _onBehalf, _getId, true, false);
) = _performEthToWethConversion(_marketParams, _assets, _onBehalf, _getId, Mode.Collateral);
// Approving collateral token
approve(_tokenContract, address(MORPHO_BLUE), _amt);
@ -638,7 +638,7 @@ abstract contract MorphoBlue is Helpers, Events {
(
TokenInterface _tokenContract,
uint256 _amt // Assets final amount to repay
) = _performEthToWethConversion(_marketParams, _assets, address(this), _getId, false, true);
) = _performEthToWethConversion(_marketParams, _assets, address(this), _getId, Mode.Repay);
// Approving loan token for repaying
approve(_tokenContract, address(MORPHO_BLUE), _amt);
@ -692,7 +692,7 @@ abstract contract MorphoBlue is Helpers, Events {
(
TokenInterface _tokenContract,
uint256 _amt // Assets final amount to repay
) = _performEthToWethConversion(_marketParams, _assets, _onBehalf, _getId, false, true);
) = _performEthToWethConversion(_marketParams, _assets, _onBehalf, _getId, Mode.Repay);
// Approving loan token for repaying
approve(_tokenContract, address(MORPHO_BLUE), _amt);