-
Notifications
You must be signed in to change notification settings - Fork 7
/
Staker.test.1.ts
3070 lines (2401 loc) · 143 KB
/
Staker.test.1.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 { ethers } from 'hardhat';
import { expect } from 'chai';
import { loadFixture, time } from '@nomicfoundation/hardhat-network-helpers';
export async function setupFixture() {
const [anyone, admin0, admin1, user0, user1, user2] = await ethers.getSigners();
const PositionMetadata = await ethers.getContractFactory('PositionMetadata');
const positionMetadata = await PositionMetadata.deploy();
const Staker = await ethers.getContractFactory('Staker');
const staker = await Staker.deploy(await positionMetadata.getAddress());
const ERC20 = await ethers.getContractFactory('MockERC20');
const erc20 = await ERC20.deploy();
const ERC721 = await ethers.getContractFactory('MockERC721');
const erc721 = await ERC721.deploy();
const ERC1155 = await ethers.getContractFactory('MockERC1155');
const erc1155 = await ERC1155.deploy();
return { staker, erc20, erc721, erc1155, anyone, admin0, admin1, user0, user1, user2 };
}
export function setupStakingPoolsFixture(transferable: boolean, lockupSeconds: number, cooldownSeconds: number) {
async function fixture() {
const { staker, erc20, erc721, erc1155, anyone, admin0, admin1, user0, user1, user2 } =
await loadFixture(setupFixture);
const stakerWithAdmin0 = staker.connect(admin0);
await stakerWithAdmin0.createPool(
await stakerWithAdmin0.NATIVE_TOKEN_TYPE(),
ethers.ZeroAddress,
0,
transferable,
lockupSeconds,
cooldownSeconds,
admin0
);
const nativePoolID = (await staker.TotalPools()) - BigInt(1);
await stakerWithAdmin0.createPool(
await stakerWithAdmin0.ERC20_TOKEN_TYPE(),
await erc20.getAddress(),
0,
transferable,
lockupSeconds,
cooldownSeconds,
admin0
);
const erc20PoolID = (await staker.TotalPools()) - BigInt(1);
await stakerWithAdmin0.createPool(
await stakerWithAdmin0.ERC721_TOKEN_TYPE(),
await erc721.getAddress(),
0,
transferable,
lockupSeconds,
cooldownSeconds,
admin0
);
const erc721PoolID = (await staker.TotalPools()) - BigInt(1);
const erc1155TokenID = 1;
await stakerWithAdmin0.createPool(
await stakerWithAdmin0.ERC1155_TOKEN_TYPE(),
await erc1155.getAddress(),
erc1155TokenID,
transferable,
lockupSeconds,
cooldownSeconds,
admin0
);
const erc1155PoolID = (await staker.TotalPools()) - BigInt(1);
return {
staker,
erc20,
erc721,
erc1155,
anyone,
admin0,
admin1,
user0,
user1,
user2,
stakerWithAdmin0,
nativePoolID,
erc20PoolID,
erc721PoolID,
erc1155TokenID,
erc1155PoolID,
};
}
return fixture;
}
describe('Staker', function () {
it('STAKER-1: Anybody should be able to deploy a Staker contract.', async function () {
const { staker } = await setupFixture();
expect(await staker.getAddress()).to.be.properAddress;
});
it('STAKER-2: The Staker implements ERC721', async function () {
const { staker } = await setupFixture();
// 1. `0x80ac58cd` (interface ID for `ERC721`)
expect(await staker.supportsInterface('0x80ac58cd')).to.be.true;
// 2. `0x5b5e139f` (interface ID for `ERC721Metadata`)
expect(await staker.supportsInterface('0x5b5e139f')).to.be.true;
// 3. `0x780e9d63` (interface ID for `ERC721Enumerable`)
expect(await staker.supportsInterface('0x780e9d63')).to.be.true;
});
it('STAKER-3: Token types', async function () {
const { staker } = await setupFixture();
expect(await staker.NATIVE_TOKEN_TYPE()).to.equal(1);
expect(await staker.ERC20_TOKEN_TYPE()).to.equal(20);
expect(await staker.ERC721_TOKEN_TYPE()).to.equal(721);
expect(await staker.ERC1155_TOKEN_TYPE()).to.equal(1155);
});
// Test written with the aid of ChatGPT 4o
it('STAKER-4: Any account should be able to create a staking pool for native tokens.', async function () {
const { staker, anyone } = await loadFixture(setupFixture);
const stakerWithAnyone = staker.connect(anyone);
const tx = await stakerWithAnyone.createPool(
await stakerWithAnyone.NATIVE_TOKEN_TYPE(),
ethers.ZeroAddress,
0,
true,
0,
0,
anyone
);
await expect(tx)
.to.emit(staker, 'StakingPoolCreated')
.withArgs(
0, // poolID should be 0 for the first pool
await stakerWithAnyone.NATIVE_TOKEN_TYPE(),
ethers.ZeroAddress,
0
);
await expect(tx)
.to.emit(staker, 'StakingPoolConfigured')
.withArgs(0, await anyone.getAddress(), true, 0, 0);
const pool = await staker.Pools(0);
expect(pool.tokenType).to.equal(await stakerWithAnyone.NATIVE_TOKEN_TYPE());
expect(pool.tokenAddress).to.equal(ethers.ZeroAddress);
expect(pool.tokenID).to.equal(0);
expect(pool.transferable).to.equal(true);
expect(pool.lockupSeconds).to.equal(0);
expect(pool.cooldownSeconds).to.equal(0);
});
// Test written with the aid of ChatGPT 4o
it('STAKER-5: Any account should be able to create a staking pool for ERC20 tokens.', async function () {
const { staker, erc20, anyone } = await loadFixture(setupFixture);
const stakerWithAnyone = staker.connect(anyone);
const tx = await stakerWithAnyone.createPool(
await stakerWithAnyone.ERC20_TOKEN_TYPE(),
await erc20.getAddress(),
0,
true,
0,
0,
anyone
);
await expect(tx)
.to.emit(staker, 'StakingPoolCreated')
.withArgs(
0, // poolID should be 0 as the chain state is reset
await stakerWithAnyone.ERC20_TOKEN_TYPE(),
await erc20.getAddress(),
0
);
await expect(tx)
.to.emit(staker, 'StakingPoolConfigured')
.withArgs(0, await anyone.getAddress(), true, 0, 0);
const pool = await staker.Pools(0);
expect(pool.tokenType).to.equal(await stakerWithAnyone.ERC20_TOKEN_TYPE());
expect(pool.tokenAddress).to.equal(await erc20.getAddress());
expect(pool.tokenID).to.equal(0);
expect(pool.transferable).to.equal(true);
expect(pool.lockupSeconds).to.equal(0);
expect(pool.cooldownSeconds).to.equal(0);
});
// Test written with the aid of ChatGPT 4o
it('STAKER-6: Any account should be able to create a staking pool for ERC721 tokens.', async function () {
const { staker, erc721, anyone } = await loadFixture(setupFixture);
const stakerWithAnyone = staker.connect(anyone);
const tx = await stakerWithAnyone.createPool(
await stakerWithAnyone.ERC721_TOKEN_TYPE(),
await erc721.getAddress(),
0,
true,
0,
0,
anyone
);
await expect(tx)
.to.emit(staker, 'StakingPoolCreated')
.withArgs(0, await stakerWithAnyone.ERC721_TOKEN_TYPE(), await erc721.getAddress(), 0);
await expect(tx)
.to.emit(staker, 'StakingPoolConfigured')
.withArgs(0, await anyone.getAddress(), true, 0, 0);
const pool = await staker.Pools(0);
expect(pool.tokenType).to.equal(await stakerWithAnyone.ERC721_TOKEN_TYPE());
expect(pool.tokenAddress).to.equal(await erc721.getAddress());
expect(pool.tokenID).to.equal(0);
expect(pool.transferable).to.equal(true);
expect(pool.lockupSeconds).to.equal(0);
expect(pool.cooldownSeconds).to.equal(0);
});
// Test written with the aid of ChatGPT 4o
it('STAKER-7: Any account should be able to create a staking pool for ERC1155 tokens.', async function () {
const { staker, erc1155, anyone } = await loadFixture(setupFixture);
const stakerWithAnyone = staker.connect(anyone);
const erc1155TokenID = 1;
const tx = await stakerWithAnyone.createPool(
await stakerWithAnyone.ERC1155_TOKEN_TYPE(),
await erc1155.getAddress(),
erc1155TokenID,
true,
0,
0,
anyone
);
await expect(tx)
.to.emit(staker, 'StakingPoolCreated')
.withArgs(0, await stakerWithAnyone.ERC1155_TOKEN_TYPE(), await erc1155.getAddress(), erc1155TokenID);
await expect(tx)
.to.emit(staker, 'StakingPoolConfigured')
.withArgs(0, await anyone.getAddress(), true, 0, 0);
const pool = await staker.Pools(0);
expect(pool.tokenType).to.equal(await stakerWithAnyone.ERC1155_TOKEN_TYPE());
expect(pool.tokenAddress).to.equal(await erc1155.getAddress());
expect(pool.tokenID).to.equal(erc1155TokenID);
expect(pool.transferable).to.equal(true);
expect(pool.lockupSeconds).to.equal(0);
expect(pool.cooldownSeconds).to.equal(0);
});
// Test written with the aid of ChatGPT 4o
it('STAKER-8: Staking pool IDs should start at 0 and increase sequentially, with correct token types.', async function () {
const { staker, erc20, erc721, erc1155, anyone } = await loadFixture(setupFixture);
const stakerWithAnyone = staker.connect(anyone);
await stakerWithAnyone.createPool(
await stakerWithAnyone.NATIVE_TOKEN_TYPE(),
ethers.ZeroAddress,
0,
true,
0,
0,
anyone
);
let poolID = (await staker.TotalPools()) - 1n;
expect(poolID).to.equal(0n);
let pool = await staker.Pools(poolID);
expect(pool.tokenType).to.equal(await stakerWithAnyone.NATIVE_TOKEN_TYPE());
await stakerWithAnyone.createPool(
await stakerWithAnyone.ERC20_TOKEN_TYPE(),
await erc20.getAddress(),
0,
true,
0,
0,
anyone
);
poolID = (await staker.TotalPools()) - 1n;
expect(poolID).to.equal(1n);
pool = await staker.Pools(poolID);
expect(pool.tokenType).to.equal(await stakerWithAnyone.ERC20_TOKEN_TYPE());
await stakerWithAnyone.createPool(
await stakerWithAnyone.ERC721_TOKEN_TYPE(),
await erc721.getAddress(),
0,
true,
0,
0,
anyone
);
poolID = (await staker.TotalPools()) - 1n;
expect(poolID).to.equal(2n);
pool = await staker.Pools(poolID);
expect(pool.tokenType).to.equal(await stakerWithAnyone.ERC721_TOKEN_TYPE());
await stakerWithAnyone.createPool(
await stakerWithAnyone.ERC1155_TOKEN_TYPE(),
await erc1155.getAddress(),
1,
true,
0,
0,
anyone
);
poolID = (await staker.TotalPools()) - 1n;
expect(poolID).to.equal(3n);
pool = await staker.Pools(poolID);
expect(pool.tokenType).to.equal(await stakerWithAnyone.ERC1155_TOKEN_TYPE());
});
// Test written with the aid of ChatGPT 4o
it('STAKER-9: It should not be possible to create a staking pool for a token of an unknown type.', async function () {
const { staker, anyone } = await loadFixture(setupFixture);
const stakerWithAnyone = staker.connect(anyone);
const invalidTokenType =
(await staker.NATIVE_TOKEN_TYPE()) +
(await staker.ERC20_TOKEN_TYPE()) +
(await staker.ERC721_TOKEN_TYPE()) +
(await staker.ERC1155_TOKEN_TYPE()) +
1n;
await expect(
stakerWithAnyone.createPool(invalidTokenType, ethers.ZeroAddress, 0, true, 0, 0, stakerWithAnyone)
).to.be.revertedWithCustomError(staker, 'InvalidTokenType');
});
// Test written with the aid of ChatGPT 4o
it('STAKER-10: It should not be possible to create native token staking pools with non-zero token address or token ID.', async function () {
const { staker, anyone, user0 } = await loadFixture(setupFixture);
const stakerWithAnyone = staker.connect(anyone);
// Non-zero token ID
await expect(
stakerWithAnyone.createPool(await stakerWithAnyone.NATIVE_TOKEN_TYPE(), ethers.ZeroAddress, 1, true, 0, 0, stakerWithAnyone)
).to.be.revertedWithCustomError(staker, 'InvalidConfiguration');
// Non-zero token address
await expect(
stakerWithAnyone.createPool(
await stakerWithAnyone.NATIVE_TOKEN_TYPE(),
await user0.getAddress(), // Non-zero address
0,
true,
0,
0,
stakerWithAnyone
)
).to.be.revertedWithCustomError(staker, 'InvalidConfiguration');
});
// Test written with the aid of ChatGPT 4o
it('STAKER-11: It should not be possible to create ERC20 token staking pools with zero token address or non-zero token ID.', async function () {
const { staker, erc20, anyone } = await loadFixture(setupFixture);
const stakerWithAnyone = staker.connect(anyone);
// Zero token address
await expect(
stakerWithAnyone.createPool(await stakerWithAnyone.ERC20_TOKEN_TYPE(), ethers.ZeroAddress, 0, true, 0, 0, stakerWithAnyone)
).to.be.revertedWithCustomError(staker, 'InvalidConfiguration');
// Non-zero token ID
await expect(
stakerWithAnyone.createPool(
await stakerWithAnyone.ERC20_TOKEN_TYPE(),
await erc20.getAddress(),
1,
true,
0,
0,
stakerWithAnyone
)
).to.be.revertedWithCustomError(staker, 'InvalidConfiguration');
});
// Test written with the aid of ChatGPT 4o
it('STAKER-12: It should not be possible to create ERC721 token staking pools with zero token address or non-zero token ID.', async function () {
const { staker, erc721, anyone } = await loadFixture(setupFixture);
const stakerWithAnyone = staker.connect(anyone);
// Zero token address
await expect(
stakerWithAnyone.createPool(await stakerWithAnyone.ERC721_TOKEN_TYPE(), ethers.ZeroAddress, 0, true, 0, 0, stakerWithAnyone)
).to.be.revertedWithCustomError(staker, 'InvalidConfiguration');
// Non-zero token ID
await expect(
stakerWithAnyone.createPool(
await stakerWithAnyone.ERC721_TOKEN_TYPE(),
await erc721.getAddress(),
1,
true,
0,
0,
stakerWithAnyone
)
).to.be.revertedWithCustomError(staker, 'InvalidConfiguration');
});
// Test written with the aid of ChatGPT 4o
it('STAKER-13: It should not be possible to create ERC1155 token staking pools with zero token address.', async function () {
const { staker, anyone } = await loadFixture(setupFixture);
const stakerWithAnyone = staker.connect(anyone);
await expect(
stakerWithAnyone.createPool(await stakerWithAnyone.ERC1155_TOKEN_TYPE(), ethers.ZeroAddress, 1, true, 0, 0, stakerWithAnyone)
).to.be.revertedWithCustomError(staker, 'InvalidConfiguration');
});
// Test written with the aid of ChatGPT 4o
it('STAKER-14: It should be possible to create ERC1155 token staking pools in which the token ID is zero.', async function () {
const { staker, erc1155, anyone } = await loadFixture(setupFixture);
const stakerWithAnyone = staker.connect(anyone);
const tx = await stakerWithAnyone.createPool(
await stakerWithAnyone.ERC1155_TOKEN_TYPE(),
await erc1155.getAddress(),
0, // Token ID is zero
true,
0,
0,
anyone
);
await expect(tx)
.to.emit(staker, 'StakingPoolCreated')
.withArgs(0, await stakerWithAnyone.ERC1155_TOKEN_TYPE(), await erc1155.getAddress(), 0);
const pool = await staker.Pools(0);
expect(pool.tokenType).to.equal(await stakerWithAnyone.ERC1155_TOKEN_TYPE());
expect(pool.tokenAddress).to.equal(await erc1155.getAddress());
expect(pool.tokenID).to.equal(0);
expect(pool.transferable).to.equal(true);
expect(pool.lockupSeconds).to.equal(0);
expect(pool.cooldownSeconds).to.equal(0);
});
it('STAKER-15: An administrator should be able to modify any subset of the configuration parameters on a pool in a single transaction', async function () {
const initialTransferable = true;
const initialLockupSeconds = 3600;
const initialCooldownSeconds = 300;
const { admin0, stakerWithAdmin0, erc20PoolID } = await loadFixture(
setupStakingPoolsFixture(initialTransferable, initialLockupSeconds, initialCooldownSeconds)
);
let poolInitial = await stakerWithAdmin0.Pools(erc20PoolID);
expect(poolInitial.administrator).to.equal(await admin0.getAddress());
const subsets = [
[false, false, true],
[false, true, false],
[false, true, true],
[true, false, false],
[true, false, true],
[true, true, false],
[true, true, true],
];
for (const [changeTransferable, changeLockupSeconds, changeCooldownSeconds] of subsets) {
const poolBefore = await stakerWithAdmin0.Pools(erc20PoolID);
const tx = await stakerWithAdmin0.updatePoolConfiguration(
erc20PoolID,
changeTransferable,
!poolBefore.transferable,
changeLockupSeconds,
poolBefore.lockupSeconds + 1n,
changeCooldownSeconds,
poolBefore.cooldownSeconds + 1n
);
await expect(tx)
.to.emit(stakerWithAdmin0, 'StakingPoolConfigured')
.withArgs(
erc20PoolID,
await admin0.getAddress(),
changeTransferable ? !poolBefore.transferable : poolBefore.transferable,
changeLockupSeconds ? poolBefore.lockupSeconds + 1n : poolBefore.lockupSeconds,
changeCooldownSeconds ? poolBefore.cooldownSeconds + 1n : poolBefore.cooldownSeconds
);
const poolAfter = await stakerWithAdmin0.Pools(erc20PoolID);
if (changeTransferable) {
expect(poolAfter.transferable).to.equal(!poolBefore.transferable);
} else {
expect(poolAfter.transferable).to.equal(poolBefore.transferable);
}
if (changeLockupSeconds) {
expect(poolAfter.lockupSeconds).to.equal(poolBefore.lockupSeconds + 1n);
} else {
expect(poolAfter.lockupSeconds).to.equal(poolBefore.lockupSeconds);
}
if (changeCooldownSeconds) {
expect(poolAfter.cooldownSeconds).to.equal(poolBefore.cooldownSeconds + 1n);
} else {
expect(poolAfter.cooldownSeconds).to.equal(poolBefore.cooldownSeconds);
}
}
});
// Test written with the aid of ChatGPT 4o
it('STAKER-16: A non-administrator (of any pool) should not be able to change any of the parameters of a staking pool.', async function () {
const transferable = true;
const lockupSeconds = 3600;
const cooldownSeconds = 300;
const { staker, user0, admin0, nativePoolID, erc20PoolID, erc721PoolID, erc1155PoolID } = await loadFixture(
setupStakingPoolsFixture(transferable, lockupSeconds, cooldownSeconds)
);
const stakerWithUser0 = staker.connect(user0);
const poolIDs = [nativePoolID, erc20PoolID, erc721PoolID, erc1155PoolID];
for (const poolID of poolIDs) {
// Verify that user0 is not the administrator of the pool
const pool = await staker.Pools(poolID);
expect(pool.administrator).to.equal(await admin0.getAddress());
await expect(
stakerWithUser0.updatePoolConfiguration(
poolID,
true, // changeTransferability
!transferable, // new transferable value
true, // changeLockup
lockupSeconds + 1, // new lockupSeconds
true, // changeCooldown
cooldownSeconds + 1 // new cooldownSeconds
)
).to.be.revertedWithCustomError(staker, 'NonAdministrator');
// Verify that the parameters did not change
const poolAfter = await staker.Pools(poolID);
expect(poolAfter.transferable).to.equal(transferable);
expect(poolAfter.lockupSeconds).to.equal(lockupSeconds);
expect(poolAfter.cooldownSeconds).to.equal(cooldownSeconds);
}
});
// Test written with the aid of ChatGPT 4o
it('STAKER-17: A non-administrator (of any pool) should not be able to change any of the parameters of a staking pool, even if they are administrators of a different pool.', async function () {
const transferable = true;
const lockupSeconds = 3600;
const cooldownSeconds = 300;
const { staker, user0, admin0, stakerWithAdmin0, erc20PoolID, erc721PoolID } = await loadFixture(
setupStakingPoolsFixture(transferable, lockupSeconds, cooldownSeconds)
);
const stakerWithUser0 = staker.connect(user0);
// Transfer administration of the ERC721 pool to user0
await stakerWithAdmin0.transferPoolAdministration(erc721PoolID, await user0.getAddress());
// Verify user0 is now the administrator of the ERC721 pool
const poolERC721 = await staker.Pools(erc721PoolID);
expect(poolERC721.administrator).to.equal(await user0.getAddress());
// Verify admin0 is still the administrator of the ERC20 pool
const poolERC20 = await staker.Pools(erc20PoolID);
expect(poolERC20.administrator).to.equal(await admin0.getAddress());
// Attempt to change the configuration of the ERC20 pool with user0
await expect(
stakerWithUser0.updatePoolConfiguration(
erc20PoolID,
true, // changeTransferability
!transferable, // new transferable value
true, // changeLockup
lockupSeconds + 1, // new lockupSeconds
true, // changeCooldown
cooldownSeconds + 1 // new cooldownSeconds
)
).to.be.revertedWithCustomError(staker, 'NonAdministrator');
// Verify that the parameters of the ERC20 pool did not change
const poolERC20After = await staker.Pools(erc20PoolID);
expect(poolERC20After.transferable).to.equal(transferable);
expect(poolERC20After.lockupSeconds).to.equal(lockupSeconds);
expect(poolERC20After.cooldownSeconds).to.equal(cooldownSeconds);
});
// Test written with the aid of ChatGPT 4o
it('STAKER-18: An administrator of a staking pool should be able to transfer administration of that pool to another account.', async function () {
const transferable = true;
const lockupSeconds = 3600;
const cooldownSeconds = 300;
const { staker, user0, admin0, stakerWithAdmin0, erc20PoolID } = await loadFixture(
setupStakingPoolsFixture(transferable, lockupSeconds, cooldownSeconds)
);
// Verify admin0 is the initial administrator of the ERC20 pool
let pool = await staker.Pools(erc20PoolID);
expect(pool.administrator).to.equal(await admin0.getAddress());
// Transfer administration of the ERC20 pool to user0
const tx = await stakerWithAdmin0.transferPoolAdministration(erc20PoolID, await user0.getAddress());
// Check for the emitted event with expected arguments
await expect(tx)
.to.emit(staker, 'StakingPoolConfigured')
.withArgs(
erc20PoolID,
await user0.getAddress(),
pool.transferable,
pool.lockupSeconds,
pool.cooldownSeconds
);
// Verify user0 is now the administrator of the ERC20 pool
pool = await staker.Pools(erc20PoolID);
expect(pool.administrator).to.equal(await user0.getAddress());
// Verify admin0 can no longer configure the ERC20 pool
await expect(
stakerWithAdmin0.updatePoolConfiguration(
erc20PoolID,
true, // changeTransferability
!transferable, // new transferable value
true, // changeLockup
lockupSeconds + 1, // new lockupSeconds
true, // changeCooldown
cooldownSeconds + 1 // new cooldownSeconds
)
).to.be.revertedWithCustomError(staker, 'NonAdministrator');
// Verify that the parameters of the ERC20 pool did not change
pool = await staker.Pools(erc20PoolID);
expect(pool.transferable).to.equal(transferable);
expect(pool.lockupSeconds).to.equal(lockupSeconds);
expect(pool.cooldownSeconds).to.equal(cooldownSeconds);
// Verify user0 can configure the ERC20 pool
const stakerWithUser0 = staker.connect(user0);
await expect(
stakerWithUser0.updatePoolConfiguration(
erc20PoolID,
true, // changeTransferability
!transferable, // new transferable value
true, // changeLockup
lockupSeconds + 1, // new lockupSeconds
true, // changeCooldown
cooldownSeconds + 1 // new cooldownSeconds
)
).to.emit(staker, 'StakingPoolConfigured');
// Verify that the parameters of the ERC20 pool have changed
pool = await staker.Pools(erc20PoolID);
expect(pool.transferable).to.equal(!transferable);
expect(pool.lockupSeconds).to.equal(lockupSeconds + 1);
expect(pool.cooldownSeconds).to.equal(cooldownSeconds + 1);
});
// Test written with the aid of ChatGPT 4o
it('STAKER-138: A non-administrator should not be able to transfer administration of a pool to another account.', async function () {
const transferable = true;
const lockupSeconds = 3600;
const cooldownSeconds = 300;
const { staker, user0, admin0, erc20PoolID } = await loadFixture(
setupStakingPoolsFixture(transferable, lockupSeconds, cooldownSeconds)
);
const stakerWithUser0 = staker.connect(user0);
// Verify admin0 is the initial administrator of the ERC20 pool
const pool = await staker.Pools(erc20PoolID);
expect(pool.administrator).to.equal(await admin0.getAddress());
// Attempt to transfer administration of the ERC20 pool with user0
await expect(
stakerWithUser0.transferPoolAdministration(erc20PoolID, await user0.getAddress())
).to.be.revertedWithCustomError(staker, 'NonAdministrator');
// Verify that the administrator of the ERC20 pool did not change
const poolAfter = await staker.Pools(erc20PoolID);
expect(poolAfter.administrator).to.equal(await admin0.getAddress());
});
// Test written with the aid of ChatGPT 4o
it('STAKER-139: A non-administrator of a staking pool should not be able to transfer administration of that pool to another account, even if they are an administrator of another pool.', async function () {
const transferable = true;
const lockupSeconds = 3600;
const cooldownSeconds = 300;
const { staker, user0, admin0, stakerWithAdmin0, erc20PoolID, erc721PoolID } = await loadFixture(
setupStakingPoolsFixture(transferable, lockupSeconds, cooldownSeconds)
);
const stakerWithUser0 = staker.connect(user0);
// Transfer administration of the ERC721 pool to user0
await stakerWithAdmin0.transferPoolAdministration(erc721PoolID, await user0.getAddress());
// Verify user0 is now the administrator of the ERC721 pool
const poolERC721 = await staker.Pools(erc721PoolID);
expect(poolERC721.administrator).to.equal(await user0.getAddress());
// Verify admin0 is still the administrator of the ERC20 pool
const poolERC20 = await staker.Pools(erc20PoolID);
expect(poolERC20.administrator).to.equal(await admin0.getAddress());
// Attempt to transfer administration of the ERC20 pool with user0
await expect(
stakerWithUser0.transferPoolAdministration(erc20PoolID, await user0.getAddress())
).to.be.revertedWithCustomError(staker, 'NonAdministrator');
// Verify that the administrator of the ERC20 pool did not change
const poolERC20After = await staker.Pools(erc20PoolID);
expect(poolERC20After.administrator).to.equal(await admin0.getAddress());
});
// Test written with the aid of ChatGPT 4o
it('STAKER-19: A holder should be able to stake any number of native tokens into a native token staking position.', async function () {
const transferable = true;
const lockupSeconds = 3600;
const cooldownSeconds = 300;
const { staker, user0, nativePoolID } = await loadFixture(
setupStakingPoolsFixture(transferable, lockupSeconds, cooldownSeconds)
);
const stakerWithUser0 = staker.connect(user0);
const stakeAmount = ethers.parseEther('1');
// Check balances before staking
const stakerBalanceBefore = await ethers.provider.getBalance(await staker.getAddress());
const user0BalanceBefore = await ethers.provider.getBalance(await user0.getAddress());
// Stake native tokens
const tx = await stakerWithUser0.stakeNative(user0, nativePoolID, { value: stakeAmount });
const txReceipt = await tx.wait();
expect(txReceipt).to.not.be.null;
// Check balances after staking
const stakerBalanceAfter = await ethers.provider.getBalance(await staker.getAddress());
const user0BalanceAfter = await ethers.provider.getBalance(await user0.getAddress());
expect(stakerBalanceAfter).to.equal(stakerBalanceBefore + stakeAmount);
expect(user0BalanceBefore).to.equal(user0BalanceAfter + stakeAmount + txReceipt!.fee);
// Verify the Staked event
await expect(tx)
.to.emit(staker, 'Staked')
.withArgs(
0, // positionTokenID
await user0.getAddress(),
nativePoolID,
stakeAmount
);
// Verify the ERC721 Transfer event
await expect(tx)
.to.emit(staker, 'Transfer')
.withArgs(ethers.ZeroAddress, await user0.getAddress(), 0);
// Get block timestamp
const block = await ethers.provider.getBlock(txReceipt!.blockNumber);
expect(block).to.not.be.null;
const blockTimestamp = block!.timestamp;
// Verify the position
const position = await staker.Positions(0);
expect(position.poolID).to.equal(nativePoolID);
expect(position.amountOrTokenID).to.equal(stakeAmount);
expect(position.stakeTimestamp).to.equal(blockTimestamp);
expect(position.unstakeInitiatedAt).to.equal(0);
});
// Test written with the aid of ChatGPT 4o
it('STAKER-20: A holder should be able to stake any number of ERC20 tokens into an ERC20 staking position.', async function () {
const transferable = true;
const lockupSeconds = 3600;
const cooldownSeconds = 300;
const { staker, erc20, user0, erc20PoolID } = await loadFixture(
setupStakingPoolsFixture(transferable, lockupSeconds, cooldownSeconds)
);
const stakerWithUser0 = staker.connect(user0);
const stakeAmount = ethers.parseUnits('100', 18);
// Mint ERC20 tokens to user0
await erc20.mint(await user0.getAddress(), stakeAmount);
// Approve staker contract to spend ERC20 tokens
await erc20.connect(user0).approve(await staker.getAddress(), stakeAmount);
// Check balances before staking
const stakerBalanceBefore = await erc20.balanceOf(await staker.getAddress());
const user0BalanceBefore = await erc20.balanceOf(await user0.getAddress());
// Stake ERC20 tokens
const tx = await stakerWithUser0.stakeERC20(user0, erc20PoolID, stakeAmount);
// Check balances after staking
const stakerBalanceAfter = await erc20.balanceOf(await staker.getAddress());
const user0BalanceAfter = await erc20.balanceOf(await user0.getAddress());
expect(stakerBalanceAfter).to.equal(stakerBalanceBefore + stakeAmount);
expect(user0BalanceAfter).to.equal(user0BalanceBefore - stakeAmount);
// Verify the Staked event
await expect(tx)
.to.emit(staker, 'Staked')
.withArgs(
0, // positionTokenID
await user0.getAddress(),
erc20PoolID,
stakeAmount
);
// Verify the ERC721 Transfer event
await expect(tx)
.to.emit(staker, 'Transfer')
.withArgs(ethers.ZeroAddress, await user0.getAddress(), 0);
// Get block timestamp
const txReceipt = await tx.wait();
expect(txReceipt).to.not.be.null;
const block = await ethers.provider.getBlock(txReceipt!.blockNumber);
expect(block).to.not.be.null;
const blockTimestamp = block!.timestamp;
// Verify the position
const position = await staker.Positions(0);
expect(position.poolID).to.equal(erc20PoolID);
expect(position.amountOrTokenID).to.equal(stakeAmount);
expect(position.stakeTimestamp).to.equal(blockTimestamp);
expect(position.unstakeInitiatedAt).to.equal(0);
});
// Test written with the aid of ChatGPT 4o
it('STAKER-21: A holder should be able to stake an ERC721 token into an ERC721 staking position.', async function () {
const transferable = true;
const lockupSeconds = 3600;
const cooldownSeconds = 300;
const { staker, erc721, user0, erc721PoolID } = await loadFixture(
setupStakingPoolsFixture(transferable, lockupSeconds, cooldownSeconds)
);
const stakerWithUser0 = staker.connect(user0);
const tokenId = 1;
// Mint ERC721 token to user0
await erc721.mint(await user0.getAddress(), tokenId);
// Approve staker contract to transfer ERC721 token
await erc721.connect(user0).approve(await staker.getAddress(), tokenId);
// Check ownership before staking
expect(await erc721.ownerOf(tokenId)).to.equal(await user0.getAddress());
// Stake ERC721 token
const tx = await stakerWithUser0.stakeERC721(user0, erc721PoolID, tokenId);
// Check ownership after staking
expect(await erc721.ownerOf(tokenId)).to.equal(await staker.getAddress());
// Verify the Staked event
await expect(tx)
.to.emit(staker, 'Staked')
.withArgs(
0, // positionTokenID
await user0.getAddress(),
erc721PoolID,
tokenId
);
// Verify the ERC721 Transfer event
await expect(tx)
.to.emit(staker, 'Transfer')
.withArgs(ethers.ZeroAddress, await user0.getAddress(), 0);
// Get block timestamp
const txReceipt = await tx.wait();
expect(txReceipt).to.not.be.null;
const block = await ethers.provider.getBlock(txReceipt!.blockNumber);
expect(block).to.not.be.null;
const blockTimestamp = block!.timestamp;
// Verify the position
const position = await staker.Positions(0);
expect(position.poolID).to.equal(erc721PoolID);
expect(position.amountOrTokenID).to.equal(tokenId);
expect(position.stakeTimestamp).to.equal(blockTimestamp);
expect(position.unstakeInitiatedAt).to.equal(0);
});
// Test written with the aid of ChatGPT 4o
it('STAKER-22: A holder should be able to stake any number of ERC1155 tokens into an ERC1155 staking position.', async function () {
const transferable = true;
const lockupSeconds = 3600;
const cooldownSeconds = 300;
const { staker, erc1155, user0, erc1155PoolID, erc1155TokenID } = await loadFixture(
setupStakingPoolsFixture(transferable, lockupSeconds, cooldownSeconds)
);
const stakerWithUser0 = staker.connect(user0);
const stakeAmount = 100n;
// Mint ERC1155 tokens to user0
await erc1155.mint(await user0.getAddress(), erc1155TokenID, stakeAmount);
// Approve staker contract to transfer ERC1155 tokens
await erc1155.connect(user0).setApprovalForAll(await staker.getAddress(), true);
// Check balances before staking
const stakerBalanceBefore = await erc1155.balanceOf(await staker.getAddress(), erc1155TokenID);
const user0BalanceBefore = await erc1155.balanceOf(await user0.getAddress(), erc1155TokenID);
// Stake ERC1155 tokens
const tx = await stakerWithUser0.stakeERC1155(user0, erc1155PoolID, stakeAmount);
// Check balances after staking
const stakerBalanceAfter = await erc1155.balanceOf(await staker.getAddress(), erc1155TokenID);
const user0BalanceAfter = await erc1155.balanceOf(await user0.getAddress(), erc1155TokenID);
expect(stakerBalanceAfter).to.equal(stakerBalanceBefore + stakeAmount);
expect(user0BalanceAfter).to.equal(user0BalanceBefore - stakeAmount);
// Verify the Staked event
await expect(tx)
.to.emit(staker, 'Staked')
.withArgs(
0, // positionTokenID
await user0.getAddress(),
erc1155PoolID,
stakeAmount
);
// Verify the ERC721 Transfer event
await expect(tx)
.to.emit(staker, 'Transfer')
.withArgs(ethers.ZeroAddress, await user0.getAddress(), 0);
// Get block timestamp
const txReceipt = await tx.wait();
expect(txReceipt).to.not.be.null;
const block = await ethers.provider.getBlock(txReceipt!.blockNumber);
expect(block).to.not.be.null;
const blockTimestamp = block!.timestamp;
// Verify the position
const position = await staker.Positions(0);
expect(position.poolID).to.equal(erc1155PoolID);
expect(position.amountOrTokenID).to.equal(stakeAmount);
expect(position.stakeTimestamp).to.equal(blockTimestamp);
expect(position.unstakeInitiatedAt).to.equal(0);
});
// Test written with the aid of ChatGPT 4o
it('STAKER-23: Staking position tokens for transferable staking pools should be transferable.', async function () {
const transferable = true;
const lockupSeconds = 3600;
const cooldownSeconds = 300;
const { staker, user0, user1, nativePoolID } = await loadFixture(
setupStakingPoolsFixture(transferable, lockupSeconds, cooldownSeconds)
);
const stakerWithUser0 = staker.connect(user0);
const stakeAmount = ethers.parseEther('1.2345'); // Using a unique stake amount
// Get total positions before staking
const totalPositionsBefore = await staker.TotalPositions();
// Stake native tokens
const tx = await stakerWithUser0.stakeNative(user0, nativePoolID, { value: stakeAmount });
const txReceipt = await tx.wait();
expect(txReceipt).to.not.be.null;
// Get total positions after staking
const totalPositionsAfter = await staker.TotalPositions();
// Verify that total positions have increased by 1
expect(totalPositionsAfter).to.equal(totalPositionsBefore + 1n);
// Get the position token ID of the newly minted token
const positionTokenID = totalPositionsBefore;