From a45835948b1421793eeadb42e03bc9e038dc91cc Mon Sep 17 00:00:00 2001 From: Shriya Tyagi Date: Wed, 5 Oct 2022 05:49:52 +0400 Subject: [PATCH 01/31] Morpho contracts set up --- .../mainnet/connectors/morpho/events.sol | 48 +++++ .../mainnet/connectors/morpho/helpers.sol | 19 ++ .../mainnet/connectors/morpho/interface.sol | 42 +++++ contracts/mainnet/connectors/morpho/main.sol | 178 ++++++++++++++++++ 4 files changed, 287 insertions(+) create mode 100644 contracts/mainnet/connectors/morpho/events.sol create mode 100644 contracts/mainnet/connectors/morpho/helpers.sol create mode 100644 contracts/mainnet/connectors/morpho/interface.sol create mode 100644 contracts/mainnet/connectors/morpho/main.sol diff --git a/contracts/mainnet/connectors/morpho/events.sol b/contracts/mainnet/connectors/morpho/events.sol new file mode 100644 index 00000000..f8f29f7b --- /dev/null +++ b/contracts/mainnet/connectors/morpho/events.sol @@ -0,0 +1,48 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.7.0; +pragma experimental ABIEncoderV2; + +contract Events { + event LogDeposit( + uint256 pool, + address tokenAddress, + address poolTokenAddress, + uint256 amount, + uint256 maxGasForMatching, + uint256 getId, + uint256 setId + ); + + event LogBorrow( + uint256 pool, + address poolTokenAddress, + uint256 amount, + uint256 maxGasForMatching, + uint256 getId, + uint256 setId + ); + + event LogWithdraw( + uint256 pool, + bool isETH, + address poolTokenAddress, + uint256 amt, + uint256 getId, + uint256 setId + ); + + event LogPayback( + uint256 pool, + bool isETH, + address poolTokenAddress, + uint256 amt, + uint256 getId, + uint256 setId + ); + + event LogClaimed( + uint256 pool, + address[] tokenAddresses, + bool tradeForMorphoToken + ); +} diff --git a/contracts/mainnet/connectors/morpho/helpers.sol b/contracts/mainnet/connectors/morpho/helpers.sol new file mode 100644 index 00000000..d99da296 --- /dev/null +++ b/contracts/mainnet/connectors/morpho/helpers.sol @@ -0,0 +1,19 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.7.0; +pragma experimental ABIEncoderV2; +import "./interface.sol"; +import "../../common/stores.sol"; +import "../../common/basic.sol"; +import "../../common/interfaces.sol"; + +abstract contract Helpers is Stores, Basic { + IMorphoCore public constant morphoCompound = + IMorphoCore(0x8888882f8f843896699869179fB6E4f7e3B58888); + IMorphoCore public constant morphoAave = + IMorphoCore(0x777777c9898D384F785Ee44Acfe945efDFf5f3E0); + + enum Underlying { + AAVEV2, + COMPOUNDV2 + } +} diff --git a/contracts/mainnet/connectors/morpho/interface.sol b/contracts/mainnet/connectors/morpho/interface.sol new file mode 100644 index 00000000..da192bb6 --- /dev/null +++ b/contracts/mainnet/connectors/morpho/interface.sol @@ -0,0 +1,42 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.7.0; +pragma experimental ABIEncoderV2; + +interface IMorphoCore { + function supply( + address _poolTokenAddress, + address _onBehalf, + uint256 _amount, + uint256 _maxGasForMatching + ) external; + + function borrow( + address _poolTokenAddress, + uint256 _amount, + uint256 _maxGasForMatching + ) external; + + function withdraw( + address _poolTokenAddress, + uint256 _amount + ) external; + + function repay( + address _poolTokenAddress, + address _onBehalf, + 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[] _tokenAddresses, + bool _tradeForMorphoToken, + ) external; +} \ No newline at end of file diff --git a/contracts/mainnet/connectors/morpho/main.sol b/contracts/mainnet/connectors/morpho/main.sol new file mode 100644 index 00000000..cafcde5d --- /dev/null +++ b/contracts/mainnet/connectors/morpho/main.sol @@ -0,0 +1,178 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.7.0; +pragma experimental ABIEncoderV2; +import './helpers.sol'; +import './events.sol'; +import './hardhat/console.sol'; + +abstract contract Morpho is Helpers, Events { + + function deposit ( + Underlying _pool, + address _tokenAddress, + address _poolTokenAddress, // if address weth (send eth) + uint256 _amount, // if max, check balance + uint256 _maxGasForMatching, + uint256 _getId, + uint256 _setId + ) external payable returns(string memory _eventName, bytes memory _eventParam) { + + require(_pool == Underlying.AAVEV2 || _pool == Underlying.COMPOUNDV2, 'protocol not supported'); + + uint256 _amt = getUint(_getId, _amount); + + bool _isETH ? _tokenAddress == ethAddr; + address _token = _isETH ? wethAddr : _tokenAddress + + TokenInterface _tokenContract = TokenInterface(_token); + + if(_amt == uint256(-1)) { + _amt = _isETH ? address(this).balance : _tokenContract.balanceOf(address(this)); + } + + if(_isETH) convertEthToWeth(_isETH, _tokenContract, _amt); + + _pool == Underlying.AAVEV2 + ? + approve(_tokenContract, morphoAave, _amt); + morphoAave.supply(_poolTokenAddress, address(this), _amt, _maxGasForMatching); + : + approve(_tokenContract, morphoCompound, _amt); + morphoCompound.supply(_poolTokenAddress, address(this), _amt, _maxGasForMatching); + + setUint(_setId, _amt); + + _eventName = "LogDeposit(uint256,address,address,uint256,uint256,uint256,uint256)"; + _eventParam = abi.encode( + _pool, + _tokenAddress, + _poolTokenAddress, + _amt, + _maxGasForMatching, + _getId, + _setId + ); + } + + function borrow ( + Underlying _pool, + address _tokenAddress, + address _poolTokenAddress, //todo: dTokenaAddress? // if address weth (send eth) + uint256 _amount, + uint256 _maxGasForMatching + uint256 _getId, + uint256 _setId + ) external payable returns(string memory _eventName, bytes memory _eventParam) { + + require(_pool == Underlying.AAVEV2 || _pool == Underlying.COMPOUNDV2, 'protocol not supported'); + + uint256 _amt = getUint(_getId, _amount); + + bool _isETH ? _tokenAddress == ethAddr; + address _token = _isETH ? wethAddr : _tokenAddress; + + if(_pool == Underlying.AAVEV2) { + morphoAave.borrow(_poolTokenAddress, _amt, _maxGasForMatching); + } else { + morphoCompound.borrow(_poolTokenAddress, _amt, _maxGasForMatching); + } + + if(_isETH) convertWethToEth(_isETH, tokenInterface(_token), _amt); + + setUint(_setId, _amt); + + _eventName = "LogBorrow(uint256,address,uint256,uint256,uint256,uint256)"; + _eventParam = abi.encode( + _pool, + _poolTokenAddress, + _amt, + _maxGasForMatching, + _getId, + _setId + ); + } + + function withdraw ( + Underlying _pool, + address _tokenAddress, + address _poolTokenAddress, // if address weth (send eth) + uint256 _amount, // amount max + uint256 _getId, + uint256 _setId + ) external payable returns(string memory _eventName, bytes memory _eventParam) { + + require(_pool == Underlying.AAVEV2 || _pool == Underlying.COMPOUNDV2, 'protocol not supported'); + + uint256 _amt = getUint(_getId, _amount); + bool _isETH ? _tokenAddress == ethAddr; + address _token = _isETH ? wethAddr : _tokenAddress; + + if (_amt == uint256(-1)) _amt = _poolTokenAddress.balanceOf(address(this)); + + if(_pool == Underlying.AAVEV2) { + morphoAave.withdraw(_poolTokenAddress, _amt); + } else { + morphoCompound.withdraw(_poolTokenAddress, _amt); + } + + convertWethToEth(_isETH, TokenInterface(_token), _amt); + + setUint(_setId, _amt); + + _eventName = "LogWithdraw(uint256,bool,address,uint256,uint256,uint256)"; + _eventParam = abi.encode(_pool, _isETH, _poolTokenAddress, _amt, _getId, _setId); + + } + + function payback ( + Underlying _pool, + address _tokenAddress, + address _poolTokenAddress, // if address weth (send eth) + uint256 _amount, // max value + uint256 _getId, + uint256 _setId + ) external payable returns(string memory _eventName, bytes memory _eventParam) { + + require(_pool == Underlying.AAVEV2 || _pool == Underlying.COMPOUNDV2, 'protocol not supported'); + + bool _isETH ? _tokenAddress == ethAddr; + uint256 _amt = getUint(_getId, _amount); + address _token = _isETH ? wethAddr : _tokenAddress; + + if(_amt == uint256(-1)) { + _amt = _isETH ? _amt = address(this).balance : TokenInterface(_token).balanceOf(address(this)) + } + + if (_isETH) convertEthToWeth(_isETH, TokenInterface(_token), _amt); + + _pool == Underlying.AAVEV2 + ? morphoAave.repay(_poolTokenAddress, address(this), _amt) + : morphoCompound.repay(_poolTokenAddress, address(this), _amt); + + setUint(_setId, _amt); + + _eventName = "LogPayback(uint256,bool,address,uint256,uint256,uint256)"; + _eventParam = abi.encode(_pool, _isETH, _poolTokenAddress, _amt, _getId, _setId); + } + + function claim ( + Underlying _pool, + address[] _tokenAddresses, //todo: eth will be claimed as weth currently? + bool _tradeForMorphoToken + ) external payable returns(string memory _eventName, bytes memory _eventParam) { + + require(_pool == Underlying.AAVEV2 || _pool == Underlying.COMPOUNDV2, 'protocol not supported'); + + _pool == Underlying.AAVEV2 + ? morphoAave.claim(_tokenAddresses, _tradeForMorphoToken) + : morphoCompound.claim(_tokenAddresses, _tradeForMorphoToken); + + _eventName = "LogClaimed(uint256,address[],bool)"; + _eventParam = abi.encode(_pool, _tokenAddresses, _tradeForMorphoToken); + } + +} + +contract ConnectV2Morpho is Morpho { + string public constant name = "Morpho-v1.0"; +} From 9976f844dde60a5a9344e57df1f78617439aaed1 Mon Sep 17 00:00:00 2001 From: Shriya Tyagi Date: Sat, 8 Oct 2022 05:23:01 +0400 Subject: [PATCH 02/31] minor fixes --- contracts/mainnet/connectors/morpho/main.sol | 32 +++++++++----------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/contracts/mainnet/connectors/morpho/main.sol b/contracts/mainnet/connectors/morpho/main.sol index cafcde5d..e6cde28d 100644 --- a/contracts/mainnet/connectors/morpho/main.sol +++ b/contracts/mainnet/connectors/morpho/main.sol @@ -3,7 +3,7 @@ pragma solidity ^0.7.0; pragma experimental ABIEncoderV2; import './helpers.sol'; import './events.sol'; -import './hardhat/console.sol'; +import 'hardhat/console.sol'; abstract contract Morpho is Helpers, Events { @@ -12,19 +12,18 @@ abstract contract Morpho is Helpers, Events { address _tokenAddress, address _poolTokenAddress, // if address weth (send eth) uint256 _amount, // if max, check balance - uint256 _maxGasForMatching, + uint256 _maxGasForMatching, // optional uint256 _getId, uint256 _setId ) external payable returns(string memory _eventName, bytes memory _eventParam) { - require(_pool == Underlying.AAVEV2 || _pool == Underlying.COMPOUNDV2, 'protocol not supported'); + require(_pool == Underlying.AAVEV2 || _pool == Underlying.COMPOUNDV2, 'underlying-protocol-not-supported'); uint256 _amt = getUint(_getId, _amount); - bool _isETH ? _tokenAddress == ethAddr; - address _token = _isETH ? wethAddr : _tokenAddress + bool _isETH = _tokenAddress == ethAddr; - TokenInterface _tokenContract = TokenInterface(_token); + TokenInterface _tokenContract = _isETH ? TokenInterface(wethAddr) : TokenInterface(_tokenAddress); if(_amt == uint256(-1)) { _amt = _isETH ? address(this).balance : _tokenContract.balanceOf(address(this)); @@ -32,13 +31,13 @@ abstract contract Morpho is Helpers, Events { if(_isETH) convertEthToWeth(_isETH, _tokenContract, _amt); - _pool == Underlying.AAVEV2 - ? + if(_pool == Underlying.AAVEV2) { approve(_tokenContract, morphoAave, _amt); morphoAave.supply(_poolTokenAddress, address(this), _amt, _maxGasForMatching); - : + } else { approve(_tokenContract, morphoCompound, _amt); morphoCompound.supply(_poolTokenAddress, address(this), _amt, _maxGasForMatching); + } setUint(_setId, _amt); @@ -59,17 +58,16 @@ abstract contract Morpho is Helpers, Events { address _tokenAddress, address _poolTokenAddress, //todo: dTokenaAddress? // if address weth (send eth) uint256 _amount, - uint256 _maxGasForMatching + uint256 _maxGasForMatching, uint256 _getId, uint256 _setId ) external payable returns(string memory _eventName, bytes memory _eventParam) { - require(_pool == Underlying.AAVEV2 || _pool == Underlying.COMPOUNDV2, 'protocol not supported'); + require(_pool == Underlying.AAVEV2 || _pool == Underlying.COMPOUNDV2, 'protocol-not-supported'); uint256 _amt = getUint(_getId, _amount); - bool _isETH ? _tokenAddress == ethAddr; - address _token = _isETH ? wethAddr : _tokenAddress; + bool _isETH = _tokenAddress == ethAddr; if(_pool == Underlying.AAVEV2) { morphoAave.borrow(_poolTokenAddress, _amt, _maxGasForMatching); @@ -77,7 +75,7 @@ abstract contract Morpho is Helpers, Events { morphoCompound.borrow(_poolTokenAddress, _amt, _maxGasForMatching); } - if(_isETH) convertWethToEth(_isETH, tokenInterface(_token), _amt); + if(_isETH) convertWethToEth(_isETH, tokenInterface(wethAddr), _amt); setUint(_setId, _amt); @@ -104,7 +102,7 @@ abstract contract Morpho is Helpers, Events { require(_pool == Underlying.AAVEV2 || _pool == Underlying.COMPOUNDV2, 'protocol not supported'); uint256 _amt = getUint(_getId, _amount); - bool _isETH ? _tokenAddress == ethAddr; + bool _isETH = _tokenAddress == ethAddr; address _token = _isETH ? wethAddr : _tokenAddress; if (_amt == uint256(-1)) _amt = _poolTokenAddress.balanceOf(address(this)); @@ -135,12 +133,12 @@ abstract contract Morpho is Helpers, Events { require(_pool == Underlying.AAVEV2 || _pool == Underlying.COMPOUNDV2, 'protocol not supported'); - bool _isETH ? _tokenAddress == ethAddr; + bool _isETH = _tokenAddress == ethAddr; uint256 _amt = getUint(_getId, _amount); address _token = _isETH ? wethAddr : _tokenAddress; if(_amt == uint256(-1)) { - _amt = _isETH ? _amt = address(this).balance : TokenInterface(_token).balanceOf(address(this)) + _amt = _isETH ? _amt = address(this).balance : TokenInterface(_token).balanceOf(address(this)); } if (_isETH) convertEthToWeth(_isETH, TokenInterface(_token), _amt); From 1bfd45527e447e149bd7e37891dc61a9773a8cd7 Mon Sep 17 00:00:00 2001 From: Shriya Tyagi Date: Wed, 12 Oct 2022 06:27:45 +0400 Subject: [PATCH 03/31] Morpho-Aave contracts set up --- .../{morpho => morpho-aave}/events.sol | 13 +- .../{morpho => morpho-aave}/helpers.sol | 8 +- .../connectors/morpho-aave/interface.sol | 55 +++++ .../mainnet/connectors/morpho-aave/main.sol | 222 ++++++++++++++++++ .../mainnet/connectors/morpho/interface.sol | 42 ---- contracts/mainnet/connectors/morpho/main.sol | 176 -------------- 6 files changed, 281 insertions(+), 235 deletions(-) rename contracts/mainnet/connectors/{morpho => morpho-aave}/events.sol (72%) rename contracts/mainnet/connectors/{morpho => morpho-aave}/helpers.sol (71%) create mode 100644 contracts/mainnet/connectors/morpho-aave/interface.sol create mode 100644 contracts/mainnet/connectors/morpho-aave/main.sol delete mode 100644 contracts/mainnet/connectors/morpho/interface.sol delete mode 100644 contracts/mainnet/connectors/morpho/main.sol diff --git a/contracts/mainnet/connectors/morpho/events.sol b/contracts/mainnet/connectors/morpho-aave/events.sol similarity index 72% rename from contracts/mainnet/connectors/morpho/events.sol rename to contracts/mainnet/connectors/morpho-aave/events.sol index f8f29f7b..47779061 100644 --- a/contracts/mainnet/connectors/morpho/events.sol +++ b/contracts/mainnet/connectors/morpho-aave/events.sol @@ -4,26 +4,22 @@ pragma experimental ABIEncoderV2; contract Events { event LogDeposit( - uint256 pool, address tokenAddress, address poolTokenAddress, uint256 amount, - uint256 maxGasForMatching, uint256 getId, uint256 setId ); event LogBorrow( - uint256 pool, + bool isETH, address poolTokenAddress, uint256 amount, - uint256 maxGasForMatching, uint256 getId, uint256 setId ); event LogWithdraw( - uint256 pool, bool isETH, address poolTokenAddress, uint256 amt, @@ -32,7 +28,6 @@ contract Events { ); event LogPayback( - uint256 pool, bool isETH, address poolTokenAddress, uint256 amt, @@ -40,9 +35,5 @@ contract Events { uint256 setId ); - event LogClaimed( - uint256 pool, - address[] tokenAddresses, - bool tradeForMorphoToken - ); + event LogClaimed(address[] tokenAddresses, bool tradeForMorphoToken); } diff --git a/contracts/mainnet/connectors/morpho/helpers.sol b/contracts/mainnet/connectors/morpho-aave/helpers.sol similarity index 71% rename from contracts/mainnet/connectors/morpho/helpers.sol rename to contracts/mainnet/connectors/morpho-aave/helpers.sol index d99da296..3d558573 100644 --- a/contracts/mainnet/connectors/morpho/helpers.sol +++ b/contracts/mainnet/connectors/morpho-aave/helpers.sol @@ -7,13 +7,9 @@ import "../../common/basic.sol"; import "../../common/interfaces.sol"; abstract contract Helpers is Stores, Basic { - IMorphoCore public constant morphoCompound = - IMorphoCore(0x8888882f8f843896699869179fB6E4f7e3B58888); IMorphoCore public constant morphoAave = IMorphoCore(0x777777c9898D384F785Ee44Acfe945efDFf5f3E0); - enum Underlying { - AAVEV2, - COMPOUNDV2 - } + IMorphoAaveLens public constant morphoAaveLens = + IMorphoAaveLens(0x507fA343d0A90786d86C7cd885f5C49263A91FF4); } diff --git a/contracts/mainnet/connectors/morpho-aave/interface.sol b/contracts/mainnet/connectors/morpho-aave/interface.sol new file mode 100644 index 00000000..fde267ca --- /dev/null +++ b/contracts/mainnet/connectors/morpho-aave/interface.sol @@ -0,0 +1,55 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.7.0; +pragma experimental ABIEncoderV2; + +interface IMorphoCore { + function supply( + address _poolTokenAddress, + address _onBehalf, + uint256 _amount + ) external; + + function borrow(address _poolTokenAddress, uint256 _amount) external; + + function withdraw(address _poolTokenAddress, uint256 _amount) external; + + function repay( + address _poolTokenAddress, + address _onBehalf, + uint256 _amount + ) external; + + function liquidate( + address _poolTokenBorrowedAddress, + address _poolTokenCollateralAddress, + address _borrower, + uint256 _amount + ) external; + + function claimRewards( + address[] calldata _tokenAddresses, + bool _tradeForMorphoToken + ) external; +} + +interface IMorphoAaveLens { + function _getCurrentBorrowBalanceInOf(address _poolToken, address _user) + external + view + returns ( + address underlyingToken, + uint256 balanceInP2P, + uint256 balanceOnPool, + uint256 totalBalance + ); + + function _getCurrentSupplyBalanceInOf(address _poolToken, address _user) + external + view + returns ( + address underlyingToken, + uint256 balanceInP2P, + uint256 balanceOnPool, + uint256 totalBalance + ); +} diff --git a/contracts/mainnet/connectors/morpho-aave/main.sol b/contracts/mainnet/connectors/morpho-aave/main.sol new file mode 100644 index 00000000..c3ef1501 --- /dev/null +++ b/contracts/mainnet/connectors/morpho-aave/main.sol @@ -0,0 +1,222 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.7.0; +pragma experimental ABIEncoderV2; +import "./helpers.sol"; +import "./events.sol"; + +abstract contract MorphoAave is Helpers, Events { + /** + * @dev Deposit ETH/ERC20_Token. + * @notice Deposit a token to Morpho Aave for lending / collaterization. + * @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 _getId ID to retrieve amt. + * @param _setId ID stores the amount of tokens deposited. + */ + function deposit( + address _tokenAddress, + address _poolTokenAddress, + uint256 _amount, + // uint256 _maxGasForMatching, // optional + 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); + + setUint(_setId, _amt); + + _eventName = "LogDeposit(address,address,uint256,uint256,uint256)"; + _eventParam = abi.encode( + _tokenAddress, + _poolTokenAddress, + _amt, + // _maxGasForMatching, + _getId, + _setId + ); + } + + /** + * @dev Borrow ETH/ERC20_Token. + * @notice Borrow a token from Morpho Aave. + * @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 _getId ID to retrieve amt. + * @param _setId ID stores the amount of tokens borrowed. + */ + function borrow( + 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); + + if (_isETH) convertWethToEth(_isETH, TokenInterface(wethAddr), _amt); + + setUint(_setId, _amt); + + _eventName = "LogBorrow(bool,address,uint256,uint256,uint256)"; + _eventParam = abi.encode( + _isETH, + _poolTokenAddress, + _amt, + // _maxGasForMatching, + _getId, + _setId + ); + } + + /** + * @dev Withdraw ETH/ERC20_Token. + * @notice Withdraw a token from Morpho Aave. + * @param _tokenAddress The address of underlying token to withdraw.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) + * @param _poolTokenAddress The address of aToken to withdraw.(For ETH: aWETH address) + * @param _amount The amount of the token (in underlying) to withdraw. (For max: `uint256(-1)`) + * @param _getId ID to retrieve amt. + * @param _setId ID stores the amount of tokens withdrawed. + */ + function withdraw( + address _tokenAddress, + address _poolTokenAddress, + uint256 _amount, + uint256 _getId, + uint256 _setId + ) + external + payable + returns (string memory _eventName, bytes memory _eventParam) + { + uint256 _amt = getUint(_getId, _amount); + bool _isETH = _tokenAddress == ethAddr; + address _token = _isETH ? wethAddr : _tokenAddress; + + if (_amt == uint256(-1)) + (, , , _amt) = morphoAaveLens._getCurrentSupplyBalanceInOf( + _poolTokenAddress, + address(this) + ); + + morphoAave.withdraw(_poolTokenAddress, _amt); + + convertWethToEth(_isETH, TokenInterface(_token), _amt); + + setUint(_setId, _amt); + + _eventName = "LogWithdraw(bool,address,uint256,uint256,uint256)"; + _eventParam = abi.encode( + _isETH, + _poolTokenAddress, + _amt, + _getId, + _setId + ); + } + + /** + * @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 _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 payback( + address _tokenAddress, + address _poolTokenAddress, + uint256 _amount, + uint256 _getId, + uint256 _setId + ) + external + payable + returns (string memory _eventName, bytes memory _eventParam) + { + bool _isETH = _tokenAddress == ethAddr; + uint256 _amt = getUint(_getId, _amount); + address _token = _isETH ? wethAddr : _tokenAddress; + + if (_amt == uint256(-1)) { + (, , , _amt) = morphoAaveLens._getCurrentBorrowBalanceInOf( + _poolTokenAddress, + address(this) + ); + } + + if (_isETH) convertEthToWeth(_isETH, TokenInterface(_token), _amt); + + approve(TokenInterface(_token), address(morphoAave), _amt); + + morphoAave.repay(_poolTokenAddress, address(this), _amt); + + setUint(_setId, _amt); + + _eventName = "LogPayback(bool,address,uint256,uint256,uint256)"; + _eventParam = abi.encode( + _isETH, + _poolTokenAddress, + _amt, + _getId, + _setId + ); + } + + /** + * @dev Claim rewards. + * @notice Claim rewards for the given assets from underlying protocol. + * @param _poolTokenAddresses The assets to claim rewards from (aToken or variable debt token).(For ETH: aToken or variable debt token address of WETH) + * @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? + bool _tradeForMorphoToken + ) + external + payable + returns (string memory _eventName, bytes memory _eventParam) + { + morphoAave.claimRewards(_poolTokenAddresses, _tradeForMorphoToken); + + _eventName = "LogClaimed(address[],bool)"; + _eventParam = abi.encode(_poolTokenAddresses, _tradeForMorphoToken); + } +} + +contract ConnectV2MorphoAave is MorphoAave { + string public constant name = "Morpho-Aave-v1.0"; +} diff --git a/contracts/mainnet/connectors/morpho/interface.sol b/contracts/mainnet/connectors/morpho/interface.sol deleted file mode 100644 index da192bb6..00000000 --- a/contracts/mainnet/connectors/morpho/interface.sol +++ /dev/null @@ -1,42 +0,0 @@ -//SPDX-License-Identifier: MIT -pragma solidity ^0.7.0; -pragma experimental ABIEncoderV2; - -interface IMorphoCore { - function supply( - address _poolTokenAddress, - address _onBehalf, - uint256 _amount, - uint256 _maxGasForMatching - ) external; - - function borrow( - address _poolTokenAddress, - uint256 _amount, - uint256 _maxGasForMatching - ) external; - - function withdraw( - address _poolTokenAddress, - uint256 _amount - ) external; - - function repay( - address _poolTokenAddress, - address _onBehalf, - 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[] _tokenAddresses, - bool _tradeForMorphoToken, - ) external; -} \ No newline at end of file diff --git a/contracts/mainnet/connectors/morpho/main.sol b/contracts/mainnet/connectors/morpho/main.sol deleted file mode 100644 index e6cde28d..00000000 --- a/contracts/mainnet/connectors/morpho/main.sol +++ /dev/null @@ -1,176 +0,0 @@ -//SPDX-License-Identifier: MIT -pragma solidity ^0.7.0; -pragma experimental ABIEncoderV2; -import './helpers.sol'; -import './events.sol'; -import 'hardhat/console.sol'; - -abstract contract Morpho is Helpers, Events { - - function deposit ( - Underlying _pool, - address _tokenAddress, - address _poolTokenAddress, // if address weth (send eth) - uint256 _amount, // if max, check balance - uint256 _maxGasForMatching, // optional - uint256 _getId, - uint256 _setId - ) external payable returns(string memory _eventName, bytes memory _eventParam) { - - require(_pool == Underlying.AAVEV2 || _pool == Underlying.COMPOUNDV2, 'underlying-protocol-not-supported'); - - 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); - - if(_pool == Underlying.AAVEV2) { - approve(_tokenContract, morphoAave, _amt); - morphoAave.supply(_poolTokenAddress, address(this), _amt, _maxGasForMatching); - } else { - approve(_tokenContract, morphoCompound, _amt); - morphoCompound.supply(_poolTokenAddress, address(this), _amt, _maxGasForMatching); - } - - setUint(_setId, _amt); - - _eventName = "LogDeposit(uint256,address,address,uint256,uint256,uint256,uint256)"; - _eventParam = abi.encode( - _pool, - _tokenAddress, - _poolTokenAddress, - _amt, - _maxGasForMatching, - _getId, - _setId - ); - } - - function borrow ( - Underlying _pool, - address _tokenAddress, - address _poolTokenAddress, //todo: dTokenaAddress? // if address weth (send eth) - uint256 _amount, - uint256 _maxGasForMatching, - uint256 _getId, - uint256 _setId - ) external payable returns(string memory _eventName, bytes memory _eventParam) { - - require(_pool == Underlying.AAVEV2 || _pool == Underlying.COMPOUNDV2, 'protocol-not-supported'); - - uint256 _amt = getUint(_getId, _amount); - - bool _isETH = _tokenAddress == ethAddr; - - if(_pool == Underlying.AAVEV2) { - morphoAave.borrow(_poolTokenAddress, _amt, _maxGasForMatching); - } else { - morphoCompound.borrow(_poolTokenAddress, _amt, _maxGasForMatching); - } - - if(_isETH) convertWethToEth(_isETH, tokenInterface(wethAddr), _amt); - - setUint(_setId, _amt); - - _eventName = "LogBorrow(uint256,address,uint256,uint256,uint256,uint256)"; - _eventParam = abi.encode( - _pool, - _poolTokenAddress, - _amt, - _maxGasForMatching, - _getId, - _setId - ); - } - - function withdraw ( - Underlying _pool, - address _tokenAddress, - address _poolTokenAddress, // if address weth (send eth) - uint256 _amount, // amount max - uint256 _getId, - uint256 _setId - ) external payable returns(string memory _eventName, bytes memory _eventParam) { - - require(_pool == Underlying.AAVEV2 || _pool == Underlying.COMPOUNDV2, 'protocol not supported'); - - uint256 _amt = getUint(_getId, _amount); - bool _isETH = _tokenAddress == ethAddr; - address _token = _isETH ? wethAddr : _tokenAddress; - - if (_amt == uint256(-1)) _amt = _poolTokenAddress.balanceOf(address(this)); - - if(_pool == Underlying.AAVEV2) { - morphoAave.withdraw(_poolTokenAddress, _amt); - } else { - morphoCompound.withdraw(_poolTokenAddress, _amt); - } - - convertWethToEth(_isETH, TokenInterface(_token), _amt); - - setUint(_setId, _amt); - - _eventName = "LogWithdraw(uint256,bool,address,uint256,uint256,uint256)"; - _eventParam = abi.encode(_pool, _isETH, _poolTokenAddress, _amt, _getId, _setId); - - } - - function payback ( - Underlying _pool, - address _tokenAddress, - address _poolTokenAddress, // if address weth (send eth) - uint256 _amount, // max value - uint256 _getId, - uint256 _setId - ) external payable returns(string memory _eventName, bytes memory _eventParam) { - - require(_pool == Underlying.AAVEV2 || _pool == Underlying.COMPOUNDV2, 'protocol not supported'); - - bool _isETH = _tokenAddress == ethAddr; - uint256 _amt = getUint(_getId, _amount); - address _token = _isETH ? wethAddr : _tokenAddress; - - if(_amt == uint256(-1)) { - _amt = _isETH ? _amt = address(this).balance : TokenInterface(_token).balanceOf(address(this)); - } - - if (_isETH) convertEthToWeth(_isETH, TokenInterface(_token), _amt); - - _pool == Underlying.AAVEV2 - ? morphoAave.repay(_poolTokenAddress, address(this), _amt) - : morphoCompound.repay(_poolTokenAddress, address(this), _amt); - - setUint(_setId, _amt); - - _eventName = "LogPayback(uint256,bool,address,uint256,uint256,uint256)"; - _eventParam = abi.encode(_pool, _isETH, _poolTokenAddress, _amt, _getId, _setId); - } - - function claim ( - Underlying _pool, - address[] _tokenAddresses, //todo: eth will be claimed as weth currently? - bool _tradeForMorphoToken - ) external payable returns(string memory _eventName, bytes memory _eventParam) { - - require(_pool == Underlying.AAVEV2 || _pool == Underlying.COMPOUNDV2, 'protocol not supported'); - - _pool == Underlying.AAVEV2 - ? morphoAave.claim(_tokenAddresses, _tradeForMorphoToken) - : morphoCompound.claim(_tokenAddresses, _tradeForMorphoToken); - - _eventName = "LogClaimed(uint256,address[],bool)"; - _eventParam = abi.encode(_pool, _tokenAddresses, _tradeForMorphoToken); - } - -} - -contract ConnectV2Morpho is Morpho { - string public constant name = "Morpho-v1.0"; -} From 0af912252ef9c77c7e07502a6ef8e73151790e50 Mon Sep 17 00:00:00 2001 From: Shriya Tyagi Date: Wed, 12 Oct 2022 06:29:40 +0400 Subject: [PATCH 04/31] Morpho-Compound contracts set up --- .../connectors/morpho-compound/events.sol | 39 ++++ .../connectors/morpho-compound/helpers.sol | 15 ++ .../connectors/morpho-compound/interface.sol | 60 +++++ .../connectors/morpho-compound/main.sol | 221 ++++++++++++++++++ 4 files changed, 335 insertions(+) create mode 100644 contracts/mainnet/connectors/morpho-compound/events.sol create mode 100644 contracts/mainnet/connectors/morpho-compound/helpers.sol create mode 100644 contracts/mainnet/connectors/morpho-compound/interface.sol create mode 100644 contracts/mainnet/connectors/morpho-compound/main.sol diff --git a/contracts/mainnet/connectors/morpho-compound/events.sol b/contracts/mainnet/connectors/morpho-compound/events.sol new file mode 100644 index 00000000..47779061 --- /dev/null +++ b/contracts/mainnet/connectors/morpho-compound/events.sol @@ -0,0 +1,39 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.7.0; +pragma experimental ABIEncoderV2; + +contract Events { + event LogDeposit( + address tokenAddress, + address poolTokenAddress, + uint256 amount, + uint256 getId, + uint256 setId + ); + + event LogBorrow( + bool isETH, + address poolTokenAddress, + uint256 amount, + uint256 getId, + uint256 setId + ); + + event LogWithdraw( + bool isETH, + address poolTokenAddress, + uint256 amt, + uint256 getId, + uint256 setId + ); + + event LogPayback( + bool isETH, + address poolTokenAddress, + uint256 amt, + uint256 getId, + uint256 setId + ); + + event LogClaimed(address[] tokenAddresses, bool tradeForMorphoToken); +} diff --git a/contracts/mainnet/connectors/morpho-compound/helpers.sol b/contracts/mainnet/connectors/morpho-compound/helpers.sol new file mode 100644 index 00000000..0ba31604 --- /dev/null +++ b/contracts/mainnet/connectors/morpho-compound/helpers.sol @@ -0,0 +1,15 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.7.0; +pragma experimental ABIEncoderV2; +import "./interface.sol"; +import "../../common/stores.sol"; +import "../../common/basic.sol"; +import "../../common/interfaces.sol"; + +abstract contract Helpers is Stores, Basic { + IMorphoCore public constant morphoCompound = + IMorphoCore(0x8888882f8f843896699869179fB6E4f7e3B58888); + + IMorphoCompoundLens public constant morphoCompoundLens = + IMorphoCompoundLens(0x930f1b46e1D081Ec1524efD95752bE3eCe51EF67); +} diff --git a/contracts/mainnet/connectors/morpho-compound/interface.sol b/contracts/mainnet/connectors/morpho-compound/interface.sol new file mode 100644 index 00000000..acb46a7d --- /dev/null +++ b/contracts/mainnet/connectors/morpho-compound/interface.sol @@ -0,0 +1,60 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.7.0; +pragma experimental ABIEncoderV2; + +interface IMorphoCore { + function supply( + address _poolTokenAddress, + address _onBehalf, + uint256 _amount + ) external; + + function borrow(address _poolTokenAddress, uint256 _amount) external; + + function withdraw(address _poolTokenAddress, uint256 _amount) external; + + function repay( + address _poolTokenAddress, + address _onBehalf, + 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 + ) external; +} + +interface IMorphoCompoundLens { + function getCurrentBorrowBalanceInOf( + address _poolTokenAddress, + address _user + ) + external + view + returns ( + uint256 balanceOnPool, + uint256 balanceInP2P, + uint256 totalBalance + ); + + function getCurrentSupplyBalanceInOf( + address _poolTokenAddress, + address _user + ) + external + view + returns ( + uint256 balanceOnPool, + uint256 balanceInP2P, + uint256 totalBalance + ); +} diff --git a/contracts/mainnet/connectors/morpho-compound/main.sol b/contracts/mainnet/connectors/morpho-compound/main.sol new file mode 100644 index 00000000..1bc3b903 --- /dev/null +++ b/contracts/mainnet/connectors/morpho-compound/main.sol @@ -0,0 +1,221 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.7.0; +pragma experimental ABIEncoderV2; +import "./helpers.sol"; +import "./events.sol"; + +abstract contract MorphoCompound is Helpers, Events { + /** + * @dev Deposit ETH/ERC20_Token. + * @notice Deposit a token to Morpho Compound for lending / collaterization. + * @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 _getId ID to retrieve amt. + * @param _setId ID stores the amount of tokens deposited. + */ + function deposit( + address _tokenAddress, + address _poolTokenAddress, + uint256 _amount, + // uint256 _maxGasForMatching, // optional + 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); + + setUint(_setId, _amt); + + _eventName = "LogDeposit(address,address,uint256,uint256,uint256)"; + _eventParam = abi.encode( + _tokenAddress, + _poolTokenAddress, + _amt, + // _maxGasForMatching, + _getId, + _setId + ); + } + + /** + * @dev Borrow ETH/ERC20_Token. + * @notice Borrow a token from Morpho Compound. + * @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 _getId ID to retrieve amt. + * @param _setId ID stores the amount of tokens borrowed. + */ + function borrow( + 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); + + if (_isETH) convertWethToEth(_isETH, TokenInterface(wethAddr), _amt); + + setUint(_setId, _amt); + + _eventName = "LogBorrow(bool,address,uint256,uint256,uint256)"; + _eventParam = abi.encode( + _isETH, + _poolTokenAddress, + _amt, + // _maxGasForMatching, + _getId, + _setId + ); + } + + /** + * @dev Withdraw ETH/ERC20_Token. + * @notice Withdraw a token from Morpho Compound. + * @param _tokenAddress The address of underlying token to withdraw.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) + * @param _poolTokenAddress The address of cToken to withdraw.(For ETH: cWETH address) + * @param _amount The amount of the token (in underlying) to withdraw. (For max: `uint256(-1)`) + * @param _getId ID to retrieve amt. + * @param _setId ID stores the amount of tokens withdrawed. + */ + function withdraw( + address _tokenAddress, + address _poolTokenAddress, + uint256 _amount, + uint256 _getId, + uint256 _setId + ) + external + payable + returns (string memory _eventName, bytes memory _eventParam) + { + uint256 _amt = getUint(_getId, _amount); + bool _isETH = _tokenAddress == ethAddr; + address _token = _isETH ? wethAddr : _tokenAddress; + + if (_amt == uint256(-1)) + (, , _amt) = morphoCompoundLens.getCurrentSupplyBalanceInOf( + _poolTokenAddress, + address(this) + ); + + morphoCompound.withdraw(_poolTokenAddress, _amt); + + convertWethToEth(_isETH, TokenInterface(_token), _amt); + + setUint(_setId, _amt); + + _eventName = "LogWithdraw(bool,address,uint256,uint256,uint256)"; + _eventParam = abi.encode( + _isETH, + _poolTokenAddress, + _amt, + _getId, + _setId + ); + } + + /** + * @dev Payback ETH/ERC20_Token. + * @notice Payback a token to Morpho Compound. + * @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 _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 payback( + address _tokenAddress, + address _poolTokenAddress, + uint256 _amount, + uint256 _getId, + uint256 _setId + ) + external + payable + returns (string memory _eventName, bytes memory _eventParam) + { + bool _isETH = _tokenAddress == ethAddr; + uint256 _amt = getUint(_getId, _amount); + address _token = _isETH ? wethAddr : _tokenAddress; + + if (_amt == uint256(-1)) { + (, , _amt) = morphoCompoundLens.getCurrentBorrowBalanceInOf( + _poolTokenAddress, + address(this) + ); + } + + if (_isETH) convertEthToWeth(_isETH, TokenInterface(_token), _amt); + + approve(TokenInterface(_token), address(morphoCompound), _amt); + + morphoCompound.repay(_poolTokenAddress, address(this), _amt); + + setUint(_setId, _amt); + + _eventName = "LogPayback(bool,address,uint256,uint256,uint256)"; + _eventParam = abi.encode( + _isETH, + _poolTokenAddress, + _amt, + _getId, + _setId + ); + } + + /** + * @dev Claim rewards. + * @notice Claim rewards for the given assets from underlying protocol. + * @param _poolTokenAddresses The cToken addresses to claim rewards from..(For ETH: cToken address of WETH) + * @param _tradeForMorphoToken Whether or not to trade COMP tokens for MORPHO tokens. + */ + function claim( + address[] calldata _poolTokenAddresses, + bool _tradeForMorphoToken + ) + external + payable + returns (string memory _eventName, bytes memory _eventParam) + { + morphoCompound.claimRewards(_poolTokenAddresses, _tradeForMorphoToken); + + _eventName = "LogClaimed(address[],bool)"; + _eventParam = abi.encode(_poolTokenAddresses, _tradeForMorphoToken); + } +} + +contract ConnectV2MorphoCompound is MorphoCompound { + string public constant name = "Morpho-Compound-v1.0"; +} From 421fbf8664841c4604fe8258ed68105ccad2f652 Mon Sep 17 00:00:00 2001 From: Shriya Tyagi Date: Wed, 12 Oct 2022 06:30:46 +0400 Subject: [PATCH 05/31] Added aToken and cTokenAddresses --- scripts/tests/mainnet/tokens.ts | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/scripts/tests/mainnet/tokens.ts b/scripts/tests/mainnet/tokens.ts index a5c4a67e..41f31237 100644 --- a/scripts/tests/mainnet/tokens.ts +++ b/scripts/tests/mainnet/tokens.ts @@ -12,6 +12,8 @@ export const tokens = { symbol: "ETH", name: "Ethereum", address: "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee", + aTokenAddress: "0x030bA81f1c18d280636F32af80b9AAd02Cf0854e", + cTokenAddress: "0x4Ddc2D193948926D02f9B1fE9e1daa0718270ED5", decimals: 18 }, dai: { @@ -19,6 +21,8 @@ export const tokens = { symbol: "DAI", name: "DAI Stable", address: "0x6B175474E89094C44Da98b954EedeAC495271d0F", + aTokenAddress: "0x028171bCA77440897B824Ca71D1c56caC55b68A3", + cTokenAddress: "0x5d3a536E4D6DbD6114cc1Ead35777bAB948E3643", decimals: 18 }, usdc: { @@ -26,6 +30,8 @@ export const tokens = { symbol: "USDC", name: "USD Coin", address: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", + aTokenAddress: "0xBcca60bB61934080951369a648Fb03DF4F96263C", + cTokenAddress: "0x39AA39c021dfbaE8faC545936693aC917d5E7563", decimals: 6 }, weth: { @@ -33,6 +39,8 @@ export const tokens = { symbol: "WETH", name: "Wrapped Ether", address: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2", + aTokenAddress: "0x030bA81f1c18d280636F32af80b9AAd02Cf0854e", + cTokenAddress: "0x4Ddc2D193948926D02f9B1fE9e1daa0718270ED5", decimals: 18 }, ens: { @@ -40,6 +48,7 @@ export const tokens = { symbol: "ENS", name: "Etherem Name Services", address: "0xC18360217D8F7Ab5e7c516566761Ea12Ce7F9D72", + aTokenAddress: "0x9a14e23A58edf4EFDcB360f68cd1b95ce2081a2F", decimals: 18 }, comp: { @@ -47,6 +56,7 @@ export const tokens = { symbol: "COMP", name: "Compound", address: "0xc00e94Cb662C3520282E6f5717214004A7f26888", + cTokenAddress: "0x70e36f6BF80a52b3B46b3aF8e106CC0ed743E8e4", decimals: 18 }, link: { @@ -54,6 +64,8 @@ export const tokens = { symbol: "LINK", name: "ChainLink Token", address: "0x514910771AF9Ca656af840dff83E8264EcF986CA", + aTokenAddress: "0xa06bC25B5805d5F8d82847D191Cb4Af5A3e873E0", + cTokenAddress: "0xFAce851a4921ce59e912d19329929CE6da6EB0c7", decimals: 18 }, uni: { @@ -61,10 +73,14 @@ export const tokens = { symbol: "UNI", name: "Uniswap", address: "0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984", + aTokenAddress: "0xB9D7CB55f463405CDfBe4E90a6D2Df01C2B92BF1", + cTokenAddress: "0x35A18000230DA775CAc24873d00Ff85BccdeD550", decimals: 18 } }; +export const dsaMaxValue = "115792089237316195423570985008687907853269984665640564039457584007913129639935"; + export const tokenMapping: Record = { usdc: { impersonateSigner: "0xfcb19e6a322b27c06842a71e8c725399f049ae3a", From 4788c599d240cd55e0b7d3302cd9f1c2022f5cc8 Mon Sep 17 00:00:00 2001 From: Shriya Tyagi Date: Wed, 12 Oct 2022 06:31:53 +0400 Subject: [PATCH 06/31] Morpho-Aave testcases added --- test/mainnet/morpho/morpho-aave.test.ts | 254 ++++++++++++++++++++++++ 1 file changed, 254 insertions(+) create mode 100644 test/mainnet/morpho/morpho-aave.test.ts diff --git a/test/mainnet/morpho/morpho-aave.test.ts b/test/mainnet/morpho/morpho-aave.test.ts new file mode 100644 index 00000000..2a3aef0d --- /dev/null +++ b/test/mainnet/morpho/morpho-aave.test.ts @@ -0,0 +1,254 @@ +import { expect } from "chai"; +import hre from "hardhat"; +import { abis } from "../../../scripts/constant/abis"; +import { addresses } from "../../../scripts/tests/mainnet/addresses"; +import { deployAndEnableConnector } from "../../../scripts/tests/deployAndEnableConnector"; +import { getMasterSigner } from "../../../scripts/tests/getMasterSigner"; +import { buildDSAv2 } from "../../../scripts/tests/buildDSAv2"; +import { ConnectV2MorphoAave__factory, IERC20Minimal__factory } from "../../../typechain"; +import { parseEther, parseUnits } from "@ethersproject/units"; +import { encodeSpells } from "../../../scripts/tests/encodeSpells"; +import { dsaMaxValue, tokens } from "../../../scripts/tests/mainnet/tokens"; +const { ethers } = hre; +import type { Signer, Contract } from "ethers"; + +const USDC = '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48' +const ACC_USDC = '0xe78388b4ce79068e89bf8aa7f218ef6b9ab0e9d0' +const Usdc = parseUnits('500', 6) + +const DAI = '0x6b175474e89094c44da98b954eedeac495271d0f' +const ACC_DAI = '0xcd6Eb888e76450eF584E8B51bB73c76ffBa21FF2' +const Dai = parseUnits('1', 18) + +const token_usdc = new ethers.Contract( + USDC, + IERC20Minimal__factory.abi, + ethers.provider, +) + +const token_dai = new ethers.Contract( + DAI, + IERC20Minimal__factory.abi, + ethers.provider, +) + +describe("Morpho-Aave", function () { + const connectorName = "MORPHO-AAVE-TEST-A"; + let connector: any; + + let wallet0: Signer, wallet1:Signer; + let dsaWallet0: any; + let instaConnectorsV2: Contract; + let masterSigner: Signer; + + before(async () => { + await hre.network.provider.request({ + method: "hardhat_reset", + params: [ + { + forking: { + // @ts-ignore + jsonRpcUrl: hre.config.networks.hardhat.forking.url, + blockNumber: 15718631, + }, + }, + ], + }); + [wallet0, wallet1] = await ethers.getSigners(); + masterSigner = await getMasterSigner(); + instaConnectorsV2 = await ethers.getContractAt( + abis.core.connectorsV2, + addresses.core.connectorsV2 + ); + connector = await deployAndEnableConnector({ + connectorName, + contractArtifact: ConnectV2MorphoAave__factory, + signer: masterSigner, + connectors: instaConnectorsV2, + }); + console.log("Connector address", connector.address); + }); + + it("should have contracts deployed", async () => { + expect(!!instaConnectorsV2.address).to.be.true; + expect(!!connector.address).to.be.true; + expect(!!(await masterSigner.getAddress())).to.be.true; + }); + + describe("DSA wallet setup", function () { + it("Should build DSA v2", async function () { + dsaWallet0 = await buildDSAv2(wallet0.getAddress()); + expect(!!dsaWallet0.address).to.be.true; + }); + + it("Deposit 10 ETH into DSA wallet", async function () { + await wallet0.sendTransaction({ + to: dsaWallet0.address, + value: parseEther("10"), + }); + expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.gte( + parseEther("10") + ); + }); + + it("Deposit 500 USDC into DSA wallet", async function () { + + await hre.network.provider.request({ + method: 'hardhat_impersonateAccount', + params: [ACC_USDC], + }) + + const signer_usdc = await ethers.getSigner(ACC_USDC) + await token_usdc.connect(signer_usdc).transfer(wallet0.getAddress(), Usdc) + + await hre.network.provider.request({ + method: 'hardhat_stopImpersonatingAccount', + params: [ACC_USDC], + }) + + await token_usdc.connect(wallet0).transfer(dsaWallet0.address, Usdc); + + expect(await token_usdc.connect(masterSigner).balanceOf(dsaWallet0.address)).to.be.gte( + parseUnits('500', 6) + ); + }); + + it("Deposit 1 DAI into DSA wallet", async function () { + + await hre.network.provider.request({ + method: 'hardhat_impersonateAccount', + params: [ACC_DAI], + }) + + const signer_dai = await ethers.getSigner(ACC_DAI) + await token_dai.connect(signer_dai).transfer(wallet0.getAddress(), Dai) + + await hre.network.provider.request({ + method: 'hardhat_stopImpersonatingAccount', + params: [ACC_DAI], + }) + + await token_dai.connect(wallet0).transfer(dsaWallet0.address, Dai); + + expect(await token_dai.connect(masterSigner).balanceOf(dsaWallet0.address)).to.be.gte( + parseUnits('1', 18) + ); + }); + }); + + describe("Main", function () { + + it("Should deposit ETH max", async function () { + const spells = [ + { + connector: connectorName, + method: "deposit", + args: [tokens.eth.address, tokens.eth.aTokenAddress, dsaMaxValue, "0", "0"], // 10 ETH + }, + ]; + + const tx = await dsaWallet0 + .connect(wallet0) + .cast(...encodeSpells(spells), wallet1.getAddress()); + + await tx.wait(); + expect(expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.lte( + parseUnits('0', 18)) + ); + }) + + it("Should deposit 10 USDC", async function () { + const spells = [ + { + connector: connectorName, + method: "deposit", + args: [tokens.usdc.address, tokens.usdc.aTokenAddress, "10000000", "0", "0"], // 10 USDC + }, + ]; + + const tx = await dsaWallet0 + .connect(wallet0) + .cast(...encodeSpells(spells), wallet1.getAddress()); + + await tx.wait(); + expect(await token_usdc.connect(wallet0).balanceOf(dsaWallet0.address)).to.be.lte( + parseUnits('490', 18) + ); + }) + + it("Should borrow DAI into DSA", async function () { + const spells = [ + { + connector: connectorName, + method: "borrow", + args: [tokens.dai.address, tokens.dai.aTokenAddress, "10000000000000000000", "0", "0"], // 10 DAI + }, + ]; + + const tx = await dsaWallet0 + .connect(wallet0) + .cast(...encodeSpells(spells), wallet1.getAddress()); + + await tx.wait(); + expect(await token_dai.connect(masterSigner).balanceOf(dsaWallet0.address)) + .to.be.gte(parseUnits('11', 18)); + }) + + it("Should payback DAI MAX", async function () { + const spells = [ + { + connector: connectorName, + method: "payback", + args: [tokens.dai.address, tokens.dai.aTokenAddress, dsaMaxValue, "0", "0"], // Max DAI + }, + ]; + + const tx = await dsaWallet0 + .connect(wallet0) + .cast(...encodeSpells(spells), wallet1.getAddress()); + + await tx.wait(); + expect(await token_dai.connect(masterSigner).balanceOf(dsaWallet0.address)).to.be.lte( + parseUnits('1', 18) + ); + }) + + it("Should withdraw ETH max", async function () { + const spells = [ + { + connector: connectorName, + method: "withdraw", + args: [tokens.eth.address, tokens.eth.aTokenAddress, dsaMaxValue, "0", "0"], // Max ETH + }, + ]; + + const tx = await dsaWallet0 + .connect(wallet0) + .cast(...encodeSpells(spells), wallet1.getAddress()); + + await tx.wait(); + expect(expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.gte( + parseUnits('10', 18)) + ); + }) + + it("Should withdraw 8 USDC", async function () { + const spells = [ + { + connector: connectorName, + method: "withdraw", + args: [tokens.usdc.address, tokens.usdc.aTokenAddress, "8000000", "0", "0"], // 8 USDC + }, + ]; + + const tx = await dsaWallet0 + .connect(wallet0) + .cast(...encodeSpells(spells), wallet1.getAddress()); + + await tx.wait(); + expect(expect(await token_usdc.connect(wallet0).balanceOf(dsaWallet0.address)).to.be.gte( + parseUnits('498', 6)) + ); + }) + }); +}); From 0674be882352efa631b9919458eed0ccc49375dc Mon Sep 17 00:00:00 2001 From: Shriya Tyagi Date: Wed, 12 Oct 2022 06:34:53 +0400 Subject: [PATCH 07/31] Morpho-Compound testcases --- test/mainnet/morpho/morpho-compound.tets.ts | 254 ++++++++++++++++++++ 1 file changed, 254 insertions(+) create mode 100644 test/mainnet/morpho/morpho-compound.tets.ts diff --git a/test/mainnet/morpho/morpho-compound.tets.ts b/test/mainnet/morpho/morpho-compound.tets.ts new file mode 100644 index 00000000..1d107427 --- /dev/null +++ b/test/mainnet/morpho/morpho-compound.tets.ts @@ -0,0 +1,254 @@ +import { expect } from "chai"; +import hre from "hardhat"; +import { abis } from "../../../scripts/constant/abis"; +import { addresses } from "../../../scripts/tests/mainnet/addresses"; +import { deployAndEnableConnector } from "../../../scripts/tests/deployAndEnableConnector"; +import { getMasterSigner } from "../../../scripts/tests/getMasterSigner"; +import { buildDSAv2 } from "../../../scripts/tests/buildDSAv2"; +import { ConnectV2MorphoCompound__factory, IERC20Minimal__factory } from "../../../typechain"; +import { parseEther, parseUnits } from "@ethersproject/units"; +import { encodeSpells } from "../../../scripts/tests/encodeSpells"; +import { dsaMaxValue, tokens } from "../../../scripts/tests/mainnet/tokens"; +const { ethers } = hre; +import type { Signer, Contract } from "ethers"; + +const USDC = '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48' +const ACC_USDC = '0xe78388b4ce79068e89bf8aa7f218ef6b9ab0e9d0' +const Usdc = parseUnits('500', 6) + +const DAI = '0x6b175474e89094c44da98b954eedeac495271d0f' +const ACC_DAI = '0xcd6Eb888e76450eF584E8B51bB73c76ffBa21FF2' +const Dai = parseUnits('1', 18) + +const token_usdc = new ethers.Contract( + USDC, + IERC20Minimal__factory.abi, + ethers.provider, +) + +const token_dai = new ethers.Contract( + DAI, + IERC20Minimal__factory.abi, + ethers.provider, +) + +describe("Morpho-Compound", function () { + const connectorName = "MORPHO-COMPOUND-TEST-A"; + let connector: any; + + let wallet0: Signer, wallet1:Signer; + let dsaWallet0: any; + let instaConnectorsV2: Contract; + let masterSigner: Signer; + + before(async () => { + await hre.network.provider.request({ + method: "hardhat_reset", + params: [ + { + forking: { + // @ts-ignore + jsonRpcUrl: hre.config.networks.hardhat.forking.url, + blockNumber: 15718631, + }, + }, + ], + }); + [wallet0, wallet1] = await ethers.getSigners(); + masterSigner = await getMasterSigner(); + instaConnectorsV2 = await ethers.getContractAt( + abis.core.connectorsV2, + addresses.core.connectorsV2 + ); + connector = await deployAndEnableConnector({ + connectorName, + contractArtifact: ConnectV2MorphoCompound__factory, + signer: masterSigner, + connectors: instaConnectorsV2, + }); + console.log("Connector address", connector.address); + }); + + it("should have contracts deployed", async () => { + expect(!!instaConnectorsV2.address).to.be.true; + expect(!!connector.address).to.be.true; + expect(!!(await masterSigner.getAddress())).to.be.true; + }); + + describe("DSA wallet setup", function () { + it("Should build DSA v2", async function () { + dsaWallet0 = await buildDSAv2(wallet0.getAddress()); + expect(!!dsaWallet0.address).to.be.true; + }); + + it("Deposit 10 ETH into DSA wallet", async function () { + await wallet0.sendTransaction({ + to: dsaWallet0.address, + value: parseEther("10"), + }); + expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.gte( + parseEther("10") + ); + }); + + it("Deposit 500 USDC into DSA wallet", async function () { + + await hre.network.provider.request({ + method: 'hardhat_impersonateAccount', + params: [ACC_USDC], + }) + + const signer_usdc = await ethers.getSigner(ACC_USDC) + await token_usdc.connect(signer_usdc).transfer(wallet0.getAddress(), Usdc) + + await hre.network.provider.request({ + method: 'hardhat_stopImpersonatingAccount', + params: [ACC_USDC], + }) + + await token_usdc.connect(wallet0).transfer(dsaWallet0.address, Usdc); + + expect(await token_usdc.connect(masterSigner).balanceOf(dsaWallet0.address)).to.be.gte( + parseUnits('500', 6) + ); + }); + + it("Deposit 1 DAI into DSA wallet", async function () { + + await hre.network.provider.request({ + method: 'hardhat_impersonateAccount', + params: [ACC_DAI], + }) + + const signer_dai = await ethers.getSigner(ACC_DAI) + await token_dai.connect(signer_dai).transfer(wallet0.getAddress(), Dai) + + await hre.network.provider.request({ + method: 'hardhat_stopImpersonatingAccount', + params: [ACC_DAI], + }) + + await token_dai.connect(wallet0).transfer(dsaWallet0.address, Dai); + + expect(await token_dai.connect(masterSigner).balanceOf(dsaWallet0.address)).to.be.gte( + parseUnits('1', 18) + ); + }); + }); + + describe("Main Morpho Compound", function () { + + it("Should deposit ETH max", async function () { + const spells = [ + { + connector: connectorName, + method: "deposit", + args: [tokens.eth.address, tokens.eth.cTokenAddress, dsaMaxValue, "0", "0"], // 10 ETH + }, + ]; + + const tx = await dsaWallet0 + .connect(wallet0) + .cast(...encodeSpells(spells), wallet1.getAddress()); + + await tx.wait(); + expect(expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.lte( + parseUnits('0', 18)) + ); + }) + + it("Should deposit 10 USDC", async function () { + const spells = [ + { + connector: connectorName, + method: "deposit", + args: [tokens.usdc.address, tokens.usdc.cTokenAddress, "10000000", "0", "0"], // 10 USDC + }, + ]; + + const tx = await dsaWallet0 + .connect(wallet0) + .cast(...encodeSpells(spells), wallet1.getAddress()); + + await tx.wait(); + expect(await token_usdc.connect(wallet0).balanceOf(dsaWallet0.address)).to.be.lte( + parseUnits('490', 18) + ); + }) + + it("Should borrow DAI into DSA", async function () { + const spells = [ + { + connector: connectorName, + method: "borrow", + args: [tokens.dai.address, tokens.dai.cTokenAddress, "10000000000000000000", "0", "0"], // 10 DAI + }, + ]; + + const tx = await dsaWallet0 + .connect(wallet0) + .cast(...encodeSpells(spells), wallet1.getAddress()); + + await tx.wait(); + expect(await token_dai.connect(masterSigner).balanceOf(dsaWallet0.address)) + .to.be.gte(parseUnits('11', 18)); + }) + + it("Should payback DAI MAX", async function () { + const spells = [ + { + connector: connectorName, + method: "payback", + args: [tokens.dai.address, tokens.dai.cTokenAddress, dsaMaxValue, "0", "0"], // Max DAI + }, + ]; + + const tx = await dsaWallet0 + .connect(wallet0) + .cast(...encodeSpells(spells), wallet1.getAddress()); + + await tx.wait(); + expect(await token_dai.connect(masterSigner).balanceOf(dsaWallet0.address)).to.be.lte( + parseUnits('1', 18) + ); + }) + + it("Should withdraw ETH max", async function () { + const spells = [ + { + connector: connectorName, + method: "withdraw", + args: [tokens.eth.address, tokens.eth.cTokenAddress, dsaMaxValue, "0", "0"], // Max ETH + }, + ]; + + const tx = await dsaWallet0 + .connect(wallet0) + .cast(...encodeSpells(spells), wallet1.getAddress()); + + await tx.wait(); + expect(expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.gte( + parseUnits('10', 18)) + ); + }) + + it("Should withdraw 8 USDC max", async function () { + const spells = [ + { + connector: connectorName, + method: "withdraw", + args: [tokens.usdc.address, tokens.usdc.cTokenAddress, "8000000", "0", "0"], // 8 USDC + }, + ]; + + const tx = await dsaWallet0 + .connect(wallet0) + .cast(...encodeSpells(spells), wallet1.getAddress()); + + await tx.wait(); + expect(expect(await token_usdc.connect(wallet0).balanceOf(dsaWallet0.address)).to.be.gte( + parseUnits('498', 6)) + ); + }) + }); +}); From cc8e7b71b03b3d409d4f95f74063b5855f46748f Mon Sep 17 00:00:00 2001 From: Shriya Tyagi Date: Thu, 13 Oct 2022 07:25:31 +0400 Subject: [PATCH 08/31] new functions added for Avae and Compound --- .../mainnet/connectors/morpho-aave/events.sol | 48 +++- .../connectors/morpho-aave/interface.sol | 20 +- .../mainnet/connectors/morpho-aave/main.sol | 256 ++++++++++++++++-- .../connectors/morpho-compound/events.sol | 42 ++- .../connectors/morpho-compound/interface.sol | 21 +- .../connectors/morpho-compound/main.sol | 255 +++++++++++++++-- 6 files changed, 582 insertions(+), 60 deletions(-) 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. From dfe82d308d01b27142c7855e0acd7db9c64fc7b2 Mon Sep 17 00:00:00 2001 From: Shriya Tyagi Date: Thu, 13 Oct 2022 07:26:42 +0400 Subject: [PATCH 09/31] testcases updated --- test/mainnet/morpho/morpho-aave.test.ts | 58 ++++++++++++++--- test/mainnet/morpho/morpho-compound.tets.ts | 70 +++++++++++++-------- 2 files changed, 93 insertions(+), 35 deletions(-) diff --git a/test/mainnet/morpho/morpho-aave.test.ts b/test/mainnet/morpho/morpho-aave.test.ts index 2a3aef0d..ebcabdeb 100644 --- a/test/mainnet/morpho/morpho-aave.test.ts +++ b/test/mainnet/morpho/morpho-aave.test.ts @@ -20,6 +20,8 @@ const DAI = '0x6b175474e89094c44da98b954eedeac495271d0f' const ACC_DAI = '0xcd6Eb888e76450eF584E8B51bB73c76ffBa21FF2' const Dai = parseUnits('1', 18) +const user = "0x41bc7d0687e6cea57fa26da78379dfdc5627c56d" + const token_usdc = new ethers.Contract( USDC, IERC20Minimal__factory.abi, @@ -49,7 +51,7 @@ describe("Morpho-Aave", function () { forking: { // @ts-ignore jsonRpcUrl: hre.config.networks.hardhat.forking.url, - blockNumber: 15718631, + blockNumber: 15714501, }, }, ], @@ -81,13 +83,13 @@ describe("Morpho-Aave", function () { expect(!!dsaWallet0.address).to.be.true; }); - it("Deposit 10 ETH into DSA wallet", async function () { + it("Deposit 1000 ETH into DSA wallet", async function () { await wallet0.sendTransaction({ to: dsaWallet0.address, - value: parseEther("10"), + value: parseEther("1000"), }); expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.gte( - parseEther("10") + parseEther("1000") ); }); @@ -138,12 +140,12 @@ describe("Morpho-Aave", function () { describe("Main", function () { - it("Should deposit ETH max", async function () { + it("Should deposit 10 ETH", async function () { const spells = [ { connector: connectorName, method: "deposit", - args: [tokens.eth.address, tokens.eth.aTokenAddress, dsaMaxValue, "0", "0"], // 10 ETH + args: [tokens.eth.address, tokens.eth.aTokenAddress, "10000000000000000000", "0", "0"], // 10 ETH }, ]; @@ -153,7 +155,7 @@ describe("Morpho-Aave", function () { await tx.wait(); expect(expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.lte( - parseUnits('0', 18)) + parseUnits('990', 18)) ); }) @@ -176,6 +178,25 @@ describe("Morpho-Aave", function () { ); }) + it("Should deposit 100 USDC on behalf", async function () { + const spells = [ + { + connector: connectorName, + method: "depositOnBehalf", + args: [tokens.usdc.address, tokens.usdc.aTokenAddress, user, "100000000", "0", "0"], // 100 USDC + }, + ]; + + const tx = await dsaWallet0 + .connect(wallet0) + .cast(...encodeSpells(spells), wallet1.getAddress()); + + await tx.wait(); + expect(await token_usdc.connect(wallet0).balanceOf(dsaWallet0.address)).to.be.lte( + parseUnits('390', 18) + ); + }) + it("Should borrow DAI into DSA", async function () { const spells = [ { @@ -213,6 +234,25 @@ describe("Morpho-Aave", function () { ); }) + it("Should payback ETH on behalf", async function () { + const spells = [ + { + connector: connectorName, + method: "paybackOnBehalf", + args: [tokens.eth.address, tokens.eth.aTokenAddress, user, dsaMaxValue, "0", "0"], // Max ETH + }, + ]; + + const tx = await dsaWallet0 + .connect(wallet0) + .cast(...encodeSpells(spells), wallet1.getAddress()); + + await tx.wait(); + expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.lte( + parseUnits('125', 18) + ); + }) + it("Should withdraw ETH max", async function () { const spells = [ { @@ -228,7 +268,7 @@ describe("Morpho-Aave", function () { await tx.wait(); expect(expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.gte( - parseUnits('10', 18)) + parseUnits('134', 18)) ); }) @@ -247,7 +287,7 @@ describe("Morpho-Aave", function () { await tx.wait(); expect(expect(await token_usdc.connect(wallet0).balanceOf(dsaWallet0.address)).to.be.gte( - parseUnits('498', 6)) + parseUnits('398', 6)) ); }) }); diff --git a/test/mainnet/morpho/morpho-compound.tets.ts b/test/mainnet/morpho/morpho-compound.tets.ts index 1d107427..461f59b9 100644 --- a/test/mainnet/morpho/morpho-compound.tets.ts +++ b/test/mainnet/morpho/morpho-compound.tets.ts @@ -18,7 +18,9 @@ const Usdc = parseUnits('500', 6) const DAI = '0x6b175474e89094c44da98b954eedeac495271d0f' const ACC_DAI = '0xcd6Eb888e76450eF584E8B51bB73c76ffBa21FF2' -const Dai = parseUnits('1', 18) +const Dai = parseUnits('400000', 18) + +const user = '0x5dd596c901987a2b28c38a9c1dfbf86fffc15d77' const token_usdc = new ethers.Contract( USDC, @@ -49,7 +51,7 @@ describe("Morpho-Compound", function () { forking: { // @ts-ignore jsonRpcUrl: hre.config.networks.hardhat.forking.url, - blockNumber: 15718631, + blockNumber: 15714501, }, }, ], @@ -113,7 +115,7 @@ describe("Morpho-Compound", function () { ); }); - it("Deposit 1 DAI into DSA wallet", async function () { + it("Deposit 400000 DAI into DSA wallet", async function () { await hre.network.provider.request({ method: 'hardhat_impersonateAccount', @@ -131,7 +133,7 @@ describe("Morpho-Compound", function () { await token_dai.connect(wallet0).transfer(dsaWallet0.address, Dai); expect(await token_dai.connect(masterSigner).balanceOf(dsaWallet0.address)).to.be.gte( - parseUnits('1', 18) + parseUnits('400000', 18) ); }); }); @@ -176,6 +178,25 @@ describe("Morpho-Compound", function () { ); }) + it("Should deposit 100 USDC on behalf", async function () { + const spells = [ + { + connector: connectorName, + method: "depositOnBehalf", + args: [tokens.usdc.address, tokens.usdc.cTokenAddress, user, "100000000", "0", "0"], // 100 USDC + }, + ]; + + const tx = await dsaWallet0 + .connect(wallet0) + .cast(...encodeSpells(spells), wallet1.getAddress()); + + await tx.wait(); + expect(await token_usdc.connect(wallet0).balanceOf(dsaWallet0.address)).to.be.lte( + parseUnits('390', 18) + ); + }) + it("Should borrow DAI into DSA", async function () { const spells = [ { @@ -191,10 +212,10 @@ describe("Morpho-Compound", function () { await tx.wait(); expect(await token_dai.connect(masterSigner).balanceOf(dsaWallet0.address)) - .to.be.gte(parseUnits('11', 18)); + .to.be.gte(parseUnits('400010', 18)); }) - it("Should payback DAI MAX", async function () { + it("Should payback DAI max", async function () { const spells = [ { connector: connectorName, @@ -209,10 +230,26 @@ describe("Morpho-Compound", function () { await tx.wait(); expect(await token_dai.connect(masterSigner).balanceOf(dsaWallet0.address)).to.be.lte( - parseUnits('1', 18) + parseUnits('400000', 18) ); }) + it("Should payback DAI on behalf", async function () { + const spells = [ + { + connector: connectorName, + method: "paybackOnBehalf", + args: [tokens.dai.address, tokens.dai.cTokenAddress, user, dsaMaxValue, "0", "0"], + }, + ]; + + const tx = await dsaWallet0 + .connect(wallet0) + .cast(...encodeSpells(spells), wallet1.getAddress()); + + await tx.wait(); + }) + it("Should withdraw ETH max", async function () { const spells = [ { @@ -231,24 +268,5 @@ describe("Morpho-Compound", function () { parseUnits('10', 18)) ); }) - - it("Should withdraw 8 USDC max", async function () { - const spells = [ - { - connector: connectorName, - method: "withdraw", - args: [tokens.usdc.address, tokens.usdc.cTokenAddress, "8000000", "0", "0"], // 8 USDC - }, - ]; - - const tx = await dsaWallet0 - .connect(wallet0) - .cast(...encodeSpells(spells), wallet1.getAddress()); - - await tx.wait(); - expect(expect(await token_usdc.connect(wallet0).balanceOf(dsaWallet0.address)).to.be.gte( - parseUnits('498', 6)) - ); - }) }); }); From d89ef2dabc9679f3546aa5de748a3beeda95d7e6 Mon Sep 17 00:00:00 2001 From: Shriya Tyagi <47134275+shriyatyagii@users.noreply.github.com> Date: Sat, 15 Oct 2022 05:18:10 +0400 Subject: [PATCH 10/31] Update contracts/mainnet/connectors/morpho-aave/main.sol Co-authored-by: Merlin Egalite <44097430+MerlinEgalite@users.noreply.github.com> --- contracts/mainnet/connectors/morpho-aave/main.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contracts/mainnet/connectors/morpho-aave/main.sol b/contracts/mainnet/connectors/morpho-aave/main.sol index 85b16395..bc4c49ba 100644 --- a/contracts/mainnet/connectors/morpho-aave/main.sol +++ b/contracts/mainnet/connectors/morpho-aave/main.sol @@ -280,7 +280,7 @@ abstract contract MorphoAave is Helpers, Events { address _token = _isETH ? wethAddr : _tokenAddress; if (_amt == uint256(-1)) - (, , , _amt) = morphoAaveLens._getCurrentSupplyBalanceInOf( + (, , , _amt) = morphoAaveLens.getCurrentSupplyBalanceInOf( _poolTokenAddress, address(this) ); From 368f9f52e70aa28ed72010e0f96f40c8426aa687 Mon Sep 17 00:00:00 2001 From: Shriya Tyagi <47134275+shriyatyagii@users.noreply.github.com> Date: Sat, 15 Oct 2022 05:18:20 +0400 Subject: [PATCH 11/31] Update contracts/mainnet/connectors/morpho-aave/main.sol Co-authored-by: Merlin Egalite <44097430+MerlinEgalite@users.noreply.github.com> --- contracts/mainnet/connectors/morpho-aave/main.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contracts/mainnet/connectors/morpho-aave/main.sol b/contracts/mainnet/connectors/morpho-aave/main.sol index bc4c49ba..bc5e342f 100644 --- a/contracts/mainnet/connectors/morpho-aave/main.sol +++ b/contracts/mainnet/connectors/morpho-aave/main.sol @@ -334,7 +334,7 @@ abstract contract MorphoAave is Helpers, Events { : _tokenContract.balanceOf(address(this)); (, , , uint256 _amtDebt) = morphoAaveLens - ._getCurrentBorrowBalanceInOf(_poolTokenAddress, address(this)); + .getCurrentBorrowBalanceInOf(_poolTokenAddress, address(this)); _amt = _amtDSA <= _amtDebt ? _amtDSA : _amtDebt; } From 02af01637943ec4c696a1695c6e24981f74d4f66 Mon Sep 17 00:00:00 2001 From: Shriya Tyagi <47134275+shriyatyagii@users.noreply.github.com> Date: Sat, 15 Oct 2022 05:22:14 +0400 Subject: [PATCH 12/31] Update contracts/mainnet/connectors/morpho-compound/helpers.sol Co-authored-by: Merlin Egalite <44097430+MerlinEgalite@users.noreply.github.com> --- contracts/mainnet/connectors/morpho-compound/helpers.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contracts/mainnet/connectors/morpho-compound/helpers.sol b/contracts/mainnet/connectors/morpho-compound/helpers.sol index 0ba31604..eee8278f 100644 --- a/contracts/mainnet/connectors/morpho-compound/helpers.sol +++ b/contracts/mainnet/connectors/morpho-compound/helpers.sol @@ -7,7 +7,7 @@ import "../../common/basic.sol"; import "../../common/interfaces.sol"; abstract contract Helpers is Stores, Basic { - IMorphoCore public constant morphoCompound = + IMorphoCore public constant MORPHO_COMPOUND = IMorphoCore(0x8888882f8f843896699869179fB6E4f7e3B58888); IMorphoCompoundLens public constant morphoCompoundLens = From 230e67c1068e32d0a80b659cfeb8beda508a1ece Mon Sep 17 00:00:00 2001 From: Shriya Tyagi <47134275+shriyatyagii@users.noreply.github.com> Date: Sat, 15 Oct 2022 05:25:06 +0400 Subject: [PATCH 13/31] Update contracts/mainnet/connectors/morpho-compound/main.sol Co-authored-by: Romain Milon --- contracts/mainnet/connectors/morpho-compound/main.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contracts/mainnet/connectors/morpho-compound/main.sol b/contracts/mainnet/connectors/morpho-compound/main.sol index 93d00887..a6f2ce0c 100644 --- a/contracts/mainnet/connectors/morpho-compound/main.sol +++ b/contracts/mainnet/connectors/morpho-compound/main.sol @@ -39,7 +39,7 @@ abstract contract MorphoCompound is Helpers, Events { : _tokenContract.balanceOf(address(this)); } - if (_isETH) convertEthToWeth(_isETH, _tokenContract, _amt); + convertEthToWeth(_isETH, _tokenContract, _amt); approve(_tokenContract, address(morphoCompound), _amt); morphoCompound.supply(_poolTokenAddress, address(this), _amt); From ce8a9e3c21ea5db211c5e564e17014b2a3a55c52 Mon Sep 17 00:00:00 2001 From: Shriya Tyagi <47134275+shriyatyagii@users.noreply.github.com> Date: Sat, 15 Oct 2022 05:25:35 +0400 Subject: [PATCH 14/31] Update contracts/mainnet/connectors/morpho-compound/main.sol Co-authored-by: Romain Milon --- contracts/mainnet/connectors/morpho-compound/main.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contracts/mainnet/connectors/morpho-compound/main.sol b/contracts/mainnet/connectors/morpho-compound/main.sol index a6f2ce0c..66845a89 100644 --- a/contracts/mainnet/connectors/morpho-compound/main.sol +++ b/contracts/mainnet/connectors/morpho-compound/main.sol @@ -92,7 +92,7 @@ abstract contract MorphoCompound is Helpers, Events { : _tokenContract.balanceOf(address(this)); } - if (_isETH) convertEthToWeth(_isETH, _tokenContract, _amt); + convertEthToWeth(_isETH, _tokenContract, _amt); approve(_tokenContract, address(morphoCompound), _amt); morphoCompound.supply( From fa00586039a823fddf65ea2586a925d46b670bab Mon Sep 17 00:00:00 2001 From: Shriya Tyagi <47134275+shriyatyagii@users.noreply.github.com> Date: Sat, 15 Oct 2022 05:25:54 +0400 Subject: [PATCH 15/31] Update contracts/mainnet/connectors/morpho-compound/main.sol Co-authored-by: Romain Milon --- contracts/mainnet/connectors/morpho-compound/main.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contracts/mainnet/connectors/morpho-compound/main.sol b/contracts/mainnet/connectors/morpho-compound/main.sol index 66845a89..58aa1d37 100644 --- a/contracts/mainnet/connectors/morpho-compound/main.sol +++ b/contracts/mainnet/connectors/morpho-compound/main.sol @@ -151,7 +151,7 @@ abstract contract MorphoCompound is Helpers, Events { : _tokenContract.balanceOf(address(this)); } - if (_isETH) convertEthToWeth(_isETH, _tokenContract, _amt); + convertEthToWeth(_isETH, _tokenContract, _amt); approve(_tokenContract, address(morphoCompound), _amt); morphoCompound.supply(_poolTokenAddress, _onBehalf, _amt); From eb8b73b608f679b1dd4d0ca51f40de80f78cd596 Mon Sep 17 00:00:00 2001 From: Shriya Tyagi <47134275+shriyatyagii@users.noreply.github.com> Date: Sat, 15 Oct 2022 05:26:06 +0400 Subject: [PATCH 16/31] Update contracts/mainnet/connectors/morpho-compound/main.sol Co-authored-by: Romain Milon --- contracts/mainnet/connectors/morpho-compound/main.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contracts/mainnet/connectors/morpho-compound/main.sol b/contracts/mainnet/connectors/morpho-compound/main.sol index 58aa1d37..276b9814 100644 --- a/contracts/mainnet/connectors/morpho-compound/main.sol +++ b/contracts/mainnet/connectors/morpho-compound/main.sol @@ -195,7 +195,7 @@ abstract contract MorphoCompound is Helpers, Events { morphoCompound.borrow(_poolTokenAddress, _amt); - if (_isETH) convertWethToEth(_isETH, TokenInterface(wethAddr), _amt); + convertWethToEth(_isETH, TokenInterface(wethAddr), _amt); setUint(_setId, _amt); From 1d3e65efb912c934350fc2b6c325d84e5de2efde Mon Sep 17 00:00:00 2001 From: Shriya Tyagi <47134275+shriyatyagii@users.noreply.github.com> Date: Sat, 15 Oct 2022 05:26:13 +0400 Subject: [PATCH 17/31] Update contracts/mainnet/connectors/morpho-compound/main.sol Co-authored-by: Romain Milon --- contracts/mainnet/connectors/morpho-compound/main.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contracts/mainnet/connectors/morpho-compound/main.sol b/contracts/mainnet/connectors/morpho-compound/main.sol index 276b9814..92f83b60 100644 --- a/contracts/mainnet/connectors/morpho-compound/main.sol +++ b/contracts/mainnet/connectors/morpho-compound/main.sol @@ -237,7 +237,7 @@ abstract contract MorphoCompound is Helpers, Events { morphoCompound.borrow(_poolTokenAddress, _amt, _maxGasForMatching); - if (_isETH) convertWethToEth(_isETH, TokenInterface(wethAddr), _amt); + convertWethToEth(_isETH, TokenInterface(wethAddr), _amt); setUint(_setId, _amt); From ed16a59387025be654841bf8f72bd0934090b509 Mon Sep 17 00:00:00 2001 From: Shriya Tyagi <47134275+shriyatyagii@users.noreply.github.com> Date: Sat, 15 Oct 2022 05:26:44 +0400 Subject: [PATCH 18/31] Update contracts/mainnet/connectors/morpho-compound/main.sol Co-authored-by: Romain Milon --- contracts/mainnet/connectors/morpho-compound/main.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contracts/mainnet/connectors/morpho-compound/main.sol b/contracts/mainnet/connectors/morpho-compound/main.sol index 92f83b60..13baf6e2 100644 --- a/contracts/mainnet/connectors/morpho-compound/main.sol +++ b/contracts/mainnet/connectors/morpho-compound/main.sol @@ -337,7 +337,7 @@ abstract contract MorphoCompound is Helpers, Events { _amt = _amtDSA <= _amtDebt ? _amtDSA : _amtDebt; } - if (_isETH) convertEthToWeth(_isETH, _tokenContract, _amt); + convertEthToWeth(_isETH, _tokenContract, _amt); approve(_tokenContract, address(morphoCompound), _amt); From 3eba9e8d7108d047fa39dcf50d3f934af5c05e82 Mon Sep 17 00:00:00 2001 From: Shriya Tyagi <47134275+shriyatyagii@users.noreply.github.com> Date: Sat, 15 Oct 2022 05:27:01 +0400 Subject: [PATCH 19/31] Update contracts/mainnet/connectors/morpho-compound/main.sol Co-authored-by: Romain Milon --- contracts/mainnet/connectors/morpho-compound/main.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contracts/mainnet/connectors/morpho-compound/main.sol b/contracts/mainnet/connectors/morpho-compound/main.sol index 13baf6e2..efa593ce 100644 --- a/contracts/mainnet/connectors/morpho-compound/main.sol +++ b/contracts/mainnet/connectors/morpho-compound/main.sol @@ -395,7 +395,7 @@ abstract contract MorphoCompound is Helpers, Events { _amt = _amtDSA <= _amtDebt ? _amtDSA : _amtDebt; } - if (_isETH) convertEthToWeth(_isETH, _tokenContract, _amt); + convertEthToWeth(_isETH, _tokenContract, _amt); approve(_tokenContract, address(morphoCompound), _amt); From bc9abcd4d60308ec405bed917a6dc02d5bcfddbd Mon Sep 17 00:00:00 2001 From: Shriya Tyagi Date: Sat, 15 Oct 2022 08:09:53 +0400 Subject: [PATCH 20/31] Morpho-Compound: changes added --- .../connectors/morpho-compound/events.sol | 6 +- .../connectors/morpho-compound/helpers.sol | 24 +++- .../connectors/morpho-compound/interface.sol | 4 +- .../connectors/morpho-compound/main.sol | 124 +++++++----------- ...mpound.tets.ts => morpho-compound.test.ts} | 0 5 files changed, 80 insertions(+), 78 deletions(-) rename test/mainnet/morpho/{morpho-compound.tets.ts => morpho-compound.test.ts} (100%) diff --git a/contracts/mainnet/connectors/morpho-compound/events.sol b/contracts/mainnet/connectors/morpho-compound/events.sol index 7516cb1f..41bec370 100644 --- a/contracts/mainnet/connectors/morpho-compound/events.sol +++ b/contracts/mainnet/connectors/morpho-compound/events.sol @@ -71,5 +71,9 @@ contract Events { uint256 setId ); - event LogClaimed(address[] tokenAddresses, bool tradeForMorphoToken); + event LogClaimed( + address[] tokenAddresses, + bool tradeForMorphoToken, + uint256 amountOfRewards + ); } diff --git a/contracts/mainnet/connectors/morpho-compound/helpers.sol b/contracts/mainnet/connectors/morpho-compound/helpers.sol index eee8278f..6a64a605 100644 --- a/contracts/mainnet/connectors/morpho-compound/helpers.sol +++ b/contracts/mainnet/connectors/morpho-compound/helpers.sol @@ -10,6 +10,28 @@ abstract contract Helpers is Stores, Basic { IMorphoCore public constant MORPHO_COMPOUND = IMorphoCore(0x8888882f8f843896699869179fB6E4f7e3B58888); - IMorphoCompoundLens public constant morphoCompoundLens = + IMorphoCompoundLens public constant MORPHO_COMPOUND_LENS = IMorphoCompoundLens(0x930f1b46e1D081Ec1524efD95752bE3eCe51EF67); + + function _performEthToWethConversion( + address _tokenAddress, + uint256 _amount, + uint256 _getId + ) internal returns (TokenInterface _tokenContract, uint256 _amt) { + _amt = getUint(_getId, _amount); + + bool _isETH = _tokenAddress == ethAddr; + + _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); + } } diff --git a/contracts/mainnet/connectors/morpho-compound/interface.sol b/contracts/mainnet/connectors/morpho-compound/interface.sol index 5a2c751f..5a98876a 100644 --- a/contracts/mainnet/connectors/morpho-compound/interface.sol +++ b/contracts/mainnet/connectors/morpho-compound/interface.sol @@ -33,9 +33,9 @@ interface IMorphoCore { ) external; function claimRewards( - address[] calldata _tokenAddresses, + address[] calldata _cTokenAddresses, bool _tradeForMorphoToken - ) external; + ) external returns (uint256 amountOfRewards); } interface IMorphoCompoundLens { diff --git a/contracts/mainnet/connectors/morpho-compound/main.sol b/contracts/mainnet/connectors/morpho-compound/main.sol index efa593ce..171ab731 100644 --- a/contracts/mainnet/connectors/morpho-compound/main.sol +++ b/contracts/mainnet/connectors/morpho-compound/main.sol @@ -25,24 +25,14 @@ abstract contract MorphoCompound is Helpers, Events { payable returns (string memory _eventName, bytes memory _eventParam) { - uint256 _amt = getUint(_getId, _amount); + ( + TokenInterface _tokenContract, + uint256 _amt + ) = _performEthToWethConversion(_tokenAddress, _amount, _getId); - bool _isETH = _tokenAddress == ethAddr; + approve(_tokenContract, address(MORPHO_COMPOUND), _amt); - TokenInterface _tokenContract = _isETH - ? TokenInterface(wethAddr) - : TokenInterface(_tokenAddress); - - if (_amt == uint256(-1)) { - _amt = _isETH - ? address(this).balance - : _tokenContract.balanceOf(address(this)); - } - - convertEthToWeth(_isETH, _tokenContract, _amt); - - approve(_tokenContract, address(morphoCompound), _amt); - morphoCompound.supply(_poolTokenAddress, address(this), _amt); + MORPHO_COMPOUND.supply(_poolTokenAddress, address(this), _amt); setUint(_setId, _amt); @@ -78,24 +68,14 @@ abstract contract MorphoCompound is Helpers, Events { payable returns (string memory _eventName, bytes memory _eventParam) { - uint256 _amt = getUint(_getId, _amount); + ( + TokenInterface _tokenContract, + uint256 _amt + ) = _performEthToWethConversion(_tokenAddress, _amount, _getId); - bool _isETH = _tokenAddress == ethAddr; + approve(_tokenContract, address(MORPHO_COMPOUND), _amt); - TokenInterface _tokenContract = _isETH - ? TokenInterface(wethAddr) - : TokenInterface(_tokenAddress); - - if (_amt == uint256(-1)) { - _amt = _isETH - ? address(this).balance - : _tokenContract.balanceOf(address(this)); - } - - convertEthToWeth(_isETH, _tokenContract, _amt); - - approve(_tokenContract, address(morphoCompound), _amt); - morphoCompound.supply( + MORPHO_COMPOUND.supply( _poolTokenAddress, address(this), _amt, @@ -137,24 +117,14 @@ abstract contract MorphoCompound is Helpers, Events { payable returns (string memory _eventName, bytes memory _eventParam) { - uint256 _amt = getUint(_getId, _amount); + ( + TokenInterface _tokenContract, + uint256 _amt + ) = _performEthToWethConversion(_tokenAddress, _amount, _getId); - bool _isETH = _tokenAddress == ethAddr; + approve(_tokenContract, address(MORPHO_COMPOUND), _amt); - TokenInterface _tokenContract = _isETH - ? TokenInterface(wethAddr) - : TokenInterface(_tokenAddress); - - if (_amt == uint256(-1)) { - _amt = _isETH - ? address(this).balance - : _tokenContract.balanceOf(address(this)); - } - - convertEthToWeth(_isETH, _tokenContract, _amt); - - approve(_tokenContract, address(morphoCompound), _amt); - morphoCompound.supply(_poolTokenAddress, _onBehalf, _amt); + MORPHO_COMPOUND.supply(_poolTokenAddress, _onBehalf, _amt); setUint(_setId, _amt); @@ -193,9 +163,9 @@ abstract contract MorphoCompound is Helpers, Events { bool _isETH = _tokenAddress == ethAddr; - morphoCompound.borrow(_poolTokenAddress, _amt); + MORPHO_COMPOUND.borrow(_poolTokenAddress, _amt); - convertWethToEth(_isETH, TokenInterface(wethAddr), _amt); + if (_isETH) convertWethToEth(_isETH, TokenInterface(wethAddr), _amt); setUint(_setId, _amt); @@ -235,9 +205,9 @@ abstract contract MorphoCompound is Helpers, Events { bool _isETH = _tokenAddress == ethAddr; - morphoCompound.borrow(_poolTokenAddress, _amt, _maxGasForMatching); + MORPHO_COMPOUND.borrow(_poolTokenAddress, _amt, _maxGasForMatching); - convertWethToEth(_isETH, TokenInterface(wethAddr), _amt); + if (_isETH) convertWethToEth(_isETH, TokenInterface(wethAddr), _amt); setUint(_setId, _amt); @@ -274,18 +244,17 @@ abstract contract MorphoCompound is Helpers, Events { { uint256 _amt = getUint(_getId, _amount); bool _isETH = _tokenAddress == ethAddr; - address _token = _isETH ? wethAddr : _tokenAddress; if (_amt == uint256(-1)) { - (, , _amt) = morphoCompoundLens.getCurrentSupplyBalanceInOf( + (, , _amt) = MORPHO_COMPOUND_LENS.getCurrentSupplyBalanceInOf( _poolTokenAddress, address(this) ); } - morphoCompound.withdraw(_poolTokenAddress, _amt); + MORPHO_COMPOUND.withdraw(_poolTokenAddress, _amt); - convertWethToEth(_isETH, TokenInterface(_token), _amt); + if (_isETH) convertWethToEth(_isETH, TokenInterface(wethAddr), _amt); setUint(_setId, _amt); @@ -322,8 +291,8 @@ abstract contract MorphoCompound is Helpers, Events { bool _isETH = _tokenAddress == ethAddr; uint256 _amt = getUint(_getId, _amount); - TokenInterface _tokenContract = _isETH - ? TokenInterface(wethAddr) + TokenInterface _tokenContract = _isETH + ? TokenInterface(wethAddr) : TokenInterface(_tokenAddress); if (_amt == uint256(-1)) { @@ -331,17 +300,17 @@ abstract contract MorphoCompound is Helpers, Events { ? address(this).balance : _tokenContract.balanceOf(address(this)); - (, , uint256 _amtDebt) = morphoCompoundLens + (, , uint256 _amtDebt) = MORPHO_COMPOUND_LENS .getCurrentBorrowBalanceInOf(_poolTokenAddress, address(this)); - _amt = _amtDSA <= _amtDebt ? _amtDSA : _amtDebt; + _amt = _amtDSA < _amtDebt ? _amtDSA : _amtDebt; } - convertEthToWeth(_isETH, _tokenContract, _amt); + if (_isETH) convertEthToWeth(_isETH, _tokenContract, _amt); - approve(_tokenContract, address(morphoCompound), _amt); + approve(_tokenContract, address(MORPHO_COMPOUND), _amt); - morphoCompound.repay(_poolTokenAddress, address(this), _amt); + MORPHO_COMPOUND.repay(_poolTokenAddress, address(this), _amt); setUint(_setId, _amt); @@ -379,9 +348,9 @@ abstract contract MorphoCompound is Helpers, Events { { bool _isETH = _tokenAddress == ethAddr; uint256 _amt = getUint(_getId, _amount); - - TokenInterface _tokenContract = _isETH - ? TokenInterface(wethAddr) + + TokenInterface _tokenContract = _isETH + ? TokenInterface(wethAddr) : TokenInterface(_tokenAddress); if (_amt == uint256(-1)) { @@ -389,17 +358,17 @@ abstract contract MorphoCompound is Helpers, Events { ? address(this).balance : _tokenContract.balanceOf(address(this)); - (, , uint256 _amtDebt) = morphoCompoundLens + (, , uint256 _amtDebt) = MORPHO_COMPOUND_LENS .getCurrentBorrowBalanceInOf(_poolTokenAddress, _onBehalf); - _amt = _amtDSA <= _amtDebt ? _amtDSA : _amtDebt; + _amt = _amtDSA < _amtDebt ? _amtDSA : _amtDebt; } - convertEthToWeth(_isETH, _tokenContract, _amt); + if (_isETH) convertEthToWeth(_isETH, _tokenContract, _amt); - approve(_tokenContract, address(morphoCompound), _amt); + approve(_tokenContract, address(MORPHO_COMPOUND), _amt); - morphoCompound.repay(_poolTokenAddress, _onBehalf, _amt); + MORPHO_COMPOUND.repay(_poolTokenAddress, _onBehalf, _amt); setUint(_setId, _amt); @@ -428,10 +397,17 @@ abstract contract MorphoCompound is Helpers, Events { payable returns (string memory _eventName, bytes memory _eventParam) { - morphoCompound.claimRewards(_poolTokenAddresses, _tradeForMorphoToken); + uint256 _amountOfRewards = MORPHO_COMPOUND.claimRewards( + _poolTokenAddresses, + _tradeForMorphoToken + ); - _eventName = "LogClaimed(address[],bool)"; - _eventParam = abi.encode(_poolTokenAddresses, _tradeForMorphoToken); + _eventName = "LogClaimed(address[],bool,uint256)"; + _eventParam = abi.encode( + _poolTokenAddresses, + _tradeForMorphoToken, + _amountOfRewards + ); } } diff --git a/test/mainnet/morpho/morpho-compound.tets.ts b/test/mainnet/morpho/morpho-compound.test.ts similarity index 100% rename from test/mainnet/morpho/morpho-compound.tets.ts rename to test/mainnet/morpho/morpho-compound.test.ts From f6db02d83a6aef7f0f9c3be55707c843adae1688 Mon Sep 17 00:00:00 2001 From: Shriya Tyagi Date: Sat, 15 Oct 2022 08:17:36 +0400 Subject: [PATCH 21/31] Morpho-Aave: changes added --- .../mainnet/connectors/morpho-aave/events.sol | 6 +- .../connectors/morpho-aave/helpers.sol | 26 +++- .../connectors/morpho-aave/interface.sol | 13 +- .../mainnet/connectors/morpho-aave/main.sol | 117 +++++++----------- test/mainnet/morpho/morpho-aave.test.ts | 4 +- 5 files changed, 81 insertions(+), 85 deletions(-) diff --git a/contracts/mainnet/connectors/morpho-aave/events.sol b/contracts/mainnet/connectors/morpho-aave/events.sol index 14bfc418..ce802964 100644 --- a/contracts/mainnet/connectors/morpho-aave/events.sol +++ b/contracts/mainnet/connectors/morpho-aave/events.sol @@ -71,5 +71,9 @@ contract Events { uint256 setId ); - event LogClaimed(address[] tokenAddresses, bool tradeForMorphoToken); + event LogClaimed( + address[] tokenAddresses, + bool tradeForMorphoToken, + uint256 claimedAmount + ); } diff --git a/contracts/mainnet/connectors/morpho-aave/helpers.sol b/contracts/mainnet/connectors/morpho-aave/helpers.sol index 3d558573..97b6b4a8 100644 --- a/contracts/mainnet/connectors/morpho-aave/helpers.sol +++ b/contracts/mainnet/connectors/morpho-aave/helpers.sol @@ -7,9 +7,31 @@ import "../../common/basic.sol"; import "../../common/interfaces.sol"; abstract contract Helpers is Stores, Basic { - IMorphoCore public constant morphoAave = + IMorphoCore public constant MORPHO_AAVE = IMorphoCore(0x777777c9898D384F785Ee44Acfe945efDFf5f3E0); - IMorphoAaveLens public constant morphoAaveLens = + IMorphoAaveLens public constant MORPHO_AAVE_LENS = IMorphoAaveLens(0x507fA343d0A90786d86C7cd885f5C49263A91FF4); + + function _performEthToWethConversion( + address _tokenAddress, + uint256 _amount, + uint256 _getId + ) internal returns (TokenInterface _tokenContract, uint256 _amt) { + _amt = getUint(_getId, _amount); + + bool _isETH = _tokenAddress == ethAddr; + + _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); + } } diff --git a/contracts/mainnet/connectors/morpho-aave/interface.sol b/contracts/mainnet/connectors/morpho-aave/interface.sol index 9ec4220b..0510c795 100644 --- a/contracts/mainnet/connectors/morpho-aave/interface.sol +++ b/contracts/mainnet/connectors/morpho-aave/interface.sol @@ -32,28 +32,25 @@ interface IMorphoCore { uint256 _amount ) external; - function claimRewards( - address[] calldata _tokenAddresses, - bool _tradeForMorphoToken - ) external; + function claimRewards(address[] calldata _assets, bool _tradeForMorphoToken) + external + returns (uint256 claimedAmount); } interface IMorphoAaveLens { - function _getCurrentBorrowBalanceInOf(address _poolToken, address _user) + function getCurrentBorrowBalanceInOf(address _poolToken, address _user) external view returns ( - address underlyingToken, uint256 balanceInP2P, uint256 balanceOnPool, uint256 totalBalance ); - function _getCurrentSupplyBalanceInOf(address _poolToken, address _user) + function getCurrentSupplyBalanceInOf(address _poolToken, address _user) external view returns ( - address underlyingToken, uint256 balanceInP2P, uint256 balanceOnPool, uint256 totalBalance diff --git a/contracts/mainnet/connectors/morpho-aave/main.sol b/contracts/mainnet/connectors/morpho-aave/main.sol index bc5e342f..2cc13ae0 100644 --- a/contracts/mainnet/connectors/morpho-aave/main.sol +++ b/contracts/mainnet/connectors/morpho-aave/main.sol @@ -4,7 +4,7 @@ pragma experimental ABIEncoderV2; import "./helpers.sol"; import "./events.sol"; -abstract contract MorphoAave is Helpers, Events { +abstract contract MorphoAaveV2 is Helpers, Events { /** * @dev Deposit ETH/ERC20_Token. * @notice Deposit a token to Morpho Aave for lending / collaterization. @@ -25,25 +25,14 @@ abstract contract MorphoAave is Helpers, Events { payable returns (string memory _eventName, bytes memory _eventParam) { - uint256 _amt = getUint(_getId, _amount); + ( + TokenInterface _tokenContract, + uint256 _amt + ) = _performEthToWethConversion(_tokenAddress, _amount, _getId); - bool _isETH = _tokenAddress == ethAddr; + approve(_tokenContract, address(MORPHO_AAVE), _amt); - 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); + MORPHO_AAVE.supply(_poolTokenAddress, address(this), _amt); setUint(_setId, _amt); @@ -79,25 +68,14 @@ abstract contract MorphoAave is Helpers, Events { payable returns (string memory _eventName, bytes memory _eventParam) { - uint256 _amt = getUint(_getId, _amount); + ( + TokenInterface _tokenContract, + uint256 _amt + ) = _performEthToWethConversion(_tokenAddress, _amount, _getId); - bool _isETH = _tokenAddress == ethAddr; + approve(_tokenContract, address(MORPHO_AAVE), _amt); - 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( + MORPHO_AAVE.supply( _poolTokenAddress, address(this), _amt, @@ -139,25 +117,14 @@ abstract contract MorphoAave is Helpers, Events { payable returns (string memory _eventName, bytes memory _eventParam) { - uint256 _amt = getUint(_getId, _amount); + ( + TokenInterface _tokenContract, + uint256 _amt + ) = _performEthToWethConversion(_tokenAddress, _amount, _getId); - bool _isETH = _tokenAddress == ethAddr; + approve(_tokenContract, address(MORPHO_AAVE), _amt); - 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); + MORPHO_AAVE.supply(_poolTokenAddress, _onBehalf, _amt); setUint(_setId, _amt); @@ -196,7 +163,7 @@ abstract contract MorphoAave is Helpers, Events { bool _isETH = _tokenAddress == ethAddr; - morphoAave.borrow(_poolTokenAddress, _amt); + MORPHO_AAVE.borrow(_poolTokenAddress, _amt); if (_isETH) convertWethToEth(_isETH, TokenInterface(wethAddr), _amt); @@ -238,7 +205,7 @@ abstract contract MorphoAave is Helpers, Events { bool _isETH = _tokenAddress == ethAddr; - morphoAave.borrow(_poolTokenAddress, _amt, _maxGasForMatching); + MORPHO_AAVE.borrow(_poolTokenAddress, _amt, _maxGasForMatching); if (_isETH) convertWethToEth(_isETH, TokenInterface(wethAddr), _amt); @@ -277,17 +244,16 @@ abstract contract MorphoAave is Helpers, Events { { uint256 _amt = getUint(_getId, _amount); bool _isETH = _tokenAddress == ethAddr; - address _token = _isETH ? wethAddr : _tokenAddress; if (_amt == uint256(-1)) - (, , , _amt) = morphoAaveLens.getCurrentSupplyBalanceInOf( + (, , _amt) = MORPHO_AAVE_LENS.getCurrentSupplyBalanceInOf( _poolTokenAddress, address(this) ); - morphoAave.withdraw(_poolTokenAddress, _amt); + MORPHO_AAVE.withdraw(_poolTokenAddress, _amt); - convertWethToEth(_isETH, TokenInterface(_token), _amt); + if (_isETH) convertWethToEth(_isETH, TokenInterface(wethAddr), _amt); setUint(_setId, _amt); @@ -333,17 +299,17 @@ abstract contract MorphoAave is Helpers, Events { ? address(this).balance : _tokenContract.balanceOf(address(this)); - (, , , uint256 _amtDebt) = morphoAaveLens + (, , uint256 _amtDebt) = MORPHO_AAVE_LENS .getCurrentBorrowBalanceInOf(_poolTokenAddress, address(this)); - _amt = _amtDSA <= _amtDebt ? _amtDSA : _amtDebt; + _amt = _amtDSA < _amtDebt ? _amtDSA : _amtDebt; } if (_isETH) convertEthToWeth(_isETH, _tokenContract, _amt); - approve(_tokenContract, address(morphoAave), _amt); + approve(_tokenContract, address(MORPHO_AAVE), _amt); - morphoAave.repay(_poolTokenAddress, address(this), _amt); + MORPHO_AAVE.repay(_poolTokenAddress, address(this), _amt); setUint(_setId, _amt); @@ -391,17 +357,17 @@ abstract contract MorphoAave is Helpers, Events { ? address(this).balance : _tokenContract.balanceOf(address(this)); - (, , , uint256 _amtDebt) = morphoAaveLens - ._getCurrentBorrowBalanceInOf(_poolTokenAddress, _onBehalf); + (, , uint256 _amtDebt) = MORPHO_AAVE_LENS + .getCurrentBorrowBalanceInOf(_poolTokenAddress, _onBehalf); - _amt = _amtDSA <= _amtDebt ? _amtDSA : _amtDebt; + _amt = _amtDSA < _amtDebt ? _amtDSA : _amtDebt; } if (_isETH) convertEthToWeth(_isETH, _tokenContract, _amt); - approve(_tokenContract, address(morphoAave), _amt); + approve(_tokenContract, address(MORPHO_AAVE), _amt); - morphoAave.repay(_poolTokenAddress, _onBehalf, _amt); + MORPHO_AAVE.repay(_poolTokenAddress, _onBehalf, _amt); setUint(_setId, _amt); @@ -430,13 +396,20 @@ abstract contract MorphoAave is Helpers, Events { payable returns (string memory _eventName, bytes memory _eventParam) { - morphoAave.claimRewards(_poolTokenAddresses, _tradeForMorphoToken); + uint256 _claimedAmount = MORPHO_AAVE.claimRewards( + _poolTokenAddresses, + _tradeForMorphoToken + ); - _eventName = "LogClaimed(address[],bool)"; - _eventParam = abi.encode(_poolTokenAddresses, _tradeForMorphoToken); + _eventName = "LogClaimed(address[],bool,uint256)"; + _eventParam = abi.encode( + _poolTokenAddresses, + _tradeForMorphoToken, + _claimedAmount + ); } } -contract ConnectV2MorphoAave is MorphoAave { - string public constant name = "Morpho-Aave-v1.0"; +contract ConnectV2MorphoAaveV2 is MorphoAaveV2 { + string public constant name = "Morpho-AaveV2-v1.0"; } diff --git a/test/mainnet/morpho/morpho-aave.test.ts b/test/mainnet/morpho/morpho-aave.test.ts index ebcabdeb..4d8dd227 100644 --- a/test/mainnet/morpho/morpho-aave.test.ts +++ b/test/mainnet/morpho/morpho-aave.test.ts @@ -5,7 +5,7 @@ import { addresses } from "../../../scripts/tests/mainnet/addresses"; import { deployAndEnableConnector } from "../../../scripts/tests/deployAndEnableConnector"; import { getMasterSigner } from "../../../scripts/tests/getMasterSigner"; import { buildDSAv2 } from "../../../scripts/tests/buildDSAv2"; -import { ConnectV2MorphoAave__factory, IERC20Minimal__factory } from "../../../typechain"; +import { ConnectV2MorphoAaveV2__factory, IERC20Minimal__factory } from "../../../typechain"; import { parseEther, parseUnits } from "@ethersproject/units"; import { encodeSpells } from "../../../scripts/tests/encodeSpells"; import { dsaMaxValue, tokens } from "../../../scripts/tests/mainnet/tokens"; @@ -64,7 +64,7 @@ describe("Morpho-Aave", function () { ); connector = await deployAndEnableConnector({ connectorName, - contractArtifact: ConnectV2MorphoAave__factory, + contractArtifact: ConnectV2MorphoAaveV2__factory, signer: masterSigner, connectors: instaConnectorsV2, }); From 32754bea19ccf1d2359a1f1ff7ff060bd7fa0f67 Mon Sep 17 00:00:00 2001 From: Thrilok kumar Date: Sat, 15 Oct 2022 22:28:02 +0530 Subject: [PATCH 22/31] removed if condition for `_isETH` --- contracts/mainnet/connectors/morpho-compound/main.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contracts/mainnet/connectors/morpho-compound/main.sol b/contracts/mainnet/connectors/morpho-compound/main.sol index 171ab731..e302ee87 100644 --- a/contracts/mainnet/connectors/morpho-compound/main.sol +++ b/contracts/mainnet/connectors/morpho-compound/main.sol @@ -364,7 +364,7 @@ abstract contract MorphoCompound is Helpers, Events { _amt = _amtDSA < _amtDebt ? _amtDSA : _amtDebt; } - if (_isETH) convertEthToWeth(_isETH, _tokenContract, _amt); + convertEthToWeth(_isETH, _tokenContract, _amt); approve(_tokenContract, address(MORPHO_COMPOUND), _amt); From a069f9569e0f27e23a619862399c0b1ddb6ede6e Mon Sep 17 00:00:00 2001 From: Thrilok kumar Date: Sat, 15 Oct 2022 22:28:33 +0530 Subject: [PATCH 23/31] refactor isETH --- contracts/mainnet/connectors/morpho-compound/main.sol | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/contracts/mainnet/connectors/morpho-compound/main.sol b/contracts/mainnet/connectors/morpho-compound/main.sol index e302ee87..e41858c8 100644 --- a/contracts/mainnet/connectors/morpho-compound/main.sol +++ b/contracts/mainnet/connectors/morpho-compound/main.sol @@ -161,11 +161,9 @@ abstract contract MorphoCompound is Helpers, Events { { uint256 _amt = getUint(_getId, _amount); - bool _isETH = _tokenAddress == ethAddr; - MORPHO_COMPOUND.borrow(_poolTokenAddress, _amt); - if (_isETH) convertWethToEth(_isETH, TokenInterface(wethAddr), _amt); + convertWethToEth(_tokenAddress == ethAddr, TokenInterface(wethAddr), _amt); setUint(_setId, _amt); From c27367aea331ce00ed4418eb76e10b961d09c1ec Mon Sep 17 00:00:00 2001 From: Thrilok kumar Date: Sat, 15 Oct 2022 22:29:00 +0530 Subject: [PATCH 24/31] refactor isETH condition --- contracts/mainnet/connectors/morpho-compound/main.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contracts/mainnet/connectors/morpho-compound/main.sol b/contracts/mainnet/connectors/morpho-compound/main.sol index e41858c8..157eda5f 100644 --- a/contracts/mainnet/connectors/morpho-compound/main.sol +++ b/contracts/mainnet/connectors/morpho-compound/main.sol @@ -304,7 +304,7 @@ abstract contract MorphoCompound is Helpers, Events { _amt = _amtDSA < _amtDebt ? _amtDSA : _amtDebt; } - if (_isETH) convertEthToWeth(_isETH, _tokenContract, _amt); + convertEthToWeth(_isETH, _tokenContract, _amt); approve(_tokenContract, address(MORPHO_COMPOUND), _amt); From 8d8969960c84c20d9e8381cfd944483ff02f3755 Mon Sep 17 00:00:00 2001 From: Thrilok kumar Date: Sat, 15 Oct 2022 22:29:15 +0530 Subject: [PATCH 25/31] refactor isETH condition --- contracts/mainnet/connectors/morpho-aave/main.sol | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/contracts/mainnet/connectors/morpho-aave/main.sol b/contracts/mainnet/connectors/morpho-aave/main.sol index 2cc13ae0..35edc9cc 100644 --- a/contracts/mainnet/connectors/morpho-aave/main.sol +++ b/contracts/mainnet/connectors/morpho-aave/main.sol @@ -161,11 +161,9 @@ abstract contract MorphoAaveV2 is Helpers, Events { { uint256 _amt = getUint(_getId, _amount); - bool _isETH = _tokenAddress == ethAddr; - MORPHO_AAVE.borrow(_poolTokenAddress, _amt); - if (_isETH) convertWethToEth(_isETH, TokenInterface(wethAddr), _amt); + convertWethToEth(_tokenAddress == ethAddr, TokenInterface(wethAddr), _amt); setUint(_setId, _amt); From 48980f750931f09f0af24c80f7ed08e946e7a54f Mon Sep 17 00:00:00 2001 From: Thrilok kumar Date: Sat, 15 Oct 2022 22:29:46 +0530 Subject: [PATCH 26/31] refactor isETH condition --- contracts/mainnet/connectors/morpho-aave/main.sol | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/contracts/mainnet/connectors/morpho-aave/main.sol b/contracts/mainnet/connectors/morpho-aave/main.sol index 35edc9cc..ab46b492 100644 --- a/contracts/mainnet/connectors/morpho-aave/main.sol +++ b/contracts/mainnet/connectors/morpho-aave/main.sol @@ -201,11 +201,9 @@ abstract contract MorphoAaveV2 is Helpers, Events { { uint256 _amt = getUint(_getId, _amount); - bool _isETH = _tokenAddress == ethAddr; - MORPHO_AAVE.borrow(_poolTokenAddress, _amt, _maxGasForMatching); - if (_isETH) convertWethToEth(_isETH, TokenInterface(wethAddr), _amt); + convertWethToEth(_tokenAddress == ethAddr, TokenInterface(wethAddr), _amt); setUint(_setId, _amt); @@ -241,8 +239,6 @@ abstract contract MorphoAaveV2 is Helpers, Events { returns (string memory _eventName, bytes memory _eventParam) { uint256 _amt = getUint(_getId, _amount); - bool _isETH = _tokenAddress == ethAddr; - if (_amt == uint256(-1)) (, , _amt) = MORPHO_AAVE_LENS.getCurrentSupplyBalanceInOf( _poolTokenAddress, @@ -251,7 +247,7 @@ abstract contract MorphoAaveV2 is Helpers, Events { MORPHO_AAVE.withdraw(_poolTokenAddress, _amt); - if (_isETH) convertWethToEth(_isETH, TokenInterface(wethAddr), _amt); + convertWethToEth(_tokenAddress == ethAddr, TokenInterface(wethAddr), _amt); setUint(_setId, _amt); @@ -303,7 +299,7 @@ abstract contract MorphoAaveV2 is Helpers, Events { _amt = _amtDSA < _amtDebt ? _amtDSA : _amtDebt; } - if (_isETH) convertEthToWeth(_isETH, _tokenContract, _amt); + convertEthToWeth(_isETH, _tokenContract, _amt); approve(_tokenContract, address(MORPHO_AAVE), _amt); @@ -361,7 +357,7 @@ abstract contract MorphoAaveV2 is Helpers, Events { _amt = _amtDSA < _amtDebt ? _amtDSA : _amtDebt; } - if (_isETH) convertEthToWeth(_isETH, _tokenContract, _amt); + convertEthToWeth(_isETH, _tokenContract, _amt); approve(_tokenContract, address(MORPHO_AAVE), _amt); From 0ea3c5613c07d2eba4bb092e0640999a64bf424e Mon Sep 17 00:00:00 2001 From: Thrilok kumar Date: Sat, 15 Oct 2022 22:30:42 +0530 Subject: [PATCH 27/31] refactor isETH condition --- contracts/mainnet/connectors/morpho-compound/main.sol | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/contracts/mainnet/connectors/morpho-compound/main.sol b/contracts/mainnet/connectors/morpho-compound/main.sol index 157eda5f..4a8d61f1 100644 --- a/contracts/mainnet/connectors/morpho-compound/main.sol +++ b/contracts/mainnet/connectors/morpho-compound/main.sol @@ -201,11 +201,9 @@ abstract contract MorphoCompound is Helpers, Events { { uint256 _amt = getUint(_getId, _amount); - bool _isETH = _tokenAddress == ethAddr; - MORPHO_COMPOUND.borrow(_poolTokenAddress, _amt, _maxGasForMatching); - if (_isETH) convertWethToEth(_isETH, TokenInterface(wethAddr), _amt); + convertWethToEth(_tokenAddress == ethAddr, TokenInterface(wethAddr), _amt); setUint(_setId, _amt); @@ -252,7 +250,7 @@ abstract contract MorphoCompound is Helpers, Events { MORPHO_COMPOUND.withdraw(_poolTokenAddress, _amt); - if (_isETH) convertWethToEth(_isETH, TokenInterface(wethAddr), _amt); + convertWethToEth(_isETH, TokenInterface(wethAddr), _amt); setUint(_setId, _amt); From 85da96b9503fbc382c86b882eda869cbb5176b2d Mon Sep 17 00:00:00 2001 From: Shriya Tyagi <47134275+shriyatyagii@users.noreply.github.com> Date: Sat, 15 Oct 2022 21:23:31 +0400 Subject: [PATCH 28/31] Update contracts/mainnet/connectors/morpho-compound/helpers.sol Co-authored-by: Thrilok kumar --- .../connectors/morpho-compound/helpers.sol | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/contracts/mainnet/connectors/morpho-compound/helpers.sol b/contracts/mainnet/connectors/morpho-compound/helpers.sol index 6a64a605..3420154b 100644 --- a/contracts/mainnet/connectors/morpho-compound/helpers.sol +++ b/contracts/mainnet/connectors/morpho-compound/helpers.sol @@ -20,18 +20,13 @@ abstract contract Helpers is Stores, Basic { ) internal returns (TokenInterface _tokenContract, uint256 _amt) { _amt = getUint(_getId, _amount); - bool _isETH = _tokenAddress == ethAddr; - - _tokenContract = _isETH - ? TokenInterface(wethAddr) - : TokenInterface(_tokenAddress); - - if (_amt == uint256(-1)) { - _amt = _isETH - ? address(this).balance - : _tokenContract.balanceOf(address(this)); + if (_tokenAddress == ethAddr) { + _tokenContract = TokenInterface(wethAddr); + if (_amt == uint256(-1)) _amt = address(this).balance; + convertEthToWeth(true, _tokenContract, _amt); + } else { + _tokenContract = TokenInterface(_tokenAddress); + if (_amt == uint256(-1)) _amt = _tokenContract.balanceOf(address(this)); } - - if (_isETH) convertEthToWeth(_isETH, _tokenContract, _amt); } } From 0289824465173c4a920dff6b0cade72ce84d082e Mon Sep 17 00:00:00 2001 From: Shriya Tyagi <47134275+shriyatyagii@users.noreply.github.com> Date: Sat, 15 Oct 2022 21:24:03 +0400 Subject: [PATCH 29/31] Update contracts/mainnet/connectors/morpho-aave/helpers.sol Co-authored-by: Thrilok kumar --- .../connectors/morpho-aave/helpers.sol | 20 +++++++------------ 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/contracts/mainnet/connectors/morpho-aave/helpers.sol b/contracts/mainnet/connectors/morpho-aave/helpers.sol index 97b6b4a8..a3a4c3bf 100644 --- a/contracts/mainnet/connectors/morpho-aave/helpers.sol +++ b/contracts/mainnet/connectors/morpho-aave/helpers.sol @@ -20,18 +20,12 @@ abstract contract Helpers is Stores, Basic { ) internal returns (TokenInterface _tokenContract, uint256 _amt) { _amt = getUint(_getId, _amount); - bool _isETH = _tokenAddress == ethAddr; - - _tokenContract = _isETH - ? TokenInterface(wethAddr) - : TokenInterface(_tokenAddress); - - if (_amt == uint256(-1)) { - _amt = _isETH - ? address(this).balance - : _tokenContract.balanceOf(address(this)); + if (_tokenAddress == ethAddr) { + _tokenContract = TokenInterface(wethAddr); + if (_amt == uint256(-1)) _amt = address(this).balance; + convertEthToWeth(true, _tokenContract, _amt); + } else { + _tokenContract = TokenInterface(_tokenAddress); + if (_amt == uint256(-1)) _amt = _tokenContract.balanceOf(address(this)); } - - if (_isETH) convertEthToWeth(_isETH, _tokenContract, _amt); - } } From e22b0bff0cf87bc5f9bc5ed255297fe7793dabce Mon Sep 17 00:00:00 2001 From: Shriya Tyagi Date: Tue, 18 Oct 2022 03:38:20 +0400 Subject: [PATCH 30/31] Removed claim func + fix cETH --- .../mainnet/connectors/morpho-aave/events.sol | 6 --- .../connectors/morpho-aave/interface.sol | 4 -- .../mainnet/connectors/morpho-aave/main.sol | 27 ------------ .../connectors/morpho-compound/events.sol | 6 --- .../connectors/morpho-compound/interface.sol | 5 --- .../connectors/morpho-compound/main.sol | 43 ++++--------------- 6 files changed, 8 insertions(+), 83 deletions(-) diff --git a/contracts/mainnet/connectors/morpho-aave/events.sol b/contracts/mainnet/connectors/morpho-aave/events.sol index ce802964..3783a80f 100644 --- a/contracts/mainnet/connectors/morpho-aave/events.sol +++ b/contracts/mainnet/connectors/morpho-aave/events.sol @@ -70,10 +70,4 @@ contract Events { uint256 getId, uint256 setId ); - - event LogClaimed( - address[] tokenAddresses, - bool tradeForMorphoToken, - uint256 claimedAmount - ); } diff --git a/contracts/mainnet/connectors/morpho-aave/interface.sol b/contracts/mainnet/connectors/morpho-aave/interface.sol index 0510c795..6e144d2c 100644 --- a/contracts/mainnet/connectors/morpho-aave/interface.sol +++ b/contracts/mainnet/connectors/morpho-aave/interface.sol @@ -31,10 +31,6 @@ interface IMorphoCore { address _onBehalf, uint256 _amount ) external; - - function claimRewards(address[] calldata _assets, bool _tradeForMorphoToken) - external - returns (uint256 claimedAmount); } interface IMorphoAaveLens { diff --git a/contracts/mainnet/connectors/morpho-aave/main.sol b/contracts/mainnet/connectors/morpho-aave/main.sol index ab46b492..05c70fa4 100644 --- a/contracts/mainnet/connectors/morpho-aave/main.sol +++ b/contracts/mainnet/connectors/morpho-aave/main.sol @@ -375,33 +375,6 @@ abstract contract MorphoAaveV2 is Helpers, Events { _setId ); } - - /** - * @dev Claim rewards. - * @notice Claim rewards for the given assets from underlying protocol. - * @param _poolTokenAddresses The assets to claim rewards from (aToken or variable debt token).(For ETH: aToken or variable debt token address of WETH) - * @param _tradeForMorphoToken Whether or not to trade reward tokens for MORPHO tokens. - */ - function claim( - address[] calldata _poolTokenAddresses, - bool _tradeForMorphoToken - ) - external - payable - returns (string memory _eventName, bytes memory _eventParam) - { - uint256 _claimedAmount = MORPHO_AAVE.claimRewards( - _poolTokenAddresses, - _tradeForMorphoToken - ); - - _eventName = "LogClaimed(address[],bool,uint256)"; - _eventParam = abi.encode( - _poolTokenAddresses, - _tradeForMorphoToken, - _claimedAmount - ); - } } contract ConnectV2MorphoAaveV2 is MorphoAaveV2 { diff --git a/contracts/mainnet/connectors/morpho-compound/events.sol b/contracts/mainnet/connectors/morpho-compound/events.sol index 41bec370..970c3f84 100644 --- a/contracts/mainnet/connectors/morpho-compound/events.sol +++ b/contracts/mainnet/connectors/morpho-compound/events.sol @@ -70,10 +70,4 @@ contract Events { uint256 getId, uint256 setId ); - - event LogClaimed( - address[] tokenAddresses, - bool tradeForMorphoToken, - uint256 amountOfRewards - ); } diff --git a/contracts/mainnet/connectors/morpho-compound/interface.sol b/contracts/mainnet/connectors/morpho-compound/interface.sol index 5a98876a..f2ec3df3 100644 --- a/contracts/mainnet/connectors/morpho-compound/interface.sol +++ b/contracts/mainnet/connectors/morpho-compound/interface.sol @@ -31,11 +31,6 @@ interface IMorphoCore { address _onBehalf, uint256 _amount ) external; - - function claimRewards( - address[] calldata _cTokenAddresses, - bool _tradeForMorphoToken - ) external returns (uint256 amountOfRewards); } interface IMorphoCompoundLens { diff --git a/contracts/mainnet/connectors/morpho-compound/main.sol b/contracts/mainnet/connectors/morpho-compound/main.sol index 4a8d61f1..15dc66f2 100644 --- a/contracts/mainnet/connectors/morpho-compound/main.sol +++ b/contracts/mainnet/connectors/morpho-compound/main.sol @@ -9,7 +9,7 @@ abstract contract MorphoCompound is Helpers, Events { * @dev Deposit ETH/ERC20_Token. * @notice Deposit a token to Morpho Compound for lending / collaterization. * @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 _poolTokenAddress The address of cToken to deposit.(For ETH: cETH address) * @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. @@ -50,7 +50,7 @@ abstract contract MorphoCompound is Helpers, Events { * @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 _poolTokenAddress The address of cToken to deposit.(For ETH: cETH 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. @@ -99,7 +99,7 @@ abstract contract MorphoCompound is Helpers, Events { * @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 _poolTokenAddress The address of cToken to deposit.(For ETH: cETH 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. @@ -143,7 +143,7 @@ abstract contract MorphoCompound is Helpers, Events { * @dev Borrow ETH/ERC20_Token. * @notice Borrow a token from Morpho Compound. * @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 _poolTokenAddress The address of cToken to borrow.(For ETH: cETH address) * @param _amount The amount of the token (in underlying) to borrow. * @param _getId ID to retrieve amt. * @param _setId ID stores the amount of tokens borrowed. @@ -181,7 +181,7 @@ abstract contract MorphoCompound is Helpers, Events { * @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 _poolTokenAddress The address of cToken to borrow.(For ETH: cETH 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. @@ -222,7 +222,7 @@ abstract contract MorphoCompound is Helpers, Events { * @dev Withdraw ETH/ERC20_Token. * @notice Withdraw a token from Morpho Compound. * @param _tokenAddress The address of underlying token to withdraw.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) - * @param _poolTokenAddress The address of cToken to withdraw.(For ETH: cWETH address) + * @param _poolTokenAddress The address of cToken to withdraw.(For ETH: cETH address) * @param _amount The amount of the token (in underlying) to withdraw. (For max: `uint256(-1)`) * @param _getId ID to retrieve amt. * @param _setId ID stores the amount of tokens withdrawed. @@ -268,7 +268,7 @@ abstract contract MorphoCompound is Helpers, Events { * @dev Payback ETH/ERC20_Token. * @notice Payback a token to Morpho Compound. * @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 _poolTokenAddress The address of cToken to payback.(For ETH: cETH address) * @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. @@ -324,7 +324,7 @@ 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 _poolTokenAddress The address of cToken to payback.(For ETH: cETH 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. @@ -378,33 +378,6 @@ abstract contract MorphoCompound is Helpers, Events { _setId ); } - - /** - * @dev Claim rewards. - * @notice Claim rewards for the given assets from underlying protocol. - * @param _poolTokenAddresses The cToken addresses to claim rewards from..(For ETH: cToken address of WETH) - * @param _tradeForMorphoToken Whether or not to trade COMP tokens for MORPHO tokens. - */ - function claim( - address[] calldata _poolTokenAddresses, - bool _tradeForMorphoToken - ) - external - payable - returns (string memory _eventName, bytes memory _eventParam) - { - uint256 _amountOfRewards = MORPHO_COMPOUND.claimRewards( - _poolTokenAddresses, - _tradeForMorphoToken - ); - - _eventName = "LogClaimed(address[],bool,uint256)"; - _eventParam = abi.encode( - _poolTokenAddresses, - _tradeForMorphoToken, - _amountOfRewards - ); - } } contract ConnectV2MorphoCompound is MorphoCompound { From 04ce9030755a5dca6c1519d06838678ad756a3bf Mon Sep 17 00:00:00 2001 From: Shriya Tyagi Date: Tue, 18 Oct 2022 07:02:00 +0400 Subject: [PATCH 31/31] minor fix --- contracts/mainnet/connectors/morpho-aave/helpers.sol | 1 + 1 file changed, 1 insertion(+) diff --git a/contracts/mainnet/connectors/morpho-aave/helpers.sol b/contracts/mainnet/connectors/morpho-aave/helpers.sol index a3a4c3bf..8a2c0172 100644 --- a/contracts/mainnet/connectors/morpho-aave/helpers.sol +++ b/contracts/mainnet/connectors/morpho-aave/helpers.sol @@ -28,4 +28,5 @@ abstract contract Helpers is Stores, Basic { _tokenContract = TokenInterface(_tokenAddress); if (_amt == uint256(-1)) _amt = _tokenContract.balanceOf(address(this)); } + } }