forked from compound-finance/comet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuy-collateral-test.ts
223 lines (195 loc) · 8.42 KB
/
buy-collateral-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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import { Comet, ethers, expect, exp, makeProtocol, portfolio, wait } from './helpers';
describe('buyCollateral', function () {
it('allows buying collateral when reserves < target reserves', async () => {
const protocol = await makeProtocol({
base: 'USDC',
storeFrontPriceFactor: exp(0.9, 18),
targetReserves: 100,
assets: {
USDC: {
initial: 1e6,
decimals: 6,
initialPrice: 1,
},
COMP: {
initial: 1e7,
decimals: 18,
initialPrice: 1,
},
}
});
const { comet, tokens, users: [alice] } = protocol;
const { USDC, COMP } = tokens;
const cometAsA = comet.connect(alice);
const baseAsA = USDC.connect(alice);
// Reserves are at 0 wei
// Set up token balances and accounting
await USDC.allocateTo(alice.address, 100e6);
await COMP.allocateTo(comet.address, exp(60, 18));
await wait(comet.setTotalsCollateral(COMP.address, { totalSupplyAsset: exp(60, 18), _reserved: 0 }));
await wait(comet.setCollateralBalance(comet.address, COMP.address, exp(60, 18)));
const r0 = await comet.getReserves();
const p0 = await portfolio(protocol, alice.address);
await wait(baseAsA.approve(comet.address, exp(50, 6)));
// Alice buys 50e6 wei USDC worth of COMP
await wait(cometAsA.buyCollateral(COMP.address, exp(50, 18), 50e6, alice.address));
const p1 = await portfolio(protocol, alice.address)
const r1 = await comet.getReserves();
const assetPriceDiscounted = exp(0.9, 8);
const basePrice = exp(1, 8);
const assetScale = exp(1, 18);
const baseScale = exp(1, 6);
const baseAmount = 50n * baseScale;
expect(r0).to.be.equal(0n);
expect(r0).to.be.lt(await comet.targetReserves());
expect(p0.internal).to.be.deep.equal({USDC: 0n, COMP: 0n});
expect(p0.external).to.be.deep.equal({USDC: exp(100, 6), COMP: 0n});
expect(p1.internal).to.be.deep.equal({USDC: 0n, COMP: 0n});
expect(p1.external).to.be.deep.equal({USDC: exp(50, 6), COMP: assetScale * basePrice / assetPriceDiscounted * baseAmount / baseScale});
expect(p1.external).to.be.deep.equal({USDC: exp(50, 6), COMP: 55555555555555555550n});
expect(r1).to.be.equal(exp(50, 6));
});
it('allows buying collateral when reserves < 0 and target reserves is 0', async () => {
const protocol = await makeProtocol({
base: 'USDC',
storeFrontPriceFactor: exp(0.9, 18),
targetReserves: 0,
assets: {
USDC: {
initial: 1e6,
decimals: 6,
initialPrice: 1,
},
COMP: {
initial: 1e7,
decimals: 18,
initialPrice: 1,
},
}
});
const { comet, tokens, users: [alice] } = protocol;
const { USDC, COMP } = tokens;
const cometAsA = comet.connect(alice);
const baseAsA = USDC.connect(alice);
// Set reserves to -100 wei
let t0 = await comet.totalsBasic();
t0 = Object.assign({}, t0, {
totalSupplyBase: 100e6,
totalBorrowBase: 0n,
});
await wait(comet.setTotalsBasic(t0));
// Set up token balances and accounting
await USDC.allocateTo(alice.address, 100e6);
await COMP.allocateTo(comet.address, exp(60, 18));
await wait(comet.setTotalsCollateral(COMP.address, { totalSupplyAsset: exp(60, 18), _reserved: 0 }));
await wait(comet.setCollateralBalance(comet.address, COMP.address, exp(60, 18)));
const r0 = await comet.getReserves();
const p0 = await portfolio(protocol, alice.address);
await wait(baseAsA.approve(comet.address, exp(50, 6)));
// Alice buys 50e6 wei USDC worth of COMP
await wait(cometAsA.buyCollateral(COMP.address, exp(50, 18), 50e6, alice.address));
const p1 = await portfolio(protocol, alice.address)
const r1 = await comet.getReserves();
expect(r0).to.be.equal(-100e6);
expect(r0).to.be.lt(await comet.targetReserves());
expect(p0.internal).to.be.deep.equal({USDC: 0n, COMP: 0n});
expect(p0.external).to.be.deep.equal({USDC: exp(100, 6), COMP: 0n});
expect(p1.internal).to.be.deep.equal({USDC: 0n, COMP: 0n});
expect(p1.external).to.be.deep.equal({USDC: exp(50, 6), COMP: 55555555555555555550n});
expect(r1).to.be.equal(-50e6);
});
it('reverts if reserves are above target reserves', async () => {
const protocol = await makeProtocol({base: 'USDC', targetReserves: 0});
const { comet, tokens, users: [alice] } = protocol;
const { USDC, COMP } = tokens;
const cometAsA = comet.connect(alice);
const baseAsA = USDC.connect(alice);
// Set reserves to 100e6 wei
await USDC.allocateTo(comet.address, 100e6);
// Set up token balances and accounting
await USDC.allocateTo(alice.address, 100e6);
await COMP.allocateTo(comet.address, exp(50, 18));
await wait(comet.setTotalsCollateral(COMP.address, { totalSupplyAsset: exp(50, 18), _reserved: 0 }));
await wait(comet.setCollateralBalance(comet.address, COMP.address, exp(50, 18)));
const r0 = await comet.getReserves();
expect(r0).to.be.equal(100e6);
expect(r0).to.be.gt(await comet.targetReserves());
// Alice buys 50e18 wei COMP for 50e6 wei USDC
await wait(baseAsA.approve(comet.address, exp(50, 6)));
await expect(cometAsA.buyCollateral(COMP.address, exp(50, 18), 50e6, alice.address)).to.be.revertedWith("custom error 'NotForSale()'");
});
it('reverts if slippage is too high', async () => {
const protocol = await makeProtocol({base: 'USDC', targetReserves: 100,
assets: {
USDC: {
initial: 1e6,
decimals: 6,
initialPrice: 1,
},
COMP: {
initial: 1e7,
decimals: 18,
initialPrice: 1,
},
}
});
const { comet, tokens, users: [alice] } = protocol;
const { USDC, COMP } = tokens;
const cometAsA = comet.connect(alice);
const baseAsA = USDC.connect(alice);
// Reserves are at 0 wei
// Set up token balances and accounting
await USDC.allocateTo(alice.address, 100e6);
await COMP.allocateTo(comet.address, exp(50, 18));
await wait(comet.setTotalsCollateral(COMP.address, { totalSupplyAsset: exp(50, 18), _reserved: 0 }));
await wait(comet.setCollateralBalance(comet.address, COMP.address, exp(50, 18)));
// Alice tries to buy 100e18 wei COMP for 50e6 wei USDC
await wait(baseAsA.approve(comet.address, exp(50, 6)));
await expect(cometAsA.buyCollateral(COMP.address, exp(100, 18), 50e6, alice.address)).to.be.revertedWith("custom error 'TooMuchSlippage()'");
});
it('reverts if not enough collateral to buy', async () => {
const protocol = await makeProtocol({base: 'USDC', targetReserves: 100,
assets: {
USDC: {
initial: 1e6,
decimals: 6,
initialPrice: 1,
},
COMP: {
initial: 1e7,
decimals: 18,
initialPrice: 1,
},
}
});
const { comet, tokens, users: [alice] } = protocol;
const { USDC, COMP } = tokens;
const cometAsA = comet.connect(alice);
const baseAsA = USDC.connect(alice);
// Reserves are at 0 wei
// Set up token balances and accounting
await USDC.allocateTo(alice.address, 200e6);
await COMP.allocateTo(comet.address, exp(50, 18));
await wait(comet.setTotalsCollateral(COMP.address, { totalSupplyAsset: exp(50, 18), _reserved: 0 }));
await wait(comet.setCollateralBalance(comet.address, COMP.address, exp(50, 18)));
// Alice tries to buy 200e18 wei COMP for 200e6 wei USDC
await wait(baseAsA.approve(comet.address, exp(50, 6)));
await expect(cometAsA.buyCollateral(COMP.address, exp(200, 18), 200e6, alice.address)).to.be.revertedWith('reverted with panic code 0x11 (Arithmetic operation underflowed or overflowed outside of an unchecked block)');
});
it('reverts if buy is paused', async () => {
const protocol = await makeProtocol({base: 'USDC', targetReserves: 0});
const { comet, tokens, pauseGuardian, users: [alice] } = protocol;
const { COMP } = tokens;
const cometAsA = comet.connect(alice);
// Pause buy collateral
await wait(comet.connect(pauseGuardian).pause(false, false, false, false, true));
expect(await comet.isBuyPaused()).to.be.true;
await expect(cometAsA.buyCollateral(COMP.address, exp(50, 18), 50e6, alice.address)).to.be.revertedWith("custom error 'Paused()'");
});
it.skip('buys the correct amount in a fee-like situation', async () => {
// XXX
});
it.skip('is not broken by malicious re-entrancy', async () => {
// XXX
});
});