From 2ca7b19ab53d8cd1d57a612fcf90e8c122a0faae Mon Sep 17 00:00:00 2001 From: q1q0 Date: Thu, 23 Feb 2023 11:34:03 -0500 Subject: [PATCH] add MorphoAave3 --- .../connectors/morpho-aave-v3/events.sol | 114 ++++ .../connectors/morpho-aave-v3/helpers.sol | 32 ++ .../connectors/morpho-aave-v3/interface.sol | 106 ++++ .../connectors/morpho-aave-v3/main.sol | 541 ++++++++++++++++++ 4 files changed, 793 insertions(+) create mode 100644 contracts/mainnet/connectors/morpho-aave-v3/events.sol create mode 100644 contracts/mainnet/connectors/morpho-aave-v3/helpers.sol create mode 100644 contracts/mainnet/connectors/morpho-aave-v3/interface.sol create mode 100644 contracts/mainnet/connectors/morpho-aave-v3/main.sol diff --git a/contracts/mainnet/connectors/morpho-aave-v3/events.sol b/contracts/mainnet/connectors/morpho-aave-v3/events.sol new file mode 100644 index 00000000..543325be --- /dev/null +++ b/contracts/mainnet/connectors/morpho-aave-v3/events.sol @@ -0,0 +1,114 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.7.0; +pragma experimental ABIEncoderV2; + +contract Events { + event LogDeposit( + address tokenAddress, + uint256 amount, + uint256 maxIteration, + uint256 getId, + uint256 setId + ); + + event LogDepositWithMaxGas( + address tokenAddress, + address poolTokenAddress, + uint256 amount, + uint256 maxGasForMatching, + uint256 getId, + uint256 setId + ); + + event LogDepositOnBehalf( + address tokenAddress, + uint256 amount, + address onBehalf, + uint256 maxIteration, + uint256 getId, + uint256 setId + ); + + event LogDepositWithPermit( + address tokenAddress, + uint256 amount, + address onBehalf, + uint256 maxIteration, + uint256 time, + uint8 v, + bytes32 r, + bytes32 s, + uint256 getId, + uint256 setId + ); + + event LogDepositCollateral( + address tokenAddress, + uint256 amount, + uint256 getId, + uint256 setId + ); + + event LogDepositCollateralOnBehalf( + address tokenAddress, + uint256 amount, + address onBehalf, + uint256 getId, + uint256 setId + ); + + event LogDepositCollateralWithPermit( + address tokenAddress, + uint256 amount, + address onBehalf, + uint256 maxIteration, + uint256 time, + uint8 v, + bytes32 r, + bytes32 s, + 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-v3/helpers.sol b/contracts/mainnet/connectors/morpho-aave-v3/helpers.sol new file mode 100644 index 00000000..7c5d554b --- /dev/null +++ b/contracts/mainnet/connectors/morpho-aave-v3/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_V3 = + 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-v3/interface.sol b/contracts/mainnet/connectors/morpho-aave-v3/interface.sol new file mode 100644 index 00000000..4e35d487 --- /dev/null +++ b/contracts/mainnet/connectors/morpho-aave-v3/interface.sol @@ -0,0 +1,106 @@ +//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; + struct Signature { + uint8 v; + bytes32 r; + bytes32 s; + } + + function supply(address underlying, uint256 amount, address onBehalf, uint256 maxIterations) + external + returns (uint256 supplied); + + function supplyWithPermit( + address underlying, + uint256 amount, + address onBehalf, + uint256 maxIterations, + uint256 deadline, + Signature calldata signature + ) external returns (uint256 supplied); + + function supplyCollateral(address underlying, uint256 amount, address onBehalf) + external + returns (uint256 supplied); + + function supplyCollateralWithPermit( + address underlying, + uint256 amount, + address onBehalf, + uint256 deadline, + Signature calldata signature + ) external returns (uint256 supplied); + + function borrow(address underlying, uint256 amount, address onBehalf, address receiver, uint256 maxIterations) + external + returns (uint256 borrowed); + + function repay(address underlying, uint256 amount, address onBehalf) external returns (uint256 repaid); + + function repayWithPermit( + address underlying, + uint256 amount, + address onBehalf, + uint256 deadline, + Signature calldata signature + ) external returns (uint256 repaid); + + function withdraw(address underlying, uint256 amount, address onBehalf, address receiver, uint256 maxIterations) + external + returns (uint256 withdrawn); + + function withdrawCollateral(address underlying, uint256 amount, address onBehalf, address receiver) + external + returns (uint256 withdrawn); +} + +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-v3/main.sol b/contracts/mainnet/connectors/morpho-aave-v3/main.sol new file mode 100644 index 00000000..960125a9 --- /dev/null +++ b/contracts/mainnet/connectors/morpho-aave-v3/main.sol @@ -0,0 +1,541 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.7.0; +pragma experimental ABIEncoderV2; +import "./helpers.sol"; +import "./events.sol"; + +abstract contract MorphoAaveV3 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 _amount The amount of the token (in underlying) to deposit. (For max: `uint256(-1)`) + * @param _maxIterations The maximum number of iterations allowed during the matching process. + * @param _getId ID to retrieve amt. + * @param _setId ID stores the amount of tokens deposited. + */ + function deposit( + address _tokenAddress, + uint256 _amount, + uint256 _maxIterations, + 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_V3), _amt); + + MORPHO_AAVE_V3.supply(_tokenContract, _amt, address(this), _maxIterations); + + setUint(_setId, _amt); + + _eventName = "LogDeposit(address,uint256,uint256,uint256,uint256)"; + _eventParam = abi.encode( + _tokenAddress, + _amt, + _maxIterations, + _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 _amount The amount of the token (in underlying) to deposit. (For max: `uint256(-1)`) + * @param _maxIterations The maximum number of iterations allowed during the matching process. + * @param _onBehalf The address of user on behalf to deposit. + * @param _getId ID to retrieve amt. + * @param _setId ID stores the amount of tokens deposited. + */ + function depositOnBehalf( + address _tokenAddress, + uint256 _amount, + address _onBehalf, + uint256 _maxIterations, + 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_V3), _amt); + + MORPHO_AAVE_V3.supply(_tokenContract, _amt, _onBehalf, _maxIterations); + + setUint(_setId, _amt); + + _eventName = "LogDepositOnBehalf(address,uint256,address,uint256,uint256,uint256)"; + _eventParam = abi.encode( + _tokenAddress, + _amt, + _onBehalf, + _maxIterations, + _getId, + _setId + ); + } + + /** + * @notice Supplies `amount` of `underlying` of `onBehalf` using permit2 in a single tx. + * @param _tokenAddress The address of underlying token to deposit.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) + * @param _amount The amount of the token (in underlying) to deposit. (For max: `uint256(-1)`) + * @param _onBehalf The address of user on behalf to deposit. + * @param _maxIterations The maximum number of iterations allowed during the matching process. + * @param _signature The permit2 signature. + * @param _getId ID to retrieve amt. + * @param _setId ID stores the amount of tokens deposited. + */ + function depositWithPermit( + address _tokenAddress, + uint256 _amount, + address _onBehalf, + uint256 _maxIterations, + Signature calldata _signature, + 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_V3), _amt); + + MORPHO_AAVE_V3.supplyWithPermit(_tokenContract, _amt, _onBehalf, _maxIterations, block.timestamp, _signature); + + setUint(_setId, _amt); + + _eventName = "LogDepositWithPermit(address,uint256,address,uint256,uint256,uint8,bytes32,bytes32,uint256,uint256)"; + _eventParam = abi.encode( + _tokenAddress, + _amt, + _onBehalf, + _maxIterations, + block.timestamp, + _signature.v, + _signature.r, + _signature.s, + _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 _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 depositCollateral( + address _tokenAddress, + 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_V3), _amt); + + MORPHO_AAVE_V3.supplyCollateral(_tokenContract, _amt, address(this)); + + setUint(_setId, _amt); + + _eventName = "LogDepositCollateral(address,uint256,address,uint256,uint256)"; + _eventParam = abi.encode( + _tokenAddress, + _amt, + _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 _amount The amount of the token (in underlying) to deposit. (For max: `uint256(-1)`) + * @param _onBehalf The address of user on behalf to deposit. + * @param _getId ID to retrieve amt. + * @param _setId ID stores the amount of tokens deposited. + */ + function depositCollateralOnBehalf( + address _tokenAddress, + uint256 _amount, + address _onBehalf, + 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_V3), _amt); + + MORPHO_AAVE_V3.supplyCollateral(_tokenContract, _amt, _onBehalf); + + setUint(_setId, _amt); + + _eventName = "LogDepositCollateralOnBehalf(address,uint256,address,uint256,uint256)"; + _eventParam = abi.encode( + _tokenAddress, + _amt, + _onBehalf, + _getId, + _setId + ); + } + + /** + * @notice Supplies `amount` of `underlying` of `onBehalf` using permit2 in a single tx. + * @param _tokenAddress The address of underlying token to deposit.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) + * @param _amount The amount of the token (in underlying) to deposit. (For max: `uint256(-1)`) + * @param _onBehalf The address of user on behalf to deposit. + * @param _signature The permit2 signature. + * @param _getId ID to retrieve amt. + * @param _setId ID stores the amount of tokens deposited. + */ + function depositCollateralWithPermit( + address _tokenAddress, + uint256 _amount, + address _onBehalf, + Signature calldata _signature, + 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_V3), _amt); + + MORPHO_AAVE_V3.supplyCollateralWithPermit(_tokenContract, _amt, _onBehalf, block.timestamp, _signature); + + setUint(_setId, _amt); + + _eventName = "LogDepositCollateralWithPermit(address,uint256,address,uint256,uint8,bytes32,bytes32,uint256,uint256)"; + _eventParam = abi.encode( + _tokenAddress, + _amt, + _onBehalf, + block.timestamp, + _signature.v, + _signature.r, + _signature.s, + _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 _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, + uint256 _amount, + uint256 _getId, + uint256 _setId + ) + external + payable + returns (string memory _eventName, bytes memory _eventParam) + { + uint256 _amt = getUint(_getId, _amount); + + MORPHO_AAVE_V3.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 Aave. + * @param _tokenAddress The address of underlying token to borrow.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) + * @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, + uint256 _amount, + uint256 _getId, + uint256 _setId + ) + external + payable + returns (string memory _eventName, bytes memory _eventParam) + { + uint256 _amt = getUint(_getId, _amount); + + MORPHO_AAVE_V3.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_V3.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_V3.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_V3), _amt); + + MORPHO_AAVE_V3.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_V3), _amt); + + MORPHO_AAVE_V3.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 ConnectV3MorphoAaveV3 is MorphoAaveV3 { + string public constant name = "Morpho-AaveV3-v1.0"; +}