-
Notifications
You must be signed in to change notification settings - Fork 22
/
1-UBI.js
453 lines (403 loc) · 17.6 KB
/
1-UBI.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
const {
BigNumber,
maxGas,
symbol,
name,
timeout,
} = require('./helpers/constants');
const { bn, convertToBaseUnit, ubiPayout, near, inflate } = require('./helpers/math');
const { increase } = require('./helpers/increaseTime');
const { getTimestampFromTx } = require('./helpers/getTimestamp');
const Hub = artifacts.require('MockHub');
const Token = artifacts.require('Token');
const findPayout = async (_token, init, inf, div, per) => {
const offset = await _token.inflationOffset();
const lastTouched = await _token.lastTouched();
const blocktime = await _token.time();
const hubDeployedAt = await _token.hubDeployedAt();
const payout = ubiPayout(init, lastTouched, blocktime, offset, inf, div, per, hubDeployedAt);
return payout;
};
require('chai')
.use(require('chai-bn')(BigNumber))
.should();
contract('UBI', ([_, owner, recipient, attacker, systemOwner]) => { // eslint-disable-line no-unused-vars
let hub = null;
let token = null;
let divisor = bn(100);
let inflation = bn(275);
let period = bn(7885000000);
let signupBonus = convertToBaseUnit(100);
describe('issuance', () => {
beforeEach(async () => {
signupBonus = convertToBaseUnit(100);
hub = await Hub
.new(
inflation,
period,
symbol,
name,
signupBonus,
signupBonus,
timeout,
{ from: systemOwner, gas: maxGas },
);
});
it('returns the correct issuance at deployment', async () => {
(await hub.issuance()).should.be.bignumber.equal(signupBonus);
});
it('returns the correct issuance after 1 period', async () => {
await increase(period.toNumber());
const q = inflation.pow(bn(1));
const d = divisor.pow(bn(1));
const compounded = (signupBonus.mul(q)).div(d);
(await hub.issuance()).should.be.bignumber.equal(compounded);
});
it('returns the correct issuance after x period', async () => {
const time = period.mul(bn(5));
await increase(time.toNumber());
const q = inflation.pow(bn(5));
const d = divisor.pow(bn(5));
const compounded = (signupBonus.mul(q)).div(d);
(await hub.issuance()).should.be.bignumber.equal(compounded);
});
});
describe('ubi payouts', () => {
let deployTime;
beforeEach(async () => {
hub = await Hub
.new(
inflation,
period,
symbol,
name,
signupBonus,
signupBonus,
period.mul(bn(10)),
{ from: systemOwner, gas: maxGas },
);
const signup = await hub.signup({ from: owner });
token = await Token.at(signup.logs[1].args.token);
deployTime = await getTimestampFromTx(signup.logs[0].transactionHash, web3);
});
it('correctly calculates the ubi payout at deployment', async () => {
const goal = await findPayout(token, signupBonus, inflation, divisor, period);
const bal = await token.look();
bal.should.bignumber.satisfy(() => near(bal, goal, signupBonus));
});
it('doesnt change balance at deployment', async () => {
await token.update();
const balance = await token.balanceOf(owner);
(balance).should.bignumber.satisfy(() => near(balance, signupBonus, signupBonus));
});
it('correctly calculates the ubi payout after 1 period', async () => {
await increase(period.toNumber());
const inf = inflate(signupBonus, inflation, divisor, bn(1));
const goal = await findPayout(token, signupBonus, inflation, divisor, period);
const bal = await token.look();
bal.should.bignumber.satisfy(() => near(bal, goal, inf));
});
it('look should return some balance after only a few seconds', async () => {
await increase(10);
const bal = await token.look();
bal.should.not.bignumber.equal(bn(0));
});
it('updates owners balance with payout after 1 period', async () => {
await increase(period.toNumber());
const inf = inflate(signupBonus, inflation, divisor, bn(1));
const goal = (await findPayout(token, signupBonus, inflation, divisor, period))
.add(signupBonus);
await token.update();
const balance = await token.balanceOf(owner);
balance.should.bignumber.satisfy(() => near(balance, goal, inf));
});
it('should update owners balance even if called by attacker', async () => {
await increase(period.toNumber());
const inf = inflate(signupBonus, inflation, divisor, bn(1));
const goal = (await findPayout(token, signupBonus, inflation, divisor, period))
.add(signupBonus);
await token.update({ from: attacker });
const balance = await token.balanceOf(owner);
(balance).should.be.bignumber.satisfy(() => near(balance, goal, inf));
});
it('should not change attacker balance if called by attacker', async () => {
await increase(period.toNumber());
await token.update({ from: attacker });
const balance = await token.balanceOf(attacker);
(balance).should.be.bignumber.equal(bn(0));
});
it('should update lastTouched by period if exactly one period has passed', async () => {
const timechange = period.toNumber();
await increase(timechange);
await token.update();
const time = await token.lastTouched();
const goal = bn(deployTime).add(bn(timechange));
time.should.bignumber.satisfy(() => near(time, goal, bn(1)));
});
it('should not update inflationOffset if exactly one period has passed', async () => {
const goal = await token.inflationOffset();
const timechange = period.toNumber();
await increase(timechange);
await token.update();
const offset = await token.inflationOffset();
offset.should.bignumber.satisfy(() => near(offset, goal, bn(1)));
});
it('should update lastTouched by period+x if more than a period but less than two has passed', async () => {
const timechange = period.toNumber() + 500;
await increase(timechange);
await token.update();
const time = await token.lastTouched();
const goal = bn(deployTime).add(bn(timechange));
time.should.bignumber.satisfy(() => near(time, goal, bn(1)));
});
it('should update inflationOffset by x if period+x has passed', async () => {
const goal = (await token.inflationOffset()).sub(bn(500));
const timechange = period.toNumber() + 500;
await increase(timechange);
await token.update();
const offset = await token.inflationOffset();
offset.should.bignumber.satisfy(() => near(offset, goal, bn(1)));
});
it('correctly calculates the ubi payout after x periods', async () => {
const time = period.mul(bn(5));
const inf = inflate(signupBonus, inflation, divisor, bn(5));
await increase(time.toNumber());
const goal = await findPayout(token, signupBonus, inflation, divisor, period);
const balance = await token.look();
balance.should.bignumber.satisfy(() => near(balance, goal, inf));
});
it('updates owners balance with payout after x periods', async () => {
const time = period.mul(bn(5));
const inf = inflate(signupBonus, inflation, divisor, bn(5));
await increase(time.toNumber());
const goal = (await findPayout(token, signupBonus, inflation, divisor, period))
.add(signupBonus);
await token.update();
const balance = await token.balanceOf(owner);
balance.should.bignumber.satisfy(() => near(balance, goal, inf));
});
it('updates owners balance with payout after x periods when update is called multiple times', async () => {
const lastTouched = await token.lastTouched();
const inf = inflate(signupBonus, inflation, divisor, bn(4));
const offset = await token.inflationOffset();
const hubDeployedAt = await token.hubDeployedAt();
await increase(period.toNumber());
await token.update();
await increase(period.toNumber());
await token.update();
await increase(period.toNumber());
await token.update();
await increase(period.toNumber());
await token.update();
const blocktime = await token.time();
const goal = (ubiPayout(
signupBonus, lastTouched, blocktime, offset, inflation, divisor, period, hubDeployedAt))
.add(signupBonus);
const balance = await token.balanceOf(owner);
balance.should.bignumber.satisfy(() => near(balance, goal, inf));
});
it('should update lastTouched by period if x period has passed', async () => {
const timechange = period.mul(bn(5));
await increase(timechange.toNumber());
await token.update();
const lastTouched = await token.lastTouched();
const goal = bn(deployTime).add(timechange);
lastTouched.should.bignumber.satisfy(() => near(lastTouched, goal, bn(1)));
});
it('should update lastTouched by period+z if more x but less than y periods have passed', async () => {
const timechange = (period.mul(bn(5))).toNumber() + 500;
await increase(timechange);
await token.update();
const lastTouched = await token.lastTouched();
const goal = bn(deployTime).add(bn(timechange));
lastTouched.should.bignumber.satisfy(() => near(lastTouched, goal, bn(1)));
});
it('should not update lastTouched if update isnt called', async () => {
const time = period.mul(bn(2));
await increase(time.toNumber() + 500);
const lastTouched = await token.lastTouched();
(lastTouched).should.be.bignumber.equal(bn(deployTime));
});
it('should show no payable ubi if manually stopped', async () => {
const time = period.mul(bn(2));
await increase(time.toNumber() + 500);
await token.stop({ from: owner });
const bal = await token.look();
bal.should.be.bignumber.equal(bn(0));
});
it('should return that it is stopped when it has been manually stopped', async () => {
await token.stop({ from: owner });
const isStopped = await token.stopped();
isStopped.should.be.equal(true);
});
it('should not payout ubi if manually stopped', async () => {
const time = period.mul(bn(2));
await increase(time.toNumber() + 500);
const bal = await token.balanceOf(owner);
await token.stop({ from: owner });
await token.update();
(await token.balanceOf(owner)).should.be.bignumber.equal(bal);
});
it('should show no payable ubi if expired', async () => {
const time = period.mul(bn(11));
await increase(time.toNumber());
const bal = await token.look();
bal.should.be.bignumber.equal(bn(0));
});
it('should return that it is stopped when it has expired', async () => {
const time = period.mul(bn(11));
await increase(time.toNumber());
const isStopped = await token.stopped();
isStopped.should.be.equal(true);
});
it('should not payout ubi if expired', async () => {
const time = period.mul(bn(11));
await increase(time.toNumber());
const bal = await token.balanceOf(owner);
await token.update();
(await token.balanceOf(owner)).should.be.bignumber.equal(bal);
});
});
describe('ubi payouts - with different config params', () => {
let deployTime;
let startingIssuance;
beforeEach(async () => {
period = bn(86400);
divisor = bn(1000);
inflation = bn(1035);
signupBonus = convertToBaseUnit(100);
startingIssuance = bn(80);
hub = await Hub
.new(
inflation,
period,
symbol,
name,
signupBonus,
startingIssuance,
timeout,
{ from: systemOwner, gas: maxGas },
);
const signup = await hub.signup({ from: owner, gas: 6721975 });
token = await Token.at(signup.logs[1].args.token);
deployTime = await getTimestampFromTx(signup.logs[0].transactionHash, web3);
});
it('correctly calculates the ubi payout at deployment', async () => {
const goal = await findPayout(token, startingIssuance, inflation, divisor, period);
const bal = await token.look();
bal.should.bignumber.satisfy(() => near(bal, goal, startingIssuance));
});
it('doesnt change balance at deployment', async () => {
await token.update();
const balance = await token.balanceOf(owner);
(balance).should.bignumber.satisfy(() => near(balance, signupBonus, startingIssuance));
});
it('correctly calculates the ubi payout after 1 period', async () => {
await increase(period.toNumber());
const inf = inflate(startingIssuance, inflation, divisor, bn(1));
const goal = await findPayout(token, startingIssuance, inflation, divisor, period);
const bal = await token.look();
bal.should.bignumber.satisfy(() => near(bal, goal, inf));
});
it('updates owners balance with payout after 1 period', async () => {
await increase(period.toNumber());
const inf = inflate(startingIssuance, inflation, divisor, bn(1));
const goal = (await findPayout(token, startingIssuance, inflation, divisor, period))
.add(signupBonus);
await token.update();
const balance = await token.balanceOf(owner);
balance.should.bignumber.satisfy(() => near(balance, goal, inf));
});
it('should update owners balance even if called by attacker', async () => {
await increase(period.toNumber());
const inf = inflate(startingIssuance, inflation, divisor, bn(1));
const goal = (await findPayout(token, startingIssuance, inflation, divisor, period))
.add(signupBonus);
await token.update({ from: attacker });
const balance = await token.balanceOf(owner);
(balance).should.be.bignumber.satisfy(() => near(balance, goal, inf));
});
it('should not change attacker balance if called by attacker', async () => {
await increase(period.toNumber());
await token.update({ from: attacker });
const balance = await token.balanceOf(attacker);
(balance).should.be.bignumber.equal(bn(0));
});
it('should update lastTouched by period if exactly one period has passed', async () => {
const timechange = period.toNumber();
await increase(timechange);
await token.update();
const time = await token.lastTouched();
const goal = bn(deployTime).add(bn(timechange));
time.should.bignumber.satisfy(() => near(time, goal, bn(1)));
});
it('should update lastTouched by period+x if more than a period but less than two has passed', async () => {
const timechange = period.toNumber() + 500;
await increase(timechange);
await token.update();
const time = await token.lastTouched();
const goal = bn(deployTime).add(bn(timechange));
time.should.bignumber.satisfy(() => near(time, goal, bn(1)));
});
it('correctly calculates the ubi payout after x periods', async () => {
const time = period.mul(bn(5));
const inf = inflate(startingIssuance, inflation, divisor, bn(5));
await increase(time.toNumber());
const goal = await findPayout(token, startingIssuance, inflation, divisor, period);
const balance = await token.look();
balance.should.bignumber.satisfy(() => near(balance, goal, inf));
});
it('updates owners balance with payout after x periods', async () => {
const time = period.mul(bn(5));
const inf = inflate(startingIssuance, inflation, divisor, bn(5));
await increase(time.toNumber());
const goal = (await findPayout(token, startingIssuance, inflation, divisor, period))
.add(signupBonus);
await token.update();
const balance = await token.balanceOf(owner);
balance.should.bignumber.satisfy(() => near(balance, goal, inf));
});
it('updates owners balance with payout after x periods when update is called multiple times', async () => {
const lastTouched = await token.lastTouched();
const inf = inflate(startingIssuance, inflation, divisor, bn(4));
const offset = await token.inflationOffset();
const hubDeployedAt = await token.hubDeployedAt();
await increase(period.toNumber());
await token.update();
await increase(period.toNumber());
await token.update();
await increase(period.toNumber());
await token.update();
await increase(period.toNumber());
await token.update();
const blocktime = await token.time();
const goal = (ubiPayout(
startingIssuance, lastTouched, blocktime, offset, inflation, divisor, period, hubDeployedAt))
.add(signupBonus);
const balance = await token.balanceOf(owner);
balance.should.bignumber.satisfy(() => near(balance, goal, inf));
});
it('should update lastTouched by period if x period has passed', async () => {
const timechange = period.mul(bn(5));
await increase(timechange.toNumber());
await token.update();
const lastTouched = await token.lastTouched();
const goal = bn(deployTime).add(timechange);
lastTouched.should.bignumber.satisfy(() => near(lastTouched, goal, bn(1)));
});
it('should update lastTouched by period+z if more x but less than y periods have passed', async () => {
const timechange = (period.mul(bn(5))).toNumber() + 500;
await increase(timechange);
await token.update();
const lastTouched = await token.lastTouched();
const goal = bn(deployTime).add(bn(timechange));
lastTouched.should.bignumber.satisfy(() => near(lastTouched, goal, bn(1)));
});
it('should not update lastTouched if update isnt called', async () => {
const time = period.mul(bn(2));
await increase(time.toNumber() + 500);
const lastTouched = await token.lastTouched();
(lastTouched).should.be.bignumber.equal(bn(deployTime));
});
});
});