-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathHub.Trust.test.js
517 lines (440 loc) · 22.5 KB
/
Hub.Trust.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
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
const truffleContract = require('@truffle/contract');
const { assertRevert } = require('./helpers/assertRevert');
const expectEvent = require('./helpers/expectEvent');
const safeArtifacts = require('@gnosis.pm/safe-contracts/build/artifacts/contracts/GnosisSafeL2.sol/GnosisSafeL2.json');
const proxyArtifacts = require('@gnosis.pm/safe-contracts/build/artifacts/contracts/proxies/GnosisSafeProxyFactory.sol/GnosisSafeProxyFactory.json');
const {
BigNumber,
extraGas,
maxGas,
inflation,
period,
symbol,
name,
signupBonus,
ZERO_ADDRESS,
timeout,
} = require('./helpers/constants');
const { bn } = require('./helpers/math');
const { increase } = require('./helpers/increaseTime');
const { createSafeWithProxy } = require('./helpers/createSafeWithProxy');
require('chai')
.use(require('chai-bn')(BigNumber))
.should();
const Hub = artifacts.require('MockHub');
const Token = artifacts.require('Token');
const GnosisSafe = truffleContract(safeArtifacts);
const ProxyFactory = truffleContract(proxyArtifacts);
GnosisSafe.setProvider(web3.currentProvider);
ProxyFactory.setProvider(web3.currentProvider);
contract('Hub - trust limits', ([_, systemOwner, attacker, safeOwner, normalUser, thirdUser, fourthUser, organization]) => { // eslint-disable-line no-unused-vars
let hub = null;
let safe = null;
let proxyFactory = null;
const organizationTrustLimit = 100;
let safeOwnerToken = null;
let normalUserToken = null;
beforeEach(async () => {
hub = await Hub
.new(
inflation,
period,
symbol,
name,
signupBonus,
signupBonus,
timeout,
{ from: systemOwner, gas: maxGas },
);
safe = await GnosisSafe.new({ from: systemOwner });
proxyFactory = await ProxyFactory.new({ from: systemOwner });
userSafe = await createSafeWithProxy(proxyFactory, safe, GnosisSafe, safeOwner);
});
describe('user can set trust limits', async () => {
const trustLimit = 50;
describe('cannot trust someone before youve signed up', async () => {
it('should throw', async () => assertRevert(hub.trust(normalUser, trustLimit, { from: safeOwner })));
it('checkSendLimit should return zero', async () => {
(await hub.checkSendLimit(safeOwner, safeOwner, normalUser))
.should.be.bignumber.equal(bn(0));
});
});
describe('when user tries to adjust their trust for themselves', async () => {
beforeEach(async () => {
await hub.signup({ from: safeOwner });
});
it('should throw', async () => assertRevert(hub.trust(safeOwner, trustLimit, { from: safeOwner })));
it('correctly sets the trust limit on signup', async () => {
(await hub.limits(safeOwner, safeOwner))
.should.be.bignumber.equal(bn(100));
});
it('checkSendLimit returns the correct amount for self-send', async () => {
(await hub.checkSendLimit(safeOwner, safeOwner, safeOwner))
.should.be.bignumber.equal(bn(100));
});
it('checkSendLimit returns the correct amount for an untrusted user', async () => {
(await hub.checkSendLimit(safeOwner, normalUser, safeOwner))
.should.be.bignumber.equal(bn(0));
});
it('checkSendLimit returns the correct amount for token that isnt deployed', async () => {
(await hub.checkSendLimit(ZERO_ADDRESS, safeOwner, safeOwner))
.should.be.bignumber.equal(bn(0));
});
});
describe('when trust destination is not a circles token or organization', async () => {
beforeEach(async () => {
await hub.signup({ from: safeOwner });
await hub.trust(normalUser, trustLimit, { from: safeOwner });
});
it('creates a trust event', async () => {
const logs = await hub.getPastEvents('Trust', { fromBlock: 0, toBlock: 'latest' });
const event = expectEvent.inLogs(logs, 'Trust', {
canSendTo: safeOwner,
user: normalUser,
});
return event.args.limit.should.be.bignumber.equal(bn(trustLimit));
});
it('correctly sets the trust limit', async () => {
(await hub.limits(safeOwner, normalUser))
.should.be.bignumber.equal(bn(trustLimit));
});
it('checkSendLimit returns correct amount', async () => {
(await hub.checkSendLimit(normalUser, normalUser, safeOwner))
.should.be.bignumber.equal(bn(0));
});
});
describe('when trust destination is an organization', async () => {
beforeEach(async () => {
await hub.signup({ from: safeOwner });
const safeOwnerTokenAddress = await hub.userToToken(safeOwner);
safeOwnerToken = await Token.at(safeOwnerTokenAddress);
await hub.organizationSignup({ from: organization });
});
it('throws when trusting organization', async () => {
await assertRevert(hub.trust(organization, trustLimit, { from: safeOwner }));
});
it('creates a trust event when organization trusts user', async () => {
await hub.trust(safeOwner, organizationTrustLimit, { from: organization });
const logs = await hub.getPastEvents('Trust', { fromBlock: 0, toBlock: 'latest' });
const event = expectEvent.inLogs(logs, 'Trust', {
canSendTo: organization,
user: safeOwner,
});
return event.args.limit.should.be.bignumber.equal(bn(organizationTrustLimit));
});
it('correctly sets the trust limit', async () => {
await hub.trust(safeOwner, organizationTrustLimit, { from: organization });
(await hub.limits(organization, safeOwner))
.should.be.bignumber.equal(bn(organizationTrustLimit));
});
it('checkSendLimit returns correct amount', async () => {
await hub.trust(safeOwner, organizationTrustLimit, { from: organization });
const balance = await safeOwnerToken.balanceOf(safeOwner);
(await hub.checkSendLimit(safeOwner, safeOwner, organization))
.should.be.bignumber.equal(bn(balance));
});
it('throws when organization uses weighted trust', async () => {
await assertRevert(hub.trust(safeOwner, trustLimit, { from: organization }));
});
});
describe('when trust destination is a circles token', async () => {
beforeEach(async () => {
await hub.signup({ from: safeOwner });
const safeOwnerTokenAddress = await hub.userToToken(safeOwner);
safeOwnerToken = await Token.at(safeOwnerTokenAddress);
await hub.signup({ from: normalUser });
const normalUserTokenAddress = await hub.userToToken(normalUser);
normalUserToken = await Token.at(normalUserTokenAddress);
await hub.trust(normalUser, trustLimit, { from: safeOwner });
});
it('creates a trust event', async () => {
const logs = await hub.getPastEvents('Trust', { fromBlock: 0, toBlock: 'latest' });
const event = expectEvent.inLogs(logs, 'Trust', {
canSendTo: safeOwner,
user: normalUser,
});
return event.args.limit.should.be.bignumber.equal(bn(trustLimit));
});
it('correctly sets the trust limit', async () => {
(await hub.limits(safeOwner, normalUser))
.should.be.bignumber.equal(bn(trustLimit));
});
it('correctly sets the trust limit at 100', async () => {
await hub.trust(normalUser, 100, { from: safeOwner });
(await hub.limits(safeOwner, normalUser))
.should.be.bignumber.equal(bn(100));
});
it('does not allow trust limit higher than 100', async () => {
await assertRevert(hub.trust(normalUser, 101, { from: safeOwner }));
});
describe('calculates the tradeable amount', async () => {
it('returns correct amount when no tokens have been traded', async () => {
const safeOwnerBalance = await safeOwnerToken.balanceOf(safeOwner);
const allowable = safeOwnerBalance * (trustLimit / 100);
(await hub.checkSendLimit(normalUser, normalUser, safeOwner))
.should.be.bignumber.equal(bn(allowable));
});
it('returns correct amount when truster receives tokens of the other user', async () => {
const amount = bn(25);
await normalUserToken.transfer(safeOwner, amount, { from: normalUser, gas: extraGas });
const safeOwnerBalance = await safeOwnerToken.balanceOf(safeOwner);
const safeOwnerBalanceOfNormalUserToken = await normalUserToken.balanceOf(safeOwner);
const allowable = bn(safeOwnerBalance * (trustLimit / 100))
.sub(bn(safeOwnerBalanceOfNormalUserToken * (1 - (trustLimit / 100))));
(await hub.checkSendLimit(normalUser, normalUser, safeOwner))
.should.be.bignumber.equal(allowable);
});
it('returns correct amount for returnable to sender, after tokens have been traded', async () => {
const amount = bn(25);
await normalUserToken.transfer(safeOwner, amount, { from: normalUser, gas: extraGas });
(await hub.checkSendLimit(normalUser, safeOwner, normalUser))
.should.be.bignumber.equal(amount);
});
it('returns correct amount when no tokens are tradeable', async () => {
const amount = bn(100);
await normalUserToken.transfer(safeOwner, amount, { from: normalUser, gas: extraGas });
(await hub.checkSendLimit(normalUser, normalUser, safeOwner))
.should.be.bignumber.equal(bn(0));
});
it('returns correct amount when no tokens are returnable', async () => {
(await hub.checkSendLimit(normalUser, safeOwner, normalUser))
.should.be.bignumber.equal(bn(0));
});
it('returns correct amount when there is not trust connection', async () => {
const amount = bn(0);
(await hub.checkSendLimit(safeOwner, safeOwner, normalUser))
.should.be.bignumber.equal(amount);
});
describe('user can update trust limits', async () => {
const newTrustLimit = 75;
let txHash;
beforeEach(async () => {
txHash = await hub.trust(normalUser, newTrustLimit, { from: safeOwner });
});
it('creates a trust event', async () => {
const { logs } = txHash;
const event = expectEvent.inLogs(logs, 'Trust', {
canSendTo: safeOwner,
user: normalUser,
});
return event.args.limit.should.be.bignumber.equal(bn(newTrustLimit));
});
it('correctly sets the trust limit', async () => {
(await hub.limits(safeOwner, normalUser))
.should.be.bignumber.equal(bn(newTrustLimit));
});
it('returns correct amount when no tokens have been traded', async () => {
const safeOwnerBalance = await safeOwnerToken.balanceOf(safeOwner);
const allowable = safeOwnerBalance * (newTrustLimit / 100);
(await hub.checkSendLimit(normalUser, normalUser, safeOwner))
.should.be.bignumber.equal(bn(allowable));
});
it('returns correct amount when user has received tokens', async () => {
const amount = bn(25);
await normalUserToken.transfer(safeOwner, amount, { from: normalUser, gas: extraGas });
const safeOwnerBalance = await safeOwnerToken.balanceOf(safeOwner);
const safeOwnerBalanceOfNormalUserToken = await normalUserToken.balanceOf(safeOwner);
const allowable = bn(safeOwnerBalance * (newTrustLimit / 100))
.sub(bn(safeOwnerBalanceOfNormalUserToken * (1 - (newTrustLimit / 100))));
(await hub.checkSendLimit(normalUser, normalUser, safeOwner))
.should.be.bignumber.equal(allowable);
});
it('returns correct amount when user has sent tokens', async () => {
const amount = bn(25);
await safeOwnerToken.transfer(normalUser, amount, { from: safeOwner, gas: extraGas });
const safeOwnerBalance = await safeOwnerToken.balanceOf(safeOwner);
const allowable = bn(safeOwnerBalance * (newTrustLimit / 100));
(await hub.checkSendLimit(normalUser, normalUser, safeOwner))
.should.be.bignumber.equal(allowable);
});
it('returns correct amount when no tokens are tradeable', async () => {
const amount = bn(50);
await normalUserToken.transfer(safeOwner, amount, { from: normalUser, gas: extraGas });
await safeOwnerToken.transfer(thirdUser, amount, { from: safeOwner, gas: extraGas });
(await hub
.checkSendLimit(normalUser, normalUser, safeOwner))
.should.be.bignumber.equal(bn(0));
});
});
describe('user can update trust limits to 100', async () => {
const newTrustLimit = 100;
let txHash;
beforeEach(async () => {
txHash = await hub.trust(normalUser, newTrustLimit, { from: safeOwner });
});
it('creates a trust event', async () => {
const { logs } = txHash;
const event = expectEvent.inLogs(logs, 'Trust', {
canSendTo: safeOwner,
user: normalUser,
});
return event.args.limit.should.be.bignumber.equal(bn(newTrustLimit));
});
it('correctly sets the trust limit', async () => {
(await hub.limits(safeOwner, normalUser))
.should.be.bignumber.equal(bn(newTrustLimit));
});
it('returns correct amount when no tokens have been traded', async () => {
const safeOwnerBalance = await safeOwnerToken.balanceOf(safeOwner);
const allowable = safeOwnerBalance * (newTrustLimit / 100);
(await hub.checkSendLimit(normalUser, normalUser, safeOwner))
.should.be.bignumber.equal(bn(allowable));
});
it('returns correct amount when user has received tokens', async () => {
const amount = bn(25);
await normalUserToken.transfer(safeOwner, amount, { from: normalUser, gas: extraGas });
const safeOwnerBalance = await safeOwnerToken.balanceOf(safeOwner);
const safeOwnerBalanceOfNormalUserToken = await normalUserToken.balanceOf(safeOwner);
const allowable = bn(safeOwnerBalance * (newTrustLimit / 100))
.sub(bn(safeOwnerBalanceOfNormalUserToken * (1 - (newTrustLimit / 100))));
(await hub.checkSendLimit(normalUser, normalUser, safeOwner))
.should.be.bignumber.equal(allowable);
});
it('returns correct amount when user has sent tokens', async () => {
const amount = bn(25);
await safeOwnerToken.transfer(normalUser, amount, { from: safeOwner, gas: extraGas });
const safeOwnerBalance = await safeOwnerToken.balanceOf(safeOwner);
const allowable = bn(safeOwnerBalance * (newTrustLimit / 100));
(await hub.checkSendLimit(normalUser, normalUser, safeOwner))
.should.be.bignumber.equal(allowable);
});
it('should always accept tokens from someone trusted 100', async () => {
const amount = bn(100);
await normalUserToken.transfer(safeOwner, amount, { from: normalUser, gas: extraGas });
(await hub
.checkSendLimit(normalUser, normalUser, safeOwner))
.should.be.bignumber.equal(bn(100));
});
});
});
});
});
describe('when users sign up at a different time', async () => {
const trustLimit = 50;
beforeEach(async () => {
await hub.signup({ from: safeOwner });
await hub.trust(normalUser, trustLimit, { from: safeOwner });
const safeOwnertokenAddress = await hub.userToToken(safeOwner);
safeOwnerToken = await Token.at(safeOwnertokenAddress);
await increase(period.toNumber());
await safeOwnerToken.update();
await hub.signup({ from: normalUser });
await hub.trust(safeOwner, trustLimit, { from: normalUser });
const normalUserTokenAddress = await hub.userToToken(normalUser);
normalUserToken = await Token.at(normalUserTokenAddress);
});
describe('from perspective of newer user', async () => {
it('returns correct amount when no tokens have been traded', async () => {
const safeOwnerBalance = await safeOwnerToken.balanceOf(safeOwner);
const allowable = safeOwnerBalance * (trustLimit / 100);
(await hub.checkSendLimit(normalUser, normalUser, safeOwner))
.should.be.bignumber.equal(bn(allowable));
});
it('returns correct amount when user has received tokens', async () => {
const amount = bn(25);
await normalUserToken.transfer(safeOwner, amount, { from: normalUser, gas: extraGas });
const safeOwnerBalance = await safeOwnerToken.balanceOf(safeOwner);
const safeOwnerBalanceOfNormalUserToken = await normalUserToken.balanceOf(safeOwner);
const allowable = bn((safeOwnerBalance * trustLimit) / 100)
.sub(bn((safeOwnerBalanceOfNormalUserToken * trustLimit) / 100));
(await hub.checkSendLimit(normalUser, normalUser, safeOwner))
.should.be.bignumber.equal(allowable);
});
it('returns correct amount when user has sent tokens', async () => {
const amount = bn(25);
await safeOwnerToken.transfer(normalUser, amount, { from: safeOwner, gas: extraGas });
const safeOwnerBalance = await safeOwnerToken.balanceOf(safeOwner);
const allowable = bn(safeOwnerBalance * (trustLimit / 100));
(await hub.checkSendLimit(normalUser, normalUser, safeOwner))
.should.be.bignumber.equal(allowable);
});
it('returns correct amount when ubi has been paid out', async () => {
await increase(period.toNumber());
await safeOwnerToken.update();
const safeOwnerBalance = await safeOwnerToken.balanceOf(safeOwner);
const allowable = bn(safeOwnerBalance * (trustLimit / 100));
(await hub.checkSendLimit(normalUser, normalUser, safeOwner))
.should.be.bignumber.equal(allowable);
});
it('returns correct amount when ubi has been paid out to trust recipient', async () => {
await increase(period.toNumber());
await normalUserToken.update();
const safeOwnerBalance = await safeOwnerToken.balanceOf(safeOwner);
const allowable = bn(safeOwnerBalance * (trustLimit / 100));
(await hub.checkSendLimit(normalUser, normalUser, safeOwner))
.should.be.bignumber.equal(allowable);
});
it('returns correct amount when fewer tokens are tradeable', async () => {
const normalUserBalance = await normalUserToken.balanceOf(normalUser);
await normalUserToken.transfer(
safeOwner, normalUserBalance, { from: normalUser, gas: extraGas });
const safeOwnerBalance = await safeOwnerToken.balanceOf(safeOwner);
const safeOwnerBalanceOfNormalUserToken = await normalUserToken.balanceOf(safeOwner);
const allowable = bn((safeOwnerBalance * trustLimit) / 100)
.sub(bn((safeOwnerBalanceOfNormalUserToken * trustLimit) / 100));
(await hub
.checkSendLimit(normalUser, normalUser, safeOwner))
.should.be.bignumber.equal(allowable);
});
});
describe('from perspective of older user', async () => {
it('returns correct amount when no tokens have been traded', async () => {
const normalUserBalance = await normalUserToken.balanceOf(normalUser);
const allowable = normalUserBalance * (trustLimit / 100);
(await hub.checkSendLimit(safeOwner, safeOwner, normalUser))
.should.be.bignumber.equal(bn(allowable));
});
it('returns correct amount when user has received tokens', async () => {
const amount = bn(25);
await safeOwnerToken.transfer(normalUser, amount, { from: safeOwner, gas: extraGas });
const normalUserBalance = await normalUserToken.balanceOf(normalUser);
const normalUserBalanceOfSafeOwnerToken = await safeOwnerToken.balanceOf(normalUser);
const allowable = bn((normalUserBalance * trustLimit) / 100)
.sub(bn((normalUserBalanceOfSafeOwnerToken * trustLimit) / 100));
(await hub.checkSendLimit(safeOwner, safeOwner, normalUser))
.should.be.bignumber.equal(allowable);
});
it('returns correct amount when user has sent tokens', async () => {
const amount = bn(25);
await normalUserToken.transfer(safeOwner, amount, { from: normalUser, gas: extraGas });
const normalUserBalance = await normalUserToken.balanceOf(normalUser);
const allowable = bn(normalUserBalance * (trustLimit / 100));
(await hub.checkSendLimit(safeOwner, safeOwner, normalUser))
.should.be.bignumber.equal(allowable);
});
it('returns correct amount when ubi has been paid out', async () => {
await increase(period.toNumber());
await normalUserToken.update();
const normalUserBalance = await normalUserToken.balanceOf(normalUser);
const allowable = bn(normalUserBalance * (trustLimit / 100));
(await hub.checkSendLimit(safeOwner, safeOwner, normalUser))
.should.be.bignumber.equal(allowable);
});
it('returns correct amount when ubi has been paid out to trust recipient', async () => {
await increase(period.toNumber());
await safeOwnerToken.update();
const normalUserBalance = await normalUserToken.balanceOf(normalUser);
const allowable = bn(normalUserBalance * (trustLimit / 100));
(await hub.checkSendLimit(safeOwner, safeOwner, normalUser))
.should.be.bignumber.equal(allowable);
});
it('returns correct amount when no tokens are tradeable', async () => {
const normalUserBalance = await normalUserToken.balanceOf(normalUser);
const allowable = bn(normalUserBalance * (trustLimit / 100));
await safeOwnerToken.transfer(normalUser, allowable, { from: safeOwner, gas: extraGas });
await normalUserToken.transfer(thirdUser, allowable, { from: normalUser, gas: extraGas });
(await hub
.checkSendLimit(safeOwner, safeOwner, normalUser))
.should.be.bignumber.equal(bn(0));
});
it('returns 0 when more tokens have been sent than the trust limit allows', async () => {
const normalUserBalance = await normalUserToken.balanceOf(normalUser);
await safeOwnerToken.transfer(
normalUser, normalUserBalance, { from: safeOwner, gas: extraGas });
(await hub
.checkSendLimit(safeOwner, safeOwner, normalUser))
.should.be.bignumber.equal(bn(0));
});
});
});
});