diff --git a/contracts/mainnet/connectors/morpho-aave/events.sol b/contracts/mainnet/connectors/morpho-aave/events.sol new file mode 100644 index 00000000..3783a80f --- /dev/null +++ b/contracts/mainnet/connectors/morpho-aave/events.sol @@ -0,0 +1,73 @@ +//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 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( + 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( + address tokenAddress, + address poolTokenAddress, + uint256 amount, + uint256 getId, + uint256 setId + ); + + event LogPayback( + address tokenAddress, + address poolTokenAddress, + 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/helpers.sol b/contracts/mainnet/connectors/morpho-aave/helpers.sol new file mode 100644 index 00000000..8a2c0172 --- /dev/null +++ b/contracts/mainnet/connectors/morpho-aave/helpers.sol @@ -0,0 +1,32 @@ +//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 MORPHO_AAVE = + IMorphoCore(0x777777c9898D384F785Ee44Acfe945efDFf5f3E0); + + 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); + + 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)); + } + } +} diff --git a/contracts/mainnet/connectors/morpho-aave/interface.sol b/contracts/mainnet/connectors/morpho-aave/interface.sol new file mode 100644 index 00000000..6e144d2c --- /dev/null +++ b/contracts/mainnet/connectors/morpho-aave/interface.sol @@ -0,0 +1,54 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.7.0; +pragma experimental ABIEncoderV2; + +interface IMorphoCore { + function supply( + address _poolTokenAddress, + address _onBehalf, + 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( + address _poolTokenAddress, + address _onBehalf, + uint256 _amount + ) external; +} + +interface IMorphoAaveLens { + function getCurrentBorrowBalanceInOf(address _poolToken, address _user) + external + view + returns ( + uint256 balanceInP2P, + uint256 balanceOnPool, + uint256 totalBalance + ); + + function getCurrentSupplyBalanceInOf(address _poolToken, address _user) + external + view + returns ( + 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..05c70fa4 --- /dev/null +++ b/contracts/mainnet/connectors/morpho-aave/main.sol @@ -0,0 +1,382 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.7.0; +pragma experimental ABIEncoderV2; +import "./helpers.sol"; +import "./events.sol"; + +abstract contract MorphoAaveV2 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 _getId, + uint256 _setId + ) + external + payable + returns (string memory _eventName, bytes memory _eventParam) + { + ( + TokenInterface _tokenContract, + uint256 _amt + ) = _performEthToWethConversion(_tokenAddress, _amount, _getId); + + approve(_tokenContract, address(MORPHO_AAVE), _amt); + + MORPHO_AAVE.supply(_poolTokenAddress, address(this), _amt); + + setUint(_setId, _amt); + + _eventName = "LogDeposit(address,address,uint256,uint256,uint256)"; + _eventParam = abi.encode( + _tokenAddress, + _poolTokenAddress, + _amt, + _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) + { + ( + TokenInterface _tokenContract, + uint256 _amt + ) = _performEthToWethConversion(_tokenAddress, _amount, _getId); + + approve(_tokenContract, address(MORPHO_AAVE), _amt); + + MORPHO_AAVE.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) + { + ( + TokenInterface _tokenContract, + uint256 _amt + ) = _performEthToWethConversion(_tokenAddress, _amount, _getId); + + approve(_tokenContract, address(MORPHO_AAVE), _amt); + + MORPHO_AAVE.supply(_poolTokenAddress, _onBehalf, _amt); + + setUint(_setId, _amt); + + _eventName = "LogDepositOnBehalf(address,address,address,uint256,uint256,uint256)"; + _eventParam = abi.encode( + _tokenAddress, + _poolTokenAddress, + _onBehalf, + _amt, + _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 _getId, + uint256 _setId + ) + external + payable + returns (string memory _eventName, bytes memory _eventParam) + { + uint256 _amt = getUint(_getId, _amount); + + MORPHO_AAVE.borrow(_poolTokenAddress, _amt); + + convertWethToEth(_tokenAddress == ethAddr, TokenInterface(wethAddr), _amt); + + setUint(_setId, _amt); + + _eventName = "LogBorrow(address,address,uint256,uint256,uint256)"; + _eventParam = abi.encode( + _tokenAddress, + _poolTokenAddress, + _amt, + _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); + + MORPHO_AAVE.borrow(_poolTokenAddress, _amt, _maxGasForMatching); + + convertWethToEth(_tokenAddress == ethAddr, TokenInterface(wethAddr), _amt); + + setUint(_setId, _amt); + + _eventName = "LogBorrowWithMaxGas(address,address,uint256,uint256,uint256,uint256)"; + _eventParam = abi.encode( + _tokenAddress, + _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); + if (_amt == uint256(-1)) + (, , _amt) = MORPHO_AAVE_LENS.getCurrentSupplyBalanceInOf( + _poolTokenAddress, + address(this) + ); + + MORPHO_AAVE.withdraw(_poolTokenAddress, _amt); + + convertWethToEth(_tokenAddress == ethAddr, TokenInterface(wethAddr), _amt); + + setUint(_setId, _amt); + + _eventName = "LogWithdraw(address,address,uint256,uint256,uint256)"; + _eventParam = abi.encode( + _tokenAddress, + _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); + + TokenInterface _tokenContract = _isETH + ? TokenInterface(wethAddr) + : TokenInterface(_tokenAddress); + + if (_amt == uint256(-1)) { + uint256 _amtDSA = _isETH + ? address(this).balance + : _tokenContract.balanceOf(address(this)); + + (, , uint256 _amtDebt) = MORPHO_AAVE_LENS + .getCurrentBorrowBalanceInOf(_poolTokenAddress, address(this)); + + _amt = _amtDSA < _amtDebt ? _amtDSA : _amtDebt; + } + + convertEthToWeth(_isETH, _tokenContract, _amt); + + approve(_tokenContract, address(MORPHO_AAVE), _amt); + + MORPHO_AAVE.repay(_poolTokenAddress, address(this), _amt); + + setUint(_setId, _amt); + + _eventName = "LogPayback(address,address,uint256,uint256,uint256)"; + _eventParam = abi.encode( + _tokenAddress, + _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 _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) = MORPHO_AAVE_LENS + .getCurrentBorrowBalanceInOf(_poolTokenAddress, _onBehalf); + + _amt = _amtDSA < _amtDebt ? _amtDSA : _amtDebt; + } + + convertEthToWeth(_isETH, _tokenContract, _amt); + + approve(_tokenContract, address(MORPHO_AAVE), _amt); + + MORPHO_AAVE.repay(_poolTokenAddress, _onBehalf, _amt); + + setUint(_setId, _amt); + + _eventName = "LogPaybackOnBehalf(address,address,address,uint256,uint256,uint256)"; + _eventParam = abi.encode( + _tokenAddress, + _poolTokenAddress, + _onBehalf, + _amt, + _getId, + _setId + ); + } +} + +contract ConnectV2MorphoAaveV2 is MorphoAaveV2 { + string public constant name = "Morpho-AaveV2-v1.0"; +} diff --git a/contracts/mainnet/connectors/morpho-compound/events.sol b/contracts/mainnet/connectors/morpho-compound/events.sol new file mode 100644 index 00000000..970c3f84 --- /dev/null +++ b/contracts/mainnet/connectors/morpho-compound/events.sol @@ -0,0 +1,73 @@ +//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 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( + 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( + address tokenAddress, + address poolTokenAddress, + uint256 amt, + uint256 getId, + uint256 setId + ); + + event LogPayback( + address tokenAddress, + address poolTokenAddress, + uint256 amt, + 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-compound/helpers.sol b/contracts/mainnet/connectors/morpho-compound/helpers.sol new file mode 100644 index 00000000..3420154b --- /dev/null +++ b/contracts/mainnet/connectors/morpho-compound/helpers.sol @@ -0,0 +1,32 @@ +//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 MORPHO_COMPOUND = + IMorphoCore(0x8888882f8f843896699869179fB6E4f7e3B58888); + + 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); + + 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)); + } + } +} diff --git a/contracts/mainnet/connectors/morpho-compound/interface.sol b/contracts/mainnet/connectors/morpho-compound/interface.sol new file mode 100644 index 00000000..f2ec3df3 --- /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 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( + address _poolTokenAddress, + address _onBehalf, + uint256 _amount + ) 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..15dc66f2 --- /dev/null +++ b/contracts/mainnet/connectors/morpho-compound/main.sol @@ -0,0 +1,385 @@ +//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: 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. + */ + function deposit( + address _tokenAddress, + address _poolTokenAddress, + uint256 _amount, + uint256 _getId, + uint256 _setId + ) + external + payable + returns (string memory _eventName, bytes memory _eventParam) + { + ( + TokenInterface _tokenContract, + uint256 _amt + ) = _performEthToWethConversion(_tokenAddress, _amount, _getId); + + approve(_tokenContract, address(MORPHO_COMPOUND), _amt); + + MORPHO_COMPOUND.supply(_poolTokenAddress, address(this), _amt); + + setUint(_setId, _amt); + + _eventName = "LogDeposit(address,address,uint256,uint256,uint256)"; + _eventParam = abi.encode( + _tokenAddress, + _poolTokenAddress, + _amt, + _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: 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. + * @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) + { + ( + TokenInterface _tokenContract, + uint256 _amt + ) = _performEthToWethConversion(_tokenAddress, _amount, _getId); + + approve(_tokenContract, address(MORPHO_COMPOUND), _amt); + + MORPHO_COMPOUND.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: 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. + * @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) + { + ( + TokenInterface _tokenContract, + uint256 _amt + ) = _performEthToWethConversion(_tokenAddress, _amount, _getId); + + approve(_tokenContract, address(MORPHO_COMPOUND), _amt); + + MORPHO_COMPOUND.supply(_poolTokenAddress, _onBehalf, _amt); + + setUint(_setId, _amt); + + _eventName = "LogDeposit(address,address,address,uint256,uint256,uint256)"; + _eventParam = abi.encode( + _tokenAddress, + _poolTokenAddress, + _onBehalf, + _amt, + _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: 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. + */ + function borrow( + address _tokenAddress, + address _poolTokenAddress, + uint256 _amount, + uint256 _getId, + uint256 _setId + ) + external + payable + returns (string memory _eventName, bytes memory _eventParam) + { + uint256 _amt = getUint(_getId, _amount); + + MORPHO_COMPOUND.borrow(_poolTokenAddress, _amt); + + convertWethToEth(_tokenAddress == ethAddr, TokenInterface(wethAddr), _amt); + + setUint(_setId, _amt); + + _eventName = "LogBorrow(address,address,uint256,uint256,uint256)"; + _eventParam = abi.encode( + _tokenAddress, + _poolTokenAddress, + _amt, + _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: 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. + * @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); + + MORPHO_COMPOUND.borrow(_poolTokenAddress, _amt, _maxGasForMatching); + + convertWethToEth(_tokenAddress == ethAddr, TokenInterface(wethAddr), _amt); + + setUint(_setId, _amt); + + _eventName = "LogBorrowWithMaxGas(address,address,uint256,uint256,uint256,uint256)"; + _eventParam = abi.encode( + _tokenAddress, + _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: 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. + */ + 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; + + if (_amt == uint256(-1)) { + (, , _amt) = MORPHO_COMPOUND_LENS.getCurrentSupplyBalanceInOf( + _poolTokenAddress, + address(this) + ); + } + + MORPHO_COMPOUND.withdraw(_poolTokenAddress, _amt); + + convertWethToEth(_isETH, TokenInterface(wethAddr), _amt); + + setUint(_setId, _amt); + + _eventName = "LogWithdraw(address,address,uint256,uint256,uint256)"; + _eventParam = abi.encode( + _tokenAddress, + _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: 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. + */ + 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); + + TokenInterface _tokenContract = _isETH + ? TokenInterface(wethAddr) + : TokenInterface(_tokenAddress); + + if (_amt == uint256(-1)) { + uint256 _amtDSA = _isETH + ? address(this).balance + : _tokenContract.balanceOf(address(this)); + + (, , uint256 _amtDebt) = MORPHO_COMPOUND_LENS + .getCurrentBorrowBalanceInOf(_poolTokenAddress, address(this)); + + _amt = _amtDSA < _amtDebt ? _amtDSA : _amtDebt; + } + + convertEthToWeth(_isETH, _tokenContract, _amt); + + approve(_tokenContract, address(MORPHO_COMPOUND), _amt); + + MORPHO_COMPOUND.repay(_poolTokenAddress, address(this), _amt); + + setUint(_setId, _amt); + + _eventName = "LogPayback(address,address,uint256,uint256,uint256)"; + _eventParam = abi.encode( + _tokenAddress, + _poolTokenAddress, + _amt, + _getId, + _setId + ); + } + + /** + * @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: 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. + * @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) = MORPHO_COMPOUND_LENS + .getCurrentBorrowBalanceInOf(_poolTokenAddress, _onBehalf); + + _amt = _amtDSA < _amtDebt ? _amtDSA : _amtDebt; + } + + convertEthToWeth(_isETH, _tokenContract, _amt); + + approve(_tokenContract, address(MORPHO_COMPOUND), _amt); + + MORPHO_COMPOUND.repay(_poolTokenAddress, _onBehalf, _amt); + + setUint(_setId, _amt); + + _eventName = "LogPaybackOnBehalf(address,address,address,uint256,uint256,uint256)"; + _eventParam = abi.encode( + _tokenAddress, + _poolTokenAddress, + _onBehalf, + _amt, + _getId, + _setId + ); + } +} + +contract ConnectV2MorphoCompound is MorphoCompound { + string public constant name = "Morpho-Compound-v1.0"; +} 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", diff --git a/test/mainnet/morpho/morpho-aave.test.ts b/test/mainnet/morpho/morpho-aave.test.ts new file mode 100644 index 00000000..4d8dd227 --- /dev/null +++ b/test/mainnet/morpho/morpho-aave.test.ts @@ -0,0 +1,294 @@ +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 { 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"; +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 user = "0x41bc7d0687e6cea57fa26da78379dfdc5627c56d" + +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: 15714501, + }, + }, + ], + }); + [wallet0, wallet1] = await ethers.getSigners(); + masterSigner = await getMasterSigner(); + instaConnectorsV2 = await ethers.getContractAt( + abis.core.connectorsV2, + addresses.core.connectorsV2 + ); + connector = await deployAndEnableConnector({ + connectorName, + contractArtifact: ConnectV2MorphoAaveV2__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 1000 ETH into DSA wallet", async function () { + await wallet0.sendTransaction({ + to: dsaWallet0.address, + value: parseEther("1000"), + }); + expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.gte( + parseEther("1000") + ); + }); + + 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 10 ETH", async function () { + const spells = [ + { + connector: connectorName, + method: "deposit", + args: [tokens.eth.address, tokens.eth.aTokenAddress, "10000000000000000000", "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('990', 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 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 = [ + { + 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 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 = [ + { + 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('134', 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('398', 6)) + ); + }) + }); +}); diff --git a/test/mainnet/morpho/morpho-compound.test.ts b/test/mainnet/morpho/morpho-compound.test.ts new file mode 100644 index 00000000..461f59b9 --- /dev/null +++ b/test/mainnet/morpho/morpho-compound.test.ts @@ -0,0 +1,272 @@ +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('400000', 18) + +const user = '0x5dd596c901987a2b28c38a9c1dfbf86fffc15d77' + +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: 15714501, + }, + }, + ], + }); + [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 400000 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('400000', 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 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 = [ + { + 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('400010', 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('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 = [ + { + 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)) + ); + }) + }); +});