123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628 |
- const { contract } = require('@openzeppelin/test-environment');
- const { BN, constants, expectEvent, expectRevert } = require('@openzeppelin/test-helpers');
- const { expect } = require('chai');
- const { ZERO_ADDRESS } = constants;
- const { shouldSupportInterfaces } = require('../../introspection/SupportsInterface.behavior');
- const ERC721ReceiverMock = contract.fromArtifact('ERC721ReceiverMock');
- const ERC721Mock = contract.fromArtifact('ERC721Mock');
- function shouldBehaveLikeERC721 (
- creator,
- minter,
- [owner, approved, anotherApproved, operator, other]
- ) {
- const firstTokenId = new BN(1);
- const secondTokenId = new BN(2);
- const unknownTokenId = new BN(3);
- const RECEIVER_MAGIC_VALUE = '0x150b7a02';
- describe('like an ERC721', function () {
- beforeEach(async function () {
- await this.token.mint(owner, firstTokenId, { from: minter });
- await this.token.mint(owner, secondTokenId, { from: minter });
- this.toWhom = other; // default to anyone 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 = unknownTokenId;
- 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('clears the approval for the token ID', async function () {
- expect(await this.token.getApproved(tokenId)).to.be.equal(ZERO_ADDRESS);
- });
- if (approved) {
- it('emit only a transfer event', async function () {
- expectEvent.inLogs(logs, 'Transfer', {
- from: owner,
- to: this.toWhom,
- tokenId: tokenId,
- });
- });
- } else {
- it('emits only a transfer event', async function () {
- expectEvent.inLogs(logs, 'Transfer', {
- from: owner,
- to: this.toWhom,
- 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 of token that is not own'
- );
- });
- });
- 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, unknownTokenId, { 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, false);
- this.toWhom = this.receiver.address;
- });
- shouldTransferTokensByUsers(transferFun);
- it('should call 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('should call 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,
- unknownTokenId,
- { 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', false);
- await expectRevert(
- this.token.safeTransferFrom(owner, invalidReceiver.address, tokenId, { from: owner }),
- 'ERC721: transfer to non ERC721Receiver implementer'
- );
- });
- });
- describe('to a receiver contract that throws', function () {
- it('reverts', async function () {
- const invalidReceiver = await ERC721ReceiverMock.new(RECEIVER_MAGIC_VALUE, true);
- await expectRevert(
- this.token.safeTransferFrom(owner, invalidReceiver.address, tokenId, { from: owner }),
- 'ERC721ReceiverMock: reverting'
- );
- });
- });
- describe('to a contract that does not implement the required function', function () {
- it('reverts', async function () {
- const invalidReceiver = this.token;
- await expectRevert.unspecified(
- this.token.safeTransferFrom(owner, invalidReceiver.address, tokenId, { from: owner })
- );
- });
- });
- });
- });
- describe('safe mint', function () {
- const fourthTokenId = new BN(4);
- const tokenId = fourthTokenId;
- const data = '0x42';
- beforeEach(async function () {
- this.ERC721Mock = await ERC721Mock.new();
- });
- describe('via safeMint', function () { // regular minting is tested in ERC721Mintable.test.js and others
- it('should call onERC721Received — with data', async function () {
- this.receiver = await ERC721ReceiverMock.new(RECEIVER_MAGIC_VALUE, false);
- const receipt = await this.ERC721Mock.safeMint(this.receiver.address, tokenId, data);
- await expectEvent.inTransaction(receipt.tx, ERC721ReceiverMock, 'Received', {
- from: ZERO_ADDRESS,
- tokenId: tokenId,
- data: data,
- });
- });
- it('should call onERC721Received — without data', async function () {
- this.receiver = await ERC721ReceiverMock.new(RECEIVER_MAGIC_VALUE, false);
- const receipt = await this.ERC721Mock.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', false);
- await expectRevert(
- this.ERC721Mock.safeMint(invalidReceiver.address, tokenId),
- 'ERC721: transfer to non ERC721Receiver implementer'
- );
- });
- });
- context('to a receiver contract that throws', function () {
- it('reverts', async function () {
- const invalidReceiver = await ERC721ReceiverMock.new(RECEIVER_MAGIC_VALUE, true);
- await expectRevert(
- this.ERC721Mock.safeMint(invalidReceiver.address, tokenId),
- 'ERC721ReceiverMock: reverting'
- );
- });
- });
- context('to a contract that does not implement the required function', function () {
- it('reverts', async function () {
- const invalidReceiver = this.token;
- await expectRevert.unspecified(
- this.ERC721Mock.safeMint(invalidReceiver.address, tokenId)
- );
- });
- });
- });
- });
- 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, unknownTokenId, { 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(unknownTokenId, { from: minter }),
- '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('should return approved account', async function () {
- expect(await this.token.getApproved(firstTokenId)).to.be.equal(approved);
- });
- });
- });
- });
- shouldSupportInterfaces([
- 'ERC165',
- 'ERC721',
- ]);
- });
- }
- module.exports = {
- shouldBehaveLikeERC721,
- };
|