forked from compound-finance/comet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathis-borrow-collateralized-test.ts
118 lines (100 loc) · 3.3 KB
/
is-borrow-collateralized-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
import { expect, exp, makeProtocol } from './helpers';
describe('isBorrowCollateralized', function () {
it('defaults to true', async () => {
const protocol = await makeProtocol({ base: 'USDC' });
const {
comet,
users: [alice],
} = protocol;
expect(await comet.isBorrowCollateralized(alice.address)).to.be.true;
});
it('is true when user is owed principal', async () => {
const {
comet,
users: [alice],
} = await makeProtocol({ base: 'USDC' });
await comet.setBasePrincipal(alice.address, 1_000_000);
expect(await comet.isBorrowCollateralized(alice.address)).to.be.true;
});
it('is false when user owes principal', async () => {
const {
comet,
users: [alice],
} = await makeProtocol({ base: 'USDC' });
await comet.setBasePrincipal(alice.address, -1_000_000);
expect(await comet.isBorrowCollateralized(alice.address)).to.be.false;
});
it('is true when value of collateral is greater than principal owed', 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.9, 18),
},
},
});
const { COMP } = tokens;
// user owes 1 USDC, but has 1.2 COMP collateral
await comet.setBasePrincipal(alice.address, -exp(1, 6));
await comet.setCollateralBalance(alice.address, COMP.address, exp(1.2, 18));
expect(await comet.isBorrowCollateralized(alice.address)).to.be.true;
});
it('takes borrow collateral factor into account when valuing 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.9, 18),
},
},
});
const { COMP } = tokens;
// user owes 1 USDC
await comet.setBasePrincipal(alice.address, -1_000_000);
// user has 1 COMP collateral, but the borrow collateral factor puts it
// below the required collateral amount
await comet.setCollateralBalance(alice.address, COMP.address, exp(1, 18));
expect(await comet.isBorrowCollateralized(alice.address)).to.be.false;
});
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, borrowCF: exp(0.2, 18) },
},
});
const { COMP } = tokens;
// user owes 1 USDC
await comet.setBasePrincipal(alice.address, -exp(1, 6));
// ...but has 5 COMP to cover their position
await comet.setCollateralBalance(alice.address, COMP.address, exp(5, 18));
expect(await comet.isBorrowCollateralized(alice.address)).to.be.true;
await priceFeeds.COMP.setRoundData(
0, // roundId
exp(0.5, 8), // answer
0, // startedAt
0, // updatedAt
0 // answeredInRound
);
expect(await comet.isBorrowCollateralized(alice.address)).to.be.false;
});
});