From f9566ae6a781f5782e90a177c5ee38373b1135e6 Mon Sep 17 00:00:00 2001 From: Mubaris NK Date: Mon, 29 Mar 2021 17:04:05 +0530 Subject: [PATCH] Add swap borrow rate method --- contracts/connectors/aave/v2-amm/events.sol | 1 + .../connectors/aave/v2-amm/interface.sol | 1 + contracts/connectors/aave/v2-amm/main.sol | 24 ++++++++++++++++++- 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/contracts/connectors/aave/v2-amm/events.sol b/contracts/connectors/aave/v2-amm/events.sol index 4decffe0..805b2209 100644 --- a/contracts/connectors/aave/v2-amm/events.sol +++ b/contracts/connectors/aave/v2-amm/events.sol @@ -6,4 +6,5 @@ contract Events { event LogBorrow(address indexed token, uint256 tokenAmt, uint256 indexed rateMode, uint256 getId, uint256 setId); event LogPayback(address indexed token, uint256 tokenAmt, uint256 indexed rateMode, uint256 getId, uint256 setId); event LogEnableCollateral(address[] tokens); + event LogSwapRateMode(address indexed token, uint256 rateMode); } diff --git a/contracts/connectors/aave/v2-amm/interface.sol b/contracts/connectors/aave/v2-amm/interface.sol index 0d170bf3..ab17fd22 100644 --- a/contracts/connectors/aave/v2-amm/interface.sol +++ b/contracts/connectors/aave/v2-amm/interface.sol @@ -12,6 +12,7 @@ interface AaveInterface { ) external; function repay(address _asset, uint256 _amount, uint256 _rateMode, address _onBehalfOf) external; function setUserUseReserveAsCollateral(address _asset, bool _useAsCollateral) external; + function swapBorrowRateMode(address _asset, uint256 _rateMode) external; } interface AaveLendingPoolProviderInterface { diff --git a/contracts/connectors/aave/v2-amm/main.sol b/contracts/connectors/aave/v2-amm/main.sol index d25fa940..5f89d04a 100644 --- a/contracts/connectors/aave/v2-amm/main.sol +++ b/contracts/connectors/aave/v2-amm/main.sol @@ -173,9 +173,31 @@ abstract contract AaveResolver is Events, Helpers { } } - _eventName = "LogEnableCollateral(address[]);"; + _eventName = "LogEnableCollateral(address[])"; _eventParam = abi.encode(tokens); } + + /** + * @dev Swap borrow rate mode + * @notice Swaps user borrow rate mode between variable and stable + * @param token The address of the token to swap borrow rate.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) + * @param rateMode Desired borrow rate mode. (Stable = 1, Variable = 2) + */ + function swapBorrowRateMode( + address token, + uint rateMode + ) external payable returns (string memory _eventName, bytes memory _eventParam) { + AaveInterface aave = AaveInterface(aaveProvider.getLendingPool()); + + uint currentRateMode = rateMode == 1 ? 2 : 1; + + if (getPaybackBalance(token, currentRateMode) > 0) { + aave.swapBorrowRateMode(token, rateMode); + } + + _eventName = "LogSwapRateMode(address,uint256)"; + _eventParam = abi.encode(token, rateMode); + } } contract ConnectV2AaveV2AMM is AaveResolver {