-
Notifications
You must be signed in to change notification settings - Fork 11.7k
/
ERC721.behavior.js
945 lines (788 loc) · 35 KB
/
ERC721.behavior.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
936
937
938
939
940
941
942
943
944
945
const { BN, constants, expectEvent, expectRevert } = require('@openzeppelin/test-helpers');
const { expect } = require('chai');
const { ZERO_ADDRESS } = constants;
const { shouldSupportInterfaces } = require('../../utils/introspection/SupportsInterface.behavior');
const ERC721ReceiverMock = artifacts.require('ERC721ReceiverMock');
const Error = [ 'None', 'RevertWithMessage', 'RevertWithoutMessage', 'Panic' ]
.reduce((acc, entry, idx) => Object.assign({ [entry]: idx }, acc), {});
const firstTokenId = new BN('5042');
const secondTokenId = new BN('79217');
const nonExistentTokenId = new BN('13');
const fourthTokenId = new BN(4);
const baseURI = 'https://api.example.com/v1/';
const RECEIVER_MAGIC_VALUE = '0x150b7a02';
function shouldBehaveLikeERC721 (errorPrefix, owner, newOwner, approved, anotherApproved, operator, other) {
shouldSupportInterfaces([
'ERC165',
'ERC721',
]);
context('with minted tokens', function () {
beforeEach(async function () {
await this.token.mint(owner, firstTokenId);
await this.token.mint(owner, secondTokenId);
this.toWhom = other; // default to other for toWhom in context-dependent tests
});
describe('balanceOf', function () {
context('when the given address owns some tokens', function () {
it('returns the amount of tokens owned by the given address', async function () {
expect(await this.token.balanceOf(owner)).to.be.bignumber.equal('2');
});
});
context('when the given address does not own any tokens', function () {
it('returns 0', async function () {
expect(await this.token.balanceOf(other)).to.be.bignumber.equal('0');
});
});
context('when querying the zero address', function () {
it('throws', async function () {
await expectRevert(
this.token.balanceOf(ZERO_ADDRESS), 'ERC721: balance query for the zero address',
);
});
});
});
describe('ownerOf', function () {
context('when the given token ID was tracked by this token', function () {
const tokenId = firstTokenId;
it('returns the owner of the given token ID', async function () {
expect(await this.token.ownerOf(tokenId)).to.be.equal(owner);
});
});
context('when the given token ID was not tracked by this token', function () {
const tokenId = nonExistentTokenId;
it('reverts', async function () {
await expectRevert(
this.token.ownerOf(tokenId), 'ERC721: owner query for nonexistent token',
);
});
});
});
describe('transfers', function () {
const tokenId = firstTokenId;
const data = '0x42';
let logs = null;
beforeEach(async function () {
await this.token.approve(approved, tokenId, { from: owner });
await this.token.setApprovalForAll(operator, true, { from: owner });
});
const transferWasSuccessful = function ({ owner, tokenId, approved }) {
it('transfers the ownership of the given token ID to the given address', async function () {
expect(await this.token.ownerOf(tokenId)).to.be.equal(this.toWhom);
});
it('emits a Transfer event', async function () {
expectEvent.inLogs(logs, 'Transfer', { from: owner, to: this.toWhom, tokenId: tokenId });
});
it('clears the approval for the token ID', async function () {
expect(await this.token.getApproved(tokenId)).to.be.equal(ZERO_ADDRESS);
});
it('emits an Approval event', async function () {
expectEvent.inLogs(logs, 'Approval', { owner, approved: ZERO_ADDRESS, tokenId: tokenId });
});
it('adjusts owners balances', async function () {
expect(await this.token.balanceOf(owner)).to.be.bignumber.equal('1');
});
it('adjusts owners tokens by index', async function () {
if (!this.token.tokenOfOwnerByIndex) return;
expect(await this.token.tokenOfOwnerByIndex(this.toWhom, 0)).to.be.bignumber.equal(tokenId);
expect(await this.token.tokenOfOwnerByIndex(owner, 0)).to.be.bignumber.not.equal(tokenId);
});
};
const shouldTransferTokensByUsers = function (transferFunction) {
context('when called by the owner', function () {
beforeEach(async function () {
({ logs } = await transferFunction.call(this, owner, this.toWhom, tokenId, { from: owner }));
});
transferWasSuccessful({ owner, tokenId, approved });
});
context('when called by the approved individual', function () {
beforeEach(async function () {
({ logs } = await transferFunction.call(this, owner, this.toWhom, tokenId, { from: approved }));
});
transferWasSuccessful({ owner, tokenId, approved });
});
context('when called by the operator', function () {
beforeEach(async function () {
({ logs } = await transferFunction.call(this, owner, this.toWhom, tokenId, { from: operator }));
});
transferWasSuccessful({ owner, tokenId, approved });
});
context('when called by the owner without an approved user', function () {
beforeEach(async function () {
await this.token.approve(ZERO_ADDRESS, tokenId, { from: owner });
({ logs } = await transferFunction.call(this, owner, this.toWhom, tokenId, { from: operator }));
});
transferWasSuccessful({ owner, tokenId, approved: null });
});
context('when sent to the owner', function () {
beforeEach(async function () {
({ logs } = await transferFunction.call(this, owner, owner, tokenId, { from: owner }));
});
it('keeps ownership of the token', async function () {
expect(await this.token.ownerOf(tokenId)).to.be.equal(owner);
});
it('clears the approval for the token ID', async function () {
expect(await this.token.getApproved(tokenId)).to.be.equal(ZERO_ADDRESS);
});
it('emits only a transfer event', async function () {
expectEvent.inLogs(logs, 'Transfer', {
from: owner,
to: owner,
tokenId: tokenId,
});
});
it('keeps the owner balance', async function () {
expect(await this.token.balanceOf(owner)).to.be.bignumber.equal('2');
});
it('keeps same tokens by index', async function () {
if (!this.token.tokenOfOwnerByIndex) return;
const tokensListed = await Promise.all(
[0, 1].map(i => this.token.tokenOfOwnerByIndex(owner, i)),
);
expect(tokensListed.map(t => t.toNumber())).to.have.members(
[firstTokenId.toNumber(), secondTokenId.toNumber()],
);
});
});
context('when the address of the previous owner is incorrect', function () {
it('reverts', async function () {
await expectRevert(
transferFunction.call(this, other, other, tokenId, { from: owner }),
'ERC721: transfer from incorrect owner',
);
});
});
context('when the sender is not authorized for the token id', function () {
it('reverts', async function () {
await expectRevert(
transferFunction.call(this, owner, other, tokenId, { from: other }),
'ERC721: transfer caller is not owner nor approved',
);
});
});
context('when the given token ID does not exist', function () {
it('reverts', async function () {
await expectRevert(
transferFunction.call(this, owner, other, nonExistentTokenId, { from: owner }),
'ERC721: operator query for nonexistent token',
);
});
});
context('when the address to transfer the token to is the zero address', function () {
it('reverts', async function () {
await expectRevert(
transferFunction.call(this, owner, ZERO_ADDRESS, tokenId, { from: owner }),
'ERC721: transfer to the zero address',
);
});
});
};
describe('via transferFrom', function () {
shouldTransferTokensByUsers(function (from, to, tokenId, opts) {
return this.token.transferFrom(from, to, tokenId, opts);
});
});
describe('via safeTransferFrom', function () {
const safeTransferFromWithData = function (from, to, tokenId, opts) {
return this.token.methods['safeTransferFrom(address,address,uint256,bytes)'](from, to, tokenId, data, opts);
};
const safeTransferFromWithoutData = function (from, to, tokenId, opts) {
return this.token.methods['safeTransferFrom(address,address,uint256)'](from, to, tokenId, opts);
};
const shouldTransferSafely = function (transferFun, data) {
describe('to a user account', function () {
shouldTransferTokensByUsers(transferFun);
});
describe('to a valid receiver contract', function () {
beforeEach(async function () {
this.receiver = await ERC721ReceiverMock.new(RECEIVER_MAGIC_VALUE, Error.None);
this.toWhom = this.receiver.address;
});
shouldTransferTokensByUsers(transferFun);
it('calls onERC721Received', async function () {
const receipt = await transferFun.call(this, owner, this.receiver.address, tokenId, { from: owner });
await expectEvent.inTransaction(receipt.tx, ERC721ReceiverMock, 'Received', {
operator: owner,
from: owner,
tokenId: tokenId,
data: data,
});
});
it('calls onERC721Received from approved', async function () {
const receipt = await transferFun.call(this, owner, this.receiver.address, tokenId, { from: approved });
await expectEvent.inTransaction(receipt.tx, ERC721ReceiverMock, 'Received', {
operator: approved,
from: owner,
tokenId: tokenId,
data: data,
});
});
describe('with an invalid token id', function () {
it('reverts', async function () {
await expectRevert(
transferFun.call(
this,
owner,
this.receiver.address,
nonExistentTokenId,
{ from: owner },
),
'ERC721: operator query for nonexistent token',
);
});
});
});
};
describe('with data', function () {
shouldTransferSafely(safeTransferFromWithData, data);
});
describe('without data', function () {
shouldTransferSafely(safeTransferFromWithoutData, null);
});
describe('to a receiver contract returning unexpected value', function () {
it('reverts', async function () {
const invalidReceiver = await ERC721ReceiverMock.new('0x42', Error.None);
await expectRevert(
this.token.safeTransferFrom(owner, invalidReceiver.address, tokenId, { from: owner }),
'ERC721: transfer to non ERC721Receiver implementer',
);
});
});
describe('to a receiver contract that reverts with message', function () {
it('reverts', async function () {
const revertingReceiver = await ERC721ReceiverMock.new(RECEIVER_MAGIC_VALUE, Error.RevertWithMessage);
await expectRevert(
this.token.safeTransferFrom(owner, revertingReceiver.address, tokenId, { from: owner }),
'ERC721ReceiverMock: reverting',
);
});
});
describe('to a receiver contract that reverts without message', function () {
it('reverts', async function () {
const revertingReceiver = await ERC721ReceiverMock.new(RECEIVER_MAGIC_VALUE, Error.RevertWithoutMessage);
await expectRevert(
this.token.safeTransferFrom(owner, revertingReceiver.address, tokenId, { from: owner }),
'ERC721: transfer to non ERC721Receiver implementer',
);
});
});
describe('to a receiver contract that panics', function () {
it('reverts', async function () {
const revertingReceiver = await ERC721ReceiverMock.new(RECEIVER_MAGIC_VALUE, Error.Panic);
await expectRevert.unspecified(
this.token.safeTransferFrom(owner, revertingReceiver.address, tokenId, { from: owner }),
);
});
});
describe('to a contract that does not implement the required function', function () {
it('reverts', async function () {
const nonReceiver = this.token;
await expectRevert(
this.token.safeTransferFrom(owner, nonReceiver.address, tokenId, { from: owner }),
'ERC721: transfer to non ERC721Receiver implementer',
);
});
});
});
});
describe('safe mint', function () {
const tokenId = fourthTokenId;
const data = '0x42';
describe('via safeMint', function () { // regular minting is tested in ERC721Mintable.test.js and others
it('calls onERC721Received — with data', async function () {
this.receiver = await ERC721ReceiverMock.new(RECEIVER_MAGIC_VALUE, Error.None);
const receipt = await this.token.safeMint(this.receiver.address, tokenId, data);
await expectEvent.inTransaction(receipt.tx, ERC721ReceiverMock, 'Received', {
from: ZERO_ADDRESS,
tokenId: tokenId,
data: data,
});
});
it('calls onERC721Received — without data', async function () {
this.receiver = await ERC721ReceiverMock.new(RECEIVER_MAGIC_VALUE, Error.None);
const receipt = await this.token.safeMint(this.receiver.address, tokenId);
await expectEvent.inTransaction(receipt.tx, ERC721ReceiverMock, 'Received', {
from: ZERO_ADDRESS,
tokenId: tokenId,
});
});
context('to a receiver contract returning unexpected value', function () {
it('reverts', async function () {
const invalidReceiver = await ERC721ReceiverMock.new('0x42', Error.None);
await expectRevert(
this.token.safeMint(invalidReceiver.address, tokenId),
'ERC721: transfer to non ERC721Receiver implementer',
);
});
});
context('to a receiver contract that reverts with message', function () {
it('reverts', async function () {
const revertingReceiver = await ERC721ReceiverMock.new(RECEIVER_MAGIC_VALUE, Error.RevertWithMessage);
await expectRevert(
this.token.safeMint(revertingReceiver.address, tokenId),
'ERC721ReceiverMock: reverting',
);
});
});
context('to a receiver contract that reverts without message', function () {
it('reverts', async function () {
const revertingReceiver = await ERC721ReceiverMock.new(RECEIVER_MAGIC_VALUE, Error.RevertWithoutMessage);
await expectRevert(
this.token.safeMint(revertingReceiver.address, tokenId),
'ERC721: transfer to non ERC721Receiver implementer',
);
});
});
context('to a receiver contract that panics', function () {
it('reverts', async function () {
const revertingReceiver = await ERC721ReceiverMock.new(RECEIVER_MAGIC_VALUE, Error.Panic);
await expectRevert.unspecified(
this.token.safeMint(revertingReceiver.address, tokenId),
);
});
});
context('to a contract that does not implement the required function', function () {
it('reverts', async function () {
const nonReceiver = this.token;
await expectRevert(
this.token.safeMint(nonReceiver.address, tokenId),
'ERC721: transfer to non ERC721Receiver implementer',
);
});
});
});
});
describe('approve', function () {
const tokenId = firstTokenId;
let logs = null;
const itClearsApproval = function () {
it('clears approval for the token', async function () {
expect(await this.token.getApproved(tokenId)).to.be.equal(ZERO_ADDRESS);
});
};
const itApproves = function (address) {
it('sets the approval for the target address', async function () {
expect(await this.token.getApproved(tokenId)).to.be.equal(address);
});
};
const itEmitsApprovalEvent = function (address) {
it('emits an approval event', async function () {
expectEvent.inLogs(logs, 'Approval', {
owner: owner,
approved: address,
tokenId: tokenId,
});
});
};
context('when clearing approval', function () {
context('when there was no prior approval', function () {
beforeEach(async function () {
({ logs } = await this.token.approve(ZERO_ADDRESS, tokenId, { from: owner }));
});
itClearsApproval();
itEmitsApprovalEvent(ZERO_ADDRESS);
});
context('when there was a prior approval', function () {
beforeEach(async function () {
await this.token.approve(approved, tokenId, { from: owner });
({ logs } = await this.token.approve(ZERO_ADDRESS, tokenId, { from: owner }));
});
itClearsApproval();
itEmitsApprovalEvent(ZERO_ADDRESS);
});
});
context('when approving a non-zero address', function () {
context('when there was no prior approval', function () {
beforeEach(async function () {
({ logs } = await this.token.approve(approved, tokenId, { from: owner }));
});
itApproves(approved);
itEmitsApprovalEvent(approved);
});
context('when there was a prior approval to the same address', function () {
beforeEach(async function () {
await this.token.approve(approved, tokenId, { from: owner });
({ logs } = await this.token.approve(approved, tokenId, { from: owner }));
});
itApproves(approved);
itEmitsApprovalEvent(approved);
});
context('when there was a prior approval to a different address', function () {
beforeEach(async function () {
await this.token.approve(anotherApproved, tokenId, { from: owner });
({ logs } = await this.token.approve(anotherApproved, tokenId, { from: owner }));
});
itApproves(anotherApproved);
itEmitsApprovalEvent(anotherApproved);
});
});
context('when the address that receives the approval is the owner', function () {
it('reverts', async function () {
await expectRevert(
this.token.approve(owner, tokenId, { from: owner }), 'ERC721: approval to current owner',
);
});
});
context('when the sender does not own the given token ID', function () {
it('reverts', async function () {
await expectRevert(this.token.approve(approved, tokenId, { from: other }),
'ERC721: approve caller is not owner nor approved');
});
});
context('when the sender is approved for the given token ID', function () {
it('reverts', async function () {
await this.token.approve(approved, tokenId, { from: owner });
await expectRevert(this.token.approve(anotherApproved, tokenId, { from: approved }),
'ERC721: approve caller is not owner nor approved for all');
});
});
context('when the sender is an operator', function () {
beforeEach(async function () {
await this.token.setApprovalForAll(operator, true, { from: owner });
({ logs } = await this.token.approve(approved, tokenId, { from: operator }));
});
itApproves(approved);
itEmitsApprovalEvent(approved);
});
context('when the given token ID does not exist', function () {
it('reverts', async function () {
await expectRevert(this.token.approve(approved, nonExistentTokenId, { from: operator }),
'ERC721: owner query for nonexistent token');
});
});
});
describe('setApprovalForAll', function () {
context('when the operator willing to approve is not the owner', function () {
context('when there is no operator approval set by the sender', function () {
it('approves the operator', async function () {
await this.token.setApprovalForAll(operator, true, { from: owner });
expect(await this.token.isApprovedForAll(owner, operator)).to.equal(true);
});
it('emits an approval event', async function () {
const { logs } = await this.token.setApprovalForAll(operator, true, { from: owner });
expectEvent.inLogs(logs, 'ApprovalForAll', {
owner: owner,
operator: operator,
approved: true,
});
});
});
context('when the operator was set as not approved', function () {
beforeEach(async function () {
await this.token.setApprovalForAll(operator, false, { from: owner });
});
it('approves the operator', async function () {
await this.token.setApprovalForAll(operator, true, { from: owner });
expect(await this.token.isApprovedForAll(owner, operator)).to.equal(true);
});
it('emits an approval event', async function () {
const { logs } = await this.token.setApprovalForAll(operator, true, { from: owner });
expectEvent.inLogs(logs, 'ApprovalForAll', {
owner: owner,
operator: operator,
approved: true,
});
});
it('can unset the operator approval', async function () {
await this.token.setApprovalForAll(operator, false, { from: owner });
expect(await this.token.isApprovedForAll(owner, operator)).to.equal(false);
});
});
context('when the operator was already approved', function () {
beforeEach(async function () {
await this.token.setApprovalForAll(operator, true, { from: owner });
});
it('keeps the approval to the given address', async function () {
await this.token.setApprovalForAll(operator, true, { from: owner });
expect(await this.token.isApprovedForAll(owner, operator)).to.equal(true);
});
it('emits an approval event', async function () {
const { logs } = await this.token.setApprovalForAll(operator, true, { from: owner });
expectEvent.inLogs(logs, 'ApprovalForAll', {
owner: owner,
operator: operator,
approved: true,
});
});
});
});
context('when the operator is the owner', function () {
it('reverts', async function () {
await expectRevert(this.token.setApprovalForAll(owner, true, { from: owner }),
'ERC721: approve to caller');
});
});
});
describe('getApproved', async function () {
context('when token is not minted', async function () {
it('reverts', async function () {
await expectRevert(
this.token.getApproved(nonExistentTokenId),
'ERC721: approved query for nonexistent token',
);
});
});
context('when token has been minted ', async function () {
it('should return the zero address', async function () {
expect(await this.token.getApproved(firstTokenId)).to.be.equal(
ZERO_ADDRESS,
);
});
context('when account has been approved', async function () {
beforeEach(async function () {
await this.token.approve(approved, firstTokenId, { from: owner });
});
it('returns approved account', async function () {
expect(await this.token.getApproved(firstTokenId)).to.be.equal(approved);
});
});
});
});
});
describe('_mint(address, uint256)', function () {
it('reverts with a null destination address', async function () {
await expectRevert(
this.token.mint(ZERO_ADDRESS, firstTokenId), 'ERC721: mint to the zero address',
);
});
context('with minted token', async function () {
beforeEach(async function () {
({ logs: this.logs } = await this.token.mint(owner, firstTokenId));
});
it('emits a Transfer event', function () {
expectEvent.inLogs(this.logs, 'Transfer', { from: ZERO_ADDRESS, to: owner, tokenId: firstTokenId });
});
it('creates the token', async function () {
expect(await this.token.balanceOf(owner)).to.be.bignumber.equal('1');
expect(await this.token.ownerOf(firstTokenId)).to.equal(owner);
});
it('reverts when adding a token id that already exists', async function () {
await expectRevert(this.token.mint(owner, firstTokenId), 'ERC721: token already minted');
});
});
});
describe('_burn', function () {
it('reverts when burning a non-existent token id', async function () {
await expectRevert(
this.token.burn(nonExistentTokenId), 'ERC721: owner query for nonexistent token',
);
});
context('with minted tokens', function () {
beforeEach(async function () {
await this.token.mint(owner, firstTokenId);
await this.token.mint(owner, secondTokenId);
});
context('with burnt token', function () {
beforeEach(async function () {
({ logs: this.logs } = await this.token.burn(firstTokenId));
});
it('emits a Transfer event', function () {
expectEvent.inLogs(this.logs, 'Transfer', { from: owner, to: ZERO_ADDRESS, tokenId: firstTokenId });
});
it('emits an Approval event', function () {
expectEvent.inLogs(this.logs, 'Approval', { owner, approved: ZERO_ADDRESS, tokenId: firstTokenId });
});
it('deletes the token', async function () {
expect(await this.token.balanceOf(owner)).to.be.bignumber.equal('1');
await expectRevert(
this.token.ownerOf(firstTokenId), 'ERC721: owner query for nonexistent token',
);
});
it('reverts when burning a token id that has been deleted', async function () {
await expectRevert(
this.token.burn(firstTokenId), 'ERC721: owner query for nonexistent token',
);
});
});
});
});
}
function shouldBehaveLikeERC721Enumerable (errorPrefix, owner, newOwner, approved, anotherApproved, operator, other) {
shouldSupportInterfaces([
'ERC721Enumerable',
]);
context('with minted tokens', function () {
beforeEach(async function () {
await this.token.mint(owner, firstTokenId);
await this.token.mint(owner, secondTokenId);
this.toWhom = other; // default to other for toWhom in context-dependent tests
});
describe('totalSupply', function () {
it('returns total token supply', async function () {
expect(await this.token.totalSupply()).to.be.bignumber.equal('2');
});
});
describe('tokenOfOwnerByIndex', function () {
describe('when the given index is lower than the amount of tokens owned by the given address', function () {
it('returns the token ID placed at the given index', async function () {
expect(await this.token.tokenOfOwnerByIndex(owner, 0)).to.be.bignumber.equal(firstTokenId);
});
});
describe('when the index is greater than or equal to the total tokens owned by the given address', function () {
it('reverts', async function () {
await expectRevert(
this.token.tokenOfOwnerByIndex(owner, 2), 'ERC721Enumerable: owner index out of bounds',
);
});
});
describe('when the given address does not own any token', function () {
it('reverts', async function () {
await expectRevert(
this.token.tokenOfOwnerByIndex(other, 0), 'ERC721Enumerable: owner index out of bounds',
);
});
});
describe('after transferring all tokens to another user', function () {
beforeEach(async function () {
await this.token.transferFrom(owner, other, firstTokenId, { from: owner });
await this.token.transferFrom(owner, other, secondTokenId, { from: owner });
});
it('returns correct token IDs for target', async function () {
expect(await this.token.balanceOf(other)).to.be.bignumber.equal('2');
const tokensListed = await Promise.all(
[0, 1].map(i => this.token.tokenOfOwnerByIndex(other, i)),
);
expect(tokensListed.map(t => t.toNumber())).to.have.members([firstTokenId.toNumber(),
secondTokenId.toNumber()]);
});
it('returns empty collection for original owner', async function () {
expect(await this.token.balanceOf(owner)).to.be.bignumber.equal('0');
await expectRevert(
this.token.tokenOfOwnerByIndex(owner, 0), 'ERC721Enumerable: owner index out of bounds',
);
});
});
});
describe('tokenByIndex', function () {
it('returns all tokens', async function () {
const tokensListed = await Promise.all(
[0, 1].map(i => this.token.tokenByIndex(i)),
);
expect(tokensListed.map(t => t.toNumber())).to.have.members([firstTokenId.toNumber(),
secondTokenId.toNumber()]);
});
it('reverts if index is greater than supply', async function () {
await expectRevert(
this.token.tokenByIndex(2), 'ERC721Enumerable: global index out of bounds',
);
});
[firstTokenId, secondTokenId].forEach(function (tokenId) {
it(`returns all tokens after burning token ${tokenId} and minting new tokens`, async function () {
const newTokenId = new BN(300);
const anotherNewTokenId = new BN(400);
await this.token.burn(tokenId);
await this.token.mint(newOwner, newTokenId);
await this.token.mint(newOwner, anotherNewTokenId);
expect(await this.token.totalSupply()).to.be.bignumber.equal('3');
const tokensListed = await Promise.all(
[0, 1, 2].map(i => this.token.tokenByIndex(i)),
);
const expectedTokens = [firstTokenId, secondTokenId, newTokenId, anotherNewTokenId].filter(
x => (x !== tokenId),
);
expect(tokensListed.map(t => t.toNumber())).to.have.members(expectedTokens.map(t => t.toNumber()));
});
});
});
});
describe('_mint(address, uint256)', function () {
it('reverts with a null destination address', async function () {
await expectRevert(
this.token.mint(ZERO_ADDRESS, firstTokenId), 'ERC721: mint to the zero address',
);
});
context('with minted token', async function () {
beforeEach(async function () {
({ logs: this.logs } = await this.token.mint(owner, firstTokenId));
});
it('adjusts owner tokens by index', async function () {
expect(await this.token.tokenOfOwnerByIndex(owner, 0)).to.be.bignumber.equal(firstTokenId);
});
it('adjusts all tokens list', async function () {
expect(await this.token.tokenByIndex(0)).to.be.bignumber.equal(firstTokenId);
});
});
});
describe('_burn', function () {
it('reverts when burning a non-existent token id', async function () {
await expectRevert(
this.token.burn(firstTokenId), 'ERC721: owner query for nonexistent token',
);
});
context('with minted tokens', function () {
beforeEach(async function () {
await this.token.mint(owner, firstTokenId);
await this.token.mint(owner, secondTokenId);
});
context('with burnt token', function () {
beforeEach(async function () {
({ logs: this.logs } = await this.token.burn(firstTokenId));
});
it('removes that token from the token list of the owner', async function () {
expect(await this.token.tokenOfOwnerByIndex(owner, 0)).to.be.bignumber.equal(secondTokenId);
});
it('adjusts all tokens list', async function () {
expect(await this.token.tokenByIndex(0)).to.be.bignumber.equal(secondTokenId);
});
it('burns all tokens', async function () {
await this.token.burn(secondTokenId, { from: owner });
expect(await this.token.totalSupply()).to.be.bignumber.equal('0');
await expectRevert(
this.token.tokenByIndex(0), 'ERC721Enumerable: global index out of bounds',
);
});
});
});
});
}
function shouldBehaveLikeERC721Metadata (errorPrefix, name, symbol, owner) {
shouldSupportInterfaces([
'ERC721Metadata',
]);
describe('metadata', function () {
it('has a name', async function () {
expect(await this.token.name()).to.be.equal(name);
});
it('has a symbol', async function () {
expect(await this.token.symbol()).to.be.equal(symbol);
});
describe('token URI', function () {
beforeEach(async function () {
await this.token.mint(owner, firstTokenId);
});
it('return empty string by default', async function () {
expect(await this.token.tokenURI(firstTokenId)).to.be.equal('');
});
it('reverts when queried for non existent token id', async function () {
await expectRevert(
this.token.tokenURI(nonExistentTokenId), 'ERC721Metadata: URI query for nonexistent token',
);
});
describe('base URI', function () {
beforeEach(function () {
if (this.token.setBaseURI === undefined) {
this.skip();
}
});
it('base URI can be set', async function () {
await this.token.setBaseURI(baseURI);
expect(await this.token.baseURI()).to.equal(baseURI);
});
it('base URI is added as a prefix to the token URI', async function () {
await this.token.setBaseURI(baseURI);
expect(await this.token.tokenURI(firstTokenId)).to.be.equal(baseURI + firstTokenId.toString());
});
it('token URI can be changed by changing the base URI', async function () {
await this.token.setBaseURI(baseURI);
const newBaseURI = 'https://api.example.com/v2/';
await this.token.setBaseURI(newBaseURI);
expect(await this.token.tokenURI(firstTokenId)).to.be.equal(newBaseURI + firstTokenId.toString());
});
});
});
});
}
module.exports = {
shouldBehaveLikeERC721,
shouldBehaveLikeERC721Enumerable,
shouldBehaveLikeERC721Metadata,
};