Added collateral test to flash liquidation

This commit is contained in:
David Racero 2020-09-09 15:43:02 +02:00
parent 2b930e4cd7
commit 398335124f
3 changed files with 46 additions and 27 deletions

View File

@ -174,7 +174,7 @@
}, },
"WalletBalanceProvider": { "WalletBalanceProvider": {
"buidlerevm": { "buidlerevm": {
"address": "0xBEF0d4b9c089a5883741fC14cbA352055f35DDA2", "address": "0xDf73fC454FA018051D4a1509e63D11530A59DE10",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6" "deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
}, },
"localhost": { "localhost": {
@ -414,7 +414,7 @@
}, },
"AaveProtocolTestHelpers": { "AaveProtocolTestHelpers": {
"buidlerevm": { "buidlerevm": {
"address": "0xDf73fC454FA018051D4a1509e63D11530A59DE10" "address": "0x2cfcA5785261fbC88EFFDd46fCFc04c22525F9e4"
}, },
"localhost": { "localhost": {
"address": "0x3b050AFb4ac4ACE646b31fF3639C1CD43aC31460" "address": "0x3b050AFb4ac4ACE646b31fF3639C1CD43aC31460"
@ -489,5 +489,10 @@
"address": "0x8733AfE8174BA7c04c6CD694bD673294079b7E10", "address": "0x8733AfE8174BA7c04c6CD694bD673294079b7E10",
"deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6" "deployer": "0xc783df8a850f42e7F7e57013759C285caa701eB6"
} }
},
"MockSwapAdapter": {
"buidlerevm": {
"address": "0xBEF0d4b9c089a5883741fC14cbA352055f35DDA2"
}
} }
} }

View File

@ -14,6 +14,7 @@
"test": "buidler test", "test": "buidler test",
"test-scenarios": "buidler test test/__setup.spec.ts test/scenario.spec.ts", "test-scenarios": "buidler test test/__setup.spec.ts test/scenario.spec.ts",
"test-repay-with-collateral": "buidler test test/__setup.spec.ts test/repay-with-collateral.spec.ts", "test-repay-with-collateral": "buidler test test/__setup.spec.ts test/repay-with-collateral.spec.ts",
"test-liquidate-with-collateral": "buidler test test/__setup.spec.ts test/flash-liquidation-with-collateral.spec.ts",
"dev:coverage": "buidler coverage", "dev:coverage": "buidler coverage",
"dev:deployment": "buidler dev-deployment", "dev:deployment": "buidler dev-deployment",
"dev:deployExample": "buidler deploy-Example", "dev:deployExample": "buidler deploy-Example",

View File

@ -11,6 +11,7 @@ import {waitForTx} from './__setup.spec';
import {timeLatest} from '../helpers/misc-utils'; import {timeLatest} from '../helpers/misc-utils';
import {tEthereumAddress, ProtocolErrors} from '../helpers/types'; import {tEthereumAddress, ProtocolErrors} from '../helpers/types';
import {convertToCurrencyDecimals} from '../helpers/contracts-helpers'; import {convertToCurrencyDecimals} from '../helpers/contracts-helpers';
import {formatUnits, formatEther} from 'ethers/lib/utils';
const {expect} = require('chai'); const {expect} = require('chai');
const {parseUnits, parseEther} = ethers.utils; const {parseUnits, parseEther} = ethers.utils;
@ -323,6 +324,7 @@ makeSuite('LendingPool. repayWithCollateral() with liquidator', (testEnv: TestEn
), ),
'INVALID_COLLATERAL_POSITION' 'INVALID_COLLATERAL_POSITION'
); );
expect(wethUserDataAfter.usageAsCollateralEnabled).to.be.true;
// Resets USDC Price // Resets USDC Price
await oracle.setAssetPrice(usdc.address, usdcPrice); await oracle.setAssetPrice(usdc.address, usdcPrice);
@ -651,6 +653,8 @@ makeSuite('LendingPool. repayWithCollateral() with liquidator', (testEnv: TestEn
expectedCollateralLiquidated.toString() expectedCollateralLiquidated.toString()
) )
); );
expect(wethUserDataAfter.usageAsCollateralEnabled).to.be.true;
// Resets DAI price // Resets DAI price
await oracle.setAssetPrice(dai.address, daiPrice); await oracle.setAssetPrice(dai.address, daiPrice);
}); });
@ -736,14 +740,14 @@ makeSuite('LendingPool. repayWithCollateral() with liquidator', (testEnv: TestEn
); );
}); });
it.skip('WIP Liquidator tries to repay 4 user a bigger amount that what can be swapped of a particular collateral, repaying only the maximum allowed by that collateral', async () => { it('Liquidator tries to repay 4 user a bigger amount that what can be swapped of a particular collateral, repaying only the maximum allowed by that collateral', async () => {
const {pool, weth, dai, users, mockSwapAdapter, oracle} = testEnv; const {pool, weth, dai, usdc, users, mockSwapAdapter, oracle} = testEnv;
const user = users[3]; const user = users[3];
const liquidator = users[5]; const liquidator = users[5];
const amountToDepositWeth = parseEther('0.1'); const amountToDepositWeth = parseEther('0.1');
const amountToDepositDAI = parseEther('500'); const amountToDepositDAI = parseEther('500');
const amountToBorrowVariable = parseEther('80'); const amountToBorrowVariable = parseUnits('80', '6');
await weth.connect(user.signer).mint(amountToDepositWeth); await weth.connect(user.signer).mint(amountToDepositWeth);
await dai.connect(user.signer).mint(amountToDepositDAI); await dai.connect(user.signer).mint(amountToDepositDAI);
@ -753,9 +757,9 @@ makeSuite('LendingPool. repayWithCollateral() with liquidator', (testEnv: TestEn
await pool.connect(user.signer).deposit(weth.address, amountToDepositWeth, '0'); await pool.connect(user.signer).deposit(weth.address, amountToDepositWeth, '0');
await pool.connect(user.signer).deposit(dai.address, amountToDepositDAI, '0'); await pool.connect(user.signer).deposit(dai.address, amountToDepositDAI, '0');
await pool.connect(user.signer).borrow(dai.address, amountToBorrowVariable, 2, 0); await pool.connect(user.signer).borrow(usdc.address, amountToBorrowVariable, 2, 0);
const amountToRepay = parseEther('80'); const amountToRepay = amountToBorrowVariable;
const {userData: wethUserDataBefore} = await getContractsData( const {userData: wethUserDataBefore} = await getContractsData(
weth.address, weth.address,
@ -763,26 +767,28 @@ makeSuite('LendingPool. repayWithCollateral() with liquidator', (testEnv: TestEn
testEnv testEnv
); );
const {reserveData: daiReserveDataBefore, userData: daiUserDataBefore} = await getContractsData( const {
dai.address, reserveData: usdcReserveDataBefore,
user.address, userData: usdcUserDataBefore,
testEnv } = await getContractsData(usdc.address, user.address, testEnv);
);
const wethPrice = await oracle.getAssetPrice(weth.address);
// Set HF below 1 // Set HF below 1
const daiPrice = await oracle.getAssetPrice(dai.address);
await oracle.setAssetPrice( await oracle.setAssetPrice(
weth.address, dai.address,
new BigNumber(wethPrice.toString()).multipliedBy(0.1).toFixed(0) new BigNumber(daiPrice.toString()).multipliedBy(0.1).toFixed(0)
); );
const userGlobalDataPrior = await pool.getUserAccountData(user.address); const userGlobalDataPrior = await pool.getUserAccountData(user.address);
expect(userGlobalDataPrior.healthFactor.toString()).to.be.bignumber.lt(oneEther, INVALID_HF); expect(userGlobalDataPrior.healthFactor.toString()).to.be.bignumber.lt(oneEther, INVALID_HF);
// Execute liquidation
await mockSwapAdapter.setAmountToReturn(amountToRepay); await mockSwapAdapter.setAmountToReturn(amountToRepay);
await waitForTx( await waitForTx(
await pool await pool
.connect(liquidator.signer) .connect(liquidator.signer)
.repayWithCollateral( .repayWithCollateral(
weth.address, weth.address,
dai.address, usdc.address,
user.address, user.address,
amountToRepay, amountToRepay,
mockSwapAdapter.address, mockSwapAdapter.address,
@ -797,16 +803,20 @@ makeSuite('LendingPool. repayWithCollateral() with liquidator', (testEnv: TestEn
testEnv testEnv
); );
const {userData: daiUserDataAfter} = await getContractsData(dai.address, user.address, testEnv); const {userData: usdcUserDataAfter} = await getContractsData(
usdc.address,
user.address,
testEnv
);
const collateralPrice = await oracle.getAssetPrice(weth.address); const collateralPrice = await oracle.getAssetPrice(weth.address);
const principalPrice = await oracle.getAssetPrice(dai.address); const principalPrice = await oracle.getAssetPrice(usdc.address);
const collateralConfig = await pool.getReserveConfigurationData(weth.address); const collateralConfig = await pool.getReserveConfigurationData(weth.address);
const collateralDecimals = collateralConfig.decimals.toString(); const collateralDecimals = collateralConfig.decimals.toString();
const principalDecimals = ( const principalDecimals = (
await pool.getReserveConfigurationData(dai.address) await pool.getReserveConfigurationData(usdc.address)
).decimals.toString(); ).decimals.toString();
const collateralLiquidationBonus = collateralConfig.liquidationBonus.toString(); const collateralLiquidationBonus = collateralConfig.liquidationBonus.toString();
@ -820,20 +830,23 @@ makeSuite('LendingPool. repayWithCollateral() with liquidator', (testEnv: TestEn
.decimalPlaces(0, BigNumber.ROUND_DOWN); .decimalPlaces(0, BigNumber.ROUND_DOWN);
const expectedVariableDebtIncrease = calcExpectedVariableDebtTokenBalance( const expectedVariableDebtIncrease = calcExpectedVariableDebtTokenBalance(
daiReserveDataBefore, usdcReserveDataBefore,
daiUserDataBefore, usdcUserDataBefore,
new BigNumber(repayWithCollateralTimestamp) new BigNumber(repayWithCollateralTimestamp)
).minus(daiUserDataBefore.currentVariableDebt); ).minus(usdcUserDataBefore.currentVariableDebt);
expect(daiUserDataAfter.currentVariableDebt).to.be.bignumber.equal( expect(usdcUserDataAfter.currentVariableDebt).to.be.bignumber.equal(
new BigNumber(daiUserDataBefore.currentVariableDebt) new BigNumber(usdcUserDataBefore.currentVariableDebt)
.minus(expectedDebtCovered.toString()) .minus(expectedDebtCovered.toString())
.plus(expectedVariableDebtIncrease), .plus(expectedVariableDebtIncrease),
'INVALID_VARIABLE_DEBT_POSITION' 'INVALID_VARIABLE_DEBT_POSITION'
); );
expect(wethUserDataAfter.usageAsCollateralEnabled).to.be.false;
expect(wethUserDataAfter.currentATokenBalance).to.be.bignumber.equal(0); expect(wethUserDataAfter.currentATokenBalance).to.be.bignumber.equal(0);
// Resets WETH Price
await oracle.setAssetPrice(weth.address, wethPrice); // Resets DAI Price
await oracle.setAssetPrice(dai.address, daiPrice);
}); });
}); });