From f6db02d83a6aef7f0f9c3be55707c843adae1688 Mon Sep 17 00:00:00 2001 From: Shriya Tyagi Date: Sat, 15 Oct 2022 08:17:36 +0400 Subject: [PATCH] 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, });