From 48438f59f5d2b98e1a8c770fd9968835984a737c Mon Sep 17 00:00:00 2001
From: emilio <emilio@ethlend.io>
Date: Thu, 3 Sep 2020 16:29:14 +0200
Subject: [PATCH] Added a new test to check an invalid interest rate mode

---
 contracts/lendingpool/LendingPool.sol         |  5 +++--
 contracts/libraries/logic/ValidationLogic.sol |  6 ++---
 helpers/types.ts                              |  1 +
 test/flashloan.spec.ts                        | 22 ++++++++++++++++++-
 4 files changed, 28 insertions(+), 6 deletions(-)

diff --git a/contracts/lendingpool/LendingPool.sol b/contracts/lendingpool/LendingPool.sol
index 503612f6..47a260d6 100644
--- a/contracts/lendingpool/LendingPool.sol
+++ b/contracts/lendingpool/LendingPool.sol
@@ -435,9 +435,10 @@ contract LendingPool is VersionedInitializable, ILendingPool {
 
     vars.premium = amount.mul(FLASHLOAN_PREMIUM_TOTAL).div(10000);
 
-    ReserveLogic.InterestRateMode debtMode = ReserveLogic.InterestRateMode(mode);
 
-    ValidationLogic.validateFlashloan(debtMode, vars.premium);
+    ValidationLogic.validateFlashloan(mode, vars.premium);
+
+    ReserveLogic.InterestRateMode debtMode = ReserveLogic.InterestRateMode(mode);
 
     vars.receiver = IFlashLoanReceiver(receiverAddress);
 
diff --git a/contracts/libraries/logic/ValidationLogic.sol b/contracts/libraries/logic/ValidationLogic.sol
index 4d9649e5..7a640458 100644
--- a/contracts/libraries/logic/ValidationLogic.sol
+++ b/contracts/libraries/logic/ValidationLogic.sol
@@ -322,11 +322,11 @@ library ValidationLogic {
 
   /**
   * @dev validates a flashloan action
-  * @param mode the flashloan mode (NONE = classic flashloan, STABLE = open a stable rate loan, VARIABLE = open a variable rate loan)
+  * @param mode the flashloan mode (0 = classic flashloan, 1 = open a stable rate loan, 2 = open a variable rate loan)
   * @param premium the premium paid on the flashloan
   **/
-  function validateFlashloan(ReserveLogic.InterestRateMode mode, uint256 premium) internal pure {
+  function validateFlashloan(uint256 mode, uint256 premium) internal pure {
     require(premium > 0, Errors.REQUESTED_AMOUNT_TOO_SMALL);
-    require(mode <= ReserveLogic.InterestRateMode.VARIABLE, Errors.INVALID_FLASHLOAN_MODE);
+    require(mode <= uint256(ReserveLogic.InterestRateMode.VARIABLE), Errors.INVALID_FLASHLOAN_MODE);
   }
 }
diff --git a/helpers/types.ts b/helpers/types.ts
index 60edd765..7757bc45 100644
--- a/helpers/types.ts
+++ b/helpers/types.ts
@@ -99,6 +99,7 @@ export enum ProtocolErrors {
   SPECIFIED_CURRENCY_NOT_BORROWED_BY_USER = '40', // 'User did not borrow the specified currency'
   NOT_ENOUGH_LIQUIDITY_TO_LIQUIDATE = '41', // "There isn't enough liquidity available to liquidate"
   NO_ERRORS = '42', // 'No errors'
+  INVALID_FLASHLOAN_MODE = '43', //Invalid flashloan mode
 
   // old
 
diff --git a/test/flashloan.spec.ts b/test/flashloan.spec.ts
index 063adc96..87e31b8d 100644
--- a/test/flashloan.spec.ts
+++ b/test/flashloan.spec.ts
@@ -18,7 +18,8 @@ makeSuite('LendingPool FlashLoan function', (testEnv: TestEnv) => {
   const {
     COLLATERAL_BALANCE_IS_0,
     REQUESTED_AMOUNT_TOO_SMALL,
-    TRANSFER_AMOUNT_EXCEEDS_BALANCE
+    TRANSFER_AMOUNT_EXCEEDS_BALANCE,
+    INVALID_FLASHLOAN_MODE
   } = ProtocolErrors;
 
   before(async () => {
@@ -110,6 +111,25 @@ makeSuite('LendingPool FlashLoan function', (testEnv: TestEnv) => {
     ).to.be.revertedWith(TRANSFER_AMOUNT_EXCEEDS_BALANCE);
   });
 
+  it('Takes a WETH flashloan with an invalid mode. (revert expected)', async () => {
+    const {pool, weth, users} = testEnv;
+    const caller = users[1];
+    await _mockFlashLoanReceiver.setFailExecutionTransfer(true);
+
+    await expect(
+      pool
+        .connect(caller.signer)
+        .flashLoan(
+          _mockFlashLoanReceiver.address,
+          weth.address,
+          ethers.utils.parseEther('0.8'),
+          4,
+          '0x10',
+          '0'
+        )
+    ).to.be.revertedWith(INVALID_FLASHLOAN_MODE);
+  });
+
   it('Caller deposits 1000 DAI as collateral, Takes WETH flashloan with mode = 2, does not return the funds. A variable loan for caller is created', async () => {
     const {dai, pool, weth, users} = testEnv;