Added pausable tests

This commit is contained in:
David Racero 2020-09-14 13:57:40 +02:00
parent 4a6517dce1
commit ad2581b0a0
6 changed files with 211 additions and 2 deletions

View File

@ -99,6 +99,8 @@ export enum ProtocolErrors {
NO_ERRORS = '42', // 'No errors'
INVALID_FLASHLOAN_MODE = '43', //Invalid flashloan mode
IS_PAUSED = 'Pausable: paused',
// old
INVALID_FROM_BALANCE_AFTER_TRANSFER = 'Invalid from balance after transfer',

View File

@ -17,6 +17,7 @@
"test-liquidate-with-collateral": "buidler test test/__setup.spec.ts test/flash-liquidation-with-collateral.spec.ts",
"test-transfers": "buidler test test/__setup.spec.ts test/atoken-transfer.spec.ts",
"test-flash": "buidler test test/__setup.spec.ts test/flashloan.spec.ts",
"test-liquidate": "buidler test test/__setup.spec.ts test/liquidation-atoken.spec.ts",
"dev:coverage": "buidler coverage --network coverage",
"dev:deployment": "buidler dev-deployment",
"dev:deployExample": "buidler deploy-Example",

View File

@ -17,7 +17,7 @@ const {expect} = require('chai');
const {parseUnits, parseEther} = ethers.utils;
makeSuite('LendingPool. repayWithCollateral() with liquidator', (testEnv: TestEnv) => {
const {INVALID_HF, COLLATERAL_CANNOT_BE_LIQUIDATED} = ProtocolErrors;
const {INVALID_HF, COLLATERAL_CANNOT_BE_LIQUIDATED, IS_PAUSED} = ProtocolErrors;
it('User 1 provides some liquidity for others to borrow', async () => {
const {pool, weth, dai, usdc, deployer} = testEnv;
@ -913,4 +913,57 @@ makeSuite('LendingPool. repayWithCollateral() with liquidator', (testEnv: TestEn
expect(wethUserDataAfter.usageAsCollateralEnabled).to.be.false;
});
it('Liquidator tries to repay 7 user a bigger amount that what can be swapped of a particular collateral, but reverts due pool is paused', async () => {
const {pool, weth, dai, usdc, users, mockSwapAdapter, oracle, configurator} = testEnv;
const user = users[6];
const liquidator = users[5];
const amountToDepositWeth = parseEther('0.1');
const amountToDepositDAI = parseEther('500');
const amountToBorrowVariable = parseUnits('80', '6');
await weth.connect(user.signer).mint(amountToDepositWeth);
await dai.connect(user.signer).mint(amountToDepositDAI);
await weth.connect(user.signer).approve(pool.address, APPROVAL_AMOUNT_LENDING_POOL);
await dai.connect(user.signer).approve(pool.address, APPROVAL_AMOUNT_LENDING_POOL);
await pool.connect(user.signer).deposit(weth.address, amountToDepositWeth, user.address, '0');
await pool.connect(user.signer).deposit(dai.address, amountToDepositDAI, user.address, '0');
await pool.connect(user.signer).borrow(usdc.address, amountToBorrowVariable, 2, 0);
const amountToRepay = amountToBorrowVariable;
// Set HF below 1
const daiPrice = await oracle.getAssetPrice(dai.address);
await oracle.setAssetPrice(
dai.address,
new BigNumber(daiPrice.toString()).multipliedBy(0.1).toFixed(0)
);
const userGlobalDataPrior = await pool.getUserAccountData(user.address);
expect(userGlobalDataPrior.healthFactor.toString()).to.be.bignumber.lt(oneEther, INVALID_HF);
await mockSwapAdapter.setAmountToReturn(amountToRepay);
// Pause the pool
await configurator.pausePool();
// Try to execute liquidation
await expect(
pool
.connect(liquidator.signer)
.repayWithCollateral(
weth.address,
usdc.address,
user.address,
amountToRepay,
mockSwapAdapter.address,
'0x'
)
).revertedWith(IS_PAUSED);
// Unpause the pool
await configurator.unpausePool();
});
});

View File

@ -22,6 +22,7 @@ makeSuite('LendingPool FlashLoan function', (testEnv: TestEnv) => {
TRANSFER_AMOUNT_EXCEEDS_BALANCE,
INVALID_FLASHLOAN_MODE,
SAFEERC20_LOWLEVEL_CALL,
IS_PAUSED,
} = ProtocolErrors;
before(async () => {
@ -355,4 +356,26 @@ makeSuite('LendingPool FlashLoan function', (testEnv: TestEnv) => {
expect(callerDebt.toString()).to.be.equal('800720000000000000', 'Invalid user debt');
});
it('Caller tries to take a WETH flash loan but pool is paused', async () => {
const {dai, pool, weth, users, configurator} = testEnv;
const caller = users[3];
const flashAmount = ethers.utils.parseEther('0.8');
await _mockFlashLoanReceiver.setFailExecutionTransfer(true);
// Pause pool
await configurator.pausePool();
await expect(
pool
.connect(caller.signer)
.flashLoan(_mockFlashLoanReceiver.address, weth.address, flashAmount, 1, '0x10', '0')
).revertedWith(IS_PAUSED);
// Unpause pool
await configurator.unpausePool();
});
});

View File

@ -17,6 +17,7 @@ makeSuite('LendingPool liquidation - liquidator receiving aToken', (testEnv) =>
INVALID_HF,
SPECIFIED_CURRENCY_NOT_BORROWED_BY_USER,
COLLATERAL_CANNOT_BE_LIQUIDATED,
IS_PAUSED,
} = ProtocolErrors;
it('LIQUIDATION - Deposits WETH, borrows DAI/Check liquidation fails because health factor is above 1', async () => {
@ -353,4 +354,84 @@ makeSuite('LendingPool liquidation - liquidator receiving aToken', (testEnv) =>
'Invalid collateral available liquidity'
);
});
it('User 4 borrows - drops HF, liquidates the borrower but reverts due paused pool', async () => {
const {users, pool, usdc, oracle, weth, configurator} = testEnv;
const depositor = users[3];
const borrower = users[4];
//mints USDC to depositor
await usdc
.connect(depositor.signer)
.mint(await convertToCurrencyDecimals(usdc.address, '1000'));
//approve protocol to access depositor wallet
await usdc.connect(depositor.signer).approve(pool.address, APPROVAL_AMOUNT_LENDING_POOL);
//user 3 deposits 1000 USDC
const amountUSDCtoDeposit = await convertToCurrencyDecimals(usdc.address, '1000');
await pool
.connect(depositor.signer)
.deposit(usdc.address, amountUSDCtoDeposit, depositor.address, '0');
//user 4 deposits 1 ETH
const amountETHtoDeposit = await convertToCurrencyDecimals(weth.address, '1');
//mints WETH to borrower
await weth.connect(borrower.signer).mint(amountETHtoDeposit);
//approve protocol to access borrower wallet
await weth.connect(borrower.signer).approve(pool.address, APPROVAL_AMOUNT_LENDING_POOL);
await pool
.connect(borrower.signer)
.deposit(weth.address, amountETHtoDeposit, borrower.address, '0');
//user 4 borrows
const userGlobalData = await pool.getUserAccountData(borrower.address);
const usdcPrice = await oracle.getAssetPrice(usdc.address);
const amountUSDCToBorrow = await convertToCurrencyDecimals(
usdc.address,
new BigNumber(userGlobalData.availableBorrowsETH.toString())
.div(usdcPrice.toString())
.multipliedBy(0.9502)
.toFixed(0)
);
await pool
.connect(borrower.signer)
.borrow(usdc.address, amountUSDCToBorrow, RateMode.Stable, '0');
//drops HF below 1
await oracle.setAssetPrice(
usdc.address,
new BigNumber(usdcPrice.toString()).multipliedBy(1.2).toFixed(0)
);
//mints dai to the liquidator
await usdc.mint(await convertToCurrencyDecimals(usdc.address, '1000'));
//approve protocol to access depositor wallet
await usdc.approve(pool.address, APPROVAL_AMOUNT_LENDING_POOL);
const userReserveDataBefore = await pool.getUserReserveData(usdc.address, borrower.address);
const amountToLiquidate = new BigNumber(userReserveDataBefore.currentStableDebt.toString())
.multipliedBy(0.5)
.toFixed(0);
// Pause pool
await configurator.pausePool();
expect(
pool.liquidationCall(weth.address, usdc.address, borrower.address, amountToLiquidate, true)
).revertedWith(IS_PAUSED);
// Unpause pool
await configurator.unpausePool();
});
});

View File

@ -9,7 +9,7 @@ import {
import {getContractsData} from './helpers/actions';
import {waitForTx} from './__setup.spec';
import {timeLatest} from '../helpers/misc-utils';
import {tEthereumAddress} from '../helpers/types';
import {ProtocolErrors, tEthereumAddress} from '../helpers/types';
import {parse} from 'path';
const {expect} = require('chai');
@ -39,6 +39,8 @@ export const expectRepayWithCollateralEvent = (
};
makeSuite('LendingPool. repayWithCollateral()', (testEnv: TestEnv) => {
const {IS_PAUSED} = ProtocolErrors;
it('User 1 provides some liquidity for others to borrow', async () => {
const {pool, weth, dai, usdc, deployer} = testEnv;
@ -637,4 +639,51 @@ makeSuite('LendingPool. repayWithCollateral()', (testEnv: TestEnv) => {
expect(wethUserDataAfter.usageAsCollateralEnabled).to.be.false;
});
it('User 6 deposits WETH and DAI, then borrows USDC at Variable', async () => {
const {pool, weth, dai, usdc, users} = testEnv;
const user = users[4];
const amountWETHToDeposit = parseEther('10');
const amountDAIToDeposit = parseEther('120');
const amountToBorrow = parseUnits('65', 6);
await weth.connect(user.signer).mint(amountWETHToDeposit);
await weth.connect(user.signer).approve(pool.address, APPROVAL_AMOUNT_LENDING_POOL);
await pool.connect(user.signer).deposit(weth.address, amountWETHToDeposit, user.address, '0');
await dai.connect(user.signer).mint(amountDAIToDeposit);
await dai.connect(user.signer).approve(pool.address, APPROVAL_AMOUNT_LENDING_POOL);
await pool.connect(user.signer).deposit(dai.address, amountDAIToDeposit, user.address, '0');
await pool.connect(user.signer).borrow(usdc.address, amountToBorrow, 2, 0);
});
it('User 6 tries to repay his USDC loan by swapping his WETH collateral, should not revert even with WETH collateral disabled', async () => {
const {pool, weth, usdc, users, mockSwapAdapter, oracle, configurator} = testEnv;
const user = users[5];
const amountToRepay = parseUnits('65', 6);
await mockSwapAdapter.setAmountToReturn(amountToRepay);
// Pause pool
await configurator.pausePool();
// Try to repay
await expect(
pool
.connect(user.signer)
.repayWithCollateral(
weth.address,
usdc.address,
user.address,
amountToRepay,
mockSwapAdapter.address,
'0x'
)
).revertedWith(IS_PAUSED);
// Unpause pool
await configurator.unpausePool();
});
});