diff --git a/contracts/mainnet/connectors/morpho-aave/events.sol b/contracts/mainnet/connectors/morpho-aave/events.sol index 47779061..14bfc418 100644 --- a/contracts/mainnet/connectors/morpho-aave/events.sol +++ b/contracts/mainnet/connectors/morpho-aave/events.sol @@ -11,26 +11,62 @@ contract Events { uint256 setId ); + event LogDepositWithMaxGas( + address tokenAddress, + address poolTokenAddress, + uint256 amount, + uint256 maxGasForMatching, + uint256 getId, + uint256 setId + ); + + event LogDepositOnBehalf( + address tokenAddress, + address poolTokenAddress, + address onBehalf, + uint256 amount, + uint256 getId, + uint256 setId + ); + event LogBorrow( - bool isETH, + address tokenAddress, address poolTokenAddress, uint256 amount, uint256 getId, uint256 setId ); - event LogWithdraw( - bool isETH, + event LogBorrowWithMaxGas( + address tokenAddress, address poolTokenAddress, - uint256 amt, + uint256 amount, + uint256 maxGasForMatching, + uint256 getId, + uint256 setId + ); + + event LogWithdraw( + address tokenAddress, + address poolTokenAddress, + uint256 amount, uint256 getId, uint256 setId ); event LogPayback( - bool isETH, + address tokenAddress, address poolTokenAddress, - uint256 amt, + uint256 amount, + uint256 getId, + uint256 setId + ); + + event LogPaybackOnBehalf( + address tokenAddress, + address poolTokenAddress, + address onBehalf, + uint256 amount, uint256 getId, uint256 setId ); diff --git a/contracts/mainnet/connectors/morpho-aave/interface.sol b/contracts/mainnet/connectors/morpho-aave/interface.sol index fde267ca..9ec4220b 100644 --- a/contracts/mainnet/connectors/morpho-aave/interface.sol +++ b/contracts/mainnet/connectors/morpho-aave/interface.sol @@ -9,8 +9,21 @@ interface IMorphoCore { uint256 _amount ) external; + function supply( + address _poolToken, + address _onBehalf, + uint256 _amount, + uint256 _maxGasForMatching + ) external; + function borrow(address _poolTokenAddress, uint256 _amount) external; + function borrow( + address _poolToken, + uint256 _amount, + uint256 _maxGasForMatching + ) external; + function withdraw(address _poolTokenAddress, uint256 _amount) external; function repay( @@ -19,13 +32,6 @@ interface IMorphoCore { uint256 _amount ) external; - function liquidate( - address _poolTokenBorrowedAddress, - address _poolTokenCollateralAddress, - address _borrower, - uint256 _amount - ) external; - function claimRewards( address[] calldata _tokenAddresses, bool _tradeForMorphoToken diff --git a/contracts/mainnet/connectors/morpho-aave/main.sol b/contracts/mainnet/connectors/morpho-aave/main.sol index c3ef1501..85b16395 100644 --- a/contracts/mainnet/connectors/morpho-aave/main.sol +++ b/contracts/mainnet/connectors/morpho-aave/main.sol @@ -18,7 +18,6 @@ abstract contract MorphoAave is Helpers, Events { address _tokenAddress, address _poolTokenAddress, uint256 _amount, - // uint256 _maxGasForMatching, // optional uint256 _getId, uint256 _setId ) @@ -53,7 +52,121 @@ abstract contract MorphoAave is Helpers, Events { _tokenAddress, _poolTokenAddress, _amt, - // _maxGasForMatching, + _getId, + _setId + ); + } + + /** + * @dev Deposit ETH/ERC20_Token with Max Gas. + * @notice Deposit a token to Morpho Aave for lending / collaterization with max gas. + * @param _tokenAddress The address of underlying token to deposit.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE). + * @param _poolTokenAddress The address of aToken to deposit.(For ETH: aWETH address). + * @param _amount The amount of the token (in underlying) to deposit. (For max: `uint256(-1)`). + * @param _maxGasForMatching The maximum amount of gas to consume within a matching engine loop. + * @param _getId ID to retrieve amt. + * @param _setId ID stores the amount of tokens deposited. + */ + function depositWithMaxGas( + address _tokenAddress, + address _poolTokenAddress, + uint256 _amount, + uint256 _maxGasForMatching, + uint256 _getId, + uint256 _setId + ) + external + payable + returns (string memory _eventName, bytes memory _eventParam) + { + uint256 _amt = getUint(_getId, _amount); + + bool _isETH = _tokenAddress == ethAddr; + + TokenInterface _tokenContract = _isETH + ? TokenInterface(wethAddr) + : TokenInterface(_tokenAddress); + + if (_amt == uint256(-1)) { + _amt = _isETH + ? address(this).balance + : _tokenContract.balanceOf(address(this)); + } + + if (_isETH) convertEthToWeth(_isETH, _tokenContract, _amt); + + approve(_tokenContract, address(morphoAave), _amt); + + morphoAave.supply( + _poolTokenAddress, + address(this), + _amt, + _maxGasForMatching + ); + + setUint(_setId, _amt); + + _eventName = "LogDepositWithMaxGas(address,address,uint256,uint256,uint256,uint256)"; + _eventParam = abi.encode( + _tokenAddress, + _poolTokenAddress, + _amt, + _maxGasForMatching, + _getId, + _setId + ); + } + + /** + * @dev Deposit ETH/ERC20_Token on behalf of a user. + * @notice Deposit a token to Morpho Aave for lending / collaterization on behalf of a user. + * @param _tokenAddress The address of underlying token to deposit.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) + * @param _poolTokenAddress The address of aToken to deposit.(For ETH: aWETH address) + * @param _onBehalf The address of user on behalf to deposit. + * @param _amount The amount of the token (in underlying) to deposit. (For max: `uint256(-1)`) + * @param _getId ID to retrieve amt. + * @param _setId ID stores the amount of tokens deposited. + */ + function depositOnBehalf( + address _tokenAddress, + address _poolTokenAddress, + address _onBehalf, + uint256 _amount, + uint256 _getId, + uint256 _setId + ) + external + payable + returns (string memory _eventName, bytes memory _eventParam) + { + uint256 _amt = getUint(_getId, _amount); + + bool _isETH = _tokenAddress == ethAddr; + + TokenInterface _tokenContract = _isETH + ? TokenInterface(wethAddr) + : TokenInterface(_tokenAddress); + + if (_amt == uint256(-1)) { + _amt = _isETH + ? address(this).balance + : _tokenContract.balanceOf(address(this)); + } + + if (_isETH) convertEthToWeth(_isETH, _tokenContract, _amt); + + approve(_tokenContract, address(morphoAave), _amt); + + morphoAave.supply(_poolTokenAddress, _onBehalf, _amt); + + setUint(_setId, _amt); + + _eventName = "LogDepositOnBehalf(address,address,address,uint256,uint256,uint256)"; + _eventParam = abi.encode( + _tokenAddress, + _poolTokenAddress, + _onBehalf, + _amt, _getId, _setId ); @@ -72,7 +185,6 @@ abstract contract MorphoAave is Helpers, Events { address _tokenAddress, address _poolTokenAddress, uint256 _amount, - // uint256 _maxGasForMatching, uint256 _getId, uint256 _setId ) @@ -90,12 +202,54 @@ abstract contract MorphoAave is Helpers, Events { setUint(_setId, _amt); - _eventName = "LogBorrow(bool,address,uint256,uint256,uint256)"; + _eventName = "LogBorrow(address,address,uint256,uint256,uint256)"; _eventParam = abi.encode( - _isETH, + _tokenAddress, _poolTokenAddress, _amt, - // _maxGasForMatching, + _getId, + _setId + ); + } + + /** + * @dev Borrow ETH/ERC20_Token with max gas. + * @notice Borrow a token from Morpho Aave with max gas. + * @param _tokenAddress The address of underlying token to borrow.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE). + * @param _poolTokenAddress The address of aToken to borrow.(For ETH: aWETH address). + * @param _amount The amount of the token (in underlying) to borrow. + * @param _maxGasForMatching The maximum amount of gas to consume within a matching engine loop. + * @param _getId ID to retrieve amt. + * @param _setId ID stores the amount of tokens borrowed. + */ + function borrowWithMaxGas( + address _tokenAddress, + address _poolTokenAddress, + uint256 _amount, + uint256 _maxGasForMatching, + uint256 _getId, + uint256 _setId + ) + external + payable + returns (string memory _eventName, bytes memory _eventParam) + { + uint256 _amt = getUint(_getId, _amount); + + bool _isETH = _tokenAddress == ethAddr; + + morphoAave.borrow(_poolTokenAddress, _amt, _maxGasForMatching); + + if (_isETH) convertWethToEth(_isETH, TokenInterface(wethAddr), _amt); + + setUint(_setId, _amt); + + _eventName = "LogBorrowWithMaxGas(address,address,uint256,uint256,uint256,uint256)"; + _eventParam = abi.encode( + _tokenAddress, + _poolTokenAddress, + _amt, + _maxGasForMatching, _getId, _setId ); @@ -137,9 +291,9 @@ abstract contract MorphoAave is Helpers, Events { setUint(_setId, _amt); - _eventName = "LogWithdraw(bool,address,uint256,uint256,uint256)"; + _eventName = "LogWithdraw(address,address,uint256,uint256,uint256)"; _eventParam = abi.encode( - _isETH, + _tokenAddress, _poolTokenAddress, _amt, _getId, @@ -169,26 +323,33 @@ abstract contract MorphoAave is Helpers, Events { { bool _isETH = _tokenAddress == ethAddr; uint256 _amt = getUint(_getId, _amount); - address _token = _isETH ? wethAddr : _tokenAddress; + + TokenInterface _tokenContract = _isETH + ? TokenInterface(wethAddr) + : TokenInterface(_tokenAddress); if (_amt == uint256(-1)) { - (, , , _amt) = morphoAaveLens._getCurrentBorrowBalanceInOf( - _poolTokenAddress, - address(this) - ); + uint256 _amtDSA = _isETH + ? address(this).balance + : _tokenContract.balanceOf(address(this)); + + (, , , uint256 _amtDebt) = morphoAaveLens + ._getCurrentBorrowBalanceInOf(_poolTokenAddress, address(this)); + + _amt = _amtDSA <= _amtDebt ? _amtDSA : _amtDebt; } - if (_isETH) convertEthToWeth(_isETH, TokenInterface(_token), _amt); + if (_isETH) convertEthToWeth(_isETH, _tokenContract, _amt); - approve(TokenInterface(_token), address(morphoAave), _amt); + approve(_tokenContract, address(morphoAave), _amt); morphoAave.repay(_poolTokenAddress, address(this), _amt); setUint(_setId, _amt); - _eventName = "LogPayback(bool,address,uint256,uint256,uint256)"; + _eventName = "LogPayback(address,address,uint256,uint256,uint256)"; _eventParam = abi.encode( - _isETH, + _tokenAddress, _poolTokenAddress, _amt, _getId, @@ -196,6 +357,65 @@ abstract contract MorphoAave is Helpers, Events { ); } + /** + * @dev Payback ETH/ERC20_Token. + * @notice Payback a token to Morpho Aave. + * @param _tokenAddress The address of underlying token to payback.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) + * @param _poolTokenAddress The address of aToken to payback.(For ETH: aWETH address) + * @param _onBehalf The address of user who's debt to repay. + * @param _amount The amount of the token (in underlying) to payback. (For max: `uint256(-1)`) + * @param _getId ID to retrieve amt. + * @param _setId ID stores the amount of tokens paid back. + */ + function paybackOnBehalf( + address _tokenAddress, + address _poolTokenAddress, + address _onBehalf, + uint256 _amount, + uint256 _getId, + uint256 _setId + ) + external + payable + returns (string memory _eventName, bytes memory _eventParam) + { + bool _isETH = _tokenAddress == ethAddr; + uint256 _amt = getUint(_getId, _amount); + + TokenInterface _tokenContract = _isETH + ? TokenInterface(wethAddr) + : TokenInterface(_tokenAddress); + + if (_amt == uint256(-1)) { + uint256 _amtDSA = _isETH + ? address(this).balance + : _tokenContract.balanceOf(address(this)); + + (, , , uint256 _amtDebt) = morphoAaveLens + ._getCurrentBorrowBalanceInOf(_poolTokenAddress, _onBehalf); + + _amt = _amtDSA <= _amtDebt ? _amtDSA : _amtDebt; + } + + if (_isETH) convertEthToWeth(_isETH, _tokenContract, _amt); + + approve(_tokenContract, address(morphoAave), _amt); + + morphoAave.repay(_poolTokenAddress, _onBehalf, _amt); + + setUint(_setId, _amt); + + _eventName = "LogPaybackOnBehalf(address,address,address,uint256,uint256,uint256)"; + _eventParam = abi.encode( + _tokenAddress, + _poolTokenAddress, + _onBehalf, + _amt, + _getId, + _setId + ); + } + /** * @dev Claim rewards. * @notice Claim rewards for the given assets from underlying protocol. @@ -203,7 +423,7 @@ abstract contract MorphoAave is Helpers, Events { * @param _tradeForMorphoToken Whether or not to trade reward tokens for MORPHO tokens. */ function claim( - address[] calldata _poolTokenAddresses, //todo: eth will be claimed as weth currently? + address[] calldata _poolTokenAddresses, bool _tradeForMorphoToken ) external diff --git a/contracts/mainnet/connectors/morpho-compound/events.sol b/contracts/mainnet/connectors/morpho-compound/events.sol index 47779061..7516cb1f 100644 --- a/contracts/mainnet/connectors/morpho-compound/events.sol +++ b/contracts/mainnet/connectors/morpho-compound/events.sol @@ -11,16 +11,43 @@ contract Events { uint256 setId ); + event LogDepositWithMaxGas( + address tokenAddress, + address poolTokenAddress, + uint256 amount, + uint256 maxGasForMatching, + uint256 getId, + uint256 setId + ); + + event LogDepositOnBehalf( + address tokenAddress, + address poolTokenAddress, + address onBehalf, + uint256 amount, + uint256 getId, + uint256 setId + ); + event LogBorrow( - bool isETH, + address tokenAddress, address poolTokenAddress, uint256 amount, uint256 getId, uint256 setId ); + event LogBorrowWithMaxGas( + address tokenAddress, + address poolTokenAddress, + uint256 amount, + uint256 maxGasForMatching, + uint256 getId, + uint256 setId + ); + event LogWithdraw( - bool isETH, + address tokenAddress, address poolTokenAddress, uint256 amt, uint256 getId, @@ -28,12 +55,21 @@ contract Events { ); event LogPayback( - bool isETH, + address tokenAddress, address poolTokenAddress, uint256 amt, uint256 getId, uint256 setId ); + event LogPaybackOnBehalf( + address tokenAddress, + address poolTokenAddress, + address onBehalf, + uint256 amount, + uint256 getId, + uint256 setId + ); + event LogClaimed(address[] tokenAddresses, bool tradeForMorphoToken); } diff --git a/contracts/mainnet/connectors/morpho-compound/interface.sol b/contracts/mainnet/connectors/morpho-compound/interface.sol index acb46a7d..5a2c751f 100644 --- a/contracts/mainnet/connectors/morpho-compound/interface.sol +++ b/contracts/mainnet/connectors/morpho-compound/interface.sol @@ -9,8 +9,21 @@ interface IMorphoCore { uint256 _amount ) external; + function supply( + address _poolTokenAddress, + address _onBehalf, + uint256 _amount, + uint256 _maxGasForMatching + ) external; + function borrow(address _poolTokenAddress, uint256 _amount) external; + function borrow( + address _poolTokenAddress, + uint256 _amount, + uint256 _maxGasForMatching + ) external; + function withdraw(address _poolTokenAddress, uint256 _amount) external; function repay( @@ -19,14 +32,6 @@ interface IMorphoCore { uint256 _amount ) external; - function liquidate( - address _poolTokenBorrowedAddress, - address _poolTokenCollateralAddress, - address _borrower, - uint256 _amount - ) external; - - // (For AAVEV2: (aToken or variable debt token), COMPOUNDV2: cToken addresses) function claimRewards( address[] calldata _tokenAddresses, bool _tradeForMorphoToken diff --git a/contracts/mainnet/connectors/morpho-compound/main.sol b/contracts/mainnet/connectors/morpho-compound/main.sol index 1bc3b903..93d00887 100644 --- a/contracts/mainnet/connectors/morpho-compound/main.sol +++ b/contracts/mainnet/connectors/morpho-compound/main.sol @@ -18,7 +18,6 @@ abstract contract MorphoCompound is Helpers, Events { address _tokenAddress, address _poolTokenAddress, uint256 _amount, - // uint256 _maxGasForMatching, // optional uint256 _getId, uint256 _setId ) @@ -52,7 +51,119 @@ abstract contract MorphoCompound is Helpers, Events { _tokenAddress, _poolTokenAddress, _amt, - // _maxGasForMatching, + _getId, + _setId + ); + } + + /** + * @dev Deposit ETH/ERC20_Token. + * @notice Deposit a token to Morpho Compound for lending / collaterization with max gas. + * @param _tokenAddress The address of underlying token to deposit.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) + * @param _poolTokenAddress The address of cToken to deposit.(For ETH: cWETH address). + * @param _amount The amount of the token (in underlying) to deposit. (For max: `uint256(-1)`). + * @param _maxGasForMatching The maximum amount of gas to consume within a matching engine loop. + * @param _getId ID to retrieve amt. + * @param _setId ID stores the amount of tokens deposited. + */ + function depositWithMaxGas( + address _tokenAddress, + address _poolTokenAddress, + uint256 _amount, + uint256 _maxGasForMatching, + uint256 _getId, + uint256 _setId + ) + external + payable + returns (string memory _eventName, bytes memory _eventParam) + { + uint256 _amt = getUint(_getId, _amount); + + bool _isETH = _tokenAddress == ethAddr; + + TokenInterface _tokenContract = _isETH + ? TokenInterface(wethAddr) + : TokenInterface(_tokenAddress); + + if (_amt == uint256(-1)) { + _amt = _isETH + ? address(this).balance + : _tokenContract.balanceOf(address(this)); + } + + if (_isETH) convertEthToWeth(_isETH, _tokenContract, _amt); + + approve(_tokenContract, address(morphoCompound), _amt); + morphoCompound.supply( + _poolTokenAddress, + address(this), + _amt, + _maxGasForMatching + ); + + setUint(_setId, _amt); + + _eventName = "depositWithMaxGas(address,address,uint256,uint256,uint256,uint256)"; + _eventParam = abi.encode( + _tokenAddress, + _poolTokenAddress, + _amt, + _maxGasForMatching, + _getId, + _setId + ); + } + + /** + * @dev Deposit ETH/ERC20_Token. + * @notice Deposit a token to Morpho Compound for lending / collaterization on behalf of a user. + * @param _tokenAddress The address of underlying token to deposit.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) + * @param _poolTokenAddress The address of cToken to deposit.(For ETH: cWETH address) + * @param _onBehalf The address of user on behalf to deposit. + * @param _amount The amount of the token (in underlying) to deposit. (For max: `uint256(-1)`) + * @param _getId ID to retrieve amt. + * @param _setId ID stores the amount of tokens deposited. + */ + function depositOnBehalf( + address _tokenAddress, + address _poolTokenAddress, + address _onBehalf, + uint256 _amount, + uint256 _getId, + uint256 _setId + ) + external + payable + returns (string memory _eventName, bytes memory _eventParam) + { + uint256 _amt = getUint(_getId, _amount); + + bool _isETH = _tokenAddress == ethAddr; + + TokenInterface _tokenContract = _isETH + ? TokenInterface(wethAddr) + : TokenInterface(_tokenAddress); + + if (_amt == uint256(-1)) { + _amt = _isETH + ? address(this).balance + : _tokenContract.balanceOf(address(this)); + } + + if (_isETH) convertEthToWeth(_isETH, _tokenContract, _amt); + + approve(_tokenContract, address(morphoCompound), _amt); + morphoCompound.supply(_poolTokenAddress, _onBehalf, _amt); + + setUint(_setId, _amt); + + _eventName = "LogDeposit(address,address,address,uint256,uint256,uint256)"; + _eventParam = abi.encode( + _tokenAddress, + _poolTokenAddress, + _onBehalf, + _amt, _getId, _setId ); @@ -71,7 +182,6 @@ abstract contract MorphoCompound is Helpers, Events { address _tokenAddress, address _poolTokenAddress, uint256 _amount, - // uint256 _maxGasForMatching, uint256 _getId, uint256 _setId ) @@ -89,12 +199,54 @@ abstract contract MorphoCompound is Helpers, Events { setUint(_setId, _amt); - _eventName = "LogBorrow(bool,address,uint256,uint256,uint256)"; + _eventName = "LogBorrow(address,address,uint256,uint256,uint256)"; _eventParam = abi.encode( - _isETH, + _tokenAddress, _poolTokenAddress, _amt, - // _maxGasForMatching, + _getId, + _setId + ); + } + + /** + * @dev Borrow ETH/ERC20_Token. + * @notice Borrow a token from Morpho Compound with max gas. + * @param _tokenAddress The address of underlying token to borrow.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) + * @param _poolTokenAddress The address of cToken to borrow.(For ETH: cWETH address) + * @param _amount The amount of the token (in underlying) to borrow. + * @param _maxGasForMatching The maximum amount of gas to consume within a matching engine loop. + * @param _getId ID to retrieve amt. + * @param _setId ID stores the amount of tokens borrowed. + */ + function borrowWithMaxGas( + address _tokenAddress, + address _poolTokenAddress, + uint256 _amount, + uint256 _maxGasForMatching, + uint256 _getId, + uint256 _setId + ) + external + payable + returns (string memory _eventName, bytes memory _eventParam) + { + uint256 _amt = getUint(_getId, _amount); + + bool _isETH = _tokenAddress == ethAddr; + + morphoCompound.borrow(_poolTokenAddress, _amt, _maxGasForMatching); + + if (_isETH) convertWethToEth(_isETH, TokenInterface(wethAddr), _amt); + + setUint(_setId, _amt); + + _eventName = "LogBorrowWithMaxGas(address,address,uint256,uint256,uint256,uint256)"; + _eventParam = abi.encode( + _tokenAddress, + _poolTokenAddress, + _amt, + _maxGasForMatching, _getId, _setId ); @@ -124,11 +276,12 @@ abstract contract MorphoCompound is Helpers, Events { bool _isETH = _tokenAddress == ethAddr; address _token = _isETH ? wethAddr : _tokenAddress; - if (_amt == uint256(-1)) + if (_amt == uint256(-1)) { (, , _amt) = morphoCompoundLens.getCurrentSupplyBalanceInOf( _poolTokenAddress, address(this) ); + } morphoCompound.withdraw(_poolTokenAddress, _amt); @@ -136,9 +289,9 @@ abstract contract MorphoCompound is Helpers, Events { setUint(_setId, _amt); - _eventName = "LogWithdraw(bool,address,uint256,uint256,uint256)"; + _eventName = "LogWithdraw(address,address,uint256,uint256,uint256)"; _eventParam = abi.encode( - _isETH, + _tokenAddress, _poolTokenAddress, _amt, _getId, @@ -168,26 +321,33 @@ abstract contract MorphoCompound is Helpers, Events { { bool _isETH = _tokenAddress == ethAddr; uint256 _amt = getUint(_getId, _amount); - address _token = _isETH ? wethAddr : _tokenAddress; + + TokenInterface _tokenContract = _isETH + ? TokenInterface(wethAddr) + : TokenInterface(_tokenAddress); if (_amt == uint256(-1)) { - (, , _amt) = morphoCompoundLens.getCurrentBorrowBalanceInOf( - _poolTokenAddress, - address(this) - ); + uint256 _amtDSA = _isETH + ? address(this).balance + : _tokenContract.balanceOf(address(this)); + + (, , uint256 _amtDebt) = morphoCompoundLens + .getCurrentBorrowBalanceInOf(_poolTokenAddress, address(this)); + + _amt = _amtDSA <= _amtDebt ? _amtDSA : _amtDebt; } - if (_isETH) convertEthToWeth(_isETH, TokenInterface(_token), _amt); + if (_isETH) convertEthToWeth(_isETH, _tokenContract, _amt); - approve(TokenInterface(_token), address(morphoCompound), _amt); + approve(_tokenContract, address(morphoCompound), _amt); morphoCompound.repay(_poolTokenAddress, address(this), _amt); setUint(_setId, _amt); - _eventName = "LogPayback(bool,address,uint256,uint256,uint256)"; + _eventName = "LogPayback(address,address,uint256,uint256,uint256)"; _eventParam = abi.encode( - _isETH, + _tokenAddress, _poolTokenAddress, _amt, _getId, @@ -195,6 +355,65 @@ abstract contract MorphoCompound is Helpers, Events { ); } + /** + * @dev Payback ETH/ERC20_Token. + * @notice Payback a token to Morpho Compound on behalf of a user. + * @param _tokenAddress The address of underlying token to payback.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) + * @param _poolTokenAddress The address of cToken to payback.(For ETH: cWETH address) + * @param _onBehalf The address of user who's debt to repay. + * @param _amount The amount of the token (in underlying) to payback. (For max: `uint256(-1)`) + * @param _getId ID to retrieve amt. + * @param _setId ID stores the amount of tokens paid back. + */ + function paybackOnBehalf( + address _tokenAddress, + address _poolTokenAddress, + address _onBehalf, + uint256 _amount, + uint256 _getId, + uint256 _setId + ) + external + payable + returns (string memory _eventName, bytes memory _eventParam) + { + bool _isETH = _tokenAddress == ethAddr; + uint256 _amt = getUint(_getId, _amount); + + TokenInterface _tokenContract = _isETH + ? TokenInterface(wethAddr) + : TokenInterface(_tokenAddress); + + if (_amt == uint256(-1)) { + uint256 _amtDSA = _isETH + ? address(this).balance + : _tokenContract.balanceOf(address(this)); + + (, , uint256 _amtDebt) = morphoCompoundLens + .getCurrentBorrowBalanceInOf(_poolTokenAddress, _onBehalf); + + _amt = _amtDSA <= _amtDebt ? _amtDSA : _amtDebt; + } + + if (_isETH) convertEthToWeth(_isETH, _tokenContract, _amt); + + approve(_tokenContract, address(morphoCompound), _amt); + + morphoCompound.repay(_poolTokenAddress, _onBehalf, _amt); + + setUint(_setId, _amt); + + _eventName = "LogPaybackOnBehalf(address,address,address,uint256,uint256,uint256)"; + _eventParam = abi.encode( + _tokenAddress, + _poolTokenAddress, + _onBehalf, + _amt, + _getId, + _setId + ); + } + /** * @dev Claim rewards. * @notice Claim rewards for the given assets from underlying protocol.