add borrowRateMode check on credit delegation

This commit is contained in:
andyk 2020-09-14 15:41:44 +03:00
parent c0c3133763
commit d135c25b39
5 changed files with 63 additions and 20 deletions

View File

@ -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(

View File

@ -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.

View File

@ -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 (

View File

@ -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;

View File

@ -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"
}
]
}
]
}