From f513ebee370a319af83a38043b3ddce34d2abade Mon Sep 17 00:00:00 2001 From: The3D Date: Tue, 3 Nov 2020 12:14:09 +0100 Subject: [PATCH 1/2] Refactored duplicated code --- contracts/lendingpool/LendingPool.sol | 25 ++++++++----------------- 1 file changed, 8 insertions(+), 17 deletions(-) diff --git a/contracts/lendingpool/LendingPool.sol b/contracts/lendingpool/LendingPool.sol index 5d476ab9..849fb49d 100644 --- a/contracts/lendingpool/LendingPool.sol +++ b/contracts/lendingpool/LendingPool.sol @@ -232,15 +232,6 @@ contract LendingPool is VersionedInitializable, ILendingPool, LendingPoolStorage _whenNotPaused(); ReserveLogic.ReserveData storage reserve = _reserves[asset]; - if (onBehalfOf != msg.sender) { - address debtToken = reserve.getDebtTokenAddress(interestRateMode); - - _borrowAllowance[debtToken][onBehalfOf][msg - .sender] = _borrowAllowance[debtToken][onBehalfOf][msg.sender].sub( - amount, - Errors.LP_BORROW_ALLOWANCE_ARE_NOT_ENOUGH - ); - } _executeBorrow( ExecuteBorrowParams( asset, @@ -583,14 +574,6 @@ contract LendingPool is VersionedInitializable, ILendingPool, LendingPoolStorage vars.currentAmountPlusPremium ); } else { - if (msg.sender != onBehalfOf) { - vars.debtToken = _reserves[vars.currentAsset].getDebtTokenAddress(modes[vars.i]); - - _borrowAllowance[vars.debtToken][onBehalfOf][msg.sender] = _borrowAllowance[vars - .debtToken][onBehalfOf][msg.sender] - .sub(vars.currentAmount, Errors.LP_BORROW_ALLOWANCE_ARE_NOT_ENOUGH); - } - //if the user didn't choose to return the funds, the system checks if there //is enough collateral and eventually open a position _executeBorrow( @@ -915,6 +898,14 @@ contract LendingPool is VersionedInitializable, ILendingPool, LendingPoolStorage oracle ); + if (vars.onBehalfOf != msg.sender) { + address debtToken = reserve.getDebtTokenAddress(vars.interestRateMode); + + _borrowAllowance[debtToken][vars.onBehalfOf][msg.sender] = _borrowAllowance[debtToken][vars + .onBehalfOf][msg.sender] + .sub(vars.amount, Errors.LP_BORROW_ALLOWANCE_ARE_NOT_ENOUGH); + } + reserve.updateState(); //caching the current stable borrow rate From 54bae079c903a26e69053dd7bfa8ac5b2dd6731b Mon Sep 17 00:00:00 2001 From: The3D Date: Tue, 3 Nov 2020 12:15:37 +0100 Subject: [PATCH 2/2] Renamed error --- contracts/lendingpool/LendingPool.sol | 2 +- contracts/libraries/helpers/Errors.sol | 2 +- helpers/types.ts | 2 +- test/flashloan.spec.ts | 4 ++-- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/contracts/lendingpool/LendingPool.sol b/contracts/lendingpool/LendingPool.sol index 849fb49d..4c70ca88 100644 --- a/contracts/lendingpool/LendingPool.sol +++ b/contracts/lendingpool/LendingPool.sol @@ -903,7 +903,7 @@ contract LendingPool is VersionedInitializable, ILendingPool, LendingPoolStorage _borrowAllowance[debtToken][vars.onBehalfOf][msg.sender] = _borrowAllowance[debtToken][vars .onBehalfOf][msg.sender] - .sub(vars.amount, Errors.LP_BORROW_ALLOWANCE_ARE_NOT_ENOUGH); + .sub(vars.amount, Errors.LP_BORROW_ALLOWANCE_NOT_ENOUGH); } reserve.updateState(); diff --git a/contracts/libraries/helpers/Errors.sol b/contracts/libraries/helpers/Errors.sol index a0a65762..2b173fff 100644 --- a/contracts/libraries/helpers/Errors.sol +++ b/contracts/libraries/helpers/Errors.sol @@ -79,7 +79,7 @@ library Errors { string public constant AT_INVALID_MINT_AMOUNT = '56'; //invalid amount to mint string public constant LP_FAILED_REPAY_WITH_COLLATERAL = '57'; string public constant AT_INVALID_BURN_AMOUNT = '58'; //invalid amount to burn - string public constant LP_BORROW_ALLOWANCE_ARE_NOT_ENOUGH = '59'; // User borrows on behalf, but allowance are too small + string public constant LP_BORROW_ALLOWANCE_NOT_ENOUGH = '59'; // User borrows on behalf, but allowance are too small string public constant LP_FAILED_COLLATERAL_SWAP = '60'; string public constant LP_INVALID_EQUAL_ASSETS_TO_SWAP = '61'; string public constant LP_REENTRANCY_NOT_ALLOWED = '62'; diff --git a/helpers/types.ts b/helpers/types.ts index f10938d2..80f3846d 100644 --- a/helpers/types.ts +++ b/helpers/types.ts @@ -135,7 +135,7 @@ export enum ProtocolErrors { AT_INVALID_MINT_AMOUNT = '56', //invalid amount to mint LP_FAILED_REPAY_WITH_COLLATERAL = '57', AT_INVALID_BURN_AMOUNT = '58', //invalid amount to burn - LP_BORROW_ALLOWANCE_ARE_NOT_ENOUGH = '59', // User borrows on behalf, but allowance are too small + LP_BORROW_ALLOWANCE_NOT_ENOUGH = '59', // User borrows on behalf, but allowance are too small LP_FAILED_COLLATERAL_SWAP = '60', LP_INVALID_EQUAL_ASSETS_TO_SWAP = '61', LP_REENTRANCY_NOT_ALLOWED = '62', diff --git a/test/flashloan.spec.ts b/test/flashloan.spec.ts index 387a091a..887479a8 100644 --- a/test/flashloan.spec.ts +++ b/test/flashloan.spec.ts @@ -24,7 +24,7 @@ makeSuite('LendingPool FlashLoan function', (testEnv: TestEnv) => { LP_INVALID_FLASHLOAN_MODE, SAFEERC20_LOWLEVEL_CALL, LP_INVALID_FLASH_LOAN_EXECUTOR_RETURN, - LP_BORROW_ALLOWANCE_ARE_NOT_ENOUGH, + LP_BORROW_ALLOWANCE_NOT_ENOUGH, } = ProtocolErrors; before(async () => { @@ -443,7 +443,7 @@ makeSuite('LendingPool FlashLoan function', (testEnv: TestEnv) => { '0x10', '0' ) - ).to.be.revertedWith(LP_BORROW_ALLOWANCE_ARE_NOT_ENOUGH); + ).to.be.revertedWith(LP_BORROW_ALLOWANCE_NOT_ENOUGH); }); it('Caller takes a WETH flashloan with mode = 1 onBehalfOf user with allowance. A loan for onBehalfOf is creatd.', async () => {