Skip to content
This repository was archived by the owner on Oct 21, 2025. It is now read-only.

Commit 7d8b7bf

Browse files
committed
fix: Fix test of inaccuracy when liquidationProtocolFee is on
1 parent 12263d2 commit 7d8b7bf

File tree

1 file changed

+54
-42
lines changed

1 file changed

+54
-42
lines changed

test-suites/liquidation-with-fee.spec.ts

Lines changed: 54 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,47 @@
1-
import { expect } from 'chai';
2-
import { BigNumber, utils } from 'ethers';
3-
import { MAX_UINT_AMOUNT, oneEther } from '../helpers/constants';
4-
import { convertToCurrencyDecimals } from '../helpers/contracts-helpers';
5-
import { ProtocolErrors, RateMode } from '../helpers/types';
6-
import { AToken__factory } from '../types';
7-
import { calcExpectedStableDebtTokenBalance } from './helpers/utils/calculations';
8-
import { getReserveData, getUserData } from './helpers/utils/helpers';
9-
import { makeSuite } from './helpers/make-suite';
10-
import { HardhatRuntimeEnvironment } from 'hardhat/types';
11-
import { waitForTx, increaseTime, evmSnapshot, evmRevert } from '@aave/deploy-v3';
1+
import {expect} from 'chai';
2+
import {BigNumber} from 'ethers';
3+
import {MAX_UINT_AMOUNT, oneEther} from '../helpers/constants';
4+
import {convertToCurrencyDecimals} from '../helpers/contracts-helpers';
5+
import {ProtocolErrors, RateMode} from '../helpers/types';
6+
import {AToken__factory} from '../types';
7+
import {calcExpectedStableDebtTokenBalance} from './helpers/utils/calculations';
8+
import {getReserveData, getUserData} from './helpers/utils/helpers';
9+
import {makeSuite} from './helpers/make-suite';
10+
import {HardhatRuntimeEnvironment} from 'hardhat/types';
11+
import {waitForTx, increaseTime, evmSnapshot, evmRevert} from '@aave/deploy-v3';
1212

1313
declare var hre: HardhatRuntimeEnvironment;
1414

1515
makeSuite('Pool Liquidation: Add fee to liquidations', (testEnv) => {
16-
const { INVALID_HF } = ProtocolErrors;
16+
const {INVALID_HF} = ProtocolErrors;
1717

1818
before(async () => {
19-
const { addressesProvider, oracle } = testEnv;
19+
const {addressesProvider, oracle} = testEnv;
2020

2121
await waitForTx(await addressesProvider.setPriceOracle(oracle.address));
2222
});
2323

2424
after(async () => {
25-
const { aaveOracle, addressesProvider } = testEnv;
25+
const {aaveOracle, addressesProvider} = testEnv;
2626
await waitForTx(await addressesProvider.setPriceOracle(aaveOracle.address));
2727
});
2828

2929
it('position should be liquidated when turn on liquidation protocol fee.', async () => {
30-
const { pool, users, usdc, weth, oracle, configurator, helpersContract } = testEnv;
30+
const {
31+
pool,
32+
users: [depositor, borrower, liquidator],
33+
usdc,
34+
weth,
35+
oracle,
36+
configurator,
37+
helpersContract,
38+
} = testEnv;
3139

32-
const depositor = users[0];
33-
const borrower = users[1];
34-
const liquidator = users[2];
40+
const snapId = await evmSnapshot();
3541

36-
//1, prepare asset price.
37-
await oracle.setAssetPrice(usdc.address, '1000000000000000'); //weth = 1000 usdc
42+
const daiPrice = await oracle.getAssetPrice(usdc.address);
3843

39-
//2, depositor deposit 100000 usdc and 10 eth
44+
//1. Depositor supplies 10000 USDC and 10 ETH
4045
await usdc
4146
.connect(depositor.signer)
4247
['mint(uint256)'](await convertToCurrencyDecimals(usdc.address, '10000'));
@@ -52,32 +57,38 @@ makeSuite('Pool Liquidation: Add fee to liquidations', (testEnv) => {
5257

5358
await weth
5459
.connect(depositor.signer)
55-
['mint(uint256)'](convertToCurrencyDecimals(weth.address, '10'));
60+
['mint(uint256)'](await convertToCurrencyDecimals(weth.address, '10'));
5661
await weth.connect(depositor.signer).approve(pool.address, MAX_UINT_AMOUNT);
5762
await pool
5863
.connect(depositor.signer)
59-
.supply(weth.address, convertToCurrencyDecimals(weth.address, '10'), depositor.address, 0);
64+
.supply(
65+
weth.address,
66+
await convertToCurrencyDecimals(weth.address, '10'),
67+
depositor.address,
68+
0
69+
);
6070

61-
//3, borrower deposit 10 eth, borrow 5000 usdc
71+
//2. Borrower supplies 10 ETH, and borrows as much USDC as it can
6272
await weth
6373
.connect(borrower.signer)
64-
['mint(uint256)'](convertToCurrencyDecimals(weth.address, '10'));
74+
['mint(uint256)'](await convertToCurrencyDecimals(weth.address, '10'));
6575
await weth.connect(borrower.signer).approve(pool.address, MAX_UINT_AMOUNT);
6676
await pool
6777
.connect(borrower.signer)
68-
.supply(weth.address, convertToCurrencyDecimals(weth.address, '10'), borrower.address, 0);
78+
.supply(
79+
weth.address,
80+
await convertToCurrencyDecimals(weth.address, '10'),
81+
borrower.address,
82+
0
83+
);
6984

85+
const {availableBorrowsBase} = await pool.getUserAccountData(borrower.address);
86+
let toBorrow = availableBorrowsBase.div(daiPrice);
7087
await pool
7188
.connect(borrower.signer)
72-
.borrow(
73-
usdc.address,
74-
await convertToCurrencyDecimals(usdc.address, '5000'),
75-
RateMode.Variable,
76-
0,
77-
borrower.address
78-
);
89+
.borrow(usdc.address, toBorrow, RateMode.Variable, 0, borrower.address);
7990

80-
//4, liquidator deposit 10000 usdc and borrow 5 eth.
91+
//3. Liquidator supplies 10000 USDC and borrow 5 ETH
8192
await usdc
8293
.connect(liquidator.signer)
8394
['mint(uint256)'](await convertToCurrencyDecimals(usdc.address, '20000'));
@@ -95,19 +106,19 @@ makeSuite('Pool Liquidation: Add fee to liquidations', (testEnv) => {
95106
.connect(liquidator.signer)
96107
.borrow(
97108
weth.address,
98-
convertToCurrencyDecimals(weth.address, '5'),
109+
await convertToCurrencyDecimals(weth.address, '1'),
99110
RateMode.Variable,
100111
0,
101112
liquidator.address
102113
);
103114

104-
//5, advance block to make ETH income index > 1
115+
//4. Advance block to make ETH income index > 1
105116
await increaseTime(86400);
106117

107-
//6, decrease weth price to allow liquidation
108-
await oracle.setAssetPrice(usdc.address, '20000000000000000'); //weth = 500 usdc
118+
//5. Decrease weth price to allow liquidation
119+
await oracle.setAssetPrice(usdc.address, '8000000000000000'); //weth = 500 usdc
109120

110-
//7, turn on liquidation protocol fee
121+
//7. Turn on liquidation protocol fee
111122
expect(await configurator.setLiquidationProtocolFee(weth.address, 500));
112123
const wethLiquidationProtocolFee = await helpersContract.getLiquidationProtocolFee(
113124
weth.address
@@ -128,14 +139,15 @@ makeSuite('Pool Liquidation: Add fee to liquidations', (testEnv) => {
128139
await evmRevert(tmpSnap);
129140
}
130141
}
131-
132142
expect(await weth.balanceOf(liquidator.address)).to.be.gt(
133-
convertToCurrencyDecimals(weth.address, '5')
143+
await convertToCurrencyDecimals(weth.address, '5')
134144
);
145+
146+
await evmRevert(snapId);
135147
});
136148

137149
it('Sets the WETH protocol liquidation fee to 1000 (10.00%)', async () => {
138-
const { configurator, weth, aave, helpersContract } = testEnv;
150+
const {configurator, weth, aave, helpersContract} = testEnv;
139151

140152
const oldWethLiquidationProtocolFee = await helpersContract.getLiquidationProtocolFee(
141153
weth.address

0 commit comments

Comments
 (0)