-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathHub.Math.test.js
333 lines (296 loc) · 10 KB
/
Hub.Math.test.js
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
const {
BigNumber,
period,
symbol,
name,
signupBonus,
maxGas,
timeout,
} = require('./helpers/constants');
const { bn, convertToBaseUnit, inflate } = require('./helpers/math');
const { assertRevert } = require('./helpers/assertRevert');
const { increase } = require('./helpers/increaseTime');
const Hub = artifacts.require('MockHub');
require('chai')
.use(require('chai-bn')(BigNumber))
.should();
contract('Hub - math utils', ([_, owner, recipient, attacker, systemOwner]) => { // eslint-disable-line no-unused-vars
let hub = null;
let inflation = bn(275);
const divisor = bn(100);
const signupBonusConverted = convertToBaseUnit(signupBonus);
beforeEach(async () => {
hub = await Hub
.new(
inflation,
period,
symbol,
name,
signupBonusConverted,
signupBonusConverted,
timeout,
{ from: systemOwner, gas: maxGas },
);
});
describe('power', () => {
it('returns the result of base^exponent', async () => {
(await hub.pow(2, 4)).should.be.bignumber.equal(bn(16));
});
it('returns the result of base^exponent for a very high number', async () => {
(await hub.pow(15833, 12)).should.be.bignumber.equal(bn('248175291811094805747824732449565240388888669248161'));
});
it('returns the result of base^exponent for base=1', async () => {
(await hub.pow(1, 12)).should.be.bignumber.equal(bn('1'));
});
it('returns the result of base^exponent for base=0', async () => {
(await hub.pow(0, 12)).should.be.bignumber.equal(bn('0'));
});
it('returns the result of base^exponent for exponent=1', async () => {
(await hub.pow(12, 1)).should.be.bignumber.equal(bn('12'));
});
it('returns the result of base^exponent for base=0 exponent=1', async () => {
(await hub.pow(0, 1)).should.be.bignumber.equal(bn('0'));
});
it('returns the result of base^exponent for exponent=0', async () => {
(await hub.pow(12, 0)).should.be.bignumber.equal(bn('1'));
});
it('should throw on overflow', async () => {
await assertRevert(hub.pow(12, 583333333));
});
});
describe('periods', () => {
it('returns the correct period', async () => {
(await hub.period()).should.be.bignumber.equal(period);
});
it('returns 0 before a full period has passed', async () => {
(await hub.periods()).should.be.bignumber.equal(bn(0));
});
it('returns the correct number of periods after 1 period has passed', async () => {
const time = period.mul(bn(1));
await increase(time.toNumber());
(await hub.periods()).should.be.bignumber.equal(bn(1));
});
it('returns the correct number of periods after 1+x period has passed', async () => {
const time = period.mul(bn(1)).toNumber() + 500;
await increase(time);
(await hub.periods()).should.be.bignumber.equal(bn(1));
});
it('returns the correct number of periods after x period has passed', async () => {
const time = period.mul(bn(8));
await increase(time.toNumber());
(await hub.periods()).should.be.bignumber.equal(bn(8));
});
it('returns the correct number of periods after x-2 period has passed', async () => {
// testing this to one-second accuracy turns out the have the same indeterminacy problems
// that effect the ubi payout tests
const time = period.mul(bn(8)).toNumber() - 2;
await increase(time);
(await hub.periods()).should.be.bignumber.equal(bn(7));
});
});
describe('issuance', () => {
it('returns the correct issuance at deployment', async () => {
(await hub.issuance()).should.be.bignumber.equal(convertToBaseUnit(100));
});
it('returns the correct issuance after 1 period', async () => {
await increase(period.toNumber());
const compounded = inflate(signupBonusConverted, inflation, divisor, bn(1));
(await hub.issuance()).should.be.bignumber.equal(compounded);
});
it('returns the correct issuance after x period', async () => {
const numPeriods = bn(5);
const time = period.mul(numPeriods);
await increase(time.toNumber());
const compounded = inflate(signupBonusConverted, inflation, divisor, numPeriods);
(await hub.issuance()).should.be.bignumber.equal(compounded);
});
});
describe('issuanceByStep', () => {
it('returns the correct issuance at deployment', async () => {
(await hub.issuanceByStep(0)).should.be.bignumber.equal(convertToBaseUnit(100));
});
it('returns the correct issuance after 1 period', async () => {
await increase(period.toNumber());
const compounded = inflate(signupBonusConverted, inflation, divisor, bn(1));
(await hub.issuanceByStep(1)).should.be.bignumber.equal(compounded);
});
it('returns the correct issuance after x period', async () => {
const numPeriods = bn(5);
const compounded = inflate(signupBonusConverted, inflation, divisor, numPeriods);
(await hub.issuanceByStep(5)).should.be.bignumber.equal(compounded);
});
});
describe('inflate', () => {
it('returns the correct inflation with no periods passed', async () => {
hub = await Hub
.new(
inflation,
period,
symbol,
name,
signupBonusConverted,
signupBonusConverted,
timeout,
{ from: systemOwner, gas: maxGas },
);
(await hub.inflate(signupBonusConverted, 0)).should.be.bignumber.equal(signupBonusConverted);
});
it('returns the correct inflation with 1 period passed', async () => {
hub = await Hub
.new(
inflation,
period,
symbol,
name,
signupBonusConverted,
signupBonusConverted,
timeout,
{ from: systemOwner, gas: maxGas },
);
const compounded = inflate(signupBonusConverted, inflation, divisor, bn(1));
(await hub.inflate(signupBonusConverted, 1)).should.be.bignumber.equal(compounded);
});
it('returns the correct inflation with x periods passed', async () => {
hub = await Hub
.new(
inflation,
period,
symbol,
name,
signupBonusConverted,
signupBonusConverted,
timeout,
{ from: systemOwner, gas: maxGas },
);
const compounded = inflate(signupBonusConverted, inflation, divisor, bn(22));
(await hub.inflate(signupBonusConverted, 22)).should.be.bignumber.equal(compounded);
});
it('returns the correct inflation with no periods passed', async () => {
const startingRate = bn(52);
inflation = bn(1035);
hub = await Hub
.new(
inflation,
period,
symbol,
name,
signupBonusConverted,
startingRate,
timeout,
{ from: systemOwner, gas: maxGas },
);
(await hub.inflate(startingRate, 0)).should.be.bignumber.equal(startingRate);
});
it('returns the correct inflation with 1 period passed', async () => {
const startingRate = bn(2);
inflation = bn(2035);
hub = await Hub
.new(
inflation,
period,
symbol,
name,
signupBonusConverted,
startingRate,
timeout,
{ from: systemOwner, gas: maxGas },
);
const compounded = inflate(startingRate, inflation, bn(1000), bn(1));
(await hub.inflate(startingRate, 1)).should.be.bignumber.equal(compounded);
});
it('returns the correct inflation with x periods passed', async () => {
const startingRate = bn(4562);
inflation = bn(705);
hub = await Hub
.new(
inflation,
period,
symbol,
name,
signupBonusConverted,
startingRate,
timeout,
{ from: systemOwner, gas: maxGas },
);
const compounded = inflate(startingRate, inflation, bn(100), bn(22));
(await hub.inflate(startingRate, 22)).should.be.bignumber.equal(compounded);
});
});
describe('finds correct divisor', () => {
it('returns the correct divisor for 6790007', async () => {
inflation = bn(6790007);
hub = await Hub
.new(
inflation,
period,
symbol,
name,
signupBonusConverted,
signupBonusConverted,
timeout,
{ from: systemOwner, gas: maxGas },
);
(await hub.divisor()).should.be.bignumber.equal(bn(1000000));
});
it('returns the correct divisor for 7', async () => {
inflation = bn(7);
hub = await Hub
.new(
inflation,
period,
symbol,
name,
signupBonusConverted,
signupBonusConverted,
timeout,
{ from: systemOwner, gas: maxGas },
);
(await hub.divisor()).should.be.bignumber.equal(bn(1));
});
it('returns the correct divisor for 10', async () => {
inflation = bn(10);
hub = await Hub
.new(
inflation,
period,
symbol,
name,
signupBonusConverted,
signupBonusConverted,
timeout,
{ from: systemOwner, gas: maxGas },
);
(await hub.divisor()).should.be.bignumber.equal(bn(10));
});
it('returns the correct divisor for 0', async () => {
inflation = bn(0);
hub = await Hub
.new(
inflation,
period,
symbol,
name,
signupBonusConverted,
signupBonusConverted,
timeout,
{ from: systemOwner, gas: maxGas },
);
(await hub.divisor()).should.be.bignumber.equal(bn(1));
});
it('returns the correct divisor for 10000', async () => {
inflation = bn(10000);
hub = await Hub
.new(
inflation,
period,
symbol,
name,
signupBonusConverted,
signupBonusConverted,
timeout,
{ from: systemOwner, gas: maxGas },
);
(await hub.divisor()).should.be.bignumber.equal(bn(10000));
});
});
});