mirror of
https://github.com/Instadapp/dsa-connectors.git
synced 2024-07-29 22:37:00 +00:00
feat: update common logics
This commit is contained in:
parent
9bf8613bf6
commit
9b8c7db025
|
@ -34,26 +34,22 @@ abstract contract Helpers is Stores, Basic {
|
||||||
address _onBehalf,
|
address _onBehalf,
|
||||||
uint256 _getId,
|
uint256 _getId,
|
||||||
Mode _mode
|
Mode _mode
|
||||||
) internal returns (TokenInterface _tokenContract, uint256 _amt) {
|
) internal returns (MarketParams memory, uint256 _amt) {
|
||||||
_amt = getUint(_getId, _assets);
|
_amt = getUint(_getId, _assets);
|
||||||
|
|
||||||
bool _isEth;
|
bool _isEth = _mode == Mode.Collateral
|
||||||
if (_mode == Mode.Collateral) {
|
? _marketParams.collateralToken == ethAddr
|
||||||
_isEth = _marketParams.collateralToken == ethAddr;
|
: _marketParams.loanToken == ethAddr;
|
||||||
} else {
|
|
||||||
_isEth = _marketParams.loanToken == ethAddr;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set the correct token contract
|
_marketParams = updateTokenAddresses(_marketParams);
|
||||||
_tokenContract = _isEth
|
|
||||||
? TokenInterface(wethAddr)
|
|
||||||
: TokenInterface(_marketParams.loanToken);
|
|
||||||
|
|
||||||
// Check for max value
|
// Check for max value
|
||||||
if (_assets == type(uint256).max) {
|
if (_assets == type(uint256).max) {
|
||||||
uint256 _maxAvailable = _isEth
|
uint256 _maxAvailable = _isEth
|
||||||
? address(this).balance
|
? address(this).balance
|
||||||
: _tokenContract.balanceOf(address(this));
|
: TokenInterface(_marketParams.loanToken).balanceOf(
|
||||||
|
address(this)
|
||||||
|
);
|
||||||
if (_mode == Mode.Repay) {
|
if (_mode == Mode.Repay) {
|
||||||
uint256 _amtDebt = getPaybackBalance(_marketParams, _onBehalf);
|
uint256 _amtDebt = getPaybackBalance(_marketParams, _onBehalf);
|
||||||
_amt = min(_maxAvailable, _amtDebt);
|
_amt = min(_maxAvailable, _amtDebt);
|
||||||
|
@ -64,8 +60,14 @@ abstract contract Helpers is Stores, Basic {
|
||||||
|
|
||||||
// Perform conversion if necessary
|
// Perform conversion if necessary
|
||||||
if (_isEth) {
|
if (_isEth) {
|
||||||
convertEthToWeth(true, _tokenContract, _amt);
|
convertEthToWeth(
|
||||||
|
true,
|
||||||
|
TokenInterface(_marketParams.loanToken),
|
||||||
|
_amt
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return (_marketParams, _amt);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// @notice Handles Eth to Weth conversion if shares are provided.
|
/// @notice Handles Eth to Weth conversion if shares are provided.
|
||||||
|
@ -75,24 +77,29 @@ abstract contract Helpers is Stores, Basic {
|
||||||
address _onBehalf,
|
address _onBehalf,
|
||||||
uint256 _getId,
|
uint256 _getId,
|
||||||
bool _isRepay
|
bool _isRepay
|
||||||
) internal returns (TokenInterface _tokenContract, uint256 _assets) {
|
) internal returns (MarketParams memory, uint256 _assets) {
|
||||||
uint256 _shareAmt = getUint(_getId, _shares);
|
uint256 _shareAmt = getUint(_getId, _shares);
|
||||||
bool _isEth = _marketParams.loanToken == ethAddr;
|
bool _isEth = _marketParams.loanToken == ethAddr;
|
||||||
|
|
||||||
// Set the token contract based on whether the loan token is ETH
|
_marketParams = updateTokenAddresses(_marketParams);
|
||||||
_tokenContract = _isEth
|
|
||||||
? TokenInterface(wethAddr)
|
|
||||||
: TokenInterface(_marketParams.loanToken);
|
|
||||||
|
|
||||||
// Handle the max share case or normal share conversion
|
// Handle the max share case
|
||||||
if (_isRepay && _shares == type(uint256).max) {
|
if (_shares == type(uint256).max) {
|
||||||
uint256 _maxAvailable = _isEth
|
uint256 _maxAvailable = _isEth
|
||||||
? address(this).balance
|
? address(this).balance
|
||||||
: _tokenContract.balanceOf(address(this));
|
: TokenInterface(_marketParams.loanToken).balanceOf(
|
||||||
_assets = min(
|
address(this)
|
||||||
_maxAvailable,
|
);
|
||||||
getPaybackBalance(_marketParams, _onBehalf)
|
|
||||||
);
|
// If it's repay calculate the min of balance available and debt to repay
|
||||||
|
if (_isRepay) {
|
||||||
|
_assets = min(
|
||||||
|
_maxAvailable,
|
||||||
|
getPaybackBalance(_marketParams, _onBehalf)
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
_assets = _maxAvailable;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
bytes32 _id = id(_marketParams);
|
bytes32 _id = id(_marketParams);
|
||||||
_assets = _toAssetsUp(
|
_assets = _toAssetsUp(
|
||||||
|
@ -104,8 +111,14 @@ abstract contract Helpers is Stores, Basic {
|
||||||
|
|
||||||
// Perform ETH to WETH conversion if necessary
|
// Perform ETH to WETH conversion if necessary
|
||||||
if (_isEth) {
|
if (_isEth) {
|
||||||
convertEthToWeth(true, _tokenContract, _assets);
|
convertEthToWeth(
|
||||||
|
true,
|
||||||
|
TokenInterface(_marketParams.loanToken),
|
||||||
|
_assets
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return (_marketParams, _assets);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// @notice Helper function to find the minimum of two values
|
/// @notice Helper function to find the minimum of two values
|
||||||
|
@ -163,4 +176,18 @@ abstract contract Helpers is Stores, Basic {
|
||||||
) internal pure returns (uint256) {
|
) internal pure returns (uint256) {
|
||||||
return (x * y + (d - 1)) / d;
|
return (x * y + (d - 1)) / d;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function updateTokenAddresses(
|
||||||
|
MarketParams memory _marketParams
|
||||||
|
) internal returns (MarketParams memory) {
|
||||||
|
_marketParams.loanToken = _marketParams.loanToken == ethAddr
|
||||||
|
? wethAddr
|
||||||
|
: _marketParams.loanToken;
|
||||||
|
|
||||||
|
_marketParams.collateralToken = _marketParams.collateralToken == ethAddr
|
||||||
|
? wethAddr
|
||||||
|
: _marketParams.collateralToken;
|
||||||
|
|
||||||
|
return _marketParams;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,26 +24,23 @@ abstract contract MorphoBlue is Helpers, Events {
|
||||||
payable
|
payable
|
||||||
returns (string memory _eventName, bytes memory _eventParam)
|
returns (string memory _eventName, bytes memory _eventParam)
|
||||||
{
|
{
|
||||||
// Final assets amount and token contract
|
uint256 _amt;
|
||||||
(
|
(
|
||||||
TokenInterface _tokenContract, // Loan token contract
|
_marketParams, // Updated token contracts in case of Eth
|
||||||
uint256 _amt
|
_amt
|
||||||
) = _performEthToWethConversion(
|
) = _performEthToWethConversion(
|
||||||
_marketParams,
|
_marketParams,
|
||||||
_assets,
|
_assets,
|
||||||
address(this),
|
address(this),
|
||||||
_getId,
|
_getId,
|
||||||
Mode.Other
|
Mode.Other
|
||||||
);
|
);
|
||||||
|
|
||||||
// Approving loan token for supplying
|
approve(
|
||||||
approve(_tokenContract, address(MORPHO_BLUE), _amt);
|
TokenInterface(_marketParams.loanToken),
|
||||||
|
address(MORPHO_BLUE),
|
||||||
// Updating token addresses
|
_amt
|
||||||
_marketParams.loanToken = address(_tokenContract);
|
);
|
||||||
_marketParams.collateralToken = _marketParams.collateralToken == ethAddr
|
|
||||||
? wethAddr
|
|
||||||
: _marketParams.collateralToken;
|
|
||||||
|
|
||||||
uint256 _shares;
|
uint256 _shares;
|
||||||
(_assets, _shares) = MORPHO_BLUE.supply(
|
(_assets, _shares) = MORPHO_BLUE.supply(
|
||||||
|
@ -86,26 +83,23 @@ abstract contract MorphoBlue is Helpers, Events {
|
||||||
payable
|
payable
|
||||||
returns (string memory _eventName, bytes memory _eventParam)
|
returns (string memory _eventName, bytes memory _eventParam)
|
||||||
{
|
{
|
||||||
// Final assets amount and token contract
|
uint256 _amt;
|
||||||
(
|
(
|
||||||
TokenInterface _tokenContract, // Loan token contract
|
_marketParams, // Updated token contracts in case of Eth
|
||||||
uint256 _amt
|
_amt
|
||||||
) = _performEthToWethConversion(
|
) = _performEthToWethConversion(
|
||||||
_marketParams,
|
_marketParams,
|
||||||
_assets,
|
_assets,
|
||||||
_onBehalf,
|
_onBehalf,
|
||||||
_getId,
|
_getId,
|
||||||
Mode.Other
|
Mode.Other
|
||||||
);
|
);
|
||||||
|
|
||||||
// Approving loan token for supplying
|
approve(
|
||||||
approve(_tokenContract, address(MORPHO_BLUE), _amt);
|
TokenInterface(_marketParams.loanToken),
|
||||||
|
address(MORPHO_BLUE),
|
||||||
// Updating token addresses
|
_amt
|
||||||
_marketParams.loanToken = address(_tokenContract);
|
);
|
||||||
_marketParams.collateralToken = _marketParams.collateralToken == ethAddr
|
|
||||||
? wethAddr
|
|
||||||
: _marketParams.collateralToken;
|
|
||||||
|
|
||||||
uint256 _shares;
|
uint256 _shares;
|
||||||
(_assets, _shares) = MORPHO_BLUE.supply(
|
(_assets, _shares) = MORPHO_BLUE.supply(
|
||||||
|
@ -133,7 +127,7 @@ abstract contract MorphoBlue is Helpers, Events {
|
||||||
* @dev Supply ETH/ERC20 Token for lending.
|
* @dev Supply ETH/ERC20 Token for lending.
|
||||||
* @notice Supplies assets for a perfect share amount to Morpho Blue for lending.
|
* @notice Supplies assets for a perfect share amount to Morpho Blue for lending.
|
||||||
* @param _marketParams The market to supply assets to. (For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
|
* @param _marketParams The market to supply assets to. (For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
|
||||||
* @param _shares The exact amount of shares to mint. (Max maount not allowed)
|
* @param _shares The exact amount of shares to mint. (For max: `uint256(-1)`)
|
||||||
* @param _onBehalf The address that will get the shares.
|
* @param _onBehalf The address that will get the shares.
|
||||||
* @param _getId ID to retrieve amt.
|
* @param _getId ID to retrieve amt.
|
||||||
* @param _setId ID stores the amount of tokens deposited.
|
* @param _setId ID stores the amount of tokens deposited.
|
||||||
|
@ -149,26 +143,23 @@ abstract contract MorphoBlue is Helpers, Events {
|
||||||
payable
|
payable
|
||||||
returns (string memory _eventName, bytes memory _eventParam)
|
returns (string memory _eventName, bytes memory _eventParam)
|
||||||
{
|
{
|
||||||
// Final converted assets amount for approval and token contract
|
uint256 _amt;
|
||||||
(
|
(
|
||||||
TokenInterface _tokenContract, // Loan token contract
|
_marketParams, // Updated token contracts in case of Eth
|
||||||
uint256 _amt // Shares amount converted to assets
|
_amt // Shares amount converted to assets
|
||||||
) = _performEthToWethSharesConversion(
|
) = _performEthToWethSharesConversion(
|
||||||
_marketParams,
|
_marketParams,
|
||||||
_shares,
|
_shares,
|
||||||
_onBehalf,
|
_onBehalf,
|
||||||
_getId,
|
_getId,
|
||||||
false
|
false
|
||||||
);
|
);
|
||||||
|
|
||||||
// Approving loan token for supplying
|
approve(
|
||||||
approve(_tokenContract, address(MORPHO_BLUE), _amt);
|
TokenInterface(_marketParams.loanToken),
|
||||||
|
address(MORPHO_BLUE),
|
||||||
// Updating token addresses
|
_amt
|
||||||
_marketParams.loanToken = address(_tokenContract);
|
);
|
||||||
_marketParams.collateralToken = _marketParams.collateralToken == ethAddr
|
|
||||||
? wethAddr
|
|
||||||
: _marketParams.collateralToken;
|
|
||||||
|
|
||||||
(uint256 _assets, ) = MORPHO_BLUE.supply(
|
(uint256 _assets, ) = MORPHO_BLUE.supply(
|
||||||
_marketParams,
|
_marketParams,
|
||||||
|
@ -208,26 +199,24 @@ abstract contract MorphoBlue is Helpers, Events {
|
||||||
payable
|
payable
|
||||||
returns (string memory _eventName, bytes memory _eventParam)
|
returns (string memory _eventName, bytes memory _eventParam)
|
||||||
{
|
{
|
||||||
// Final assets amount and token contract
|
uint256 _amt;
|
||||||
(
|
(
|
||||||
TokenInterface _tokenContract, // Collateral token contract
|
_marketParams, // Updated token contracts in case of Eth
|
||||||
uint256 _amt
|
_amt
|
||||||
) = _performEthToWethConversion(
|
) = _performEthToWethConversion(
|
||||||
_marketParams,
|
_marketParams,
|
||||||
_assets,
|
_assets,
|
||||||
address(this),
|
address(this),
|
||||||
_getId,
|
_getId,
|
||||||
Mode.Collateral
|
Mode.Collateral
|
||||||
);
|
);
|
||||||
|
|
||||||
// Approving collateral token
|
// Approving collateral token
|
||||||
approve(_tokenContract, address(MORPHO_BLUE), _amt);
|
approve(
|
||||||
|
TokenInterface(_marketParams.collateralToken),
|
||||||
// Updating token addresses
|
address(MORPHO_BLUE),
|
||||||
_marketParams.collateralToken = address(_tokenContract);
|
_amt
|
||||||
_marketParams.loanToken = _marketParams.loanToken == ethAddr
|
);
|
||||||
? wethAddr
|
|
||||||
: _marketParams.loanToken;
|
|
||||||
|
|
||||||
MORPHO_BLUE.supplyCollateral(
|
MORPHO_BLUE.supplyCollateral(
|
||||||
_marketParams,
|
_marketParams,
|
||||||
|
@ -261,26 +250,25 @@ abstract contract MorphoBlue is Helpers, Events {
|
||||||
payable
|
payable
|
||||||
returns (string memory _eventName, bytes memory _eventParam)
|
returns (string memory _eventName, bytes memory _eventParam)
|
||||||
{
|
{
|
||||||
|
uint256 _amt;
|
||||||
// Final assets amount and token contract
|
// Final assets amount and token contract
|
||||||
(
|
(
|
||||||
TokenInterface _tokenContract, // Collateral token contract
|
_marketParams, // Updated token contracts in case of Eth
|
||||||
uint256 _amt
|
_amt
|
||||||
) = _performEthToWethConversion(
|
) = _performEthToWethConversion(
|
||||||
_marketParams,
|
_marketParams,
|
||||||
_assets,
|
_assets,
|
||||||
_onBehalf,
|
_onBehalf,
|
||||||
_getId,
|
_getId,
|
||||||
Mode.Collateral
|
Mode.Collateral
|
||||||
);
|
);
|
||||||
|
|
||||||
// Approving collateral token
|
// Approving collateral token
|
||||||
approve(_tokenContract, address(MORPHO_BLUE), _amt);
|
approve(
|
||||||
|
TokenInterface(_marketParams.collateralToken),
|
||||||
// Updating token addresses
|
address(MORPHO_BLUE),
|
||||||
_marketParams.collateralToken = address(_tokenContract);
|
_amt
|
||||||
_marketParams.loanToken = _marketParams.loanToken == ethAddr
|
);
|
||||||
? wethAddr
|
|
||||||
: _marketParams.loanToken;
|
|
||||||
|
|
||||||
MORPHO_BLUE.supplyCollateral(
|
MORPHO_BLUE.supplyCollateral(
|
||||||
_marketParams,
|
_marketParams,
|
||||||
|
@ -302,7 +290,7 @@ abstract contract MorphoBlue is Helpers, Events {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @notice Handles the withdrawal of collateral by a user from a specific market of a specific amount.
|
* @notice Handles the collateral withdrawals.
|
||||||
* @dev The market to withdraw assets from. (For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
|
* @dev The market to withdraw assets from. (For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
|
||||||
* @param _marketParams The market to withdraw assets from.
|
* @param _marketParams The market to withdraw assets from.
|
||||||
* @param _assets The amount of assets to withdraw. (For max: `uint256(-1)`)
|
* @param _assets The amount of assets to withdraw. (For max: `uint256(-1)`)
|
||||||
|
@ -321,14 +309,7 @@ abstract contract MorphoBlue is Helpers, Events {
|
||||||
{
|
{
|
||||||
uint256 _amt = getUint(_getId, _assets);
|
uint256 _amt = getUint(_getId, _assets);
|
||||||
|
|
||||||
// Updating token addresses
|
_marketParams = updateTokenAddresses(_marketParams);
|
||||||
bool _collateralIsEth = _marketParams.collateralToken == ethAddr;
|
|
||||||
_marketParams.collateralToken = _collateralIsEth
|
|
||||||
? wethAddr
|
|
||||||
: _marketParams.collateralToken;
|
|
||||||
_marketParams.loanToken = _marketParams.loanToken == ethAddr
|
|
||||||
? wethAddr
|
|
||||||
: _marketParams.loanToken;
|
|
||||||
|
|
||||||
// If amount is max, fetch collateral value from Morpho's contract
|
// If amount is max, fetch collateral value from Morpho's contract
|
||||||
if (_amt == type(uint256).max) {
|
if (_amt == type(uint256).max) {
|
||||||
|
@ -344,7 +325,11 @@ abstract contract MorphoBlue is Helpers, Events {
|
||||||
address(this)
|
address(this)
|
||||||
);
|
);
|
||||||
|
|
||||||
convertWethToEth(_collateralIsEth, TokenInterface(wethAddr), _amt);
|
convertWethToEth(
|
||||||
|
_marketParams.collateralToken == ethAddr,
|
||||||
|
TokenInterface(wethAddr),
|
||||||
|
_amt
|
||||||
|
);
|
||||||
|
|
||||||
setUint(_setId, _amt);
|
setUint(_setId, _amt);
|
||||||
|
|
||||||
|
@ -375,15 +360,9 @@ abstract contract MorphoBlue is Helpers, Events {
|
||||||
{
|
{
|
||||||
uint256 _amt = getUint(_getId, _assets);
|
uint256 _amt = getUint(_getId, _assets);
|
||||||
|
|
||||||
// Updating token addresses
|
_marketParams = updateTokenAddresses(_marketParams);
|
||||||
bool _collateralIsEth = _marketParams.collateralToken == ethAddr;
|
|
||||||
_marketParams.collateralToken = _collateralIsEth
|
|
||||||
? wethAddr
|
|
||||||
: _marketParams.collateralToken;
|
|
||||||
_marketParams.loanToken = _marketParams.loanToken == ethAddr
|
|
||||||
? wethAddr
|
|
||||||
: _marketParams.loanToken;
|
|
||||||
|
|
||||||
|
// If amount is max, fetch collateral value from Morpho's contract
|
||||||
if (_amt == type(uint256).max) {
|
if (_amt == type(uint256).max) {
|
||||||
bytes32 _id = id(_marketParams);
|
bytes32 _id = id(_marketParams);
|
||||||
Position memory _pos = MORPHO_BLUE.position(_id, _onBehalf);
|
Position memory _pos = MORPHO_BLUE.position(_id, _onBehalf);
|
||||||
|
@ -398,7 +377,11 @@ abstract contract MorphoBlue is Helpers, Events {
|
||||||
);
|
);
|
||||||
|
|
||||||
if (_receiver == address(this))
|
if (_receiver == address(this))
|
||||||
convertWethToEth(_collateralIsEth, TokenInterface(wethAddr), _amt);
|
convertWethToEth(
|
||||||
|
_marketParams.collateralToken == ethAddr,
|
||||||
|
TokenInterface(wethAddr),
|
||||||
|
_amt
|
||||||
|
);
|
||||||
|
|
||||||
setUint(_setId, _amt);
|
setUint(_setId, _amt);
|
||||||
|
|
||||||
|
@ -432,16 +415,14 @@ abstract contract MorphoBlue is Helpers, Events {
|
||||||
returns (string memory _eventName, bytes memory _eventParam)
|
returns (string memory _eventName, bytes memory _eventParam)
|
||||||
{
|
{
|
||||||
uint256 _amt = getUint(_getId, _assets);
|
uint256 _amt = getUint(_getId, _assets);
|
||||||
bool _isEth = _marketParams.loanToken == ethAddr;
|
|
||||||
_marketParams.loanToken = _isEth ? wethAddr : _marketParams.loanToken;
|
_marketParams = updateTokenAddresses(_marketParams);
|
||||||
_marketParams.collateralToken = _marketParams.collateralToken == ethAddr
|
|
||||||
? wethAddr
|
|
||||||
: _marketParams.collateralToken;
|
|
||||||
|
|
||||||
if (_amt == type(uint256).max) {
|
if (_amt == type(uint256).max) {
|
||||||
bytes32 _id = id(_marketParams);
|
bytes32 _id = id(_marketParams);
|
||||||
Position memory _pos = MORPHO_BLUE.position(_id, address(this));
|
Position memory _pos = MORPHO_BLUE.position(_id, address(this));
|
||||||
uint256 _shares = _pos.supplyShares;
|
uint256 _shares = _pos.supplyShares;
|
||||||
|
|
||||||
_amt = _toAssetsUp(
|
_amt = _toAssetsUp(
|
||||||
_shares,
|
_shares,
|
||||||
MORPHO_BLUE.market(_id).totalSupplyAssets,
|
MORPHO_BLUE.market(_id).totalSupplyAssets,
|
||||||
|
@ -458,7 +439,11 @@ abstract contract MorphoBlue is Helpers, Events {
|
||||||
address(this)
|
address(this)
|
||||||
);
|
);
|
||||||
|
|
||||||
convertWethToEth(_isEth, TokenInterface(wethAddr), _assets);
|
convertWethToEth(
|
||||||
|
_marketParams.loanToken == ethAddr,
|
||||||
|
TokenInterface(wethAddr),
|
||||||
|
_assets
|
||||||
|
);
|
||||||
|
|
||||||
setUint(_setId, _assets);
|
setUint(_setId, _assets);
|
||||||
|
|
||||||
|
@ -494,16 +479,14 @@ abstract contract MorphoBlue is Helpers, Events {
|
||||||
returns (string memory _eventName, bytes memory _eventParam)
|
returns (string memory _eventName, bytes memory _eventParam)
|
||||||
{
|
{
|
||||||
uint256 _amt = getUint(_getId, _assets);
|
uint256 _amt = getUint(_getId, _assets);
|
||||||
bool _isEth = _marketParams.loanToken == ethAddr;
|
|
||||||
_marketParams.loanToken = _isEth ? wethAddr : _marketParams.loanToken;
|
_marketParams = updateTokenAddresses(_marketParams);
|
||||||
_marketParams.collateralToken = _marketParams.collateralToken == ethAddr
|
|
||||||
? wethAddr
|
|
||||||
: _marketParams.collateralToken;
|
|
||||||
|
|
||||||
if (_amt == type(uint256).max) {
|
if (_amt == type(uint256).max) {
|
||||||
bytes32 _id = id(_marketParams);
|
bytes32 _id = id(_marketParams);
|
||||||
Position memory _pos = MORPHO_BLUE.position(_id, _onBehalf);
|
Position memory _pos = MORPHO_BLUE.position(_id, _onBehalf);
|
||||||
uint256 _shares = _pos.supplyShares;
|
uint256 _shares = _pos.supplyShares;
|
||||||
|
|
||||||
_amt = _toAssetsUp(
|
_amt = _toAssetsUp(
|
||||||
_shares,
|
_shares,
|
||||||
MORPHO_BLUE.market(_id).totalSupplyAssets,
|
MORPHO_BLUE.market(_id).totalSupplyAssets,
|
||||||
|
@ -521,7 +504,11 @@ abstract contract MorphoBlue is Helpers, Events {
|
||||||
);
|
);
|
||||||
|
|
||||||
if (_receiver == address(this))
|
if (_receiver == address(this))
|
||||||
convertWethToEth(_isEth, TokenInterface(wethAddr), _assets);
|
convertWethToEth(
|
||||||
|
_marketParams.loanToken == ethAddr,
|
||||||
|
TokenInterface(wethAddr),
|
||||||
|
_assets
|
||||||
|
);
|
||||||
|
|
||||||
setUint(_setId, _assets);
|
setUint(_setId, _assets);
|
||||||
|
|
||||||
|
@ -558,11 +545,8 @@ abstract contract MorphoBlue is Helpers, Events {
|
||||||
returns (string memory _eventName, bytes memory _eventParam)
|
returns (string memory _eventName, bytes memory _eventParam)
|
||||||
{
|
{
|
||||||
uint256 _shareAmt = getUint(_getId, _shares);
|
uint256 _shareAmt = getUint(_getId, _shares);
|
||||||
bool _isEth = _marketParams.loanToken == ethAddr;
|
|
||||||
_marketParams.loanToken = _isEth ? wethAddr : _marketParams.loanToken;
|
_marketParams = updateTokenAddresses(_marketParams);
|
||||||
_marketParams.collateralToken = _marketParams.collateralToken == ethAddr
|
|
||||||
? wethAddr
|
|
||||||
: _marketParams.collateralToken;
|
|
||||||
|
|
||||||
if (_shareAmt == type(uint256).max) {
|
if (_shareAmt == type(uint256).max) {
|
||||||
bytes32 _id = id(_marketParams);
|
bytes32 _id = id(_marketParams);
|
||||||
|
@ -579,7 +563,11 @@ abstract contract MorphoBlue is Helpers, Events {
|
||||||
);
|
);
|
||||||
|
|
||||||
if (_receiver == address(this))
|
if (_receiver == address(this))
|
||||||
convertWethToEth(_isEth, TokenInterface(wethAddr), _assets);
|
convertWethToEth(
|
||||||
|
_marketParams.loanToken == ethAddr,
|
||||||
|
TokenInterface(wethAddr),
|
||||||
|
_assets
|
||||||
|
);
|
||||||
|
|
||||||
setUint(_setId, _assets);
|
setUint(_setId, _assets);
|
||||||
|
|
||||||
|
@ -613,12 +601,8 @@ abstract contract MorphoBlue is Helpers, Events {
|
||||||
returns (string memory _eventName, bytes memory _eventParam)
|
returns (string memory _eventName, bytes memory _eventParam)
|
||||||
{
|
{
|
||||||
uint256 _amt = getUint(_getId, _assets);
|
uint256 _amt = getUint(_getId, _assets);
|
||||||
bool _isETH = _marketParams.loanToken == ethAddr;
|
|
||||||
|
|
||||||
_marketParams.loanToken = _isETH ? wethAddr : _marketParams.loanToken;
|
_marketParams = updateTokenAddresses(_marketParams);
|
||||||
_marketParams.collateralToken = _marketParams.collateralToken == ethAddr
|
|
||||||
? wethAddr
|
|
||||||
: _marketParams.collateralToken;
|
|
||||||
|
|
||||||
(, uint256 _shares) = MORPHO_BLUE.borrow(
|
(, uint256 _shares) = MORPHO_BLUE.borrow(
|
||||||
_marketParams,
|
_marketParams,
|
||||||
|
@ -628,7 +612,11 @@ abstract contract MorphoBlue is Helpers, Events {
|
||||||
address(this)
|
address(this)
|
||||||
);
|
);
|
||||||
|
|
||||||
convertWethToEth(_isETH, TokenInterface(wethAddr), _amt);
|
convertWethToEth(
|
||||||
|
_marketParams.loanToken == ethAddr,
|
||||||
|
TokenInterface(wethAddr),
|
||||||
|
_amt
|
||||||
|
);
|
||||||
|
|
||||||
setUint(_setId, _amt);
|
setUint(_setId, _amt);
|
||||||
|
|
||||||
|
@ -659,12 +647,8 @@ abstract contract MorphoBlue is Helpers, Events {
|
||||||
returns (string memory _eventName, bytes memory _eventParam)
|
returns (string memory _eventName, bytes memory _eventParam)
|
||||||
{
|
{
|
||||||
uint256 _amt = getUint(_getId, _assets);
|
uint256 _amt = getUint(_getId, _assets);
|
||||||
bool _isETH = _marketParams.loanToken == ethAddr;
|
|
||||||
|
|
||||||
_marketParams.loanToken = _isETH ? wethAddr : _marketParams.loanToken;
|
_marketParams = updateTokenAddresses(_marketParams);
|
||||||
_marketParams.collateralToken = _marketParams.collateralToken == ethAddr
|
|
||||||
? wethAddr
|
|
||||||
: _marketParams.collateralToken;
|
|
||||||
|
|
||||||
(, uint256 _shares) = MORPHO_BLUE.borrow(
|
(, uint256 _shares) = MORPHO_BLUE.borrow(
|
||||||
_marketParams,
|
_marketParams,
|
||||||
|
@ -675,7 +659,11 @@ abstract contract MorphoBlue is Helpers, Events {
|
||||||
);
|
);
|
||||||
|
|
||||||
if (_receiver == address(this))
|
if (_receiver == address(this))
|
||||||
convertWethToEth(_isETH, TokenInterface(wethAddr), _amt);
|
convertWethToEth(
|
||||||
|
_marketParams.loanToken == ethAddr,
|
||||||
|
TokenInterface(wethAddr),
|
||||||
|
_amt
|
||||||
|
);
|
||||||
|
|
||||||
setUint(_setId, _amt);
|
setUint(_setId, _amt);
|
||||||
|
|
||||||
|
@ -714,12 +702,8 @@ abstract contract MorphoBlue is Helpers, Events {
|
||||||
returns (string memory _eventName, bytes memory _eventParam)
|
returns (string memory _eventName, bytes memory _eventParam)
|
||||||
{
|
{
|
||||||
uint256 _shareAmt = getUint(_getId, _shares);
|
uint256 _shareAmt = getUint(_getId, _shares);
|
||||||
bool _isETH = _marketParams.loanToken == ethAddr;
|
|
||||||
|
|
||||||
_marketParams.loanToken = _isETH ? wethAddr : _marketParams.loanToken;
|
_marketParams = updateTokenAddresses(_marketParams);
|
||||||
_marketParams.collateralToken = _marketParams.collateralToken == ethAddr
|
|
||||||
? wethAddr
|
|
||||||
: _marketParams.collateralToken;
|
|
||||||
|
|
||||||
(uint256 _assets, ) = MORPHO_BLUE.borrow(
|
(uint256 _assets, ) = MORPHO_BLUE.borrow(
|
||||||
_marketParams,
|
_marketParams,
|
||||||
|
@ -730,7 +714,11 @@ abstract contract MorphoBlue is Helpers, Events {
|
||||||
);
|
);
|
||||||
|
|
||||||
if (_receiver == address(this))
|
if (_receiver == address(this))
|
||||||
convertWethToEth(_isETH, TokenInterface(wethAddr), _assets);
|
convertWethToEth(
|
||||||
|
_marketParams.loanToken == ethAddr,
|
||||||
|
TokenInterface(wethAddr),
|
||||||
|
_assets
|
||||||
|
);
|
||||||
|
|
||||||
setUint(_setId, _assets);
|
setUint(_setId, _assets);
|
||||||
|
|
||||||
|
@ -747,7 +735,7 @@ abstract contract MorphoBlue is Helpers, Events {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @notice Repays assets.
|
* @notice Repay assets.
|
||||||
* @dev The market to repay assets to. (For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
|
* @dev The market to repay assets to. (For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
|
||||||
* @param _marketParams The market to repay assets to.
|
* @param _marketParams The market to repay assets to.
|
||||||
* @param _assets The amount of assets to repay. (For max: `uint256(-1)`)
|
* @param _assets The amount of assets to repay. (For max: `uint256(-1)`)
|
||||||
|
@ -764,26 +752,24 @@ abstract contract MorphoBlue is Helpers, Events {
|
||||||
payable
|
payable
|
||||||
returns (string memory _eventName, bytes memory _eventParam)
|
returns (string memory _eventName, bytes memory _eventParam)
|
||||||
{
|
{
|
||||||
// Final assets amount and token contract
|
uint256 _amt;
|
||||||
(
|
(
|
||||||
TokenInterface _tokenContract,
|
_marketParams, // Updated token contracts in case of Eth
|
||||||
uint256 _amt // Assets final amount to repay
|
_amt // Assets final amount to repay
|
||||||
) = _performEthToWethConversion(
|
) = _performEthToWethConversion(
|
||||||
_marketParams,
|
_marketParams,
|
||||||
_assets,
|
_assets,
|
||||||
address(this),
|
address(this),
|
||||||
_getId,
|
_getId,
|
||||||
Mode.Repay
|
Mode.Repay
|
||||||
);
|
);
|
||||||
|
|
||||||
// Approving loan token for repaying
|
// Approving loan token for repaying
|
||||||
approve(_tokenContract, address(MORPHO_BLUE), _amt);
|
approve(
|
||||||
|
TokenInterface(_marketParams.loanToken),
|
||||||
// Updating token addresses
|
address(MORPHO_BLUE),
|
||||||
_marketParams.loanToken = address(_tokenContract);
|
_amt
|
||||||
_marketParams.collateralToken = _marketParams.collateralToken == ethAddr
|
);
|
||||||
? wethAddr
|
|
||||||
: _marketParams.collateralToken;
|
|
||||||
|
|
||||||
uint256 _shares;
|
uint256 _shares;
|
||||||
(_assets, _shares) = MORPHO_BLUE.repay(
|
(_assets, _shares) = MORPHO_BLUE.repay(
|
||||||
|
@ -826,26 +812,24 @@ abstract contract MorphoBlue is Helpers, Events {
|
||||||
payable
|
payable
|
||||||
returns (string memory _eventName, bytes memory _eventParam)
|
returns (string memory _eventName, bytes memory _eventParam)
|
||||||
{
|
{
|
||||||
// Final assets amount and token contract
|
uint256 _amt;
|
||||||
(
|
(
|
||||||
TokenInterface _tokenContract,
|
_marketParams, // Updated token contracts in case of Eth
|
||||||
uint256 _amt // Assets final amount to repay
|
_amt // Assets final amount to repay
|
||||||
) = _performEthToWethConversion(
|
) = _performEthToWethConversion(
|
||||||
_marketParams,
|
_marketParams,
|
||||||
_assets,
|
_assets,
|
||||||
_onBehalf,
|
_onBehalf,
|
||||||
_getId,
|
_getId,
|
||||||
Mode.Repay
|
Mode.Repay
|
||||||
);
|
);
|
||||||
|
|
||||||
// Approving loan token for repaying
|
// Approving loan token for repaying
|
||||||
approve(_tokenContract, address(MORPHO_BLUE), _amt);
|
approve(
|
||||||
|
TokenInterface(_marketParams.loanToken),
|
||||||
// Updating token addresses
|
address(MORPHO_BLUE),
|
||||||
_marketParams.loanToken = address(_tokenContract);
|
_amt
|
||||||
_marketParams.collateralToken = _marketParams.collateralToken == ethAddr
|
);
|
||||||
? wethAddr
|
|
||||||
: _marketParams.collateralToken;
|
|
||||||
|
|
||||||
uint256 _shares;
|
uint256 _shares;
|
||||||
(_assets, _shares) = MORPHO_BLUE.repay(
|
(_assets, _shares) = MORPHO_BLUE.repay(
|
||||||
|
@ -889,19 +873,23 @@ abstract contract MorphoBlue is Helpers, Events {
|
||||||
payable
|
payable
|
||||||
returns (string memory _eventName, bytes memory _eventParam)
|
returns (string memory _eventName, bytes memory _eventParam)
|
||||||
{
|
{
|
||||||
// Final assets amount and token contract
|
uint256 _assetsAmt;
|
||||||
(
|
(
|
||||||
TokenInterface _tokenContract,
|
_marketParams, // Updated token contracts in case of Eth
|
||||||
uint256 _assetsAmt // Assets final amount to repay
|
_assetsAmt // Assets final amount to repay
|
||||||
) = _performEthToWethSharesConversion(
|
) = _performEthToWethSharesConversion(
|
||||||
_marketParams,
|
_marketParams,
|
||||||
_shares,
|
_shares,
|
||||||
_onBehalf,
|
_onBehalf,
|
||||||
_getId,
|
_getId,
|
||||||
true
|
true
|
||||||
);
|
);
|
||||||
|
|
||||||
approve(_tokenContract, address(MORPHO_BLUE), _assetsAmt);
|
approve(
|
||||||
|
TokenInterface(_marketParams.loanToken),
|
||||||
|
address(MORPHO_BLUE),
|
||||||
|
_assetsAmt
|
||||||
|
);
|
||||||
|
|
||||||
(uint256 _assets, ) = MORPHO_BLUE.repay(
|
(uint256 _assets, ) = MORPHO_BLUE.repay(
|
||||||
_marketParams,
|
_marketParams,
|
||||||
|
|
Loading…
Reference in New Issue
Block a user