Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Borrow/repay check removal #705

Merged
merged 3 commits into from
Oct 28, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion contracts/protocol/libraries/helpers/Errors.sol
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ library Errors {
string public constant HEALTH_FACTOR_NOT_BELOW_THRESHOLD = '45'; // 'Health factor is not below the threshold'
string public constant COLLATERAL_CANNOT_BE_LIQUIDATED = '46'; // 'The collateral chosen cannot be liquidated'
string public constant SPECIFIED_CURRENCY_NOT_BORROWED_BY_USER = '47'; // 'User did not borrow the specified currency'
string public constant SAME_BLOCK_BORROW_REPAY = '48'; // 'Borrow and repay in same block is not allowed'
string public constant INCONSISTENT_FLASHLOAN_PARAMS = '49'; // 'Inconsistent flashloan parameters'
string public constant BORROW_CAP_EXCEEDED = '50'; // 'Borrow cap is exceeded'
string public constant SUPPLY_CAP_EXCEEDED = '51'; // 'Supply cap is exceeded'
Expand Down
14 changes: 0 additions & 14 deletions contracts/protocol/libraries/logic/ValidationLogic.sol
Original file line number Diff line number Diff line change
Expand Up @@ -327,20 +327,6 @@ library ValidationLogic {
require(isActive, Errors.RESERVE_INACTIVE);
require(!isPaused, Errors.RESERVE_PAUSED);

uint256 variableDebtPreviousIndex = IScaledBalanceToken(reserveCache.variableDebtTokenAddress)
.getPreviousIndex(onBehalfOf);

uint40 stableRatePreviousTimestamp = IStableDebtToken(reserveCache.stableDebtTokenAddress)
.getUserLastUpdated(onBehalfOf);

require(
(stableRatePreviousTimestamp < uint40(block.timestamp) &&
interestRateMode == DataTypes.InterestRateMode.STABLE) ||
(variableDebtPreviousIndex < reserveCache.nextVariableBorrowIndex &&
interestRateMode == DataTypes.InterestRateMode.VARIABLE),
Errors.SAME_BLOCK_BORROW_REPAY
);

require(
(stableDebt != 0 && interestRateMode == DataTypes.InterestRateMode.STABLE) ||
(variableDebt != 0 && interestRateMode == DataTypes.InterestRateMode.VARIABLE),
Expand Down
1 change: 0 additions & 1 deletion helpers/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ export enum ProtocolErrors {
HEALTH_FACTOR_NOT_BELOW_THRESHOLD = '45', // 'Health factor is not below the threshold'
COLLATERAL_CANNOT_BE_LIQUIDATED = '46', // 'The collateral chosen cannot be liquidated'
SPECIFIED_CURRENCY_NOT_BORROWED_BY_USER = '47', // 'User did not borrow the specified currency'
SAME_BLOCK_BORROW_REPAY = '48', // 'Borrow and repay in same block is not allowed'
INCONSISTENT_FLASHLOAN_PARAMS = '49', // 'Inconsistent flashloan parameters'
BORROW_CAP_EXCEEDED = '50', // 'Borrow cap is exceeded'
SUPPLY_CAP_EXCEEDED = '51', // 'Supply cap is exceeded'
Expand Down
126 changes: 0 additions & 126 deletions test-suites/validation-logic.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -404,132 +404,6 @@ makeSuite('ValidationLogic: Edge cases', (testEnv: TestEnv) => {
).to.be.revertedWith(RESERVE_INACTIVE);
});

it('validateRepay() when variable borrowing and repaying in same block (revert expected)', async () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe its better to add these tests cases as non-reverting

Copy link
Contributor

@miguelmtzinf miguelmtzinf Aug 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests added

// Same block repay

const { pool, users, dai, aDai, usdc } = testEnv;
const user = users[0];

// We need some debt.
await usdc.connect(user.signer)['mint(uint256)'](utils.parseEther('2000'));
await usdc.connect(user.signer).approve(pool.address, MAX_UINT_AMOUNT);
await pool
.connect(user.signer)
.deposit(usdc.address, utils.parseEther('2000'), user.address, 0);
await dai.connect(user.signer)['mint(uint256)'](utils.parseEther('2000'));
await dai.connect(user.signer).transfer(aDai.address, utils.parseEther('2000'));

// Turn off automining - pretty sure that coverage is getting messed up here.
await setAutomine(false);

// Borrow 500 dai
await pool
.connect(user.signer)
.borrow(dai.address, utils.parseEther('500'), RateMode.Variable, 0, user.address);

// Turn on automining, but not mine a new block until next tx
await setAutomineEvm(true);

await expect(
pool
.connect(user.signer)
.repay(dai.address, utils.parseEther('500'), RateMode.Variable, user.address)
).to.be.revertedWith(SAME_BLOCK_BORROW_REPAY);
});

it('validateRepay() when variable borrowing and repaying in same block using credit delegation (revert expected)', async () => {
const {
pool,
dai,
weth,
users: [user1, user2, user3],
} = testEnv;

// Add liquidity
await dai.connect(user3.signer)['mint(uint256)'](utils.parseUnits('1000', 18));
await dai.connect(user3.signer).approve(pool.address, MAX_UINT_AMOUNT);
await pool
.connect(user3.signer)
.supply(dai.address, utils.parseUnits('1000', 18), user3.address, 0);

// User1 supplies 10 WETH
await dai.connect(user1.signer)['mint(uint256)'](utils.parseUnits('100', 18));
await dai.connect(user1.signer).approve(pool.address, MAX_UINT_AMOUNT);
await weth.connect(user1.signer)['mint(uint256)'](utils.parseUnits('10', 18));
await weth.connect(user1.signer).approve(pool.address, MAX_UINT_AMOUNT);
await pool
.connect(user1.signer)
.supply(weth.address, utils.parseUnits('10', 18), user1.address, 0);

const daiData = await pool.getReserveData(dai.address);
const variableDebtToken = await getVariableDebtToken(daiData.variableDebtTokenAddress);

// User1 approves User2 to borrow 1000 DAI
expect(
await variableDebtToken
.connect(user1.signer)
.approveDelegation(user2.address, utils.parseUnits('1000', 18))
);

// User2 borrows on behalf of User1
const borrowOnBehalfAmount = utils.parseUnits('100', 18);
expect(
await pool
.connect(user2.signer)
.borrow(dai.address, borrowOnBehalfAmount, RateMode.Variable, 0, user1.address)
);

// Turn off automining to simulate actions in same block
await setAutomine(false);

// User2 borrows 2 DAI on behalf of User1
await pool
.connect(user2.signer)
.borrow(dai.address, utils.parseEther('2'), RateMode.Variable, 0, user1.address);

// Turn on automining, but not mine a new block until next tx
await setAutomineEvm(true);

await expect(
pool
.connect(user1.signer)
.repay(dai.address, utils.parseEther('2'), RateMode.Variable, user1.address)
).to.be.revertedWith(SAME_BLOCK_BORROW_REPAY);
});

it('validateRepay() when stable borrowing and repaying in same block (revert expected)', async () => {
// Same block repay

const { pool, users, dai, aDai, usdc } = testEnv;
const user = users[0];

// We need some debt.
await usdc.connect(user.signer)['mint(uint256)'](utils.parseEther('2000'));
await usdc.connect(user.signer).approve(pool.address, MAX_UINT_AMOUNT);
await pool
.connect(user.signer)
.deposit(usdc.address, utils.parseEther('2000'), user.address, 0);
await dai.connect(user.signer)['mint(uint256)'](utils.parseEther('2000'));
await dai.connect(user.signer).transfer(aDai.address, utils.parseEther('2000'));

// Turn off automining - pretty sure that coverage is getting messed up here.
await setAutomine(false);

// Borrow 500 dai
await pool
.connect(user.signer)
.borrow(dai.address, utils.parseEther('500'), RateMode.Stable, 0, user.address);

// Turn on automining, but not mine a new block until next tx
await setAutomineEvm(true);

await expect(
pool
.connect(user.signer)
.repay(dai.address, utils.parseEther('500'), RateMode.Stable, user.address)
).to.be.revertedWith(SAME_BLOCK_BORROW_REPAY);
});

it('validateRepay() the variable debt when is 0 (stableDebt > 0) (revert expected)', async () => {
// (stableDebt > 0 && DataTypes.InterestRateMode(rateMode) == DataTypes.InterestRateMode.STABLE) ||
// (variableDebt > 0 && DataTypes.InterestRateMode(rateMode) == DataTypes.InterestRateMode.VARIABLE),
Expand Down