mirror of
https://github.com/Instadapp/aave-protocol-v2.git
synced 2024-07-29 21:47:30 +00:00
add borrowRateMode check on credit delegation
This commit is contained in:
parent
c0c3133763
commit
d135c25b39
|
@ -167,11 +167,8 @@ contract LendingPool is VersionedInitializable, ILendingPool {
|
||||||
address asset,
|
address asset,
|
||||||
uint256 interestRateMode
|
uint256 interestRateMode
|
||||||
) external override view returns (uint256) {
|
) external override view returns (uint256) {
|
||||||
address debtToken = ReserveLogic.InterestRateMode.STABLE ==
|
return
|
||||||
ReserveLogic.InterestRateMode(interestRateMode)
|
_borrowAllowance[_reserves[asset].getDebtTokenAddress(interestRateMode)][fromUser][toUser];
|
||||||
? _reserves[asset].stableDebtTokenAddress
|
|
||||||
: _reserves[asset].variableDebtTokenAddress;
|
|
||||||
return _borrowAllowance[debtToken][fromUser][toUser];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function delegateBorrowAllowance(
|
function delegateBorrowAllowance(
|
||||||
|
@ -180,10 +177,8 @@ contract LendingPool is VersionedInitializable, ILendingPool {
|
||||||
uint256 interestRateMode,
|
uint256 interestRateMode,
|
||||||
uint256 amount
|
uint256 amount
|
||||||
) external override {
|
) external override {
|
||||||
address debtToken = ReserveLogic.InterestRateMode.STABLE ==
|
address debtToken = _reserves[asset].getDebtTokenAddress(interestRateMode);
|
||||||
ReserveLogic.InterestRateMode(interestRateMode)
|
|
||||||
? _reserves[asset].stableDebtTokenAddress
|
|
||||||
: _reserves[asset].variableDebtTokenAddress;
|
|
||||||
_borrowAllowance[debtToken][msg.sender][user] = amount;
|
_borrowAllowance[debtToken][msg.sender][user] = amount;
|
||||||
emit BorrowAllowanceDelegated(asset, msg.sender, user, interestRateMode, amount);
|
emit BorrowAllowanceDelegated(asset, msg.sender, user, interestRateMode, amount);
|
||||||
}
|
}
|
||||||
|
@ -206,10 +201,7 @@ contract LendingPool is VersionedInitializable, ILendingPool {
|
||||||
ReserveLogic.ReserveData storage reserve = _reserves[asset];
|
ReserveLogic.ReserveData storage reserve = _reserves[asset];
|
||||||
|
|
||||||
if (onBehalfOf != msg.sender) {
|
if (onBehalfOf != msg.sender) {
|
||||||
address debtToken = ReserveLogic.InterestRateMode.STABLE ==
|
address debtToken = reserve.getDebtTokenAddress(interestRateMode);
|
||||||
ReserveLogic.InterestRateMode(interestRateMode)
|
|
||||||
? reserve.stableDebtTokenAddress
|
|
||||||
: reserve.variableDebtTokenAddress;
|
|
||||||
|
|
||||||
_borrowAllowance[debtToken][onBehalfOf][msg
|
_borrowAllowance[debtToken][onBehalfOf][msg
|
||||||
.sender] = _borrowAllowance[debtToken][onBehalfOf][msg.sender].sub(
|
.sender] = _borrowAllowance[debtToken][onBehalfOf][msg.sender].sub(
|
||||||
|
|
|
@ -116,6 +116,22 @@ library ReserveLogic {
|
||||||
return cumulated;
|
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
|
* @dev Updates the liquidity cumulative index Ci and variable borrow cumulative index Bvc. Refer to the whitepaper for
|
||||||
* a formal specification.
|
* a formal specification.
|
||||||
|
|
|
@ -284,19 +284,27 @@ export const delegateBorrowAllowance = async (
|
||||||
interestRateMode: string,
|
interestRateMode: string,
|
||||||
user: SignerWithAddress,
|
user: SignerWithAddress,
|
||||||
receiver: tEthereumAddress,
|
receiver: tEthereumAddress,
|
||||||
testEnv: TestEnv
|
expectedResult: string,
|
||||||
|
testEnv: TestEnv,
|
||||||
|
revertMessage?: string
|
||||||
) => {
|
) => {
|
||||||
const {pool} = testEnv;
|
const {pool} = testEnv;
|
||||||
|
|
||||||
const reserve = await getReserveAddressFromSymbol(reserveSymbol);
|
const reserve = await getReserveAddressFromSymbol(reserveSymbol);
|
||||||
const amountToDelegate = await convertToCurrencyDecimals(reserve, amount);
|
const amountToDelegate = await convertToCurrencyDecimals(reserve, amount);
|
||||||
|
|
||||||
await pool
|
const delegateAllowancePromise = pool
|
||||||
.connect(user.signer)
|
.connect(user.signer)
|
||||||
.delegateBorrowAllowance(receiver, reserve, interestRateMode, amountToDelegate.toString());
|
.delegateBorrowAllowance(receiver, reserve, interestRateMode, amountToDelegate.toString());
|
||||||
expect(
|
if (expectedResult === 'revert') {
|
||||||
(await pool.getBorrowAllowance(user.address, receiver, reserve, interestRateMode)).toString()
|
await expect(delegateAllowancePromise, revertMessage).to.be.reverted;
|
||||||
).to.be.equal(amountToDelegate.toString(), 'borrowAllowance are set incorrectly');
|
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 (
|
export const borrow = async (
|
||||||
|
|
|
@ -111,7 +111,16 @@ const executeAction = async (action: Action, users: SignerWithAddress[], testEnv
|
||||||
throw `Invalid amount to deposit into the ${reserve} reserve`;
|
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;
|
break;
|
||||||
|
|
||||||
|
|
|
@ -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": [
|
"actions": [
|
||||||
{
|
{
|
||||||
"name": "delegateBorrowAllowance",
|
"name": "delegateBorrowAllowance",
|
||||||
|
@ -81,6 +81,7 @@
|
||||||
"reserve": "WETH",
|
"reserve": "WETH",
|
||||||
"amount": "1",
|
"amount": "1",
|
||||||
"user": "0",
|
"user": "0",
|
||||||
|
"borrowRateMode": "variable",
|
||||||
"toUser": "4"
|
"toUser": "4"
|
||||||
},
|
},
|
||||||
"expected": "success"
|
"expected": "success"
|
||||||
|
@ -125,6 +126,23 @@
|
||||||
"expected": "success"
|
"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"
|
||||||
|
}
|
||||||
|
]
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user