diff --git a/contracts/polygon/connectors/aave/v3/events.sol b/contracts/polygon/connectors/aave/v3/events.sol index 800c7873..0324edcd 100644 --- a/contracts/polygon/connectors/aave/v3/events.sol +++ b/contracts/polygon/connectors/aave/v3/events.sol @@ -54,4 +54,12 @@ contract Events { uint256 getId, uint256 setId ); + event LogPaybackOnBehalfOf( + address token, + uint256 amt, + uint256 rateMode, + address onBehalfOf, + uint256 getId, + uint256 setId + ); } diff --git a/contracts/polygon/connectors/aave/v3/helpers.sol b/contracts/polygon/connectors/aave/v3/helpers.sol index 4b2d239b..1f21db85 100644 --- a/contracts/polygon/connectors/aave/v3/helpers.sol +++ b/contracts/polygon/connectors/aave/v3/helpers.sol @@ -25,7 +25,7 @@ abstract contract Helpers is DSMath, Basic { /** * @dev Checks if collateral is enabled for an asset - * @param token token address of the asset.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) + * @param token token address of the asset.(For matic: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) */ function getIsColl(address token) internal view returns (bool isCol) { @@ -37,7 +37,7 @@ abstract contract Helpers is DSMath, Basic { /** * @dev Get total debt balance & fee for an asset - * @param token token address of the debt.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) + * @param token token address of the debt.(For matic: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) * @param rateMode Borrow rate mode (Stable = 1, Variable = 2) */ function getPaybackBalance(address token, uint256 rateMode) @@ -50,9 +50,24 @@ abstract contract Helpers is DSMath, Basic { return rateMode == 1 ? stableDebt : variableDebt; } + /** + * @dev Get OnBehalfOf user's total debt balance & fee for an asset + * @param token token address of the debt.(For matic: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) + * @param rateMode Borrow rate mode (Stable = 1, Variable = 2) + */ + function getOnBehalfOfPaybackBalance(address token, uint256 rateMode, address onBehalfOf) + internal + view + returns (uint256) + { + (, uint256 stableDebt, uint256 variableDebt, , , , , , ) = aaveData + .getUserReserveData(token, onBehalfOf); + return rateMode == 1 ? stableDebt : variableDebt; + } + /** * @dev Get total collateral balance for an asset - * @param token token address of the collateral.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) + * @param token token address of the collateral.(For matic: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) */ function getCollateralBalance(address token) internal diff --git a/contracts/polygon/connectors/aave/v3/main.sol b/contracts/polygon/connectors/aave/v3/main.sol index 5ff5f53d..a1336a8e 100644 --- a/contracts/polygon/connectors/aave/v3/main.sol +++ b/contracts/polygon/connectors/aave/v3/main.sol @@ -313,6 +313,51 @@ abstract contract AaveResolver is Events, Helpers { _eventParam = abi.encode(token, _amt, rateMode, getId, setId); } + /** + * @dev Payback borrowed matic/ERC20_Token on behalf os a user. + * @notice Payback debt owed on behalf os a user. + * @param token The address of the token to payback.(For matic: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) + * @param amt The amount of the token to payback. (For max: `uint256(-1)`) + * @param rateMode The type of debt paying back. (For Stable: 1, Variable: 2) + * @param onBehalfOf Address of user who's debt to repay. + * @param getId ID to retrieve amt. + * @param setId ID stores the amount of tokens paid back. + */ + function paybackOnBehalfOf( + address token, + uint256 amt, + uint256 rateMode, + address onBehalfOf, + uint256 getId, + uint256 setId + ) + external + payable + returns (string memory _eventName, bytes memory _eventParam) + { + uint256 _amt = getUint(getId, amt); + + AaveInterface aave = AaveInterface(aaveProvider.getPool()); + + bool isMatic = token == maticAddr; + address _token = isMatic ? wmaticAddr : token; + + TokenInterface tokenContract = TokenInterface(_token); + + _amt = _amt == uint256(-1) ? getOnBehalfOfPaybackBalance(_token, rateMode, onBehalfOf) : _amt; + + if (isMatic) convertMaticToWmatic(isMatic, tokenContract, _amt); + + approve(tokenContract, address(aave), _amt); + + aave.repay(_token, _amt, rateMode, onBehalfOf); + + setUint(setId, _amt); + + _eventName = "LogPaybackOnBehalfOf(address,uint256,uint256,address,uint256,uint256)"; + _eventParam = abi.encode(token, _amt, rateMode, onBehalfOf, getId, setId); + } + /** * @dev Enable collateral * @notice Enable an array of tokens as collateral