From d135c25b39ae16ceb7fb7bd6762f11f9cfb5169c Mon Sep 17 00:00:00 2001 From: andyk Date: Mon, 14 Sep 2020 15:41:44 +0300 Subject: [PATCH] add borrowRateMode check on credit delegation --- contracts/lendingpool/LendingPool.sol | 18 +++++------------ contracts/libraries/logic/ReserveLogic.sol | 16 +++++++++++++++ test/helpers/actions.ts | 18 ++++++++++++----- test/helpers/scenario-engine.ts | 11 +++++++++- ...delegation.json => credit-delegation.json} | 20 ++++++++++++++++++- 5 files changed, 63 insertions(+), 20 deletions(-) rename test/helpers/scenarios/{borrow-delegation.json => credit-delegation.json} (84%) diff --git a/contracts/lendingpool/LendingPool.sol b/contracts/lendingpool/LendingPool.sol index 48f4b7fe..f0e3214e 100644 --- a/contracts/lendingpool/LendingPool.sol +++ b/contracts/lendingpool/LendingPool.sol @@ -167,11 +167,8 @@ contract LendingPool is VersionedInitializable, ILendingPool { address asset, uint256 interestRateMode ) external override view returns (uint256) { - address debtToken = ReserveLogic.InterestRateMode.STABLE == - ReserveLogic.InterestRateMode(interestRateMode) - ? _reserves[asset].stableDebtTokenAddress - : _reserves[asset].variableDebtTokenAddress; - return _borrowAllowance[debtToken][fromUser][toUser]; + return + _borrowAllowance[_reserves[asset].getDebtTokenAddress(interestRateMode)][fromUser][toUser]; } function delegateBorrowAllowance( @@ -180,10 +177,8 @@ contract LendingPool is VersionedInitializable, ILendingPool { uint256 interestRateMode, uint256 amount ) external override { - address debtToken = ReserveLogic.InterestRateMode.STABLE == - ReserveLogic.InterestRateMode(interestRateMode) - ? _reserves[asset].stableDebtTokenAddress - : _reserves[asset].variableDebtTokenAddress; + address debtToken = _reserves[asset].getDebtTokenAddress(interestRateMode); + _borrowAllowance[debtToken][msg.sender][user] = amount; emit BorrowAllowanceDelegated(asset, msg.sender, user, interestRateMode, amount); } @@ -206,10 +201,7 @@ contract LendingPool is VersionedInitializable, ILendingPool { ReserveLogic.ReserveData storage reserve = _reserves[asset]; if (onBehalfOf != msg.sender) { - address debtToken = ReserveLogic.InterestRateMode.STABLE == - ReserveLogic.InterestRateMode(interestRateMode) - ? reserve.stableDebtTokenAddress - : reserve.variableDebtTokenAddress; + address debtToken = reserve.getDebtTokenAddress(interestRateMode); _borrowAllowance[debtToken][onBehalfOf][msg .sender] = _borrowAllowance[debtToken][onBehalfOf][msg.sender].sub( diff --git a/contracts/libraries/logic/ReserveLogic.sol b/contracts/libraries/logic/ReserveLogic.sol index 06d08499..2f8b668a 100644 --- a/contracts/libraries/logic/ReserveLogic.sol +++ b/contracts/libraries/logic/ReserveLogic.sol @@ -116,6 +116,22 @@ library ReserveLogic { return cumulated; } + function getDebtTokenAddress(ReserveLogic.ReserveData storage reserve, uint256 interestRateMode) + internal + view + returns (address) + { + require( + ReserveLogic.InterestRateMode.STABLE == ReserveLogic.InterestRateMode(interestRateMode) || + ReserveLogic.InterestRateMode.VARIABLE == ReserveLogic.InterestRateMode(interestRateMode), + Errors.INVALID_INTEREST_RATE_MODE_SELECTED + ); + return + ReserveLogic.InterestRateMode.STABLE == ReserveLogic.InterestRateMode(interestRateMode) + ? reserve.stableDebtTokenAddress + : reserve.variableDebtTokenAddress; + } + /** * @dev Updates the liquidity cumulative index Ci and variable borrow cumulative index Bvc. Refer to the whitepaper for * a formal specification. diff --git a/test/helpers/actions.ts b/test/helpers/actions.ts index a2bef096..c988c106 100644 --- a/test/helpers/actions.ts +++ b/test/helpers/actions.ts @@ -284,19 +284,27 @@ export const delegateBorrowAllowance = async ( interestRateMode: string, user: SignerWithAddress, receiver: tEthereumAddress, - testEnv: TestEnv + expectedResult: string, + testEnv: TestEnv, + revertMessage?: string ) => { const {pool} = testEnv; const reserve = await getReserveAddressFromSymbol(reserveSymbol); const amountToDelegate = await convertToCurrencyDecimals(reserve, amount); - await pool + const delegateAllowancePromise = pool .connect(user.signer) .delegateBorrowAllowance(receiver, reserve, interestRateMode, amountToDelegate.toString()); - expect( - (await pool.getBorrowAllowance(user.address, receiver, reserve, interestRateMode)).toString() - ).to.be.equal(amountToDelegate.toString(), 'borrowAllowance are set incorrectly'); + if (expectedResult === 'revert') { + await expect(delegateAllowancePromise, revertMessage).to.be.reverted; + return; + } else { + await delegateAllowancePromise; + expect( + (await pool.getBorrowAllowance(user.address, receiver, reserve, interestRateMode)).toString() + ).to.be.equal(amountToDelegate.toString(), 'borrowAllowance are set incorrectly'); + } }; export const borrow = async ( diff --git a/test/helpers/scenario-engine.ts b/test/helpers/scenario-engine.ts index 8de5780d..fd886114 100644 --- a/test/helpers/scenario-engine.ts +++ b/test/helpers/scenario-engine.ts @@ -111,7 +111,16 @@ const executeAction = async (action: Action, users: SignerWithAddress[], testEnv throw `Invalid amount to deposit into the ${reserve} reserve`; } - await delegateBorrowAllowance(reserve, amount, rateMode, user, toUser, testEnv); + await delegateBorrowAllowance( + reserve, + amount, + rateMode, + user, + toUser, + expected, + testEnv, + revertMessage + ); } break; diff --git a/test/helpers/scenarios/borrow-delegation.json b/test/helpers/scenarios/credit-delegation.json similarity index 84% rename from test/helpers/scenarios/borrow-delegation.json rename to test/helpers/scenarios/credit-delegation.json index eed21c5a..a67924ee 100644 --- a/test/helpers/scenarios/borrow-delegation.json +++ b/test/helpers/scenarios/credit-delegation.json @@ -73,7 +73,7 @@ ] }, { - "description": "User 0 delegates borrowing of 1 WETH to user 4, user 4 borrows 3 WETH on behalf of user 0, revert expected", + "description": "User 0 delegates borrowing of 1 WETH to user 4, user 4 borrows 3 WETH variable on behalf of user 0, revert expected", "actions": [ { "name": "delegateBorrowAllowance", @@ -81,6 +81,7 @@ "reserve": "WETH", "amount": "1", "user": "0", + "borrowRateMode": "variable", "toUser": "4" }, "expected": "success" @@ -125,6 +126,23 @@ "expected": "success" } ] + }, + { + "description": "User 0 delegates borrowing of 1 WETH to user 2 with wrong borrowRateMode, revert expected", + "actions": [ + { + "name": "delegateBorrowAllowance", + "args": { + "reserve": "WETH", + "amount": "1", + "user": "0", + "borrowRateMode": "random", + "toUser": "2" + }, + "expected": "revert", + "revertMessage": "8" + } + ] } ] }