-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathSnapshotGateTest.js
935 lines (752 loc) Β· 33.9 KB
/
SnapshotGateTest.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
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
const { ethers } = require("hardhat");
const { getContractAt, parseUnits, ZeroAddress, getSigners, MaxUint256 } = ethers;
const { expect } = require("chai");
const { RevertReasons } = require("../../scripts/config/revert-reasons.js");
const Role = require("../../scripts/domain/Role");
const TokenType = require("../../scripts/domain/TokenType");
const Group = require("../../scripts/domain/Group");
const EvaluationMethod = require("../../scripts/domain/EvaluationMethod");
const { DisputeResolverFee } = require("../../scripts/domain/DisputeResolverFee");
const { deployProtocolDiamond } = require("../../scripts/util/deploy-protocol-diamond.js");
const { deployAndCutFacets } = require("../../scripts/util/deploy-protocol-handler-facets.js");
const { deployProtocolClients } = require("../../scripts/util/deploy-protocol-clients");
const {
mockOffer,
mockDisputeResolver,
mockAuthToken,
mockVoucherInitValues,
mockSeller,
mockCondition,
accountId,
} = require("../util/mock");
const { oneWeek, oneMonth, maxPriorityFeePerGas } = require("../util/constants");
const { deploySnapshotGateExample } = require("../../scripts/example/SnapshotGate/deploy-snapshot-gate");
const { deployMockTokens } = require("../../scripts/util/deploy-mock-tokens");
const { getEvent, getFacetsWithArgs } = require("../util/utils");
/**
* Test the SnapshotGate example contract
*/
describe("SnapshotGate", function () {
// Common vars
let deployer,
pauser,
assistant,
assistant2,
admin,
clerk,
treasury,
rando,
assistantDR,
adminDR,
clerkDR,
treasuryDR,
protocolTreasury,
bosonToken,
holder1,
holder2,
holder3,
holder4,
holder5;
let protocolDiamond, accessController, accountHandler, offerHandler, groupHandler, exchangeHandler;
let offerId, seller, seller2, disputeResolverId;
let price, foreign20;
let protocolFeePercentage, protocolFeeFlatBoson, buyerEscalationDepositPercentage;
let disputeResolver, disputeResolverFees;
let snapshotGate;
let groupId, offerIds, condition, group, groups;
let voucherInitValues;
let emptyAuthToken;
let sellerId, agentId;
let offer, offers, otherSellerOfferId;
let offerDates, offerDurations;
let snapshot, snapshotTokenSupplies, snapshotTokenCount, holders, holderByAddress;
let offerFeeLimit;
let bosonErrors;
beforeEach(async function () {
// Reset the accountId iterator
accountId.next(true);
let priceDiscovery;
// Make accounts available
[
deployer,
pauser,
admin,
treasury,
rando,
adminDR,
treasuryDR,
protocolTreasury,
assistant2,
bosonToken,
holder1,
holder2,
holder3,
holder4,
holder5,
priceDiscovery,
] = await getSigners();
// make all account the same
assistant = admin;
assistantDR = adminDR;
clerk = clerkDR = { address: ZeroAddress };
// Deploy the Protocol Diamond
[protocolDiamond, , , , accessController] = await deployProtocolDiamond(maxPriorityFeePerGas);
// Temporarily grant UPGRADER role to deployer account
await accessController.grantRole(Role.UPGRADER, await deployer.getAddress());
// Grant PROTOCOL role to ProtocolDiamond address and renounces admin
await accessController.grantRole(Role.PROTOCOL, await protocolDiamond.getAddress());
// Temporarily grant PAUSER role to pauser account
await accessController.grantRole(Role.PAUSER, await pauser.getAddress());
// Deploy the Protocol client implementation/proxy pairs (currently just the Boson Voucher)
const protocolClientArgs = [await protocolDiamond.getAddress()];
const [, beacons] = await deployProtocolClients(protocolClientArgs, maxPriorityFeePerGas);
const [beacon] = beacons;
// Set protocolFees
protocolFeePercentage = "200"; // 2 %
protocolFeeFlatBoson = parseUnits("0.01", "ether").toString();
buyerEscalationDepositPercentage = "1000"; // 10%
// Add config Handler, so ids start at 1, and so voucher address can be found
const protocolConfig = [
// Protocol addresses
{
treasury: await protocolTreasury.getAddress(),
token: await bosonToken.getAddress(),
voucherBeacon: await beacon.getAddress(),
beaconProxy: ZeroAddress,
priceDiscovery: priceDiscovery.address, // dummy address
},
// Protocol limits
{
maxExchangesPerBatch: 50,
maxOffersPerGroup: 100,
maxTwinsPerBundle: 100,
maxOffersPerBundle: 100,
maxOffersPerBatch: 100,
maxTokensPerWithdrawal: 100,
maxFeesPerDisputeResolver: 100,
maxEscalationResponsePeriod: oneMonth,
maxDisputesPerBatch: 100,
maxAllowedSellers: 100,
maxTotalOfferFeePercentage: 4000, //40%
maxRoyaltyPercentage: 1000, //10%
minResolutionPeriod: oneWeek,
maxResolutionPeriod: oneMonth,
minDisputePeriod: oneWeek,
maxPremintedVouchers: 10000,
},
// Protocol fees
protocolFeePercentage,
protocolFeeFlatBoson,
buyerEscalationDepositPercentage,
];
const facetNames = [
"AccountHandlerFacet",
"SellerHandlerFacet",
"DisputeResolverHandlerFacet",
"ExchangeHandlerFacet",
"OfferHandlerFacet",
"GroupHandlerFacet",
"ProtocolInitializationHandlerFacet",
"ConfigHandlerFacet",
];
const facetsToDeploy = await getFacetsWithArgs(facetNames, protocolConfig);
// Cut the protocol handler facets into the Diamond
await deployAndCutFacets(await protocolDiamond.getAddress(), facetsToDeploy, maxPriorityFeePerGas);
// Cast Diamond to IBosonAccountHandler. Use this interface to call all individual account handlers
accountHandler = await getContractAt("IBosonAccountHandler", await protocolDiamond.getAddress());
// Cast Diamond to IBosonOfferHandler
offerHandler = await getContractAt("IBosonOfferHandler", await protocolDiamond.getAddress());
// Cast Diamond to IGroupHandler
groupHandler = await getContractAt("IBosonGroupHandler", await protocolDiamond.getAddress());
// Cast Diamond to IBosonExchangeHandler
exchangeHandler = await getContractAt("IBosonExchangeHandler", await protocolDiamond.getAddress());
bosonErrors = await getContractAt("BosonErrors", await protocolDiamond.getAddress());
accountId.next(true);
// Deploy the SnapshotGate example
sellerId = "1";
[snapshotGate] = await deploySnapshotGateExample(
["SnapshotGateToken", "SGT", await protocolDiamond.getAddress(), sellerId],
maxPriorityFeePerGas
);
// Deploy the mock tokens
[foreign20] = await deployMockTokens(["Foreign20"]);
// Initial ids for all the things
offerId = "1";
groupId = "1";
agentId = "0"; // agent id is optional while creating an offer
offerFeeLimit = MaxUint256; // unlimited to not affect the test
// Create a valid seller
seller = mockSeller(
await assistant.getAddress(),
await admin.getAddress(),
clerk.address,
await treasury.getAddress()
);
expect(seller.isValid()).is.true;
// Create a second seller
seller2 = mockSeller(
await assistant2.getAddress(),
await assistant2.getAddress(),
ZeroAddress,
await assistant2.getAddress()
);
expect(seller2.isValid()).is.true;
// AuthToken
emptyAuthToken = mockAuthToken();
expect(emptyAuthToken.isValid()).is.true;
// VoucherInitValues
voucherInitValues = mockVoucherInitValues();
expect(voucherInitValues.isValid()).is.true;
// Create the seller
await accountHandler.connect(admin).createSeller(seller, emptyAuthToken, voucherInitValues);
// Create the second seller
await accountHandler.connect(assistant2).createSeller(seller2, emptyAuthToken, voucherInitValues);
// Create a valid dispute resolver
disputeResolver = mockDisputeResolver(
await assistantDR.getAddress(),
await adminDR.getAddress(),
clerkDR.address,
await treasuryDR.getAddress(),
true
);
expect(disputeResolver.isValid()).is.true;
// Create DisputeResolverFee array so offer creation will succeed
disputeResolverFees = [
new DisputeResolverFee(ZeroAddress, "Native", "0"),
new DisputeResolverFee(await foreign20.getAddress(), "Foriegn20", "0"),
];
// Make empty seller list, so every seller is allowed
const sellerAllowList = [];
// Register the dispute resolver
await accountHandler.connect(adminDR).createDisputeResolver(disputeResolver, disputeResolverFees, sellerAllowList);
// Manufacture snapshot for upload
snapshot = []; // { owner : string; tokenId: string; amount: string }[]
snapshotTokenSupplies = {}; // map token ids to supplies
snapshotTokenCount = 5; // create 5 snapshot token ids
holders = [
// holder accounts
holder1,
holder2,
holder3,
holder4,
holder5,
];
holderByAddress = {
[await holder1.getAddress()]: holder1,
[await holder2.getAddress()]: holder2,
[await holder3.getAddress()]: holder3,
[await holder4.getAddress()]: holder4,
[await holder5.getAddress()]: holder5,
};
// Each holder will have a random amount of each token
for (let holder of holders) {
// Mint a bunch of exchange tokens for the holder and approve the gate to transfer them
const amountToMint = "15000000000000000000";
await foreign20.connect(holder).mint(await holder.getAddress(), amountToMint);
await foreign20.connect(holder).approve(await snapshotGate.getAddress(), amountToMint);
// Create snapshot entry for holder / token
for (let i = 1; i <= snapshotTokenCount; i++) {
// The token id
const tokenId = i.toString();
// Get a random balance 1 - 9
const balance = Math.floor(Math.random() * 10) + 1;
// Track the total supply of each token - corresponding offer's qty available must match
snapshotTokenSupplies[tokenId] = String(Number(snapshotTokenSupplies[tokenId] || 0) + balance);
// Add snapshot entry
snapshot.push({
owner: await holder.getAddress(),
tokenId: i.toString(),
amount: balance.toString(),
});
}
}
// Create gated offers in a loop
offers = [];
groups = [];
// Make 2 passes, creating native token offers and then ERC20 offers
for (let j = 0; j < 2; j++) {
for (let i = 1; i <= snapshotTokenCount; i++) {
// The token id
const tokenId = i.toString(); // first and second batches use same token ids
offerId = Number(snapshotTokenCount * j + i).toString(); // offer id from first or second batch
groupId = offerId;
// The supply of this token
const tokenSupply = snapshotTokenSupplies[tokenId];
// Create the offer
const mo = await mockOffer();
({ offerDates, offerDurations } = mo);
offer = mo.offer;
price = offer.price;
// Set price in ERC-20 token if on second pass
if (j > 0) {
offer.exchangeToken = await foreign20.getAddress();
offer.buyerCancelPenalty = "0";
}
offer.sellerDeposit = "0";
offer.quantityAvailable = tokenSupply;
disputeResolverId = disputeResolver.id;
// Check if entities are valid
expect(offer.isValid()).is.true;
expect(offerDates.isValid()).is.true;
expect(offerDurations.isValid()).is.true;
// Create the offer
await offerHandler
.connect(assistant)
.createOffer(offer, offerDates, offerDurations, disputeResolverId, agentId, offerFeeLimit);
offers.push(offer);
// Required constructor params for Group
offerIds = [offerId];
// Create Condition
condition = mockCondition({
tokenAddress: await snapshotGate.getAddress(),
threshold: "0",
maxCommits: tokenSupply,
tokenType: TokenType.NonFungibleToken,
minTokenId: tokenId,
method: EvaluationMethod.SpecificToken,
maxTokenId: tokenId,
});
expect(condition.isValid()).to.be.true;
// Create Group
group = new Group(groupId, seller.id, offerIds);
expect(group.isValid()).is.true;
await groupHandler.connect(assistant).createGroup(group, condition);
groups.push(group);
}
}
// End of gated offers creation
// Create second seller offer
const mo = await mockOffer();
({ offerDates, offerDurations } = mo);
offer = mo.offer;
offer.sellerId = "2"; // second seller
offer.price = price;
offer.sellerDeposit = "0";
offer.quantityAvailable = "5";
offer.buyerCancelPenalty = "0";
// Check if entities are valid
expect(offer.isValid()).is.true;
expect(offerDates.isValid()).is.true;
expect(offerDurations.isValid()).is.true;
// Create the offer
let tx = await offerHandler
.connect(assistant2)
.createOffer(offer, offerDates, offerDurations, disputeResolverId, agentId, offerFeeLimit);
offers.push(offer);
const txReceipt = await tx.wait();
const event = getEvent(txReceipt, offerHandler, "OfferCreated");
otherSellerOfferId = event.offerId;
});
afterEach(async function () {
// Reset the accountId iterator
accountId.next(true);
});
// All supported SnapshotGate methods
context("π SnapshotGate Methods", async function () {
context("π appendToSnapshot()", async function () {
it("should emit a SnapshotAppended event", async function () {
// Batch of one
const batch = snapshot.slice(0, 1);
// Append to snapshot
const tx = await snapshotGate.connect(deployer).appendToSnapshot(batch);
const txReceipt = await tx.wait();
const event = getEvent(txReceipt, snapshotGate, "SnapshotAppended");
// Check executedBy
expect(event.executedBy.toString()).to.equal((await deployer.getAddress()).toString());
// Verify batch contents emitted match what was sent
for (let i = 0; i < batch.length; i++) {
let holder = event.holders[i];
expect(holder.owner.toString()).to.equal(batch[i].owner);
expect(holder.tokenId.toString()).to.equal(batch[i].tokenId);
expect(holder.amount.toString()).to.equal(batch[i].amount);
}
});
it("should allow multiple invocations", async function () {
// Split snapshot into two batches
const batchSize = Math.floor(snapshot.length / 2);
const batch1 = snapshot.slice(0, batchSize);
const batch2 = snapshot.slice(batchSize + 1);
// Append first batch
let tx = await snapshotGate.connect(deployer).appendToSnapshot(batch1);
let txReceipt = await tx.wait();
let event = getEvent(txReceipt, snapshotGate, "SnapshotAppended");
// Check executedBy
expect(event.executedBy.toString()).to.equal((await deployer.getAddress()).toString());
// Verify batch contents emitted match what was sent
for (let i = 0; i < batch1.length; i++) {
let holder = event.holders[i];
expect(holder.owner.toString()).to.equal(batch1[i].owner);
expect(holder.tokenId.toString()).to.equal(batch1[i].tokenId);
expect(holder.amount.toString()).to.equal(batch1[i].amount);
}
// Append second batch
tx = await snapshotGate.connect(deployer).appendToSnapshot(batch2);
txReceipt = await tx.wait();
event = getEvent(txReceipt, snapshotGate, "SnapshotAppended");
// Check executedBy
expect(event.executedBy.toString()).to.equal((await deployer.getAddress()).toString());
// Verify batch contents emitted match what was sent
for (let i = 0; i < batch2.length; i++) {
let holder = event.holders[i];
expect(holder.owner.toString()).to.equal(batch2[i].owner);
expect(holder.tokenId.toString()).to.equal(batch2[i].tokenId);
expect(holder.amount.toString()).to.equal(batch2[i].amount);
}
});
it("should create custodial tokens for each token in the snapshot", async function () {
// Append to snapshot
await snapshotGate.connect(deployer).appendToSnapshot(snapshot);
// Verify tokens were created
// Expect the owner of all the tokens to be the gate contract itself
for (let i = 1; i <= snapshotTokenCount; i++) {
const tokenId = i.toString();
const owner = await snapshotGate.ownerOf(tokenId);
expect(owner).to.equal(await snapshotGate.getAddress());
}
});
it("should store snapshot correctly", async function () {
// Append to snapshot
await snapshotGate.connect(deployer).appendToSnapshot(snapshot);
// Verify tokens were created
// Expect the owner of all the tokens to be the gate contract itself
for (let i = 1; i <= snapshotTokenCount; i++) {
const tokenId = i.toString();
const owner = await snapshotGate.ownerOf(tokenId);
expect(owner).to.equal(await snapshotGate.getAddress());
}
});
context("π Revert Reasons", async function () {
it("snapshot is frozen", async function () {
// Batch of one
const batch = snapshot.slice(0, 1);
// Freeze the snapsho
await snapshotGate.connect(deployer).freezeSnapshot();
// Attempt to append, expecting revert
await expect(snapshotGate.connect(deployer).appendToSnapshot(batch)).to.revertedWith(
"Cannot append to frozen snapshot"
);
});
it("caller is not contract owner", async function () {
// Batch of one
const batch = snapshot.slice(0, 1);
// Freeze the snapshot
await snapshotGate.connect(deployer).freezeSnapshot();
// Attempt to append from non-owner wallet, expecting revert
await expect(snapshotGate.connect(holder1).appendToSnapshot(batch)).to.revertedWith(
"Ownable: caller is not the owner"
);
});
});
});
context("π freezeSnapshot()", async function () {
it("should emit a SnapshotFrozen event", async function () {
await expect(snapshotGate.connect(deployer).freezeSnapshot())
.to.emit(snapshotGate, "SnapshotFrozen")
.withArgs(await deployer.getAddress());
});
it("should store true value for snapshotFrozen", async function () {
// Freeze the snapshot
await snapshotGate.connect(deployer).freezeSnapshot();
// Check the flag
let isFrozen = await snapshotGate.snapshotFrozen();
expect(isFrozen).to.be.true;
});
context("π Revert Reasons", async function () {
it("snapshot already frozen", async function () {
// Freeze the snapshot
await snapshotGate.connect(deployer).freezeSnapshot();
// Attempt to append, expecting revert
await expect(snapshotGate.connect(deployer).freezeSnapshot()).to.revertedWith("Snapshot already frozen");
});
it("caller is not contract owner", async function () {
// Attempt to append from non-owner wallet, expecting revert
await expect(snapshotGate.connect(holder1).freezeSnapshot()).to.revertedWith(
"Ownable: caller is not the owner"
);
});
});
});
context("π commitToGatedOffer()", async function () {
it("should emit a SnapshotTokenCommitted event when price is in native token", async function () {
// Upload the snapshot
await snapshotGate.connect(deployer).appendToSnapshot(snapshot);
// Freeze the snapshot
await snapshotGate.connect(deployer).freezeSnapshot();
// Grab an entry from the snapshot
let entry = snapshot[Math.floor(snapshot.length / 4)];
// Offer, token, and group ids are aligned for the sake of sanity
offerId = entry.tokenId;
// Get the account to make the call with
let holder = holderByAddress[entry.owner];
// Commit to the offer
await expect(
snapshotGate.connect(holder).commitToGatedOffer(entry.owner, offerId, entry.tokenId, { value: price })
)
.to.emit(snapshotGate, "SnapshotTokenCommitted")
.withArgs(entry.owner, offerId, entry.tokenId, await holder.getAddress());
});
it("should emit a SnapshotTokenCommitted event when price is in ERC20 token", async function () {
// Upload the snapshot
await snapshotGate.connect(deployer).appendToSnapshot(snapshot);
// Freeze the snapshot
await snapshotGate.connect(deployer).freezeSnapshot();
// Grab an entry from the snapshot
let entry = snapshot[Math.floor(snapshot.length / 4)];
// ERC20 offers are in second batch
offerId = String(Number(entry.tokenId) + snapshotTokenCount);
// Get the account to make the call with
let holder = holderByAddress[entry.owner];
// Commit to the offer
await expect(snapshotGate.connect(holder).commitToGatedOffer(entry.owner, offerId, entry.tokenId))
.to.emit(snapshotGate, "SnapshotTokenCommitted")
.withArgs(entry.owner, offerId, entry.tokenId, await holder.getAddress());
});
context("π Revert Reasons", async function () {
it("offerId is invalid", async function () {
// Upload the snapshot
await snapshotGate.connect(deployer).appendToSnapshot(snapshot);
// Freeze the snapshot
await snapshotGate.connect(deployer).freezeSnapshot();
// Grab an entry from the snapshot
let entry = snapshot[Math.floor(snapshot.length / 4)];
// Invalid offer id
offerId = "999";
// Get the account to make the call with
let caller = holderByAddress[entry.owner];
// Commit to the offer
await expect(
snapshotGate
.connect(caller)
.commitToGatedOffer(await caller.getAddress(), offerId, entry.tokenId, { value: price })
).to.revertedWith("Invalid offer id");
});
it("snapshot is not frozen", async function () {
// Upload the snapshot but don't freeze
await snapshotGate.connect(deployer).appendToSnapshot(snapshot);
// Grab an entry from the snapshot
let entry = snapshot[Math.floor(snapshot.length / 4)];
// Offer, token, and group ids are aligned for the sake of sanity
offerId = entry.tokenId;
// Get the account to make the call with
let caller = holderByAddress[entry.owner];
// Commit to the offer
await expect(
snapshotGate
.connect(caller)
.commitToGatedOffer(await caller.getAddress(), offerId, entry.tokenId, { value: price })
).to.revertedWith("Snapshot is not frozen");
});
it("buyer doesn't have a balance of the given token in the snapshot", async function () {
// Upload the snapshot
await snapshotGate.connect(deployer).appendToSnapshot(snapshot);
// Freeze the snapshot
await snapshotGate.connect(deployer).freezeSnapshot();
// Grab an entry from the snapshot
let entry = snapshot[Math.floor(snapshot.length / 4)];
// Offer, token, and group ids are aligned for the sake of sanity
offerId = entry.tokenId;
// Get an account to make the call with that does not have a balance in the snapshot
let caller = deployer;
// Commit to the offer
await expect(
snapshotGate
.connect(caller)
.commitToGatedOffer(await caller.getAddress(), offerId, entry.tokenId, { value: price })
).to.revertedWith("Buyer held no balance of the given token id at time of snapshot");
});
it("buyer has exhausted allowable commits for a given token they hold in the snapshot", async function () {
// Upload the snapshot
await snapshotGate.connect(deployer).appendToSnapshot(snapshot);
// Freeze the snapshot
await snapshotGate.connect(deployer).freezeSnapshot();
// Grab an entry from the snapshot
let entry = snapshot[Math.floor(snapshot.length / 4)];
// Offer, token, and group ids are aligned for the sake of sanity,
offerId = entry.tokenId;
// Get the account to make the call with
let holder = holderByAddress[entry.owner];
// Exhaust all commits for this buyer for this token
for (let i = 0; i < entry.amount; i++) {
await snapshotGate
.connect(holder)
.commitToGatedOffer(entry.owner, offerId, entry.tokenId, { value: price });
}
// Commit to the offer
await expect(
snapshotGate.connect(holder).commitToGatedOffer(entry.owner, offerId, entry.tokenId, { value: price })
).to.revertedWith("Buyer's balance of the snapshot token id has been used");
});
it("condition specifies a different tokenId from the one given", async function () {
// Upload the snapshot
await snapshotGate.connect(deployer).appendToSnapshot(snapshot);
// Freeze the snapshot
await snapshotGate.connect(deployer).freezeSnapshot();
// Grab first entry from the snapshot
let entry = snapshot[0];
// Grab second entry from the snapshot
let entry2 = snapshot[1];
// wrong offer id
offerId = entry2.tokenId;
// Get the account to make the call with
let holder = holderByAddress[entry.owner];
// Commit to the offer
await expect(
snapshotGate.connect(holder).commitToGatedOffer(entry.owner, offerId, entry.tokenId, { value: price })
).to.revertedWithCustomError(bosonErrors, RevertReasons.TOKEN_ID_NOT_IN_CONDITION_RANGE);
});
it("offer is from another seller", async function () {
// Upload the snapshot but don't freeze
await snapshotGate.connect(deployer).appendToSnapshot(snapshot);
// Freeze the snapshot
await snapshotGate.connect(deployer).freezeSnapshot();
// Grab an entry from the snapshot
let entry = snapshot[Math.floor(snapshot.length / 4)];
// Offer, token, and group ids are aligned for the sake of sanity
offerId = entry.tokenId;
// Get the account to make the call with
let caller = holderByAddress[entry.owner];
// Commit to the offer
await expect(
snapshotGate
.connect(caller)
.commitToGatedOffer(await caller.getAddress(), otherSellerOfferId, entry.tokenId, { value: price })
).to.revertedWith("Offer is from another seller");
});
it("incorrect payment is sent when price is in native token", async function () {
// Upload the snapshot
await snapshotGate.connect(deployer).appendToSnapshot(snapshot);
// Freeze the snapshot
await snapshotGate.connect(deployer).freezeSnapshot();
// Grab an entry from the snapshot
let entry = snapshot[Math.floor(snapshot.length / 4)];
// Offer, token, and group ids are aligned for the sake of sanity
offerId = entry.tokenId;
// Get the account to make the call with
let holder = holderByAddress[entry.owner];
// Wrong price
const halfPrice = (BigInt(price) / BigInt(2)).toString();
// Commit to the offer
await expect(
snapshotGate.connect(holder).commitToGatedOffer(entry.owner, offerId, entry.tokenId, { value: halfPrice })
).to.revertedWith("Incorrect payment amount");
});
it("insufficient approval for payment transfer when price is in ERC20 token", async function () {
// Upload the snapshot
await snapshotGate.connect(deployer).appendToSnapshot(snapshot);
// Freeze the snapshot
await snapshotGate.connect(deployer).freezeSnapshot();
// Grab an entry from the snapshot
let entry = snapshot[Math.floor(snapshot.length / 4)];
// ERC20 offers are in second batch
offerId = String(Number(entry.tokenId) + snapshotTokenCount);
// Get the account to make the call with
let holder = holderByAddress[entry.owner];
// Zero out the gate's approval to transfer the holder's payment ERC20 tokens
await foreign20.connect(holder).approve(await snapshotGate.getAddress(), "0");
// Commit to the offer
await expect(
snapshotGate.connect(holder).commitToGatedOffer(entry.owner, offerId, entry.tokenId)
).to.revertedWith("Insufficient approval for payment transfer");
});
});
});
context("π checkSnapshot()", async function () {
it("should return expected values after initial upload", async function () {
// Upload the snapshot
await snapshotGate.connect(deployer).appendToSnapshot(snapshot);
// Check every entry in the snapshot
for (let entry of snapshot) {
const response = await snapshotGate.connect(deployer).checkSnapshot(entry.tokenId, entry.owner);
// Expect owned value to match snapshot value for holder
expect(response.owned.toString()).to.equal(entry.amount);
// Expect used value to be zero
expect(response.used.toString()).to.equal("0");
}
});
it("should return expected values after a commit when price is in native token", async function () {
// Upload the snapshot
await snapshotGate.connect(deployer).appendToSnapshot(snapshot);
// Freeze the snapshot
await snapshotGate.connect(deployer).freezeSnapshot();
// Grab an entry from the snapshot
let entry = snapshot[Math.floor(snapshot.length / 3)];
// Get the account to make the call with
let holder = holderByAddress[entry.owner];
// Commit to the gated offer
offerId = entry.tokenId; // Offer, token, and group ids are aligned for the sake of sanity,
await snapshotGate.connect(holder).commitToGatedOffer(entry.owner, offerId, entry.tokenId, { value: price });
// Check that the committed snapshot token is now marked used
const response = await snapshotGate.connect(rando).checkSnapshot(entry.tokenId, entry.owner);
// Expect owned value to match snapshot value for holder
expect(response.owned.toString()).to.equal(entry.amount);
// Expect used value to be one
expect(response.used.toString()).to.equal("1");
});
it("should return expected values after a commit when price is in ERC20 token", async function () {
// Upload the snapshot
await snapshotGate.connect(deployer).appendToSnapshot(snapshot);
// Freeze the snapshot
await snapshotGate.connect(deployer).freezeSnapshot();
// Grab an entry from the snapshot
let entry = snapshot[Math.floor(snapshot.length / 3)];
// ERC20 offers are in second batch
offerId = String(Number(entry.tokenId) + snapshotTokenCount);
// Get the account to make the call with
let holder = holderByAddress[entry.owner];
// Commit to the gated offer
await snapshotGate.connect(holder).commitToGatedOffer(entry.owner, offerId, entry.tokenId);
// Check that the committed snapshot token is now marked used
const response = await snapshotGate.connect(rando).checkSnapshot(entry.tokenId, entry.owner);
// Expect owned value to match snapshot value for holder
expect(response.owned.toString()).to.equal(entry.amount);
// Expect used value to be one
expect(response.used.toString()).to.equal("1");
});
});
context("π ownerOf()", async function () {
it("should report gate as owner of all tokens when tx not in-flight", async function () {
// Upload the snapshot, creating all tokens in the process
await snapshotGate.connect(deployer).appendToSnapshot(snapshot);
// Expect the owner of all the tokens to be the gate contract itself
for (let i = 1; i <= snapshotTokenCount; i++) {
const tokenId = i.toString();
const owner = await snapshotGate.connect(rando).ownerOf(tokenId);
expect(owner).to.equal(await snapshotGate.getAddress());
}
});
context("π Revert Reasons", async function () {
it("tokenId is invalid", async function () {
// Upload the snapshot
await snapshotGate.connect(deployer).appendToSnapshot(snapshot);
// Invalid token id (not in snapshot)
const tokenId = "999";
// Check
await expect(snapshotGate.ownerOf(tokenId)).to.revertedWith("ERC721: invalid token ID");
});
});
});
});
// Relevant Boson Protocol methods
context("π Protocol Methods", async function () {
context("π commitToConditionalOffer()", async function () {
context("π Revert Reasons", async function () {
it("buyer is in snapshot but attempts to commit directly on protocol", async function () {
// Upload the snapshot
await snapshotGate.connect(deployer).appendToSnapshot(snapshot);
// Freeze the snapshot
await snapshotGate.connect(deployer).freezeSnapshot();
// Grab an entry from the snapshot
let entry = snapshot[Math.floor(snapshot.length / 4)];
// Offer, token, and group ids are aligned for the sake of sanity
offerId = entry.tokenId;
// Get the account to make the call with
let holder = holderByAddress[entry.owner];
// Check that holder cannot commit directly to the offer on the protocol itself
await expect(
exchangeHandler.connect(holder).commitToConditionalOffer(await holder.getAddress(), offerId, entry.tokenId)
).to.revertedWithCustomError(bosonErrors, RevertReasons.CANNOT_COMMIT);
});
});
});
});
});