forked from compound-finance/comet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathis-liquidatable-test.ts
157 lines (133 loc) · 4.15 KB
/
is-liquidatable-test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import { expect, exp, makeProtocol } from './helpers';
/*
Prices are set in terms of the base token (USDC with 6 decimals, by default):
await comet.setBasePrincipal(alice.address, 1_000_000);
But the prices returned are denominated in terms of price scale (USD with 8
decimals, by default)
*/
describe('isLiquidatable', function () {
it('defaults to false', async () => {
const protocol = await makeProtocol();
const {
comet,
users: [alice],
} = protocol;
expect(await comet.isLiquidatable(alice.address)).to.be.false;
});
it('is false when user is owed principal', async () => {
const {
comet,
users: [alice],
} = await makeProtocol();
await comet.setBasePrincipal(alice.address, 1_000_000);
expect(await comet.isLiquidatable(alice.address)).to.be.false;
});
it('is true when user owes principal', async () => {
const {
comet,
users: [alice],
} = await makeProtocol();
await comet.setBasePrincipal(alice.address, -1_000_000);
expect(await comet.isLiquidatable(alice.address)).to.be.true;
});
it('is false when collateral can cover the borrowed principal', async () => {
const {
comet,
tokens,
users: [alice],
} = await makeProtocol({
assets: {
USDC: { decimals: 6 },
COMP: {
initial: 1e7,
decimals: 18,
initialPrice: 1, // 1 COMP = 1 USDC
},
},
});
const { COMP } = tokens;
// user owes $100,000
await comet.setBasePrincipal(alice.address, -100_000_000_000);
// but has $100,000 in COMP to cover
await comet.setCollateralBalance(alice.address, COMP.address, exp(100_000, 18));
expect(await comet.isLiquidatable(alice.address)).to.be.false;
});
it('is true when the collateral cannot cover the borrowed principal', async () => {
const {
comet,
tokens,
users: [alice],
} = await makeProtocol({
assets: {
USDC: { decimals: 6 },
COMP: {
initial: 1e7,
decimals: 18,
initialPrice: 1, // 1 COMP = 1 USDC
},
},
});
const { COMP } = tokens;
// user owes $100,000 is
await comet.setBasePrincipal(alice.address, -100_000_000_000);
// and only has $95,000 in COMP
await comet.setCollateralBalance(alice.address, COMP.address, exp(95_000, 18));
expect(await comet.isLiquidatable(alice.address)).to.be.true;
});
it('takes liquidateCollateralFactor into account when comparing principal to collateral', async () => {
const {
comet,
tokens,
users: [alice],
} = await makeProtocol({
assets: {
USDC: { decimals: 6 },
COMP: {
initial: 1e7,
decimals: 18,
initialPrice: 1, // 1 COMP = 1 USDC
borrowCF: exp(0.75, 18),
liquidateCF: exp(0.8, 18),
},
},
});
const { COMP } = tokens;
// user owes $100,000
await comet.setBasePrincipal(alice.address, -100_000_000_000);
// has $100,000 in COMP to cover, but at a .8 liquidateCollateralFactor
await comet.setCollateralBalance(alice.address, COMP.address, exp(100_000, 18));
expect(await comet.isLiquidatable(alice.address)).to.be.true;
});
it('changes when the underlying asset price changes', async () => {
const {
comet,
tokens,
users: [alice],
priceFeeds,
} = await makeProtocol({
assets: {
USDC: { decimals: 6 },
COMP: {
initial: 1e7,
decimals: 18,
initialPrice: 1, // 1 COMP = 1 USDC
},
},
});
const { COMP } = tokens;
// user owes $100,000
await comet.setBasePrincipal(alice.address, -100_000_000_000);
// has $100,000 in COMP to cover
await comet.setCollateralBalance(alice.address, COMP.address, exp(100_000, 18));
expect(await comet.isLiquidatable(alice.address)).to.be.false;
// price drops
await priceFeeds.COMP.setRoundData(
0, // roundId
exp(0.5, 8), // answer
0, // startedAt
0, // updatedAt
0 // answeredInRound
);
expect(await comet.isLiquidatable(alice.address)).to.be.true;
});
});