forked from compound-finance/comet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtracking-index-bounds-test.ts
265 lines (207 loc) · 10 KB
/
tracking-index-bounds-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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import { expect, exp, fastForward, makeProtocol, setTotalsBasic, toYears } from './helpers';
import { BigNumber } from 'ethers';
describe('total tracking index bounds', function () {
describe('base scale of 6', function () {
it('upper bound hit on tracking supply index', async () => {
const baseMinForRewards = exp(10_000, 6); // 10k USDC
const params = {
trackingIndexScale: exp(1, 15),
baseTrackingSupplySpeed: exp(1, 15),
baseTrackingBorrowSpeed: exp(1, 15),
baseMinForRewards
};
const protocol = await makeProtocol(params);
const { comet } = protocol;
const baseScale = (await comet.baseScale()).toBigInt();
// Formula: MAX_UINT64 / (baseTrackingSupplySpeed * baseScale / baseMinForRewards)
const secondsUntilOverflow = Number(2n**64n * (baseMinForRewards / baseScale) / params.baseTrackingSupplySpeed);
// Assert there are at least 5.85 years until tracking index can overflow
const expectedYearsUntilOverflow = 5.85;
expect(toYears(secondsUntilOverflow)).to.be.approximately(expectedYearsUntilOverflow, 0.01);
await setTotalsBasic(comet, {
totalSupplyBase: BigNumber.from(baseMinForRewards), // 10k USDC base units
});
await fastForward(secondsUntilOverflow-1);
// First accrue is successful without overflow
await comet.accrue();
// Second accrue should overflow
await expect(comet.accrue()).to.be.revertedWith('code 0x11 (Arithmetic operation underflowed or overflowed outside of an unchecked block)');
});
it('upper bound hit on tracking borrow index', async () => {
const baseMinForRewards = exp(10_000, 6); // 10k USDC
const params = {
trackingIndexScale: exp(1, 15),
baseTrackingSupplySpeed: exp(1, 15),
baseTrackingBorrowSpeed: exp(1, 15),
baseMinForRewards
};
const protocol = await makeProtocol(params);
const { comet } = protocol;
const baseScale = (await comet.baseScale()).toBigInt();
// Formula: MAX_UINT64 / (baseTrackingBorrowSpeed * baseScale / baseMinForRewards)
const secondsUntilOverflow = Number(2n**64n * (baseMinForRewards / baseScale) / params.baseTrackingBorrowSpeed);
// Assert there are at least 5.85 years until tracking index can overflow
const expectedYearsUntilOverflow = 5.85;
expect(toYears(secondsUntilOverflow)).to.be.approximately(expectedYearsUntilOverflow, 0.01);
await setTotalsBasic(comet, {
totalBorrowBase: BigNumber.from(baseMinForRewards), // 10k USDC base units
});
await fastForward(secondsUntilOverflow-1);
// First accrue is successful without overflow
await comet.accrue();
// Second accrue should overflow
await expect(comet.accrue()).to.be.revertedWith('code 0x11 (Arithmetic operation underflowed or overflowed outside of an unchecked block)');
});
it('lower bound hit on tracking supply index', async () => {
const params = {
trackingIndexScale: exp(1, 15),
baseTrackingSupplySpeed: exp(1, 15),
baseTrackingBorrowSpeed: exp(1, 15),
};
const protocol = await makeProtocol(params);
const { comet } = protocol;
const t0 = await setTotalsBasic(comet, {
totalSupplyBase: BigNumber.from(exp(1, 15)).mul(await comet.baseScale()), // 1e15 base units
});
await comet.accrue();
const t1 = await comet.totalsBasic();
// Tracking index should properly accrue
expect(t1.trackingSupplyIndex).to.not.be.equal(t0.trackingSupplyIndex);
const t2 = await setTotalsBasic(comet, {
totalSupplyBase: BigNumber.from(exp(1, 15)).mul(await comet.baseScale()).mul(3), // 3e15 base units
});
await comet.accrue();
const t3 = await comet.totalsBasic();
// Lower bound has hit and tracking index no longer accrues
expect(t3.trackingSupplyIndex).to.be.equal(t2.trackingSupplyIndex);
});
it('lower bound hit on tracking borrow index', async () => {
const params = {
trackingIndexScale: exp(1, 15),
baseTrackingSupplySpeed: exp(1, 15),
baseTrackingBorrowSpeed: exp(1, 15),
};
const protocol = await makeProtocol(params);
const { comet } = protocol;
const t0 = await setTotalsBasic(comet, {
totalBorrowBase: BigNumber.from(exp(1, 15)).mul(await comet.baseScale()), // 1e15 base units
});
await comet.accrue();
const t1 = await comet.totalsBasic();
// Tracking index should properly accrue
expect(t1.trackingBorrowIndex).to.not.be.equal(t0.trackingBorrowIndex);
const t2 = await setTotalsBasic(comet, {
totalBorrowBase: BigNumber.from(exp(1, 15)).mul(await comet.baseScale()).mul(3), // 3e15 base units
});
await comet.accrue();
const t3 = await comet.totalsBasic();
// Lower bound has hit and tracking index no longer accrues
expect(t3.trackingBorrowIndex).to.be.equal(t2.trackingBorrowIndex);
});
});
describe('base scale of 18', function () {
it('upper bound hit on tracking supply index', async () => {
const baseMinForRewards = exp(100, 18); // 100 WETH
const params = {
base: 'WETH',
trackingIndexScale: exp(1, 15),
baseTrackingSupplySpeed: exp(0.001, 15), // 86.4 units/day
baseTrackingBorrowSpeed: exp(0.001, 15),
baseMinForRewards
};
const protocol = await makeProtocol(params);
const { comet } = protocol;
const baseScale = (await comet.baseScale()).toBigInt();
// Formula: MAX_UINT64 / (baseTrackingSupplySpeed * baseScale / baseMinForRewards)
const secondsUntilOverflow = Number(2n**64n * (baseMinForRewards / baseScale) / params.baseTrackingSupplySpeed);
// Assert there are at least 58.5 years until tracking index can overflow
const expectedYearsUntilOverflow = 58.5;
expect(toYears(secondsUntilOverflow)).to.be.approximately(expectedYearsUntilOverflow, 0.01);
await setTotalsBasic(comet, {
totalSupplyBase: BigNumber.from(baseMinForRewards), // 100 WETH base units
});
await fastForward(secondsUntilOverflow-1);
// First accrue is successful without overflow
await comet.accrue();
// Second accrue should overflow
await expect(comet.accrue()).to.be.revertedWith('code 0x11 (Arithmetic operation underflowed or overflowed outside of an unchecked block)');
});
it('upper bound hit on tracking borrow index', async () => {
const baseMinForRewards = exp(100, 18); // 100 WETH
const params = {
base: 'WETH',
trackingIndexScale: exp(1, 15),
baseTrackingSupplySpeed: exp(0.001, 15), // 86.4 units/day
baseTrackingBorrowSpeed: exp(0.001, 15),
baseMinForRewards
};
const protocol = await makeProtocol(params);
const { comet } = protocol;
const baseScale = (await comet.baseScale()).toBigInt();
// Formula: MAX_UINT64 / (baseTrackingBorrowSpeed * baseScale / baseMinForRewards)
const secondsUntilOverflow = Number(2n**64n * (baseMinForRewards / baseScale) / params.baseTrackingBorrowSpeed);
// Assert there are at least 58.5 years until tracking index can overflow
const expectedYearsUntilOverflow = 58.5;
expect(toYears(secondsUntilOverflow)).to.be.approximately(expectedYearsUntilOverflow, 0.01);
await setTotalsBasic(comet, {
totalBorrowBase: BigNumber.from(baseMinForRewards), // 10k USDC base units
});
await fastForward(secondsUntilOverflow-1);
// First accrue is successful without overflow
await comet.accrue();
// Second accrue should overflow
await expect(comet.accrue()).to.be.revertedWith('code 0x11 (Arithmetic operation underflowed or overflowed outside of an unchecked block)');
});
it('lower bound hit on tracking supply index', async () => {
const params = {
base: 'WETH',
trackingIndexScale: exp(1, 15),
baseTrackingSupplySpeed: exp(0.001, 15), // 86.4 units/day
baseTrackingBorrowSpeed: exp(0.001, 15),
};
const protocol = await makeProtocol(params);
const { comet } = protocol;
const t0 = await setTotalsBasic(comet, {
totalSupplyBase: BigNumber.from(exp(1, 12)).mul(await comet.baseScale()), // 1e12 base units
});
await comet.accrue();
const t1 = await comet.totalsBasic();
// Tracking index should properly accrue
expect(t1.trackingSupplyIndex).to.not.be.equal(t0.trackingSupplyIndex);
const t2 = await setTotalsBasic(comet, {
totalSupplyBase: BigNumber.from(exp(1, 13)).mul(await comet.baseScale()), // 1e13 base units
});
await comet.accrue();
const t3 = await comet.totalsBasic();
// Lower bound has hit and tracking index no longer accrues
expect(t3.trackingSupplyIndex).to.be.equal(t2.trackingSupplyIndex);
});
it('lower bound hit on tracking borrow index', async () => {
const params = {
base: 'WETH',
trackingIndexScale: exp(1, 15),
baseTrackingSupplySpeed: exp(0.001, 15), // 86.4 units/day
baseTrackingBorrowSpeed: exp(0.001, 15),
};
const protocol = await makeProtocol(params);
const { comet } = protocol;
const t0 = await setTotalsBasic(comet, {
totalBorrowBase: BigNumber.from(exp(1, 12)).mul(await comet.baseScale()), // 1e12 base units
});
await comet.accrue();
const t1 = await comet.totalsBasic();
// Tracking index should properly accrue
expect(t1.trackingBorrowIndex).to.not.be.equal(t0.trackingBorrowIndex);
const t2 = await setTotalsBasic(comet, {
totalBorrowBase: BigNumber.from(exp(1, 13)).mul(await comet.baseScale()), // 1e13 base units
});
await comet.accrue();
const t3 = await comet.totalsBasic();
// Lower bound has hit and tracking index no longer accrues
expect(t3.trackingBorrowIndex).to.be.equal(t2.trackingBorrowIndex);
});
});
});
describe('user tracking index bounds', function () {
// XXX test if small supply/borrow causes users to not accrue rewards
});