-
Notifications
You must be signed in to change notification settings - Fork 1
/
TablelandRigs.ts
2685 lines (2522 loc) · 99.1 KB
/
TablelandRigs.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 { SignerWithAddress } from "@nomiclabs/hardhat-ethers/signers";
import { loadFixture } from "@nomicfoundation/hardhat-network-helpers";
import { TablelandTables } from "@tableland/evm";
import chai from "chai";
import chaiAsPromised from "chai-as-promised";
import { BigNumber, utils } from "ethers";
import { ethers, network, upgrades } from "hardhat";
import { MerkleTree } from "merkletreejs";
import { AllowList, buildTree, hashEntry } from "../helpers/allowlist";
import { getURITemplate, normalize } from "../helpers/uris";
import {
PaymentSplitter,
TablelandRigs,
TablelandRigPilots,
DelegateCashMock,
} from "../typechain-types";
chai.use(chaiAsPromised);
const expect = chai.expect;
const assert = chai.assert;
const DELEGATE_CASH_ADDRESS = "0x00000000000076a84fef008cdabe6409d2fe638b";
function getCost(quantity: number, price: number): BigNumber {
return utils.parseEther((quantity * price).toFixed(2));
}
describe("Rigs", function () {
// Rigs contract deployment
let rigs: TablelandRigs;
let splitter: PaymentSplitter;
let accounts: SignerWithAddress[];
let beneficiary: SignerWithAddress;
const allowlist: AllowList = {};
const waitlist: AllowList = {};
let allowlistTree: MerkleTree;
let waitlistTree: MerkleTree;
let pilots: TablelandRigPilots;
let delegateCash: DelegateCashMock;
// Use a fixture, which runs *once* to help ensure deterministic contract addresses
async function deployRigsFixture() {
// First, deploy the `TablelandTables` registry contract
// Required for creating table from contract in `TablelandRigPilots.initialize()`
const TablelandTablesFactory = await ethers.getContractFactory(
"TablelandTables"
);
await (
(await upgrades.deployProxy(
TablelandTablesFactory,
["https://foo.xyz/"],
{
kind: "uups",
}
)) as TablelandTables
).deployed();
// Account setup
accounts = await ethers.getSigners();
beneficiary = accounts[1];
accounts.slice(0, 5).forEach((a: SignerWithAddress, i: number) => {
allowlist[a.address] = {
freeAllowance: i + 1,
paidAllowance: i + 1,
};
});
allowlistTree = buildTree(allowlist);
expect(allowlistTree.getLeafCount()).to.equal(5);
// Include an address that is on allowlist and waitlist
accounts.slice(4, 10).forEach((a: SignerWithAddress, i: number) => {
waitlist[a.address] = {
freeAllowance: i + 1,
paidAllowance: i + 1,
};
});
waitlistTree = buildTree(waitlist);
expect(waitlistTree.getLeafCount()).to.equal(6);
// Deploy the Rigs contract and its dependencies
const SplitterFactory = await ethers.getContractFactory("PaymentSplitter");
splitter = (await SplitterFactory.deploy(
[accounts[2].address, accounts[3].address],
[20, 80]
)) as PaymentSplitter;
await splitter.deployed();
const RigsFactory = await ethers.getContractFactory("TablelandRigs");
rigs = await ((await RigsFactory.deploy()) as TablelandRigs).deployed();
await (
await rigs.initialize(
BigNumber.from(3000),
utils.parseEther("0.05"),
beneficiary.address,
splitter.address,
allowlistTree.getHexRoot(),
waitlistTree.getHexRoot()
)
).wait();
await rigs.setContractURI("https://foo.xyz");
await rigs.setURITemplate(["https://foo.xyz/", "/bar"]);
// Check can only init once
await expect(
rigs.initialize(
BigNumber.from(3000),
utils.parseEther("0.05"),
beneficiary.address,
splitter.address,
allowlistTree.getHexRoot(),
waitlistTree.getHexRoot()
)
).to.be.revertedWith(
"ERC721A__Initializable: contract is already initialized"
);
// Deploy the Pilots contract
const PilotsFactory = await ethers.getContractFactory("TablelandRigPilots");
pilots = await (
(await PilotsFactory.deploy()) as TablelandRigPilots
).deployed();
await (await pilots.initialize(rigs.address)).wait();
// Set pilots on rigs
await (await rigs.initPilots(pilots.address)).wait();
// Deploy our delegate.cash mock,
// get the byte code from the deployed mock contract,
// and set the mock bytecode at the hardcoded delegate.cash address
const DelegateCashFactory = await ethers.getContractFactory(
"DelegateCashMock"
);
const delegateCashMock = await (
(await DelegateCashFactory.deploy()) as DelegateCashMock
).deployed();
const mockDelegateCashCode = await network.provider.send("eth_getCode", [
delegateCashMock.address,
]);
await network.provider.send("hardhat_setCode", [
DELEGATE_CASH_ADDRESS,
mockDelegateCashCode,
]);
delegateCash = DelegateCashFactory.attach(
DELEGATE_CASH_ADDRESS
) as DelegateCashMock;
}
beforeEach(async function () {
// Deploy the `TablelandRigs` contract
await loadFixture(deployRigsFixture);
});
describe("Deployment and minting", function () {
it("Should not mint during closed phase", async function () {
// try public minting
let minter = accounts[10];
await expect(
rigs.connect(minter)["mint(uint256)"](1, { value: getCost(1, 0.05) })
).to.be.rejectedWith("MintingClosed");
// try allowlist minting
minter = accounts[4];
const entry = allowlist[minter.address];
const proof = allowlistTree.getHexProof(hashEntry(minter.address, entry));
await expect(
rigs
.connect(minter)
["mint(uint256,uint256,uint256,bytes32[])"](
1,
entry.freeAllowance,
entry.paidAllowance,
proof,
{
value: getCost(1, 0.05),
}
)
).to.be.rejectedWith("MintingClosed");
});
it("Should not mint with zero quantity", async function () {
await rigs.setMintPhase(3);
const minter = accounts[10];
await expect(rigs.connect(minter)["mint(uint256)"](0)).to.be.rejectedWith(
"ZeroQuantity"
);
});
it("Should mint with allowlist during allowlist phase", async function () {
await rigs.setMintPhase(1);
// try public minting
let minter = accounts[10];
await expect(
rigs.connect(minter)["mint(uint256)"](1, { value: getCost(1, 0.05) })
).to.be.rejectedWith("InvalidProof");
// mint one of free allowance
minter = accounts[4];
let entry = allowlist[minter.address];
let proof = allowlistTree.getHexProof(hashEntry(minter.address, entry));
await expect(
rigs
.connect(minter)
["mint(uint256,uint256,uint256,bytes32[])"](
1,
entry.freeAllowance,
entry.paidAllowance,
proof
)
)
.to.emit(rigs, "Transfer")
.withArgs(
ethers.constants.AddressZero,
minter.address,
BigNumber.from(1)
);
// check new balance
expect(await rigs.balanceOf(minter.address)).to.equal(BigNumber.from(1));
// check owned tokens
let tokens = await rigs.tokensOfOwner(minter.address);
expect(tokens.length).to.equal(1);
expect(tokens[0]).to.equal(BigNumber.from(1));
// check total supply
expect(await rigs.totalSupply()).to.equal(BigNumber.from(1));
// minting over free allowance should require ether
await expect(
rigs
.connect(minter)
["mint(uint256,uint256,uint256,bytes32[])"](
entry.freeAllowance,
entry.freeAllowance,
entry.paidAllowance,
proof
)
).to.be.rejectedWith("InsufficientValue(50000000000000000)");
// mint remaining free allowance
await expect(
rigs
.connect(minter)
["mint(uint256,uint256,uint256,bytes32[])"](
entry.freeAllowance - 1,
entry.freeAllowance,
entry.paidAllowance,
proof
)
).to.emit(rigs, "Transfer");
// mint all paid allowance allowance
await expect(
rigs
.connect(minter)
["mint(uint256,uint256,uint256,bytes32[])"](
entry.paidAllowance,
entry.freeAllowance,
entry.paidAllowance,
proof,
{
value: getCost(entry.paidAllowance, 0.05),
}
)
)
.to.emit(rigs, "Transfer")
.to.emit(rigs, "Revenue")
.withArgs(
beneficiary.address,
BigNumber.from(entry.paidAllowance),
getCost(entry.paidAllowance, 0.05)
)
.to.not.emit(rigs, "Refund");
// re-check owned tokens
tokens = await rigs.tokensOfOwner(minter.address);
expect(tokens.length).to.equal(10);
// check allowance is now exhausted
await expect(
rigs
.connect(minter)
["mint(uint256,uint256,uint256,bytes32[])"](
1,
entry.freeAllowance,
entry.paidAllowance,
proof,
{
value: getCost(1, 0.05),
}
)
).to.be.rejectedWith("InsufficientAllowance");
// Check claimed count
const claimed = await rigs.getClaimed(minter.address);
expect(claimed.allowClaims).to.equal(
entry.freeAllowance + entry.paidAllowance
);
expect(claimed.waitClaims).to.equal(0);
// try waitlist minting
minter = accounts[5];
entry = waitlist[minter.address];
proof = waitlistTree.getHexProof(hashEntry(minter.address, entry));
await expect(
rigs
.connect(minter)
["mint(uint256,uint256,uint256,bytes32[])"](
1,
entry.freeAllowance,
entry.paidAllowance,
proof,
{
value: getCost(1, 0.05),
}
)
).to.be.rejectedWith("InvalidProof");
});
it("Should mint with waitlist during waitlist phase", async function () {
await rigs.setMintPhase(2);
// try public minting
let minter = accounts[10];
await expect(
rigs.connect(minter)["mint(uint256)"](1, { value: getCost(1, 0.05) })
).to.be.rejectedWith("InvalidProof");
// mint all allowance and send extra ether
minter = accounts[5];
const entry = waitlist[minter.address];
const proof = waitlistTree.getHexProof(hashEntry(minter.address, entry));
const quantity = entry.freeAllowance + entry.paidAllowance + 1;
await expect(
rigs
.connect(minter)
["mint(uint256,uint256,uint256,bytes32[])"](
quantity,
entry.freeAllowance,
entry.paidAllowance,
proof,
{
value: getCost(quantity, 0.05),
}
)
)
.to.emit(rigs, "Transfer")
.to.emit(rigs, "Revenue")
.withArgs(
beneficiary.address,
BigNumber.from(entry.paidAllowance),
getCost(entry.paidAllowance, 0.05)
)
.to.emit(rigs, "Refund")
.withArgs(
minter.address,
getCost(quantity - entry.paidAllowance, 0.05)
);
// check allowance is now exhausted
await expect(
rigs
.connect(minter)
["mint(uint256,uint256,uint256,bytes32[])"](
1,
entry.freeAllowance,
entry.paidAllowance,
proof,
{
value: getCost(1, 0.05),
}
)
).to.be.rejectedWith("InsufficientAllowance");
// Check claimed count
const claimed = await rigs.getClaimed(minter.address);
expect(claimed.allowClaims).to.equal(0);
expect(claimed.waitClaims).to.equal(
entry.freeAllowance + entry.paidAllowance
);
// try unused allowlist
minter = accounts[3];
await expect(
rigs
.connect(minter)
["mint(uint256,uint256,uint256,bytes32[])"](
1,
entry.freeAllowance,
entry.paidAllowance,
proof,
{
value: getCost(1, 0.05),
}
)
).to.be.rejectedWith("InvalidProof");
});
it("Should mint during public phase", async function () {
await rigs.setMintPhase(3);
let minter = accounts[10];
await expect(
rigs.connect(minter)["mint(uint256)"](1, { value: getCost(1, 0.05) })
).to.emit(rigs, "Transfer");
// allowlist minting should not allow free minting
minter = accounts[4];
const entry = allowlist[minter.address];
const proof = allowlistTree.getHexProof(hashEntry(minter.address, entry));
await expect(
rigs
.connect(minter)
["mint(uint256,uint256,uint256,bytes32[])"](
1,
entry.freeAllowance,
entry.paidAllowance,
proof
)
).to.be.rejectedWith("InsufficientValue");
// allowlist minting should still work with value
await expect(
rigs
.connect(minter)
["mint(uint256,uint256,uint256,bytes32[])"](
1,
entry.freeAllowance,
entry.paidAllowance,
proof,
{ value: getCost(1, 0.05) }
)
).to.emit(rigs, "Transfer");
});
it("Should mint through phases until sold out", async function () {
const maxSupply = await rigs.maxSupply();
// allowlist
await rigs.setMintPhase(1);
let minter = accounts[4];
let entry = allowlist[minter.address];
let proof = allowlistTree.getHexProof(hashEntry(minter.address, entry));
await expect(
rigs
.connect(minter)
["mint(uint256,uint256,uint256,bytes32[])"](
entry.freeAllowance + entry.paidAllowance,
entry.freeAllowance,
entry.paidAllowance,
proof,
{ value: getCost(entry.paidAllowance, 0.05) }
)
).to.emit(rigs, "Transfer");
let minted = entry.freeAllowance + entry.paidAllowance;
// Check claimed count
let claimed = await rigs.getClaimed(minter.address);
expect(claimed.allowClaims).to.equal(
entry.freeAllowance + entry.paidAllowance
);
expect(claimed.waitClaims).to.equal(0);
// waitlist, same address
await rigs.setMintPhase(2);
minter = accounts[4];
entry = waitlist[minter.address];
proof = waitlistTree.getHexProof(hashEntry(minter.address, entry));
await expect(
rigs
.connect(minter)
["mint(uint256,uint256,uint256,bytes32[])"](
entry.freeAllowance + entry.paidAllowance,
entry.freeAllowance,
entry.paidAllowance,
proof,
{ value: getCost(entry.paidAllowance, 0.05) }
)
).to.be.rejectedWith("InsufficientAllowance");
// waitlist
await rigs.setMintPhase(2);
minter = accounts[5];
entry = waitlist[minter.address];
proof = waitlistTree.getHexProof(hashEntry(minter.address, entry));
await expect(
rigs
.connect(minter)
["mint(uint256,uint256,uint256,bytes32[])"](
entry.freeAllowance + entry.paidAllowance,
entry.freeAllowance,
entry.paidAllowance,
proof,
{ value: getCost(entry.paidAllowance, 0.05) }
)
).to.emit(rigs, "Transfer");
minted += entry.freeAllowance + entry.paidAllowance;
// Check claimed count
claimed = await rigs.getClaimed(minter.address);
expect(claimed.allowClaims).to.equal(0);
expect(claimed.waitClaims).to.equal(
entry.freeAllowance + entry.paidAllowance
);
// public
await rigs.setMintPhase(3);
minter = accounts[10];
const remaining = maxSupply.toNumber() - minted;
await expect(
rigs.connect(minter)["mint(uint256)"](remaining, {
value: getCost(remaining, 0.05),
})
).to.emit(rigs, "Transfer");
minted += remaining;
// sold out
await expect(
rigs.connect(minter)["mint(uint256)"](1, {
value: getCost(1, 0.05),
})
).to.be.rejectedWith("SoldOut");
assert.equal(maxSupply.toNumber(), minted);
});
it("Should set URI template", async function () {
await rigs.setMintPhase(3);
const minter = accounts[10];
const tx = await rigs
.connect(minter)
["mint(uint256)"](1, { value: getCost(1, 0.05) });
const receipt = await tx.wait();
const [event] = receipt.events ?? [];
const tokenId = event.args?.tokenId;
await rigs.setURITemplate([]);
expect(await rigs.tokenURI(tokenId)).to.equal("");
await rigs.setURITemplate([""]);
expect(await rigs.tokenURI(tokenId)).to.equal("");
await rigs.setURITemplate(["https://fake.com/"]);
expect(await rigs.tokenURI(tokenId)).to.equal("https://fake.com/");
await rigs.setURITemplate(["https://fake.com/", "/boo"]);
expect(await rigs.tokenURI(tokenId)).to.equal("https://fake.com/1/boo");
await rigs.setURITemplate(["https://fake.com/", "/boo/", ""]);
expect(await rigs.tokenURI(tokenId)).to.equal("https://fake.com/1/boo/1");
});
it("Should have pending metadata if no attributeTable", async function () {
const tablelandHost = "http://tableland.network";
const table1 = "table1";
const table2 = "table2";
const table3 = "table3";
const table4 = "table4";
const table5 = "table5";
const uriTemplate = await getURITemplate(
tablelandHost,
table1,
table2,
table3,
table4,
table5,
false
);
const uri = new URL(uriTemplate.join("1"));
const statement = uri.searchParams.get("statement");
/* eslint-disable no-unused-expressions */
expect(statement).not.to.be.null;
// normalizing the statement checks that it is valid SQL, will throw
// an error if it isn't
const normalizedStatement = await normalize(statement!);
expect(normalizedStatement).contains("pre-reveal");
});
it("Should have final metadata if attributeTable", async function () {
const tablelandHost = "http://tableland.network";
const table1 = "table1";
const table2 = "table2";
const table3 = "table3";
const table4 = "table4";
const table5 = "table5";
const uriTemplate = await getURITemplate(
tablelandHost,
table1,
table2,
table3,
table4,
table5,
true
);
const uri = new URL(uriTemplate.join("1"));
const statement = uri.searchParams.get("statement");
/* eslint-disable no-unused-expressions */
expect(statement).not.to.be.null;
// normalizing the statement checks that it is valid SQL, will throw
// an error if it isn't
const normalizedStatement = await normalize(statement!);
expect(normalizedStatement).contains("Garage Status");
});
it("Should not return token URI for non-existent token", async function () {
await expect(rigs.tokenURI(BigNumber.from(1))).to.be.rejectedWith(
"URIQueryForNonexistentToken"
);
});
it("Should set contract URI", async function () {
await rigs.setContractURI("https://fake.com");
expect(await rigs.contractURI()).to.equal("https://fake.com");
});
it("Should set royalty receiver", async function () {
const receiver = accounts[2].address;
await rigs.setRoyaltyReceiver(receiver);
const info = await rigs.royaltyInfo(1, utils.parseEther("1"));
expect(info[0]).to.equal(receiver);
expect(info[1]).to.equal(utils.parseEther("0.05"));
});
it("Should pause and unpause minting", async function () {
await rigs.setMintPhase(3);
await rigs.pause();
const minter = accounts[10];
await expect(
rigs.connect(minter)["mint(uint256)"](1, { value: getCost(1, 0.05) })
).to.be.revertedWith("Pausable: paused");
await expect(
rigs
.connect(minter)
["mint(uint256,uint256,uint256,bytes32[])"](1, 0, 0, [], {
value: getCost(1, 0.05),
})
).to.be.revertedWith("Pausable: paused");
await rigs.unpause();
});
it("Should restrict owner-only methods to owners", async function () {
const _rigs = rigs.connect(accounts[2]);
await expect(_rigs.setMintPhase(1)).to.be.revertedWith(
"Ownable: caller is not the owner"
);
await expect(
_rigs.setBeneficiary(accounts[2].address)
).to.be.revertedWith("Ownable: caller is not the owner");
await expect(_rigs.setURITemplate(["foo"])).to.be.revertedWith(
"Ownable: caller is not the owner"
);
await expect(_rigs.setContractURI("bar")).to.be.revertedWith(
"Ownable: caller is not the owner"
);
await expect(
_rigs.setRoyaltyReceiver(accounts[2].address)
).to.be.revertedWith("Ownable: caller is not the owner");
await expect(_rigs.pause()).to.be.rejectedWith(
"Ownable: caller is not the owner"
);
await expect(_rigs.unpause()).to.be.rejectedWith(
"Ownable: caller is not the owner"
);
});
it("Should support required interfaces", async function () {
// ERC165 interface ID for ERC165
expect(await rigs.supportsInterface("0x01ffc9a7")).to.equal(true);
// ERC165 interface ID for ERC721
expect(await rigs.supportsInterface("0x80ac58cd")).to.equal(true);
// ERC165 interface ID for ERC721Metadata
expect(await rigs.supportsInterface("0x5b5e139f")).to.equal(true);
// ERC165 interface ID for ERC2981
expect(await rigs.supportsInterface("0x2a55205a")).to.equal(true);
// ERC165 interface ID for ERC4906
expect(await rigs.supportsInterface("0x49064906")).to.equal(true);
});
});
describe("The Garage", function () {
describe("initPilots", function () {
it("Should block contract non-owner", async function () {
const _rigs = rigs.connect(accounts[2]);
await expect(
_rigs.initPilots(ethers.constants.AddressZero)
).to.be.rejectedWith("Ownable: caller is not the owner");
});
});
describe("pilotSessionsTable", function () {
it("Should return pilot sessions table", async function () {
expect(await rigs.pilotSessionsTable()).to.be.equal(
`pilot_sessions_${network.config.chainId}_2`
);
});
});
describe("admin", function () {
it("Owner should be able to set admin", async function () {
const admin = accounts[2];
expect(await rigs.admin()).to.not.equal(admin.address);
await rigs.setAdmin(admin.address);
expect(await rigs.admin()).to.equal(admin.address);
});
it("Non-owner should not be able to set admin", async function () {
const hacker = accounts[2];
await expect(
rigs.connect(hacker).setAdmin(hacker.address)
).to.be.rejectedWith("Ownable: caller is not the owner");
});
});
describe("pilotInfo", function () {
it("Should not return pilot info for non-existent token", async function () {
// Try calling with a non-existent token
await expect(
rigs["pilotInfo(uint256)"](BigNumber.from(0))
).to.be.rejectedWith("OwnerQueryForNonexistentToken");
});
it("Should get default pilot info for a garaged Rig", async function () {
// Mint a token and then get its pilot's default info (i.e., still in the garage)
await rigs.setMintPhase(3);
const tokenOwner = accounts[4];
const tx = await rigs
.connect(tokenOwner)
["mint(uint256)"](1, { value: getCost(1, 0.05) });
const receipt = await tx.wait();
const [event] = receipt.events ?? [];
const tokenId = event.args?.tokenId;
const pilotInfo = await rigs["pilotInfo(uint256)"](
BigNumber.from(tokenId)
);
expect(pilotInfo.status).to.equal(0);
expect(pilotInfo.pilotable).to.equal(false);
expect(pilotInfo.started).to.equal(BigNumber.from(0));
expect(pilotInfo.addr).to.equal(ethers.constants.AddressZero);
expect(pilotInfo.id).to.equal(BigNumber.from(0));
});
it("Should get pilot info for more than one Rig", async function () {
// Mint 2 tokens
await rigs.setMintPhase(3);
const tokenOwner = accounts[4];
let tx = await rigs
.connect(tokenOwner)
["mint(uint256)"](1, { value: getCost(1, 0.05) });
let receipt = await tx.wait();
let [event] = receipt.events ?? [];
const tokenId1 = event.args?.tokenId;
tx = await rigs
.connect(tokenOwner)
["mint(uint256)"](1, { value: getCost(1, 0.05) });
receipt = await tx.wait();
[event] = receipt.events ?? [];
const tokenId2 = event.args?.tokenId;
const pilotInfo = await rigs["pilotInfo(uint256[])"]([
BigNumber.from(tokenId1),
BigNumber.from(tokenId2),
]);
// Pilot info for tokenId1
expect(pilotInfo[0].status).to.equal(0);
expect(pilotInfo[0].pilotable).to.equal(false);
expect(pilotInfo[0].started).to.equal(BigNumber.from(0));
expect(pilotInfo[0].addr).to.equal(ethers.constants.AddressZero);
expect(pilotInfo[0].id).to.equal(BigNumber.from(0));
// Pilot info for tokenId2
expect(pilotInfo[1].status).to.equal(0);
expect(pilotInfo[1].pilotable).to.equal(false);
expect(pilotInfo[1].started).to.equal(BigNumber.from(0));
expect(pilotInfo[1].addr).to.equal(ethers.constants.AddressZero);
expect(pilotInfo[1].id).to.equal(BigNumber.from(0));
});
});
describe("trainRig", function () {
it("Should not train Rig when paused", async function () {
await rigs.pause();
// Try to train a single Rig when paused
const sender = accounts[4];
await expect(
rigs.connect(sender)["trainRig(uint256)"](BigNumber.from(0))
).to.be.revertedWith("Pausable: paused");
// Try to train a multiple Rigs when paused
await expect(
rigs
.connect(sender)
["trainRig(uint256[])"]([BigNumber.from(0), BigNumber.from(0)])
).to.be.revertedWith("Pausable: paused");
await rigs.unpause();
});
it("Should not train Rig for non-existent token", async function () {
await expect(
rigs["trainRig(uint256)"](BigNumber.from(0))
).to.be.rejectedWith("OwnerQueryForNonexistentToken");
});
it("Should not train Rig if msg.sender is not token owner", async function () {
// First, mint a Rig to `tokenOwner`
await rigs.setMintPhase(3);
const tokenOwner = accounts[4];
const tx = await rigs
.connect(tokenOwner)
["mint(uint256)"](1, { value: getCost(1, 0.05) });
const receipt = await tx.wait();
const [event] = receipt.events ?? [];
const tokenId = event.args?.tokenId;
// Attempt to train the Rig with an address that doesn't own the token
const sender = accounts[5];
await expect(
rigs.connect(sender)["trainRig(uint256)"](BigNumber.from(tokenId))
).to.be.rejectedWith("Unauthorized");
});
it("Should train Rig if msg.sender is token owner", async function () {
// First, mint a Rig to `tokenOwner`
await rigs.setMintPhase(3);
const tokenOwner = accounts[4];
const tx = await rigs
.connect(tokenOwner)
["mint(uint256)"](1, { value: getCost(1, 0.05) });
const receipt = await tx.wait();
const [event] = receipt.events ?? [];
const tokenId = event.args?.tokenId;
// Train the Rig
await expect(
rigs.connect(tokenOwner)["trainRig(uint256)"](BigNumber.from(tokenId))
)
.to.emit(pilots, "Training")
.withArgs(BigNumber.from(tokenId));
});
it("Should not train Rig if it has already left the garage", async function () {
// First, mint a Rig to `tokenOwner`
await rigs.setMintPhase(3);
const tokenOwner = accounts[4];
const tx = await rigs
.connect(tokenOwner)
["mint(uint256)"](1, { value: getCost(1, 0.05) });
const receipt = await tx.wait();
const [event] = receipt.events ?? [];
const tokenId = event.args?.tokenId;
// Train the Rig
await rigs
.connect(tokenOwner)
["trainRig(uint256)"](BigNumber.from(tokenId));
// Try to train the Rig again
await expect(
rigs.connect(tokenOwner)["trainRig(uint256)"](BigNumber.from(tokenId))
).to.be.rejectedWith("InvalidPilotStatus");
});
it("Should batch train Rigs", async function () {
// First, mint 2 Rigs to `tokenOwner`
await rigs.setMintPhase(3);
const tokenOwner = accounts[4];
let tx = await rigs
.connect(tokenOwner)
["mint(uint256)"](1, { value: getCost(1, 0.05) });
let receipt = await tx.wait();
let [event] = receipt.events ?? [];
const tokenId1 = event.args?.tokenId;
tx = await rigs
.connect(tokenOwner)
["mint(uint256)"](1, { value: getCost(1, 0.05) });
receipt = await tx.wait();
[event] = receipt.events ?? [];
const tokenId2 = event.args?.tokenId;
// Train the Rigs
await expect(
rigs
.connect(tokenOwner)
["trainRig(uint256[])"]([
BigNumber.from(tokenId1),
BigNumber.from(tokenId2),
])
)
.to.emit(pilots, "Training")
.withArgs(BigNumber.from(tokenId1))
.to.emit(pilots, "Training")
.withArgs(BigNumber.from(tokenId2));
});
it("Should not batch train a duplicate Rig token value", async function () {
// First, mint a Rig to `tokenOwner`
await rigs.setMintPhase(3);
const tokenOwner = accounts[4];
const tx = await rigs
.connect(tokenOwner)
["mint(uint256)"](1, { value: getCost(1, 0.05) });
const receipt = await tx.wait();
const [event] = receipt.events ?? [];
const tokenId = event.args?.tokenId;
// Train the Rig, but pass the same Rig `tokenId` twice -- the second training attempt will fail
await expect(
rigs
.connect(tokenOwner)
["trainRig(uint256[])"]([
BigNumber.from(tokenId),
BigNumber.from(tokenId),
])
)
.to.emit(pilots, "Training")
.withArgs(BigNumber.from(tokenId))
.to.be.rejectedWith("InvalidPilotStatus");
});
it("Should not batch train Rig with empty array or exceeding max length for array", async function () {
// Try with an empty array
await expect(rigs["trainRig(uint256[])"]([])).to.be.rejectedWith(
"InvalidBatchPilotAction"
);
// Try with an array of tokens exceeding 255 in length (the arbitrary limit)
const tokenIds = [...Array(256).keys()];
await expect(rigs["trainRig(uint256[])"](tokenIds)).to.be.rejectedWith(
"InvalidBatchPilotAction"
);
});
});
describe("pilotRig", function () {
it("Should not pilot Rig when paused", async function () {
await rigs.pause();
// Try to pilot when paused
await expect(
rigs["pilotRig(uint256,address,uint256)"](
BigNumber.from(0),
ethers.constants.AddressZero,
BigNumber.from(1)
)
).to.be.rejectedWith("Pausable: paused");
await expect(
rigs["pilotRig(uint256[],address[],uint256[])"](
[BigNumber.from(0)],
[ethers.constants.AddressZero],
[BigNumber.from(1)]
)
).to.be.rejectedWith("Pausable: paused");
await rigs.unpause();
});
it("Should not pilot Rig for non-existent token", async function () {
// Try with a single Rig and `pilotRig`
await expect(
rigs["pilotRig(uint256,address,uint256)"](
BigNumber.from(0),
ethers.constants.AddressZero,
BigNumber.from(1)
)
).to.be.rejectedWith("OwnerQueryForNonexistentToken");
// Try with multiple Rigs and `pilotRig` (batch)
await expect(
rigs["pilotRig(uint256[],address[],uint256[])"](
[BigNumber.from(0)],
[ethers.constants.AddressZero],
[BigNumber.from(1)]
)
).to.be.rejectedWith("OwnerQueryForNonexistentToken");
});
it("Should not pilot Rig if msg.sender is not token owner", async function () {
// First, mint a Rig to `tokenOwner`
await rigs.setMintPhase(3);
const tokenOwner = accounts[4];
const tx = await rigs
.connect(tokenOwner)
["mint(uint256)"](1, { value: getCost(1, 0.05) });
const receipt = await tx.wait();
const [event] = receipt.events ?? [];
const tokenId = event.args?.tokenId;
// Attempt to pilot the Rig with an address that doesn't own the token
const sender = accounts[5];
await expect(
rigs
.connect(sender)