123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946 |
- const { ethers } = require('hardhat');
- const { expect } = require('chai');
- const { PANIC_CODES } = require('@nomicfoundation/hardhat-chai-matchers/panic');
- const { anyValue } = require('@nomicfoundation/hardhat-chai-matchers/withArgs');
- const { shouldSupportInterfaces } = require('../../utils/introspection/SupportsInterface.behavior');
- const { RevertType } = require('../../helpers/enums');
- const firstTokenId = 5042n;
- const secondTokenId = 79217n;
- const nonExistentTokenId = 13n;
- const fourthTokenId = 4n;
- const RECEIVER_MAGIC_VALUE = '0x150b7a02';
- function shouldBehaveLikeERC721() {
- beforeEach(async function () {
- const [owner, newOwner, approved, operator, other] = this.accounts;
- Object.assign(this, { owner, newOwner, approved, operator, other });
- });
- shouldSupportInterfaces(['ERC721']);
- describe('with minted tokens', function () {
- beforeEach(async function () {
- await this.token.$_mint(this.owner, firstTokenId);
- await this.token.$_mint(this.owner, secondTokenId);
- this.to = this.other;
- });
- describe('balanceOf', function () {
- describe('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(this.owner)).to.equal(2n);
- });
- });
- describe('when the given address does not own any tokens', function () {
- it('returns 0', async function () {
- expect(await this.token.balanceOf(this.other)).to.equal(0n);
- });
- });
- describe('when querying the zero address', function () {
- it('throws', async function () {
- await expect(this.token.balanceOf(ethers.ZeroAddress))
- .to.be.revertedWithCustomError(this.token, 'ERC721InvalidOwner')
- .withArgs(ethers.ZeroAddress);
- });
- });
- });
- describe('ownerOf', function () {
- describe('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.equal(this.owner);
- });
- });
- describe('when the given token ID was not tracked by this token', function () {
- const tokenId = nonExistentTokenId;
- it('reverts', async function () {
- await expect(this.token.ownerOf(tokenId))
- .to.be.revertedWithCustomError(this.token, 'ERC721NonexistentToken')
- .withArgs(tokenId);
- });
- });
- });
- describe('transfers', function () {
- const tokenId = firstTokenId;
- const data = '0x42';
- beforeEach(async function () {
- await this.token.connect(this.owner).approve(this.approved, tokenId);
- await this.token.connect(this.owner).setApprovalForAll(this.operator, true);
- });
- const transferWasSuccessful = () => {
- it('transfers the ownership of the given token ID to the given address', async function () {
- await this.tx();
- expect(await this.token.ownerOf(tokenId)).to.equal(this.to);
- });
- it('emits a Transfer event', async function () {
- await expect(this.tx()).to.emit(this.token, 'Transfer').withArgs(this.owner, this.to, tokenId);
- });
- it('clears the approval for the token ID with no event', async function () {
- await expect(this.tx()).to.not.emit(this.token, 'Approval');
- expect(await this.token.getApproved(tokenId)).to.equal(ethers.ZeroAddress);
- });
- it('adjusts owners balances', async function () {
- const balanceBefore = await this.token.balanceOf(this.owner);
- await this.tx();
- expect(await this.token.balanceOf(this.owner)).to.equal(balanceBefore - 1n);
- });
- it('adjusts owners tokens by index', async function () {
- if (!this.token.tokenOfOwnerByIndex) return;
- await this.tx();
- expect(await this.token.tokenOfOwnerByIndex(this.to, 0n)).to.equal(tokenId);
- expect(await this.token.tokenOfOwnerByIndex(this.owner, 0n)).to.not.equal(tokenId);
- });
- };
- const shouldTransferTokensByUsers = function (fragment, opts = {}) {
- describe('when called by the owner', function () {
- beforeEach(async function () {
- this.tx = () =>
- this.token.connect(this.owner)[fragment](this.owner, this.to, tokenId, ...(opts.extra ?? []));
- });
- transferWasSuccessful();
- });
- describe('when called by the approved individual', function () {
- beforeEach(async function () {
- this.tx = () =>
- this.token.connect(this.approved)[fragment](this.owner, this.to, tokenId, ...(opts.extra ?? []));
- });
- transferWasSuccessful();
- });
- describe('when called by the operator', function () {
- beforeEach(async function () {
- this.tx = () =>
- this.token.connect(this.operator)[fragment](this.owner, this.to, tokenId, ...(opts.extra ?? []));
- });
- transferWasSuccessful();
- });
- describe('when called by the owner without an approved user', function () {
- beforeEach(async function () {
- await this.token.connect(this.owner).approve(ethers.ZeroAddress, tokenId);
- this.tx = () =>
- this.token.connect(this.operator)[fragment](this.owner, this.to, tokenId, ...(opts.extra ?? []));
- });
- transferWasSuccessful();
- });
- describe('when sent to the owner', function () {
- beforeEach(async function () {
- this.tx = () =>
- this.token.connect(this.owner)[fragment](this.owner, this.owner, tokenId, ...(opts.extra ?? []));
- });
- it('keeps ownership of the token', async function () {
- await this.tx();
- expect(await this.token.ownerOf(tokenId)).to.equal(this.owner);
- });
- it('clears the approval for the token ID', async function () {
- await this.tx();
- expect(await this.token.getApproved(tokenId)).to.equal(ethers.ZeroAddress);
- });
- it('emits only a transfer event', async function () {
- await expect(this.tx()).to.emit(this.token, 'Transfer').withArgs(this.owner, this.owner, tokenId);
- });
- it('keeps the owner balance', async function () {
- const balanceBefore = await this.token.balanceOf(this.owner);
- await this.tx();
- expect(await this.token.balanceOf(this.owner)).to.equal(balanceBefore);
- });
- it('keeps same tokens by index', async function () {
- if (!this.token.tokenOfOwnerByIndex) return;
- expect(await Promise.all([0n, 1n].map(i => this.token.tokenOfOwnerByIndex(this.owner, i)))).to.have.members(
- [firstTokenId, secondTokenId],
- );
- });
- });
- describe('when the address of the previous owner is incorrect', function () {
- it('reverts', async function () {
- await expect(
- this.token.connect(this.owner)[fragment](this.other, this.other, tokenId, ...(opts.extra ?? [])),
- )
- .to.be.revertedWithCustomError(this.token, 'ERC721IncorrectOwner')
- .withArgs(this.other, tokenId, this.owner);
- });
- });
- describe('when the sender is not authorized for the token id', function () {
- if (opts.unrestricted) {
- it('does not revert', async function () {
- await this.token.connect(this.other)[fragment](this.owner, this.other, tokenId, ...(opts.extra ?? []));
- });
- } else {
- it('reverts', async function () {
- await expect(
- this.token.connect(this.other)[fragment](this.owner, this.other, tokenId, ...(opts.extra ?? [])),
- )
- .to.be.revertedWithCustomError(this.token, 'ERC721InsufficientApproval')
- .withArgs(this.other, tokenId);
- });
- }
- });
- describe('when the given token ID does not exist', function () {
- it('reverts', async function () {
- await expect(
- this.token
- .connect(this.owner)
- [fragment](this.owner, this.other, nonExistentTokenId, ...(opts.extra ?? [])),
- )
- .to.be.revertedWithCustomError(this.token, 'ERC721NonexistentToken')
- .withArgs(nonExistentTokenId);
- });
- });
- describe('when the address to transfer the token to is the zero address', function () {
- it('reverts', async function () {
- await expect(
- this.token.connect(this.owner)[fragment](this.owner, ethers.ZeroAddress, tokenId, ...(opts.extra ?? [])),
- )
- .to.be.revertedWithCustomError(this.token, 'ERC721InvalidReceiver')
- .withArgs(ethers.ZeroAddress);
- });
- });
- };
- const shouldTransferSafely = function (fragment, data, opts = {}) {
- // sanity
- it('function exists', async function () {
- expect(this.token.interface.hasFunction(fragment)).to.be.true;
- });
- describe('to a user account', function () {
- shouldTransferTokensByUsers(fragment, opts);
- });
- describe('to a valid receiver contract', function () {
- beforeEach(async function () {
- this.to = await ethers.deployContract('ERC721ReceiverMock', [RECEIVER_MAGIC_VALUE, RevertType.None]);
- });
- shouldTransferTokensByUsers(fragment, opts);
- it('calls onERC721Received', async function () {
- await expect(this.token.connect(this.owner)[fragment](this.owner, this.to, tokenId, ...(opts.extra ?? [])))
- .to.emit(this.to, 'Received')
- .withArgs(this.owner, this.owner, tokenId, data, anyValue);
- });
- it('calls onERC721Received from approved', async function () {
- await expect(
- this.token.connect(this.approved)[fragment](this.owner, this.to, tokenId, ...(opts.extra ?? [])),
- )
- .to.emit(this.to, 'Received')
- .withArgs(this.approved, this.owner, tokenId, data, anyValue);
- });
- describe('with an invalid token id', function () {
- it('reverts', async function () {
- await expect(
- this.token
- .connect(this.approved)
- [fragment](this.owner, this.to, nonExistentTokenId, ...(opts.extra ?? [])),
- )
- .to.be.revertedWithCustomError(this.token, 'ERC721NonexistentToken')
- .withArgs(nonExistentTokenId);
- });
- });
- });
- };
- for (const { fnName, opts } of [
- { fnName: 'transferFrom', opts: {} },
- { fnName: '$_transfer', opts: { unrestricted: true } },
- ]) {
- describe(`via ${fnName}`, function () {
- shouldTransferTokensByUsers(fnName, opts);
- });
- }
- for (const { fnName, opts } of [
- { fnName: 'safeTransferFrom', opts: {} },
- { fnName: '$_safeTransfer', opts: { unrestricted: true } },
- ]) {
- describe(`via ${fnName}`, function () {
- describe('with data', function () {
- shouldTransferSafely(fnName, data, { ...opts, extra: [ethers.Typed.bytes(data)] });
- });
- describe('without data', function () {
- shouldTransferSafely(fnName, '0x', opts);
- });
- describe('to a receiver contract returning unexpected value', function () {
- it('reverts', async function () {
- const invalidReceiver = await ethers.deployContract('ERC721ReceiverMock', [
- '0xdeadbeef',
- RevertType.None,
- ]);
- await expect(this.token.connect(this.owner)[fnName](this.owner, invalidReceiver, tokenId))
- .to.be.revertedWithCustomError(this.token, 'ERC721InvalidReceiver')
- .withArgs(invalidReceiver);
- });
- });
- describe('to a receiver contract that reverts with message', function () {
- it('reverts', async function () {
- const revertingReceiver = await ethers.deployContract('ERC721ReceiverMock', [
- RECEIVER_MAGIC_VALUE,
- RevertType.RevertWithMessage,
- ]);
- await expect(
- this.token.connect(this.owner)[fnName](this.owner, revertingReceiver, tokenId),
- ).to.be.revertedWith('ERC721ReceiverMock: reverting');
- });
- });
- describe('to a receiver contract that reverts without message', function () {
- it('reverts', async function () {
- const revertingReceiver = await ethers.deployContract('ERC721ReceiverMock', [
- RECEIVER_MAGIC_VALUE,
- RevertType.RevertWithoutMessage,
- ]);
- await expect(this.token.connect(this.owner)[fnName](this.owner, revertingReceiver, tokenId))
- .to.be.revertedWithCustomError(this.token, 'ERC721InvalidReceiver')
- .withArgs(revertingReceiver);
- });
- });
- describe('to a receiver contract that reverts with custom error', function () {
- it('reverts', async function () {
- const revertingReceiver = await ethers.deployContract('ERC721ReceiverMock', [
- RECEIVER_MAGIC_VALUE,
- RevertType.RevertWithCustomError,
- ]);
- await expect(this.token.connect(this.owner)[fnName](this.owner, revertingReceiver, tokenId))
- .to.be.revertedWithCustomError(revertingReceiver, 'CustomError')
- .withArgs(RECEIVER_MAGIC_VALUE);
- });
- });
- describe('to a receiver contract that panics', function () {
- it('reverts', async function () {
- const revertingReceiver = await ethers.deployContract('ERC721ReceiverMock', [
- RECEIVER_MAGIC_VALUE,
- RevertType.Panic,
- ]);
- await expect(
- this.token.connect(this.owner)[fnName](this.owner, revertingReceiver, tokenId),
- ).to.be.revertedWithPanic(PANIC_CODES.DIVISION_BY_ZERO);
- });
- });
- describe('to a contract that does not implement the required function', function () {
- it('reverts', async function () {
- const nonReceiver = await ethers.deployContract('CallReceiverMock');
- await expect(this.token.connect(this.owner)[fnName](this.owner, nonReceiver, tokenId))
- .to.be.revertedWithCustomError(this.token, 'ERC721InvalidReceiver')
- .withArgs(nonReceiver);
- });
- });
- });
- }
- });
- 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 () {
- const receiver = await ethers.deployContract('ERC721ReceiverMock', [RECEIVER_MAGIC_VALUE, RevertType.None]);
- await expect(await this.token.$_safeMint(receiver, tokenId, ethers.Typed.bytes(data)))
- .to.emit(receiver, 'Received')
- .withArgs(anyValue, ethers.ZeroAddress, tokenId, data, anyValue);
- });
- it('calls onERC721Received — without data', async function () {
- const receiver = await ethers.deployContract('ERC721ReceiverMock', [RECEIVER_MAGIC_VALUE, RevertType.None]);
- await expect(await this.token.$_safeMint(receiver, tokenId))
- .to.emit(receiver, 'Received')
- .withArgs(anyValue, ethers.ZeroAddress, tokenId, '0x', anyValue);
- });
- describe('to a receiver contract returning unexpected value', function () {
- it('reverts', async function () {
- const invalidReceiver = await ethers.deployContract('ERC721ReceiverMock', ['0xdeadbeef', RevertType.None]);
- await expect(this.token.$_safeMint(invalidReceiver, tokenId))
- .to.be.revertedWithCustomError(this.token, 'ERC721InvalidReceiver')
- .withArgs(invalidReceiver);
- });
- });
- describe('to a receiver contract that reverts with message', function () {
- it('reverts', async function () {
- const revertingReceiver = await ethers.deployContract('ERC721ReceiverMock', [
- RECEIVER_MAGIC_VALUE,
- RevertType.RevertWithMessage,
- ]);
- await expect(this.token.$_safeMint(revertingReceiver, tokenId)).to.be.revertedWith(
- 'ERC721ReceiverMock: reverting',
- );
- });
- });
- describe('to a receiver contract that reverts without message', function () {
- it('reverts', async function () {
- const revertingReceiver = await ethers.deployContract('ERC721ReceiverMock', [
- RECEIVER_MAGIC_VALUE,
- RevertType.RevertWithoutMessage,
- ]);
- await expect(this.token.$_safeMint(revertingReceiver, tokenId))
- .to.be.revertedWithCustomError(this.token, 'ERC721InvalidReceiver')
- .withArgs(revertingReceiver);
- });
- });
- describe('to a receiver contract that reverts with custom error', function () {
- it('reverts', async function () {
- const revertingReceiver = await ethers.deployContract('ERC721ReceiverMock', [
- RECEIVER_MAGIC_VALUE,
- RevertType.RevertWithCustomError,
- ]);
- await expect(this.token.$_safeMint(revertingReceiver, tokenId))
- .to.be.revertedWithCustomError(revertingReceiver, 'CustomError')
- .withArgs(RECEIVER_MAGIC_VALUE);
- });
- });
- describe('to a receiver contract that panics', function () {
- it('reverts', async function () {
- const revertingReceiver = await ethers.deployContract('ERC721ReceiverMock', [
- RECEIVER_MAGIC_VALUE,
- RevertType.Panic,
- ]);
- await expect(this.token.$_safeMint(revertingReceiver, tokenId)).to.be.revertedWithPanic(
- PANIC_CODES.DIVISION_BY_ZERO,
- );
- });
- });
- describe('to a contract that does not implement the required function', function () {
- it('reverts', async function () {
- const nonReceiver = await ethers.deployContract('CallReceiverMock');
- await expect(this.token.$_safeMint(nonReceiver, tokenId))
- .to.be.revertedWithCustomError(this.token, 'ERC721InvalidReceiver')
- .withArgs(nonReceiver);
- });
- });
- });
- });
- describe('approve', function () {
- const tokenId = firstTokenId;
- const itClearsApproval = function () {
- it('clears approval for the token', async function () {
- expect(await this.token.getApproved(tokenId)).to.equal(ethers.ZeroAddress);
- });
- };
- const itApproves = function () {
- it('sets the approval for the target address', async function () {
- expect(await this.token.getApproved(tokenId)).to.equal(this.approved ?? this.approved);
- });
- };
- const itEmitsApprovalEvent = function () {
- it('emits an approval event', async function () {
- await expect(this.tx)
- .to.emit(this.token, 'Approval')
- .withArgs(this.owner, this.approved ?? this.approved, tokenId);
- });
- };
- describe('when clearing approval', function () {
- describe('when there was no prior approval', function () {
- beforeEach(async function () {
- this.approved = ethers.ZeroAddress;
- this.tx = await this.token.connect(this.owner).approve(this.approved, tokenId);
- });
- itClearsApproval();
- itEmitsApprovalEvent();
- });
- describe('when there was a prior approval', function () {
- beforeEach(async function () {
- await this.token.connect(this.owner).approve(this.other, tokenId);
- this.approved = ethers.ZeroAddress;
- this.tx = await this.token.connect(this.owner).approve(this.approved, tokenId);
- });
- itClearsApproval();
- itEmitsApprovalEvent();
- });
- });
- describe('when approving a non-zero address', function () {
- describe('when there was no prior approval', function () {
- beforeEach(async function () {
- this.tx = await this.token.connect(this.owner).approve(this.approved, tokenId);
- });
- itApproves();
- itEmitsApprovalEvent();
- });
- describe('when there was a prior approval to the same address', function () {
- beforeEach(async function () {
- await this.token.connect(this.owner).approve(this.approved, tokenId);
- this.tx = await this.token.connect(this.owner).approve(this.approved, tokenId);
- });
- itApproves();
- itEmitsApprovalEvent();
- });
- describe('when there was a prior approval to a different address', function () {
- beforeEach(async function () {
- await this.token.connect(this.owner).approve(this.other, tokenId);
- this.tx = await this.token.connect(this.owner).approve(this.approved, tokenId);
- });
- itApproves();
- itEmitsApprovalEvent();
- });
- });
- describe('when the sender does not own the given token ID', function () {
- it('reverts', async function () {
- await expect(this.token.connect(this.other).approve(this.approved, tokenId))
- .to.be.revertedWithCustomError(this.token, 'ERC721InvalidApprover')
- .withArgs(this.other);
- });
- });
- describe('when the sender is approved for the given token ID', function () {
- it('reverts', async function () {
- await this.token.connect(this.owner).approve(this.approved, tokenId);
- await expect(this.token.connect(this.approved).approve(this.other, tokenId))
- .to.be.revertedWithCustomError(this.token, 'ERC721InvalidApprover')
- .withArgs(this.approved);
- });
- });
- describe('when the sender is an operator', function () {
- beforeEach(async function () {
- await this.token.connect(this.owner).setApprovalForAll(this.operator, true);
- this.tx = await this.token.connect(this.operator).approve(this.approved, tokenId);
- });
- itApproves();
- itEmitsApprovalEvent();
- });
- describe('when the given token ID does not exist', function () {
- it('reverts', async function () {
- await expect(this.token.connect(this.operator).approve(this.approved, nonExistentTokenId))
- .to.be.revertedWithCustomError(this.token, 'ERC721NonexistentToken')
- .withArgs(nonExistentTokenId);
- });
- });
- });
- describe('setApprovalForAll', function () {
- describe('when the operator willing to approve is not the owner', function () {
- describe('when there is no operator approval set by the sender', function () {
- it('approves the operator', async function () {
- await this.token.connect(this.owner).setApprovalForAll(this.operator, true);
- expect(await this.token.isApprovedForAll(this.owner, this.operator)).to.be.true;
- });
- it('emits an approval event', async function () {
- await expect(this.token.connect(this.owner).setApprovalForAll(this.operator, true))
- .to.emit(this.token, 'ApprovalForAll')
- .withArgs(this.owner, this.operator, true);
- });
- });
- describe('when the operator was set as not approved', function () {
- beforeEach(async function () {
- await this.token.connect(this.owner).setApprovalForAll(this.operator, false);
- });
- it('approves the operator', async function () {
- await this.token.connect(this.owner).setApprovalForAll(this.operator, true);
- expect(await this.token.isApprovedForAll(this.owner, this.operator)).to.be.true;
- });
- it('emits an approval event', async function () {
- await expect(this.token.connect(this.owner).setApprovalForAll(this.operator, true))
- .to.emit(this.token, 'ApprovalForAll')
- .withArgs(this.owner, this.operator, true);
- });
- it('can unset the operator approval', async function () {
- await this.token.connect(this.owner).setApprovalForAll(this.operator, false);
- expect(await this.token.isApprovedForAll(this.owner, this.operator)).to.be.false;
- });
- });
- describe('when the operator was already approved', function () {
- beforeEach(async function () {
- await this.token.connect(this.owner).setApprovalForAll(this.operator, true);
- });
- it('keeps the approval to the given address', async function () {
- await this.token.connect(this.owner).setApprovalForAll(this.operator, true);
- expect(await this.token.isApprovedForAll(this.owner, this.operator)).to.be.true;
- });
- it('emits an approval event', async function () {
- await expect(this.token.connect(this.owner).setApprovalForAll(this.operator, true))
- .to.emit(this.token, 'ApprovalForAll')
- .withArgs(this.owner, this.operator, true);
- });
- });
- });
- describe('when the operator is address zero', function () {
- it('reverts', async function () {
- await expect(this.token.connect(this.owner).setApprovalForAll(ethers.ZeroAddress, true))
- .to.be.revertedWithCustomError(this.token, 'ERC721InvalidOperator')
- .withArgs(ethers.ZeroAddress);
- });
- });
- });
- describe('getApproved', function () {
- describe('when token is not minted', function () {
- it('reverts', async function () {
- await expect(this.token.getApproved(nonExistentTokenId))
- .to.be.revertedWithCustomError(this.token, 'ERC721NonexistentToken')
- .withArgs(nonExistentTokenId);
- });
- });
- describe('when token has been minted ', function () {
- it('should return the zero address', async function () {
- expect(await this.token.getApproved(firstTokenId)).to.equal(ethers.ZeroAddress);
- });
- describe('when account has been approved', function () {
- beforeEach(async function () {
- await this.token.connect(this.owner).approve(this.approved, firstTokenId);
- });
- it('returns approved account', async function () {
- expect(await this.token.getApproved(firstTokenId)).to.equal(this.approved);
- });
- });
- });
- });
- });
- describe('_mint(address, uint256)', function () {
- it('reverts with a null destination address', async function () {
- await expect(this.token.$_mint(ethers.ZeroAddress, firstTokenId))
- .to.be.revertedWithCustomError(this.token, 'ERC721InvalidReceiver')
- .withArgs(ethers.ZeroAddress);
- });
- describe('with minted token', function () {
- beforeEach(async function () {
- this.tx = await this.token.$_mint(this.owner, firstTokenId);
- });
- it('emits a Transfer event', async function () {
- await expect(this.tx).to.emit(this.token, 'Transfer').withArgs(ethers.ZeroAddress, this.owner, firstTokenId);
- });
- it('creates the token', async function () {
- expect(await this.token.balanceOf(this.owner)).to.equal(1n);
- expect(await this.token.ownerOf(firstTokenId)).to.equal(this.owner);
- });
- it('reverts when adding a token id that already exists', async function () {
- await expect(this.token.$_mint(this.owner, firstTokenId))
- .to.be.revertedWithCustomError(this.token, 'ERC721InvalidSender')
- .withArgs(ethers.ZeroAddress);
- });
- });
- });
- describe('_burn', function () {
- it('reverts when burning a non-existent token id', async function () {
- await expect(this.token.$_burn(nonExistentTokenId))
- .to.be.revertedWithCustomError(this.token, 'ERC721NonexistentToken')
- .withArgs(nonExistentTokenId);
- });
- describe('with minted tokens', function () {
- beforeEach(async function () {
- await this.token.$_mint(this.owner, firstTokenId);
- await this.token.$_mint(this.owner, secondTokenId);
- });
- describe('with burnt token', function () {
- beforeEach(async function () {
- this.tx = await this.token.$_burn(firstTokenId);
- });
- it('emits a Transfer event', async function () {
- await expect(this.tx).to.emit(this.token, 'Transfer').withArgs(this.owner, ethers.ZeroAddress, firstTokenId);
- });
- it('deletes the token', async function () {
- expect(await this.token.balanceOf(this.owner)).to.equal(1n);
- await expect(this.token.ownerOf(firstTokenId))
- .to.be.revertedWithCustomError(this.token, 'ERC721NonexistentToken')
- .withArgs(firstTokenId);
- });
- it('reverts when burning a token id that has been deleted', async function () {
- await expect(this.token.$_burn(firstTokenId))
- .to.be.revertedWithCustomError(this.token, 'ERC721NonexistentToken')
- .withArgs(firstTokenId);
- });
- });
- });
- });
- }
- function shouldBehaveLikeERC721Enumerable() {
- beforeEach(async function () {
- const [owner, newOwner, approved, operator, other] = this.accounts;
- Object.assign(this, { owner, newOwner, approved, operator, other });
- });
- shouldSupportInterfaces(['ERC721Enumerable']);
- describe('with minted tokens', function () {
- beforeEach(async function () {
- await this.token.$_mint(this.owner, firstTokenId);
- await this.token.$_mint(this.owner, secondTokenId);
- this.to = this.other;
- });
- describe('totalSupply', function () {
- it('returns total token supply', async function () {
- expect(await this.token.totalSupply()).to.equal(2n);
- });
- });
- 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(this.owner, 0n)).to.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 expect(this.token.tokenOfOwnerByIndex(this.owner, 2n))
- .to.be.revertedWithCustomError(this.token, 'ERC721OutOfBoundsIndex')
- .withArgs(this.owner, 2n);
- });
- });
- describe('when the given address does not own any token', function () {
- it('reverts', async function () {
- await expect(this.token.tokenOfOwnerByIndex(this.other, 0n))
- .to.be.revertedWithCustomError(this.token, 'ERC721OutOfBoundsIndex')
- .withArgs(this.other, 0n);
- });
- });
- describe('after transferring all tokens to another user', function () {
- beforeEach(async function () {
- await this.token.connect(this.owner).transferFrom(this.owner, this.other, firstTokenId);
- await this.token.connect(this.owner).transferFrom(this.owner, this.other, secondTokenId);
- });
- it('returns correct token IDs for target', async function () {
- expect(await this.token.balanceOf(this.other)).to.equal(2n);
- expect(await Promise.all([0n, 1n].map(i => this.token.tokenOfOwnerByIndex(this.other, i)))).to.have.members([
- firstTokenId,
- secondTokenId,
- ]);
- });
- it('returns empty collection for original owner', async function () {
- expect(await this.token.balanceOf(this.owner)).to.equal(0n);
- await expect(this.token.tokenOfOwnerByIndex(this.owner, 0n))
- .to.be.revertedWithCustomError(this.token, 'ERC721OutOfBoundsIndex')
- .withArgs(this.owner, 0n);
- });
- });
- });
- describe('tokenByIndex', function () {
- it('returns all tokens', async function () {
- expect(await Promise.all([0n, 1n].map(i => this.token.tokenByIndex(i)))).to.have.members([
- firstTokenId,
- secondTokenId,
- ]);
- });
- it('reverts if index is greater than supply', async function () {
- await expect(this.token.tokenByIndex(2n))
- .to.be.revertedWithCustomError(this.token, 'ERC721OutOfBoundsIndex')
- .withArgs(ethers.ZeroAddress, 2n);
- });
- for (const tokenId of [firstTokenId, secondTokenId]) {
- it(`returns all tokens after burning token ${tokenId} and minting new tokens`, async function () {
- const newTokenId = 300n;
- const anotherNewTokenId = 400n;
- await this.token.$_burn(tokenId);
- await this.token.$_mint(this.newOwner, newTokenId);
- await this.token.$_mint(this.newOwner, anotherNewTokenId);
- expect(await this.token.totalSupply()).to.equal(3n);
- expect(await Promise.all([0n, 1n, 2n].map(i => this.token.tokenByIndex(i))))
- .to.have.members([firstTokenId, secondTokenId, newTokenId, anotherNewTokenId].filter(x => x !== tokenId))
- .to.not.include(tokenId);
- });
- }
- });
- });
- describe('_mint(address, uint256)', function () {
- it('reverts with a null destination address', async function () {
- await expect(this.token.$_mint(ethers.ZeroAddress, firstTokenId))
- .to.be.revertedWithCustomError(this.token, 'ERC721InvalidReceiver')
- .withArgs(ethers.ZeroAddress);
- });
- describe('with minted token', function () {
- beforeEach(async function () {
- await this.token.$_mint(this.owner, firstTokenId);
- });
- it('adjusts owner tokens by index', async function () {
- expect(await this.token.tokenOfOwnerByIndex(this.owner, 0n)).to.equal(firstTokenId);
- });
- it('adjusts all tokens list', async function () {
- expect(await this.token.tokenByIndex(0n)).to.equal(firstTokenId);
- });
- });
- });
- describe('_burn', function () {
- it('reverts when burning a non-existent token id', async function () {
- await expect(this.token.$_burn(firstTokenId))
- .to.be.revertedWithCustomError(this.token, 'ERC721NonexistentToken')
- .withArgs(firstTokenId);
- });
- describe('with minted tokens', function () {
- beforeEach(async function () {
- await this.token.$_mint(this.owner, firstTokenId);
- await this.token.$_mint(this.owner, secondTokenId);
- });
- describe('with burnt token', function () {
- beforeEach(async function () {
- await this.token.$_burn(firstTokenId);
- });
- it('removes that token from the token list of the owner', async function () {
- expect(await this.token.tokenOfOwnerByIndex(this.owner, 0n)).to.equal(secondTokenId);
- });
- it('adjusts all tokens list', async function () {
- expect(await this.token.tokenByIndex(0n)).to.equal(secondTokenId);
- });
- it('burns all tokens', async function () {
- await this.token.$_burn(secondTokenId);
- expect(await this.token.totalSupply()).to.equal(0n);
- await expect(this.token.tokenByIndex(0n))
- .to.be.revertedWithCustomError(this.token, 'ERC721OutOfBoundsIndex')
- .withArgs(ethers.ZeroAddress, 0n);
- });
- });
- });
- });
- }
- function shouldBehaveLikeERC721Metadata(name, symbol) {
- shouldSupportInterfaces(['ERC721Metadata']);
- describe('metadata', function () {
- it('has a name', async function () {
- expect(await this.token.name()).to.equal(name);
- });
- it('has a symbol', async function () {
- expect(await this.token.symbol()).to.equal(symbol);
- });
- describe('token URI', function () {
- beforeEach(async function () {
- await this.token.$_mint(this.owner, firstTokenId);
- });
- it('return empty string by default', async function () {
- expect(await this.token.tokenURI(firstTokenId)).to.equal('');
- });
- it('reverts when queried for non existent token id', async function () {
- await expect(this.token.tokenURI(nonExistentTokenId))
- .to.be.revertedWithCustomError(this.token, 'ERC721NonexistentToken')
- .withArgs(nonExistentTokenId);
- });
- });
- });
- }
- module.exports = {
- shouldBehaveLikeERC721,
- shouldBehaveLikeERC721Enumerable,
- shouldBehaveLikeERC721Metadata,
- };
|