-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathHub.TransitiveTx.test.js
641 lines (553 loc) · 23.7 KB
/
Hub.TransitiveTx.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
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
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,
timeout,
} = require('./helpers/constants');
const { bn } = require('./helpers/math');
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 - transtive trust', ([_, systemOwner, attacker, safeOwner, normalUser, thirdUser, fourthUser, organization]) => { // eslint-disable-line no-unused-vars
let hub = null;
let safe = null;
let proxyFactory = null;
let userSafe = null;
const organizationTrustLimit = 100;
let safeOwnerToken = null;
let normalUserToken = null;
let fourthUserToken = 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 transact transitively when there is a valid path', async () => {
describe('when each user is sending their own token and path is valid', async () => {
const trustLimit = 50;
beforeEach(async () => {
await hub.signup({ from: safeOwner });
await hub.signup({ from: normalUser });
await hub.signup({ from: thirdUser });
const safeOwnertokenAddress = await hub.userToToken(safeOwner);
safeOwnerToken = await Token.at(safeOwnertokenAddress);
const normalUsertokenAddress = await hub.userToToken(normalUser);
normalUserToken = await Token.at(normalUsertokenAddress);
await hub.trust(safeOwner, trustLimit, { from: normalUser });
await hub.trust(normalUser, trustLimit, { from: thirdUser });
const amount = bn(25);
await hub
.transferThrough(
[safeOwner, normalUser],
[safeOwner, normalUser],
[normalUser, thirdUser],
[amount, amount],
{ from: safeOwner, gas: extraGas });
});
it('deducts senders balance of own token', async () => {
const bal = await safeOwnerToken.balanceOf(safeOwner);
bal.should.be.bignumber.equal(bn(75));
});
it('sends senders token to first user', async () => {
(await safeOwnerToken.balanceOf(normalUser))
.should.be.bignumber.equal(bn(25));
});
it('deducts first users balance', async () => {
(await normalUserToken.balanceOf(normalUser))
.should.be.bignumber.equal(bn(75));
});
it('sends first users token to destination', async () => {
(await normalUserToken.balanceOf(thirdUser))
.should.be.bignumber.equal(bn(25));
});
it('cleans up the seen array', async () => {
const seen = await hub.getSeen();
seen.should.be.bignumber.equal(bn(0));
});
it('cleans up the validation mapping for safeOwner', async () => {
const validation = await hub.getValidation(safeOwner);
validation['0'].should.be.equal(false);
validation['1'].should.be.bignumber.equal(bn(0));
validation['2'].should.be.bignumber.equal(bn(0));
});
it('cleans up the validation mapping for normalUser', async () => {
const validation = await hub.getValidation(normalUser);
validation['0'].should.be.equal(false);
validation['1'].should.be.bignumber.equal(bn(0));
validation['2'].should.be.bignumber.equal(bn(0));
});
it('cleans up the validation mapping for thirdUser', async () => {
const validation = await hub.getValidation(thirdUser);
validation['0'].should.be.equal(false);
validation['1'].should.be.bignumber.equal(bn(0));
validation['2'].should.be.bignumber.equal(bn(0));
});
it('creates a HubTransfer event', async () => {
const logs = await hub.getPastEvents('HubTransfer', { fromBlock: 0, toBlock: 'latest' });
const event = expectEvent.inLogs(logs, 'HubTransfer', {
from: safeOwner,
to: thirdUser,
});
return event.args.amount.should.be.bignumber.equal(bn(25));
});
});
describe('when each user is sending their own token and path is valid but forks', async () => {
const trustLimit = 50;
beforeEach(async () => {
await hub.signup({ from: safeOwner });
await hub.signup({ from: normalUser });
await hub.signup({ from: thirdUser });
await hub.signup({ from: fourthUser });
const safeOwnertokenAddress = await hub.userToToken(safeOwner);
safeOwnerToken = await Token.at(safeOwnertokenAddress);
const normalUsertokenAddress = await hub.userToToken(normalUser);
normalUserToken = await Token.at(normalUsertokenAddress);
const fourthUsertokenAddress = await hub.userToToken(fourthUser);
fourthUserToken = await Token.at(fourthUsertokenAddress);
await hub.trust(safeOwner, trustLimit, { from: normalUser });
await hub.trust(safeOwner, trustLimit, { from: fourthUser });
await hub.trust(normalUser, trustLimit, { from: thirdUser });
await hub.trust(fourthUser, trustLimit, { from: thirdUser });
await hub
.transferThrough(
[safeOwner, normalUser, fourthUser, safeOwner],
[safeOwner, normalUser, fourthUser, safeOwner],
[normalUser, thirdUser, thirdUser, fourthUser],
[15, 15, 10, 10],
{ from: safeOwner, gas: extraGas });
});
it('deducts senders balance of own token', async () => {
const bal = await safeOwnerToken.balanceOf(safeOwner);
bal.should.be.bignumber.equal(bn(75));
});
it('sends senders token to first user', async () => {
(await safeOwnerToken.balanceOf(normalUser))
.should.be.bignumber.equal(bn(15));
});
it('deducts first users balance', async () => {
(await normalUserToken.balanceOf(normalUser)).should.be.bignumber.equal(bn(85));
});
it('sends first users token to destination', async () => {
(await normalUserToken.balanceOf(thirdUser))
.should.be.bignumber.equal(bn(15));
});
it('sends senders token to fourth user', async () => {
(await safeOwnerToken.balanceOf(fourthUser))
.should.be.bignumber.equal(bn(10));
});
it('deducts fourth users balance', async () => {
(await fourthUserToken.balanceOf(fourthUser))
.should.be.bignumber.equal(bn(90));
});
it('sends fourth users token to destination', async () => {
(await fourthUserToken.balanceOf(thirdUser))
.should.be.bignumber.equal(bn(10));
});
it('cleans up the seen array', async () => {
const seen = await hub.getSeen();
seen.should.be.bignumber.equal(bn(0));
});
it('cleans up the validation mapping for safeOwner', async () => {
const validation = await hub.getValidation(safeOwner);
validation['0'].should.be.equal(false);
validation['1'].should.be.bignumber.equal(bn(0));
validation['2'].should.be.bignumber.equal(bn(0));
});
it('cleans up the validation mapping for normalUser', async () => {
const validation = await hub.getValidation(normalUser);
validation['0'].should.be.equal(false);
validation['1'].should.be.bignumber.equal(bn(0));
validation['2'].should.be.bignumber.equal(bn(0));
});
it('cleans up the validation mapping for thirdUser', async () => {
const validation = await hub.getValidation(thirdUser);
validation['0'].should.be.equal(false);
validation['1'].should.be.bignumber.equal(bn(0));
validation['2'].should.be.bignumber.equal(bn(0));
});
it('cleans up the validation mapping for fourthUser', async () => {
const validation = await hub.getValidation(fourthUser);
validation['0'].should.be.equal(false);
validation['1'].should.be.bignumber.equal(bn(0));
validation['2'].should.be.bignumber.equal(bn(0));
});
it('creates a HubTransfer event', async () => {
const logs = await hub.getPastEvents('HubTransfer', { fromBlock: 0, toBlock: 'latest' });
const event = expectEvent.inLogs(logs, 'HubTransfer', {
from: safeOwner,
to: thirdUser,
});
return event.args.amount.should.be.bignumber.equal(bn(25));
});
});
describe('when each user is sending their own token but trust path is invalid', async () => {
const trustLimit = 50;
beforeEach(async () => {
await hub.signup({ from: safeOwner });
await hub.signup({ from: normalUser });
await hub.signup({ from: thirdUser });
const safeOwnertokenAddress = await hub.userToToken(safeOwner);
safeOwnerToken = await Token.at(safeOwnertokenAddress);
await hub.trust(safeOwner, trustLimit, { from: normalUser });
});
it('should throw when missing trust', async () => {
const amount = bn(25);
await assertRevert(hub
.transferThrough(
[safeOwner, normalUser],
[safeOwner, normalUser],
[normalUser, thirdUser],
[amount, amount],
{ from: safeOwner, gas: extraGas }));
});
it('should throw when trust limit is too low', async () => {
await hub.trust(normalUser, 15, { from: thirdUser });
const amount = bn(25);
await assertRevert(hub
.transferThrough(
[safeOwner, normalUser],
[safeOwner, normalUser],
[normalUser, thirdUser],
[amount, amount],
{ from: safeOwner, gas: extraGas }));
});
it('should throw when passed too many srcs', async () => {
await hub.trust(normalUser, trustLimit, { from: thirdUser });
const amount = bn(25);
await assertRevert(hub
.transferThrough(
[safeOwner, normalUser],
[safeOwner, normalUser, thirdUser],
[normalUser, thirdUser],
[amount, amount],
{ from: safeOwner, gas: extraGas }));
});
it('should throw when passed too many tokenOwners', async () => {
await hub.trust(normalUser, trustLimit, { from: thirdUser });
const amount = bn(25);
await assertRevert(hub
.transferThrough(
[safeOwner, normalUser, thirdUser],
[safeOwner, normalUser],
[normalUser, thirdUser],
[amount, amount],
{ from: safeOwner, gas: extraGas }));
});
it('should throw when passed too many dests', async () => {
await hub.trust(normalUser, trustLimit, { from: thirdUser });
const amount = bn(25);
await assertRevert(hub
.transferThrough(
[safeOwner, normalUser],
[safeOwner, normalUser],
[normalUser, thirdUser, safeOwner],
[amount, amount],
{ from: safeOwner, gas: extraGas }));
});
it('should throw when passed too many amounts', async () => {
await hub.trust(normalUser, trustLimit, { from: thirdUser });
const amount = bn(25);
await assertRevert(hub
.transferThrough(
[safeOwner, normalUser],
[safeOwner, normalUser],
[normalUser, thirdUser],
[amount, amount, amount],
{ from: safeOwner, gas: extraGas }));
});
it('should throw when sender is not sending enough', async () => {
await hub.trust(normalUser, trustLimit, { from: thirdUser });
const amount = bn(25);
await assertRevert(hub
.transferThrough(
[safeOwner, normalUser],
[safeOwner, normalUser],
[normalUser, thirdUser],
[15, amount],
{ from: safeOwner, gas: extraGas }));
});
it('should throw when sender is sending too much', async () => {
await hub.trust(normalUser, trustLimit, { from: thirdUser });
const amount = bn(25);
await assertRevert(hub
.transferThrough(
[safeOwner, normalUser],
[safeOwner, normalUser],
[normalUser, thirdUser],
[amount, 15],
{ from: safeOwner, gas: extraGas }));
});
it('should throw when sender is receiving', async () => {
await hub.trust(normalUser, trustLimit, { from: safeOwner });
const amount = bn(25);
await assertRevert(hub
.transferThrough(
[safeOwner, normalUser],
[safeOwner, normalUser],
[normalUser, safeOwner],
[amount, amount],
{ from: safeOwner, gas: extraGas }));
});
it('should be able to send max based on trust limit', async () => {
const amount = await hub.checkSendLimit(safeOwner, safeOwner, normalUser);
await hub
.transferThrough(
[safeOwner],
[safeOwner],
[normalUser],
[amount],
{ from: safeOwner, gas: extraGas });
(await safeOwnerToken.balanceOf(normalUser))
.should.be.bignumber.equal(amount);
});
it('should throw when passing trust limit', async () => {
const amount = await hub.checkSendLimit(safeOwner, safeOwner, normalUser);
await assertRevert(hub
.transferThrough(
[safeOwner],
[safeOwner],
[normalUser],
[amount.add(bn(1))],
{ from: safeOwner, gas: extraGas }));
});
});
describe('when each user is not necessarily sending their own token and path is valid', async () => {
const trustLimit = 50;
beforeEach(async () => {
await hub.signup({ from: safeOwner });
await hub.signup({ from: normalUser });
await hub.signup({ from: thirdUser });
const safeOwnertokenAddress = await hub.userToToken(safeOwner);
safeOwnerToken = await Token.at(safeOwnertokenAddress);
const normalUsertokenAddress = await hub.userToToken(normalUser);
normalUserToken = await Token.at(normalUsertokenAddress);
await hub.trust(safeOwner, trustLimit, { from: normalUser });
await hub.trust(normalUser, trustLimit, { from: safeOwner });
await hub.trust(normalUser, trustLimit, { from: thirdUser });
const amount = bn(25);
const tokenAddress = await hub.userToToken(normalUser);
const token = await Token.at(tokenAddress);
await token.transfer(safeOwner, amount, { from: normalUser });
await hub
.transferThrough(
[normalUser],
[safeOwner],
[normalUser],
[amount],
{ from: safeOwner, gas: extraGas });
});
it('correctly set senders balance', async () => {
(await safeOwnerToken.balanceOf(safeOwner))
.should.be.bignumber.equal(bn(100));
});
it('sender has all their tokens back', async () => {
(await safeOwnerToken.balanceOf(normalUser))
.should.be.bignumber.equal(bn(0));
});
it('correctly sets normalUsers balance', async () => {
(await normalUserToken.balanceOf(normalUser))
.should.be.bignumber.equal(bn(100));
});
it('normalUser has all their own tokens', async () => {
(await normalUserToken.balanceOf(safeOwner))
.should.be.bignumber.equal(bn(0));
});
it('cleans up the seen array', async () => {
const seen = await hub.getSeen();
seen.should.be.bignumber.equal(bn(0));
});
it('cleans up the validation mapping for safeOwner', async () => {
const validation = await hub.getValidation(safeOwner);
validation['0'].should.be.equal(false);
validation['1'].should.be.bignumber.equal(bn(0));
validation['2'].should.be.bignumber.equal(bn(0));
});
it('cleans up the validation mapping for normalUser', async () => {
const validation = await hub.getValidation(normalUser);
validation['0'].should.be.equal(false);
validation['1'].should.be.bignumber.equal(bn(0));
validation['2'].should.be.bignumber.equal(bn(0));
});
it('creates a HubTransfer event', async () => {
const logs = await hub.getPastEvents('HubTransfer', { fromBlock: 0, toBlock: 'latest' });
const event = expectEvent.inLogs(logs, 'HubTransfer', {
from: safeOwner,
to: normalUser,
});
return event.args.amount.should.be.bignumber.equal(bn(25));
});
});
describe('when each final destination is an organization', async () => {
const trustLimit = 50;
beforeEach(async () => {
await hub.signup({ from: safeOwner });
await hub.signup({ from: normalUser });
await hub.organizationSignup({ from: organization });
await hub.trust(safeOwner, trustLimit, { from: normalUser });
await hub.trust(normalUser, trustLimit, { from: safeOwner });
await hub.trust(normalUser, organizationTrustLimit, { from: organization });
const safeOwnertokenAddress = await hub.userToToken(safeOwner);
safeOwnerToken = await Token.at(safeOwnertokenAddress);
const normalUsertokenAddress = await hub.userToToken(normalUser);
normalUserToken = await Token.at(normalUsertokenAddress);
const amount = bn(25);
await normalUserToken.transfer(safeOwner, amount, { from: normalUser });
await hub
.transferThrough(
[normalUser],
[safeOwner],
[organization],
[amount],
{ from: safeOwner, gas: extraGas });
});
it('correctly set senders balance', async () => {
(await safeOwnerToken.balanceOf(safeOwner))
.should.be.bignumber.equal(bn(100));
});
it('normalUser has no tokens back', async () => {
(await safeOwnerToken.balanceOf(normalUser))
.should.be.bignumber.equal(bn(0));
});
it('correctly sets normalUsers balance', async () => {
(await normalUserToken.balanceOf(normalUser))
.should.be.bignumber.equal(bn(75));
});
it('safeOwner has no tokens at normalUser tokens', async () => {
(await normalUserToken.balanceOf(safeOwner))
.should.be.bignumber.equal(bn(0));
});
it('organization has no safeOwner tokens', async () => {
(await safeOwnerToken.balanceOf(organization))
.should.be.bignumber.equal(bn(0));
});
it('organization has the correct balance of normalUser tokens', async () => {
(await normalUserToken.balanceOf(organization))
.should.be.bignumber.equal(bn(25));
});
it('cleans up the seen array', async () => {
const seen = await hub.getSeen();
seen.should.be.bignumber.equal(bn(0));
});
it('cleans up the validation mapping for safeOwner', async () => {
const validation = await hub.getValidation(safeOwner);
validation['0'].should.be.equal(false);
validation['1'].should.be.bignumber.equal(bn(0));
validation['2'].should.be.bignumber.equal(bn(0));
});
it('cleans up the validation mapping for normalUser', async () => {
const validation = await hub.getValidation(normalUser);
validation['0'].should.be.equal(false);
validation['1'].should.be.bignumber.equal(bn(0));
validation['2'].should.be.bignumber.equal(bn(0));
});
it('creates a HubTransfer event', async () => {
const logs = await hub.getPastEvents('HubTransfer', { fromBlock: 0, toBlock: 'latest' });
const event = expectEvent.inLogs(logs, 'HubTransfer', {
from: safeOwner,
to: organization,
});
return event.args.amount.should.be.bignumber.equal(bn(25));
});
});
describe('when sender is an organization', async () => {
const trustLimit = 50;
beforeEach(async () => {
await hub.signup({ from: safeOwner });
await hub.signup({ from: normalUser });
await hub.organizationSignup({ from: organization });
await hub.trust(safeOwner, trustLimit, { from: normalUser });
await hub.trust(normalUser, trustLimit, { from: safeOwner });
await hub.trust(normalUser, organizationTrustLimit, { from: organization });
const safeOwnertokenAddress = await hub.userToToken(safeOwner);
safeOwnerToken = await Token.at(safeOwnertokenAddress);
const normalUsertokenAddress = await hub.userToToken(normalUser);
normalUserToken = await Token.at(normalUsertokenAddress);
const amount = bn(25);
await normalUserToken.transfer(organization, amount, { from: normalUser });
await hub
.transferThrough(
[normalUser],
[organization],
[safeOwner],
[amount],
{ from: organization, gas: extraGas });
});
it('safeOwners tokens are not touched', async () => {
(await safeOwnerToken.balanceOf(safeOwner))
.should.be.bignumber.equal(bn(100));
});
it('correctly sets safeOwners balance of normalUsers token', async () => {
(await normalUserToken.balanceOf(safeOwner))
.should.be.bignumber.equal(bn(25));
});
it('normalUsers balance of own tokens are untouched', async () => {
(await normalUserToken.balanceOf(normalUser))
.should.be.bignumber.equal(bn(75));
});
it('normalUser has no tokens of safeOwner', async () => {
(await safeOwnerToken.balanceOf(normalUser))
.should.be.bignumber.equal(bn(0));
});
it('organization has no safeOwner tokens', async () => {
(await safeOwnerToken.balanceOf(organization))
.should.be.bignumber.equal(bn(0));
});
it('organization has no normalUser tokens', async () => {
(await normalUserToken.balanceOf(organization))
.should.be.bignumber.equal(bn(0));
});
it('cleans up the seen array', async () => {
const seen = await hub.getSeen();
seen.should.be.bignumber.equal(bn(0));
});
it('cleans up the validation mapping for safeOwner', async () => {
const validation = await hub.getValidation(safeOwner);
validation['0'].should.be.equal(false);
validation['1'].should.be.bignumber.equal(bn(0));
validation['2'].should.be.bignumber.equal(bn(0));
});
it('cleans up the validation mapping for normalUser', async () => {
const validation = await hub.getValidation(normalUser);
validation['0'].should.be.equal(false);
validation['1'].should.be.bignumber.equal(bn(0));
validation['2'].should.be.bignumber.equal(bn(0));
});
it('creates a HubTransfer event', async () => {
const logs = await hub.getPastEvents('HubTransfer', { fromBlock: 0, toBlock: 'latest' });
const event = expectEvent.inLogs(logs, 'HubTransfer', {
from: organization,
to: safeOwner,
});
return event.args.amount.should.be.bignumber.equal(bn(25));
});
});
});
});