-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathPostageStamp.test.ts
1436 lines (1182 loc) · 53.1 KB
/
PostageStamp.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
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
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { expect } from './util/chai';
import { ethers, deployments, getNamedAccounts, getUnnamedAccounts } from 'hardhat';
import { Contract } from 'ethers';
import { zeroAddress, mineNBlocks, computeBatchId, mintAndApprove, getBlockNumber } from './util/tools';
const { read, execute } = deployments;
interface Batch {
id?: string;
nonce: string;
initialPaymentPerChunk: number;
depth: number;
immutable: boolean;
bucketDepth: number;
}
let stamper: string;
let deployer: string;
let oracle: string;
let others: string[];
before(async function () {
const namedAccounts = await getNamedAccounts();
deployer = namedAccounts.deployer;
stamper = namedAccounts.stamper;
oracle = namedAccounts.oracle;
others = await getUnnamedAccounts();
});
async function setPrice(price: number) {
return await (await ethers.getContract('PostageStamp', oracle)).setPrice(price);
}
const maxInt256 = 0xffff; //js can't handle the full maxInt256 value
const errors = {
remainingBalance: {
doesNotExist: 'BatchDoesNotExist()',
},
erc20: {
exceedsBalance: 'ERC20: insufficient allowance',
},
createBatch: {
invalidDepth: 'InvalidDepth()',
alreadyExists: 'BatchExists()',
paused: 'Pausable: paused',
},
firstBatchId: {
noneExist: 'NoBatchesExist()',
},
};
describe('PostageStamp', function () {
let minimumPrice: number;
describe('when deploying contract', function () {
beforeEach(async function () {
await deployments.fixture();
const priceOracle = await ethers.getContract('PriceOracle');
minimumPrice = await priceOracle.minimumPrice();
});
it('should have minimum bucket depth set to 16', async function () {
const postageStamp = await ethers.getContract('PostageStamp');
expect(await postageStamp.minimumBucketDepth()).to.be.eq(16);
});
it('should deploy PostageStamp', async function () {
const postageStamp = await ethers.getContract('PostageStamp');
expect(postageStamp.address).to.be.properAddress;
});
it('should set the correct token', async function () {
const postageStamp = await ethers.getContract('PostageStamp');
const token = await ethers.getContract('TestToken');
expect(await postageStamp.bzzToken()).to.be.eq(token.address);
});
it('should assign the admin role', async function () {
const postageStamp = await ethers.getContract('PostageStamp');
const adminRole = await postageStamp.DEFAULT_ADMIN_ROLE();
expect(await postageStamp.hasRole(adminRole, deployer)).to.be.true;
});
it('should assign the pauser role', async function () {
const postageStamp = await ethers.getContract('PostageStamp');
const pauserRole = await postageStamp.DEFAULT_ADMIN_ROLE();
expect(await postageStamp.hasRole(pauserRole, deployer)).to.be.true;
});
});
describe('with deployed contract', async function () {
let postageStampStamper: Contract, token: Contract, priceOracle: Contract;
let batch: Batch;
let batchSize: number, transferAmount: number;
let minimumPrice: number;
let price0: number;
let setPrice0Block: number;
beforeEach(async function () {
await deployments.fixture();
const postageStamp = await ethers.getContract('PostageStamp', deployer);
await postageStamp.setMinimumValidityBlocks(0);
const priceOracle = await ethers.getContract('PriceOracle');
minimumPrice = await priceOracle.minimumPrice();
price0 = minimumPrice;
const priceOracleRole = await read('PostageStamp', 'PRICE_ORACLE_ROLE');
await execute('PostageStamp', { from: deployer }, 'grantRole', priceOracleRole, oracle);
});
describe('when creating a batch', function () {
beforeEach(async function () {
postageStampStamper = await ethers.getContract('PostageStamp', stamper);
token = await ethers.getContract('TestToken', deployer);
priceOracle = await ethers.getContract('PriceOracle', deployer);
setPrice0Block = await getBlockNumber();
await priceOracle.setPrice(price0);
batch = {
nonce: '0x000000000000000000000000000000000000000000000000000000000000abcd',
initialPaymentPerChunk: price0 * 10, //good for ten blocks at minimum price
depth: 17,
immutable: false,
bucketDepth: 16,
};
batchSize = 2 ** batch.depth;
transferAmount = batch.initialPaymentPerChunk * batchSize;
batch.id = computeBatchId(stamper, batch.nonce);
await mintAndApprove(deployer, stamper, postageStampStamper.address, transferAmount.toString());
});
it('should fire the BatchCreated event', async function () {
const blocksElapsed = (await getBlockNumber()) - setPrice0Block;
const expectedNormalisedBalance = batch.initialPaymentPerChunk + blocksElapsed * price0;
await expect(
postageStampStamper.createBatch(
stamper,
batch.initialPaymentPerChunk,
batch.depth,
batch.bucketDepth,
batch.nonce,
batch.immutable
)
)
.to.emit(postageStampStamper, 'BatchCreated')
.withArgs(
batch.id,
transferAmount,
expectedNormalisedBalance,
stamper,
batch.depth,
batch.bucketDepth,
batch.immutable
);
});
it('should store the batch', async function () {
const blocksElapsed = (await getBlockNumber()) - setPrice0Block;
const expectedNormalisedBalance = batch.initialPaymentPerChunk + blocksElapsed * price0;
await postageStampStamper.createBatch(
stamper,
batch.initialPaymentPerChunk,
batch.depth,
batch.bucketDepth,
batch.nonce,
batch.immutable
);
const stamp = await postageStampStamper.batches(batch.id);
expect(stamp[0]).to.equal(stamper);
expect(stamp[1]).to.equal(batch.depth);
expect(stamp[2]).to.equal(batch.bucketDepth);
expect(stamp[3]).to.equal(batch.immutable);
expect(stamp[4]).to.equal(expectedNormalisedBalance);
});
it('should report the correct remaining balance', async function () {
await postageStampStamper.createBatch(
stamper,
batch.initialPaymentPerChunk,
batch.depth,
batch.bucketDepth,
batch.nonce,
batch.immutable
);
const buyStampBlock = await getBlockNumber();
const normalisedBalance0 = parseInt(await postageStampStamper.remainingBalance(batch.id));
const expectedNormalisedBalance0 =
batch.initialPaymentPerChunk - ((await getBlockNumber()) - buyStampBlock) * price0;
expect(normalisedBalance0).to.be.equal(expectedNormalisedBalance0);
await mineNBlocks(1);
const normalisedBalance1 = parseInt(await postageStampStamper.remainingBalance(batch.id));
const expectedNormalisedBalance1 =
batch.initialPaymentPerChunk - ((await getBlockNumber()) - buyStampBlock) * price0;
expect(normalisedBalance1).to.be.equal(expectedNormalisedBalance1);
await mineNBlocks(12);
const expectedNormalisedBalance2 =
batch.initialPaymentPerChunk - ((await getBlockNumber()) - buyStampBlock) * price0;
const normalisedBalance2 = await postageStampStamper.remainingBalance(batch.id);
expect(expectedNormalisedBalance2).to.be.lessThan(0);
expect(normalisedBalance2).to.be.equal(0);
await postageStampStamper.expireLimited(maxInt256);
await expect(postageStampStamper.remainingBalance(batch.id)).to.be.revertedWith(
errors.remainingBalance.doesNotExist
);
});
it('should keep batches ordered by normalisedBalance', async function () {
const initialPaymentPerChunk0 = 3300;
const initialPaymentPerChunk1 = 1100;
const initialPaymentPerChunk2 = 2200;
const nonce0 = '0x0000000000000000000000000000000000000000000000000000000000001234';
await postageStampStamper.createBatch(
stamper,
initialPaymentPerChunk0,
batch.depth,
batch.bucketDepth,
nonce0,
batch.immutable
);
const batch0 = computeBatchId(stamper, nonce0);
expect(batch0).equal(await postageStampStamper.firstBatchId());
const blocksElapsed = (await getBlockNumber()) - setPrice0Block;
const expectedNormalisedBalance1 = initialPaymentPerChunk1 + blocksElapsed * price0;
const nonce1 = '0x0000000000000000000000000000000000000000000000000000000000001235';
await postageStampStamper.createBatch(
stamper,
initialPaymentPerChunk1,
batch.depth,
batch.bucketDepth,
nonce1,
batch.immutable
);
const batch1 = computeBatchId(stamper, nonce1);
expect(batch1).equal(await postageStampStamper.firstBatchId());
const blocksElapsed2 = (await getBlockNumber()) - setPrice0Block;
const nonce2 = '0x0000000000000000000000000000000000000000000000000000000000001236';
await postageStampStamper.createBatch(
stamper,
initialPaymentPerChunk2,
batch.depth,
batch.bucketDepth,
nonce2,
batch.immutable
);
const batch2 = computeBatchId(stamper, nonce2);
expect(batch2).equal(await postageStampStamper.firstBatchId());
expect(batch1).not.equal(await postageStampStamper.firstBatchId());
const stamp = await postageStampStamper.batches(batch2);
const expectedNormalisedBalance2 = initialPaymentPerChunk2 + blocksElapsed2 * price0;
expect(stamp[0]).to.equal(stamper);
expect(stamp[1]).to.equal(batch.depth);
expect(stamp[2]).to.equal(batch.bucketDepth);
expect(stamp[3]).to.equal(batch.immutable);
expect(stamp[4]).to.equal(expectedNormalisedBalance2);
});
it('should transfer the token', async function () {
await postageStampStamper.createBatch(
stamper,
batch.initialPaymentPerChunk,
batch.depth,
batch.bucketDepth,
batch.nonce,
batch.immutable
);
expect(await token.balanceOf(stamper)).to.equal(0);
expect(await token.balanceOf(postageStampStamper.address)).to.equal(transferAmount);
});
it('should not create batch if insufficient funds', async function () {
await expect(
postageStampStamper.createBatch(
stamper,
batch.initialPaymentPerChunk + 1,
batch.depth,
batch.bucketDepth,
batch.nonce,
batch.immutable
)
).to.be.revertedWith(errors.erc20.exceedsBalance);
});
it('should not allow zero as bucket depth', async function () {
await expect(
postageStampStamper.createBatch(
stamper,
batch.initialPaymentPerChunk,
batch.depth,
0,
batch.nonce,
batch.immutable
)
).to.be.revertedWith(errors.createBatch.invalidDepth);
});
it('should not allow bucket depth larger than depth', async function () {
await expect(
postageStampStamper.createBatch(
stamper,
batch.initialPaymentPerChunk,
batch.depth,
batch.depth + 1,
batch.nonce,
batch.immutable
)
).to.be.revertedWith(errors.createBatch.invalidDepth);
});
it('should not allow bucket depth equal to depth', async function () {
await expect(
postageStampStamper.createBatch(
stamper,
batch.initialPaymentPerChunk,
batch.depth,
batch.depth,
batch.nonce,
batch.immutable
)
).to.be.revertedWith(errors.createBatch.invalidDepth);
});
it('should not allow duplicate batch', async function () {
await postageStampStamper.createBatch(
stamper,
batch.initialPaymentPerChunk,
batch.depth,
batch.bucketDepth,
batch.nonce,
batch.immutable
);
await expect(
postageStampStamper.createBatch(
stamper,
batch.initialPaymentPerChunk,
batch.depth,
batch.bucketDepth,
batch.nonce,
batch.immutable
)
).to.be.revertedWith(errors.createBatch.alreadyExists);
});
it('should correctly return if batches are empty', async function () {
const initialPaymentPerChunk0 = 2048;
const blocksElapsed = (await getBlockNumber()) - setPrice0Block;
const expectedNormalisedBalance = initialPaymentPerChunk0 + blocksElapsed * price0;
await postageStampStamper.createBatch(
stamper,
initialPaymentPerChunk0,
batch.depth,
batch.bucketDepth,
batch.nonce,
batch.immutable
);
const stamp = await postageStampStamper.batches(batch.id);
expect(stamp[0]).to.equal(stamper);
expect(stamp[1]).to.equal(batch.depth);
expect(stamp[2]).to.equal(batch.bucketDepth);
expect(stamp[3]).to.equal(batch.immutable);
expect(stamp[4]).to.equal(expectedNormalisedBalance);
expect(await postageStampStamper.isBatchesTreeEmpty()).equal(false);
mineNBlocks(10);
await postageStampStamper.expireLimited(maxInt256);
expect(await postageStampStamper.isBatchesTreeEmpty()).equal(true);
});
it('should not allow batch creation when paused', async function () {
const postageStamp = await ethers.getContract('PostageStamp', deployer);
await postageStamp.pause();
await expect(
postageStamp.createBatch(stamper, 0, batch.depth, batch.bucketDepth, batch.nonce, batch.immutable)
).to.be.revertedWith(errors.createBatch.paused);
});
it('should allow batch creation when unpaused', async function () {
const postage_p = await ethers.getContract('PostageStamp', deployer);
await postage_p.pause();
await expect(
postageStampStamper.createBatch(
stamper,
batch.initialPaymentPerChunk,
batch.depth,
batch.bucketDepth,
batch.nonce,
batch.immutable
)
).to.be.revertedWith(errors.createBatch.paused);
await postage_p.unPause();
const blocksElapsed = (await getBlockNumber()) - setPrice0Block;
const expectedNormalisedBalance = batch.initialPaymentPerChunk + blocksElapsed * price0;
await postageStampStamper.createBatch(
stamper,
batch.initialPaymentPerChunk,
batch.depth,
batch.bucketDepth,
batch.nonce,
batch.immutable
);
const stamp = await postageStampStamper.batches(batch.id);
expect(stamp[0]).to.equal(stamper);
expect(stamp[1]).to.equal(batch.depth);
expect(stamp[2]).to.equal(batch.bucketDepth);
expect(stamp[3]).to.equal(batch.immutable);
expect(stamp[4]).to.equal(expectedNormalisedBalance);
});
it('should delete expired batches', async function () {
const initialPaymentPerChunk0 = price0 * 8;
const initialPaymentPerChunk1 = price0 * 4;
const initialPaymentPerChunk2 = price0 * 16;
const transferAmount0 = initialPaymentPerChunk0 * 2 ** batch.depth;
await mintAndApprove(deployer, stamper, postageStampStamper.address, transferAmount0.toString());
const nonce0 = '0x0000000000000000000000000000000000000000000000000000000000001234';
await postageStampStamper.createBatch(
stamper,
initialPaymentPerChunk0,
batch.depth,
batch.bucketDepth,
nonce0,
batch.immutable
);
const batch0 = computeBatchId(stamper, nonce0);
expect(await postageStampStamper.firstBatchId()).to.equal(batch0);
const transferAmount1 = initialPaymentPerChunk1 * 2 ** batch.depth;
await mintAndApprove(deployer, stamper, postageStampStamper.address, transferAmount1.toString());
const nonce1 = '0x0000000000000000000000000000000000000000000000000000000000001235';
await postageStampStamper.createBatch(
stamper,
initialPaymentPerChunk1,
batch.depth,
batch.bucketDepth,
nonce1,
batch.immutable
);
const batch1 = computeBatchId(stamper, nonce1);
expect(await postageStampStamper.firstBatchId()).to.equal(batch1);
const transferAmount2 = initialPaymentPerChunk2 * 2 ** batch.depth;
await mintAndApprove(deployer, stamper, postageStampStamper.address, transferAmount2.toString());
const nonce2 = '0x0000000000000000000000000000000000000000000000000000000000001236';
await postageStampStamper.createBatch(
stamper,
initialPaymentPerChunk2,
batch.depth,
batch.bucketDepth,
nonce2,
batch.immutable
);
const batch2 = computeBatchId(stamper, nonce2);
expect(await postageStampStamper.firstBatchId()).to.equal(batch1);
expect(await postageStampStamper.firstBatchId()).not.to.equal(batch2);
await mineNBlocks(1);
expect(await postageStampStamper.firstBatchId()).to.equal(batch1);
expect(await postageStampStamper.firstBatchId()).not.to.equal(batch2);
await postageStampStamper.expireLimited(maxInt256);
expect(batch0).not.equal(await postageStampStamper.firstBatchId());
expect(batch1).not.equal(await postageStampStamper.firstBatchId());
expect(batch2).equal(await postageStampStamper.firstBatchId());
});
it('should calculate the correct remaining balances and update the pot', async function () {
const blocksBeforeExpired0 = 8;
const initialPaymentPerChunk0 = price0 * blocksBeforeExpired0;
const blocksBeforeExpired1 = 4;
const initialPaymentPerChunk1 = price0 * blocksBeforeExpired1;
const blocksBeforeExpired2 = 16;
const initialPaymentPerChunk2 = price0 * blocksBeforeExpired2;
const transferAmount0 = initialPaymentPerChunk0 * 2 ** batch.depth;
await mintAndApprove(deployer, stamper, postageStampStamper.address, transferAmount0.toString());
const nonce0 = '0x0000000000000000000000000000000000000000000000000000000000001234';
await postageStampStamper.createBatch(
stamper,
initialPaymentPerChunk0,
batch.depth,
batch.bucketDepth,
nonce0,
batch.immutable
);
const buyStamp0Block = await getBlockNumber();
expect(await postageStampStamper.pot()).equal(0);
await postageStampStamper.expireLimited(maxInt256);
const blocksElapsed0Stamp0 = (await getBlockNumber()) - buyStamp0Block;
const blocksCharged0Stamp0 =
blocksBeforeExpired0 - blocksElapsed0Stamp0 < 0 ? blocksBeforeExpired0 : blocksElapsed0Stamp0;
const outpayment0Stamp0 = price0 * blocksCharged0Stamp0 * 2 ** batch.depth;
expect(await postageStampStamper.pot()).equal(outpayment0Stamp0);
const transferAmount1 = initialPaymentPerChunk1 * 2 ** batch.depth;
await mintAndApprove(deployer, stamper, postageStampStamper.address, transferAmount1.toString());
const nonce1 = '0x0000000000000000000000000000000000000000000000000000000000001235';
await postageStampStamper.createBatch(
stamper,
initialPaymentPerChunk1,
batch.depth,
batch.bucketDepth,
nonce1,
batch.immutable
);
const buyStamp1Block = await getBlockNumber();
await postageStampStamper.expireLimited(maxInt256);
const blocksElapsed1Stamp0 = (await getBlockNumber()) - buyStamp0Block;
const blocksCharged1Stamp0 =
blocksBeforeExpired0 - blocksElapsed1Stamp0 < 0 ? blocksBeforeExpired0 : blocksElapsed1Stamp0;
const outpayment1Stamp0 = price0 * blocksCharged1Stamp0 * 2 ** batch.depth;
const blocksElapsed1Stamp1 = (await getBlockNumber()) - buyStamp1Block;
const blocksCharged1Stamp1 =
blocksBeforeExpired1 - blocksElapsed1Stamp1 < 0 ? blocksBeforeExpired1 : blocksElapsed1Stamp1;
const outpayment1Stamp1 = price0 * blocksCharged1Stamp1 * 2 ** batch.depth;
const expectedPot1 = outpayment1Stamp0 + outpayment1Stamp1;
expect(await postageStampStamper.pot()).equal(expectedPot1);
const transferAmount2 = initialPaymentPerChunk2 * 2 ** batch.depth;
await mintAndApprove(deployer, stamper, postageStampStamper.address, transferAmount2.toString());
const nonce2 = '0x0000000000000000000000000000000000000000000000000000000000001236';
await postageStampStamper.createBatch(
stamper,
initialPaymentPerChunk2,
batch.depth,
batch.bucketDepth,
nonce2,
batch.immutable
);
const buyStamp2Block = await getBlockNumber();
await postageStampStamper.expireLimited(maxInt256);
const blocksElapsed2Stamp0 = (await getBlockNumber()) - buyStamp0Block;
const blocksCharged2Stamp0 =
blocksBeforeExpired0 - blocksElapsed2Stamp0 < 0 ? blocksBeforeExpired0 : blocksElapsed2Stamp0;
const outpayment2Stamp0 = price0 * blocksCharged2Stamp0 * 2 ** batch.depth;
const blocksElapsed2Stamp1 = (await getBlockNumber()) - buyStamp1Block;
const blocksCharged2Stamp1 =
blocksBeforeExpired1 - blocksElapsed2Stamp1 < 0 ? blocksBeforeExpired1 : blocksElapsed2Stamp1;
const outpayment2Stamp1 = price0 * blocksCharged2Stamp1 * 2 ** batch.depth;
const blocksElapsed2Stamp2 = (await getBlockNumber()) - buyStamp2Block;
const blocksCharged2Stamp2 =
blocksBeforeExpired2 - blocksElapsed2Stamp2 < 0 ? blocksBeforeExpired2 : blocksElapsed2Stamp2;
const outpayment2Stamp2 = price0 * blocksCharged2Stamp2 * 2 ** batch.depth;
const expectedPot2 = outpayment2Stamp0 + outpayment2Stamp1 + outpayment2Stamp2;
expect(await postageStampStamper.pot()).equal(expectedPot2);
await mineNBlocks(1);
await postageStampStamper.expireLimited(maxInt256);
const blocksElapsed3Stamp0 = (await getBlockNumber()) - buyStamp0Block;
const blocksCharged3Stamp0 =
blocksBeforeExpired0 - blocksElapsed3Stamp0 < 0 ? blocksBeforeExpired0 : blocksElapsed3Stamp0;
const outpayment3Stamp0 = price0 * blocksCharged3Stamp0 * 2 ** batch.depth;
const blocksElapsed3Stamp1 = (await getBlockNumber()) - buyStamp1Block;
const blocksCharged3Stamp1 =
blocksBeforeExpired1 - blocksElapsed3Stamp1 < 0 ? blocksBeforeExpired1 : blocksElapsed3Stamp1;
const outpayment3Stamp1 = price0 * blocksCharged3Stamp1 * 2 ** batch.depth;
const blocksElapsed3Stamp2 = (await getBlockNumber()) - buyStamp2Block;
const blocksCharged3Stamp2 =
blocksBeforeExpired2 - blocksElapsed3Stamp2 < 0 ? blocksBeforeExpired2 : blocksElapsed3Stamp2;
const outpayment3Stamp2 = price0 * blocksCharged3Stamp2 * 2 ** batch.depth;
const expectedPot3 = outpayment3Stamp0 + outpayment3Stamp1 + outpayment3Stamp2;
expect(await postageStampStamper.pot()).equal(expectedPot3);
});
});
describe('when topping up a batch', function () {
let postageStamp: Contract, token: Contract, priceOracle: Contract;
let batch: Batch;
let batchSize: number, transferAmount: number;
let setPrice0Block: number, buyStampBlock: number;
let topupAmountPerChunk: number;
const initialBatchBlocks = 10;
beforeEach(async function () {
postageStamp = await ethers.getContract('PostageStamp', stamper);
token = await ethers.getContract('TestToken', deployer);
priceOracle = await ethers.getContract('PriceOracle', deployer);
topupAmountPerChunk = minimumPrice;
setPrice0Block = await getBlockNumber();
await priceOracle.setPrice(price0);
batch = {
nonce: '0x000000000000000000000000000000000000000000000000000000000000abce',
initialPaymentPerChunk: price0 * initialBatchBlocks,
depth: 17,
immutable: false,
bucketDepth: 16,
};
batchSize = 2 ** batch.depth;
transferAmount = batch.initialPaymentPerChunk * batchSize;
transferAmount += topupAmountPerChunk * batchSize;
batch.id = computeBatchId(stamper, batch.nonce);
await mintAndApprove(deployer, stamper, postageStamp.address, transferAmount.toString());
buyStampBlock = await getBlockNumber();
await postageStamp.createBatch(
stamper,
batch.initialPaymentPerChunk,
batch.depth,
batch.bucketDepth,
batch.nonce,
batch.immutable
);
});
it('should fire the BatchTopUp event', async function () {
const expectedNormalisedBalance =
price0 * (buyStampBlock - setPrice0Block) + price0 * initialBatchBlocks + topupAmountPerChunk;
await expect(postageStamp.topUp(batch.id, topupAmountPerChunk))
.to.emit(postageStamp, 'BatchTopUp')
.withArgs(batch.id, topupAmountPerChunk * batchSize, expectedNormalisedBalance);
});
it('should update the normalised balance', async function () {
const expectedNormalisedBalance =
price0 * (buyStampBlock - setPrice0Block) + price0 * initialBatchBlocks + topupAmountPerChunk;
await postageStamp.topUp(batch.id, topupAmountPerChunk);
const stamp = await postageStamp.batches(batch.id);
expect(stamp.normalisedBalance).to.equal(expectedNormalisedBalance);
});
it('should transfer the token', async function () {
await postageStamp.topUp(batch.id, topupAmountPerChunk);
expect(await token.balanceOf(stamper)).to.equal(0);
expect(await token.balanceOf(postageStamp.address)).to.equal(
(batch.initialPaymentPerChunk + topupAmountPerChunk) * batchSize
);
});
it('should not top up non-existing batches', async function () {
const nonExistingBatchId = computeBatchId(deployer, batch.nonce);
await expect(postageStamp.topUp(nonExistingBatchId, topupAmountPerChunk)).to.be.revertedWith(
'BatchDoesNotExist()'
);
});
it('should not top up with insufficient funds', async function () {
await expect(postageStamp.topUp(batch.id, topupAmountPerChunk + 1)).to.be.revertedWith(
errors.erc20.exceedsBalance
);
});
it('should not top up expired batches', async function () {
await mineNBlocks(initialBatchBlocks + 10);
await expect(postageStamp.topUp(batch.id, topupAmountPerChunk)).to.be.revertedWith('BatchExpired()');
});
it('should not top up when paused', async function () {
const postageStamp = await ethers.getContract('PostageStamp', deployer);
await postageStamp.pause();
await expect(postageStamp.topUp(batch.id, topupAmountPerChunk)).to.be.revertedWith('Pausable: paused');
});
it('should keep batches ordered by normalisedBalance', async function () {
const batch2Blocks = 20;
const batch2 = {
nonce: '0x000000000000000000000000000000000000000000000000000000000000abc1',
initialPaymentPerChunk: price0 * batch2Blocks,
depth: 17,
immutable: false,
bucketDepth: 16,
};
const batch2TransferAmount = price0 * batch2Blocks * 2 ** batch2.depth;
await mintAndApprove(deployer, stamper, postageStamp.address, batch2TransferAmount.toString());
await postageStamp.createBatch(
stamper,
batch2.initialPaymentPerChunk,
batch2.depth,
batch2.bucketDepth,
batch2.nonce,
batch2.immutable
);
const batch2Id = computeBatchId(stamper, batch2.nonce);
expect(await postageStamp.firstBatchId()).equal(batch.id);
const batch0TopUpBlocks = 40;
const topUpAmountBatch0 = price0 * batch0TopUpBlocks;
const batch2TopUpTransferAmount = price0 * topUpAmountBatch0 * 2 ** batch2.depth;
await mintAndApprove(deployer, stamper, postageStamp.address, batch2TopUpTransferAmount.toString());
await postageStamp.topUp(batch.id, topUpAmountBatch0);
expect(await postageStamp.firstBatchId()).equal(batch2Id);
});
});
describe('when increasing the depth', function () {
let postageStamp: Contract, priceOracle: Contract;
let batch: Batch;
let batchSize: number, transferAmount: number;
let setPrice0Block: number, buyStampBlock: number;
const initialBatchBlocks = 100;
const newDepth = 18;
let depthChange: number;
beforeEach(async function () {
postageStamp = await ethers.getContract('PostageStamp', stamper);
priceOracle = await ethers.getContract('PriceOracle', deployer);
setPrice0Block = await getBlockNumber();
await priceOracle.setPrice(price0);
batch = {
nonce: '0x000000000000000000000000000000000000000000000000000000000000abce',
initialPaymentPerChunk: price0 * initialBatchBlocks,
depth: 17,
immutable: false,
bucketDepth: 16,
};
depthChange = newDepth - batch.depth;
batchSize = 2 ** batch.depth;
transferAmount = batch.initialPaymentPerChunk * batchSize;
batch.id = computeBatchId(stamper, batch.nonce);
await mintAndApprove(deployer, stamper, postageStamp.address, transferAmount.toString());
buyStampBlock = await getBlockNumber();
await postageStamp.createBatch(
stamper,
batch.initialPaymentPerChunk,
batch.depth,
batch.bucketDepth,
batch.nonce,
batch.immutable
);
});
it('should fire the BatchDepthIncrease event', async function () {
const depthChange = newDepth - batch.depth;
// the expected normalised balance should be changed - the total amount remaining should be multiplied by the new batch size over the old batch size
// so the total expected normalised balance should be the normalised balance up to that point, plus the future normalised balance adjusted by this factor
const expectedNormalisedBalance = batch.initialPaymentPerChunk + (buyStampBlock - setPrice0Block) * price0;
const stamp = await postageStamp.batches(batch.id);
expect(stamp.normalisedBalance.toString()).to.be.equal(expectedNormalisedBalance.toString());
const remainingBalanceNextBlock = parseInt(await postageStamp.remainingBalance(batch.id)) - price0 * 1;
const currentTotalOutPaymentNextBlock = parseInt(await postageStamp.currentTotalOutPayment()) + price0 * 1;
const expectedNormalisedBalanceAfter =
currentTotalOutPaymentNextBlock + remainingBalanceNextBlock / 2 ** depthChange;
await expect(postageStamp.increaseDepth(batch.id, newDepth))
.to.emit(postageStamp, 'BatchDepthIncrease')
.withArgs(batch.id, newDepth, expectedNormalisedBalanceAfter.toString());
});
it('should update the stamp data', async function () {
const depthChange = newDepth - batch.depth;
const remainingBalanceNextBlock = parseInt(await postageStamp.remainingBalance(batch.id)) - price0 * 1;
const currentTotalOutPaymentNextBlock = parseInt(await postageStamp.currentTotalOutPayment()) + price0 * 1;
const expectedNormalisedBalanceAfter =
currentTotalOutPaymentNextBlock + remainingBalanceNextBlock / 2 ** depthChange;
await postageStamp.increaseDepth(batch.id, newDepth);
const stamp = await postageStamp.batches(batch.id);
expect(stamp.owner).to.equal(stamper);
expect(stamp.depth).to.equal(newDepth);
expect(stamp.immutableFlag).to.equal(batch.immutable);
expect(stamp.normalisedBalance).to.equal(expectedNormalisedBalanceAfter);
});
it('should not allow other accounts to increase depth', async function () {
const postageStamp = await ethers.getContract('PostageStamp', others[0]);
await expect(postageStamp.increaseDepth(batch.id, newDepth)).to.be.revertedWith('NotBatchOwner()');
});
it('should not allow decreasing the depth', async function () {
await expect(postageStamp.increaseDepth(batch.id, batch.depth - 1)).to.be.revertedWith('DepthNotIncreasing()');
});
it('should not allow the same depth', async function () {
await expect(postageStamp.increaseDepth(batch.id, batch.depth)).to.be.revertedWith('DepthNotIncreasing()');
});
it('should not increase depth of expired batches', async function () {
// one price applied so far, this ensures the currentTotalOutpayment will be exactly the batch value when increaseDepth is called
await mineNBlocks(100);
await expect(postageStamp.increaseDepth(batch.id, newDepth)).to.be.revertedWith('BatchExpired()');
});
it('should not increase depth when paused', async function () {
const postageStamp = await ethers.getContract('PostageStamp', deployer);
await postageStamp.pause();
await expect(postageStamp.increaseDepth(batch.id, newDepth)).to.be.revertedWith('Pausable: paused');
});
it('should compute correct balance if outpayments changed since creation', async function () {
const newPrice = 2 * minimumPrice;
await priceOracle.setPrice(newPrice);
const remainingBalanceNextBlock = parseInt(await postageStamp.remainingBalance(batch.id)) - newPrice * 1;
const currentTotalOutPaymentNextBlock = parseInt(await postageStamp.currentTotalOutPayment()) + newPrice * 1;
const expectedNormalisedBalanceAfter =
currentTotalOutPaymentNextBlock + remainingBalanceNextBlock / 2 ** depthChange;
await expect(postageStamp.increaseDepth(batch.id, newDepth))
.to.emit(postageStamp, 'BatchDepthIncrease')
.withArgs(batch.id, newDepth, expectedNormalisedBalanceAfter);
});
it('should keep batches ordered by normalisedBalance', async function () {
const batch2NewDepth = 20;
const batch2Blocks = 200;
const batch2 = {
nonce: '0x000000000000000000000000000000000000000000000000000000000000abc1',
initialPaymentPerChunk: price0 * batch2Blocks,
depth: 17,
immutable: false,
bucketDepth: 16,
};
const batch2TransferAmount = price0 * batch2Blocks * 2 ** batch2.depth;
await mintAndApprove(deployer, stamper, postageStamp.address, batch2TransferAmount.toString());
await postageStamp.createBatch(
stamper,
batch2.initialPaymentPerChunk,
batch2.depth,
batch2.bucketDepth,
batch2.nonce,
batch2.immutable
);
const batch2Id = computeBatchId(stamper, batch2.nonce);
expect(await postageStamp.firstBatchId()).equal(batch.id);
await postageStamp.increaseDepth(batch2Id, batch2NewDepth);
expect(await postageStamp.firstBatchId()).equal(batch2Id);
});
it('should delete expired batches', async function () {
const batch2NewDepth = 24;
const batch2NewDepth2 = 36;
const batch2Blocks = 200;
const batch2 = {
nonce: '0x000000000000000000000000000000000000000000000000000000000000abc1',
initialPaymentPerChunk: price0 * batch2Blocks,
depth: 17,
immutable: false,
bucketDepth: 16,
};
const batch2TransferAmount = price0 * batch2Blocks * 2 ** batch2.depth;
await mintAndApprove(deployer, stamper, postageStamp.address, batch2TransferAmount.toString());
await postageStamp.createBatch(
stamper,
batch2.initialPaymentPerChunk,
batch2.depth,
batch2.bucketDepth,
batch2.nonce,
batch2.immutable
);
const batch2Id = computeBatchId(stamper, batch2.nonce);
expect(await postageStamp.firstBatchId()).equal(batch.id);
await postageStamp.increaseDepth(batch2Id, batch2NewDepth);
expect(await postageStamp.firstBatchId()).equal(batch2Id);
await postageStamp.increaseDepth(batch2Id, batch2NewDepth2);
await postageStamp.expireLimited(maxInt256);
expect(await postageStamp.firstBatchId()).equal(batch.id);
});
});
describe('when setting the price', function () {
it('should increase the outpayment if called by oracle', async function () {
const postageStamp_o = await ethers.getContract('PostageStamp', oracle);
const price1 = 2048;
await postageStamp_o.setPrice(price1);
await mineNBlocks(1);
expect(await postageStamp_o.currentTotalOutPayment()).to.be.eq(price1);
await mineNBlocks(1);
expect(await postageStamp_o.currentTotalOutPayment()).to.be.eq(2 * price1);
const price2 = 4096;
await postageStamp_o.setPrice(price2);
expect(await postageStamp_o.currentTotalOutPayment()).to.be.eq(3 * price1);
await mineNBlocks(1);
expect(await postageStamp_o.currentTotalOutPayment()).to.be.eq(3 * price1 + 1 * price2);
await mineNBlocks(1);
expect(await postageStamp_o.currentTotalOutPayment()).to.be.eq(3 * price1 + 2 * price2);
});
it('should emit event if called by oracle', async function () {
const postageStamp = await ethers.getContract('PostageStamp', oracle);
const price = 2048;
await expect(postageStamp.setPrice(price)).to.emit(postageStamp, 'PriceUpdate').withArgs(price);
});
it('should revert if not called by oracle', async function () {
const postageStamp = await ethers.getContract('PostageStamp', deployer);
await expect(postageStamp.setPrice(100)).to.be.revertedWith('PriceOracleOnly()');
});
});
describe('when pausing', function () {
it('should not allow anybody but the pauser to pause', async function () {
const postageStamp = await ethers.getContract('PostageStamp', stamper);
await expect(postageStamp.pause()).to.be.revertedWith('OnlyPauser()');
});
});
describe('when unpausing', function () {
it('should unpause when pause and then unpause', async function () {
const postageStamp = await ethers.getContract('PostageStamp', deployer);
await postageStamp.pause();
await postageStamp.unPause();
expect(await postageStamp.paused()).to.be.false;
});