Migrate ERC721 tests (#4793)

Co-authored-by: ernestognw <ernestognw@gmail.com>
This commit is contained in:
Hadrien Croubois
2023-12-14 10:07:55 +01:00
committed by GitHub
parent 88512b23d2
commit 88211e8fba
11 changed files with 950 additions and 1018 deletions

File diff suppressed because it is too large Load Diff

View File

@ -1,15 +1,23 @@
const { ethers } = require('hardhat');
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
const { shouldBehaveLikeERC721, shouldBehaveLikeERC721Metadata } = require('./ERC721.behavior');
const ERC721 = artifacts.require('$ERC721');
const name = 'Non Fungible Token';
const symbol = 'NFT';
contract('ERC721', function (accounts) {
const name = 'Non Fungible Token';
const symbol = 'NFT';
async function fixture() {
return {
accounts: await ethers.getSigners(),
token: await ethers.deployContract('$ERC721', [name, symbol]),
};
}
describe('ERC721', function () {
beforeEach(async function () {
this.token = await ERC721.new(name, symbol);
Object.assign(this, await loadFixture(fixture));
});
shouldBehaveLikeERC721(...accounts);
shouldBehaveLikeERC721Metadata(name, symbol, ...accounts);
shouldBehaveLikeERC721();
shouldBehaveLikeERC721Metadata(name, symbol);
});

View File

@ -1,20 +1,28 @@
const { ethers } = require('hardhat');
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
const {
shouldBehaveLikeERC721,
shouldBehaveLikeERC721Metadata,
shouldBehaveLikeERC721Enumerable,
} = require('./ERC721.behavior');
const ERC721Enumerable = artifacts.require('$ERC721Enumerable');
const name = 'Non Fungible Token';
const symbol = 'NFT';
contract('ERC721Enumerable', function (accounts) {
const name = 'Non Fungible Token';
const symbol = 'NFT';
async function fixture() {
return {
accounts: await ethers.getSigners(),
token: await ethers.deployContract('$ERC721Enumerable', [name, symbol]),
};
}
describe('ERC721', function () {
beforeEach(async function () {
this.token = await ERC721Enumerable.new(name, symbol);
Object.assign(this, await loadFixture(fixture));
});
shouldBehaveLikeERC721(...accounts);
shouldBehaveLikeERC721Metadata(name, symbol, ...accounts);
shouldBehaveLikeERC721Enumerable(...accounts);
shouldBehaveLikeERC721();
shouldBehaveLikeERC721Metadata(name, symbol);
shouldBehaveLikeERC721Enumerable();
});

View File

@ -1,80 +1,75 @@
const { BN, constants, expectEvent } = require('@openzeppelin/test-helpers');
const { ethers } = require('hardhat');
const { expect } = require('chai');
const { expectRevertCustomError } = require('../../../helpers/customError');
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
const ERC721Burnable = artifacts.require('$ERC721Burnable');
const name = 'Non Fungible Token';
const symbol = 'NFT';
const tokenId = 1n;
const otherTokenId = 2n;
const unknownTokenId = 3n;
contract('ERC721Burnable', function (accounts) {
const [owner, approved, another] = accounts;
const firstTokenId = new BN(1);
const secondTokenId = new BN(2);
const unknownTokenId = new BN(3);
const name = 'Non Fungible Token';
const symbol = 'NFT';
async function fixture() {
const [owner, approved, another] = await ethers.getSigners();
const token = await ethers.deployContract('$ERC721Burnable', [name, symbol]);
return { owner, approved, another, token };
}
describe('ERC721Burnable', function () {
beforeEach(async function () {
this.token = await ERC721Burnable.new(name, symbol);
Object.assign(this, await loadFixture(fixture));
});
describe('like a burnable ERC721', function () {
beforeEach(async function () {
await this.token.$_mint(owner, firstTokenId);
await this.token.$_mint(owner, secondTokenId);
await this.token.$_mint(this.owner, tokenId);
await this.token.$_mint(this.owner, otherTokenId);
});
describe('burn', function () {
const tokenId = firstTokenId;
let receipt = null;
describe('when successful', function () {
beforeEach(async function () {
receipt = await this.token.burn(tokenId, { from: owner });
});
it('emits a burn event, burns the given token ID and adjusts the balance of the owner', async function () {
const balanceBefore = await this.token.balanceOf(this.owner);
it('burns the given token ID and adjusts the balance of the owner', async function () {
await expectRevertCustomError(this.token.ownerOf(tokenId), 'ERC721NonexistentToken', [tokenId]);
expect(await this.token.balanceOf(owner)).to.be.bignumber.equal('1');
});
await expect(this.token.connect(this.owner).burn(tokenId))
.to.emit(this.token, 'Transfer')
.withArgs(this.owner.address, ethers.ZeroAddress, tokenId);
it('emits a burn event', async function () {
expectEvent(receipt, 'Transfer', {
from: owner,
to: constants.ZERO_ADDRESS,
tokenId: tokenId,
});
await expect(this.token.ownerOf(tokenId))
.to.be.revertedWithCustomError(this.token, 'ERC721NonexistentToken')
.withArgs(tokenId);
expect(await this.token.balanceOf(this.owner)).to.equal(balanceBefore - 1n);
});
});
describe('when there is a previous approval burned', function () {
beforeEach(async function () {
await this.token.approve(approved, tokenId, { from: owner });
receipt = await this.token.burn(tokenId, { from: owner });
await this.token.connect(this.owner).approve(this.approved, tokenId);
await this.token.connect(this.owner).burn(tokenId);
});
context('getApproved', function () {
it('reverts', async function () {
await expectRevertCustomError(this.token.getApproved(tokenId), 'ERC721NonexistentToken', [tokenId]);
await expect(this.token.getApproved(tokenId))
.to.be.revertedWithCustomError(this.token, 'ERC721NonexistentToken')
.withArgs(tokenId);
});
});
});
describe('when there is no previous approval burned', function () {
it('reverts', async function () {
await expectRevertCustomError(this.token.burn(tokenId, { from: another }), 'ERC721InsufficientApproval', [
another,
tokenId,
]);
await expect(this.token.connect(this.another).burn(tokenId))
.to.be.revertedWithCustomError(this.token, 'ERC721InsufficientApproval')
.withArgs(this.another.address, tokenId);
});
});
describe('when the given token ID was not tracked by this contract', function () {
it('reverts', async function () {
await expectRevertCustomError(this.token.burn(unknownTokenId, { from: owner }), 'ERC721NonexistentToken', [
unknownTokenId,
]);
await expect(this.token.connect(this.owner).burn(unknownTokenId))
.to.be.revertedWithCustomError(this.token, 'ERC721NonexistentToken')
.withArgs(unknownTokenId);
});
});
});

View File

@ -1,55 +1,60 @@
const { constants, expectEvent } = require('@openzeppelin/test-helpers');
const { ethers } = require('hardhat');
const { expect } = require('chai');
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
const { sum } = require('../../../helpers/math');
const { expectRevertCustomError } = require('../../../helpers/customError');
const { ZERO_ADDRESS } = require('@openzeppelin/test-helpers/src/constants');
const ERC721ConsecutiveMock = artifacts.require('$ERC721ConsecutiveMock');
const ERC721ConsecutiveEnumerableMock = artifacts.require('$ERC721ConsecutiveEnumerableMock');
const ERC721ConsecutiveNoConstructorMintMock = artifacts.require('$ERC721ConsecutiveNoConstructorMintMock');
const name = 'Non Fungible Token';
const symbol = 'NFT';
contract('ERC721Consecutive', function (accounts) {
const [user1, user2, user3, receiver] = accounts;
const name = 'Non Fungible Token';
const symbol = 'NFT';
const batches = [
{ receiver: user1, amount: 0 },
{ receiver: user1, amount: 1 },
{ receiver: user1, amount: 2 },
{ receiver: user2, amount: 5 },
{ receiver: user3, amount: 0 },
{ receiver: user1, amount: 7 },
];
const delegates = [user1, user3];
for (const offset of [0, 1, 42]) {
describe('ERC721Consecutive', function () {
for (const offset of [0n, 1n, 42n]) {
describe(`with offset ${offset}`, function () {
beforeEach(async function () {
this.token = await ERC721ConsecutiveMock.new(
async function fixture() {
const accounts = await ethers.getSigners();
const [alice, bruce, chris, receiver] = accounts;
const batches = [
{ receiver: alice, amount: 0n },
{ receiver: alice, amount: 1n },
{ receiver: alice, amount: 2n },
{ receiver: bruce, amount: 5n },
{ receiver: chris, amount: 0n },
{ receiver: alice, amount: 7n },
];
const delegates = [alice, chris];
const token = await ethers.deployContract('$ERC721ConsecutiveMock', [
name,
symbol,
offset,
delegates,
batches.map(({ receiver }) => receiver),
batches.map(({ amount }) => amount),
);
]);
return { accounts, alice, bruce, chris, receiver, batches, delegates, token };
}
beforeEach(async function () {
Object.assign(this, await loadFixture(fixture));
});
describe('minting during construction', function () {
it('events are emitted at construction', async function () {
let first = offset;
for (const batch of batches) {
for (const batch of this.batches) {
if (batch.amount > 0) {
await expectEvent.inConstruction(this.token, 'ConsecutiveTransfer', {
fromTokenId: web3.utils.toBN(first),
toTokenId: web3.utils.toBN(first + batch.amount - 1),
fromAddress: constants.ZERO_ADDRESS,
toAddress: batch.receiver,
});
await expect(this.token.deploymentTransaction())
.to.emit(this.token, 'ConsecutiveTransfer')
.withArgs(
first /* fromTokenId */,
first + batch.amount - 1n /* toTokenId */,
ethers.ZeroAddress /* fromAddress */,
batch.receiver.address /* toAddress */,
);
} else {
// expectEvent.notEmitted.inConstruction only looks at event name, and doesn't check the parameters
// ".to.not.emit" only looks at event name, and doesn't check the parameters
}
first += batch.amount;
}
@ -57,166 +62,175 @@ contract('ERC721Consecutive', function (accounts) {
it('ownership is set', async function () {
const owners = [
...Array(offset).fill(constants.ZERO_ADDRESS),
...batches.flatMap(({ receiver, amount }) => Array(amount).fill(receiver)),
...Array(Number(offset)).fill(ethers.ZeroAddress),
...this.batches.flatMap(({ receiver, amount }) => Array(Number(amount)).fill(receiver.address)),
];
for (const tokenId in owners) {
if (owners[tokenId] != constants.ZERO_ADDRESS) {
expect(await this.token.ownerOf(tokenId)).to.be.equal(owners[tokenId]);
if (owners[tokenId] != ethers.ZeroAddress) {
expect(await this.token.ownerOf(tokenId)).to.equal(owners[tokenId]);
}
}
});
it('balance & voting power are set', async function () {
for (const account of accounts) {
for (const account of this.accounts) {
const balance =
sum(...batches.filter(({ receiver }) => receiver === account).map(({ amount }) => amount)) ?? 0;
sum(...this.batches.filter(({ receiver }) => receiver === account).map(({ amount }) => amount)) ?? 0n;
expect(await this.token.balanceOf(account)).to.be.bignumber.equal(web3.utils.toBN(balance));
expect(await this.token.balanceOf(account)).to.equal(balance);
// If not delegated at construction, check before + do delegation
if (!delegates.includes(account)) {
expect(await this.token.getVotes(account)).to.be.bignumber.equal(web3.utils.toBN(0));
if (!this.delegates.includes(account)) {
expect(await this.token.getVotes(account)).to.equal(0n);
await this.token.delegate(account, { from: account });
await this.token.connect(account).delegate(account);
}
// At this point all accounts should have delegated
expect(await this.token.getVotes(account)).to.be.bignumber.equal(web3.utils.toBN(balance));
expect(await this.token.getVotes(account)).to.equal(balance);
}
});
it('reverts on consecutive minting to the zero address', async function () {
await expectRevertCustomError(
ERC721ConsecutiveMock.new(name, symbol, offset, delegates, [ZERO_ADDRESS], [10]),
'ERC721InvalidReceiver',
[ZERO_ADDRESS],
);
await expect(
ethers.deployContract('$ERC721ConsecutiveMock', [
name,
symbol,
offset,
this.delegates,
[ethers.ZeroAddress],
[10],
]),
)
.to.be.revertedWithCustomError(this.token, 'ERC721InvalidReceiver')
.withArgs(ethers.ZeroAddress);
});
});
describe('minting after construction', function () {
it('consecutive minting is not possible after construction', async function () {
await expectRevertCustomError(this.token.$_mintConsecutive(user1, 10), 'ERC721ForbiddenBatchMint', []);
await expect(this.token.$_mintConsecutive(this.alice, 10)).to.be.revertedWithCustomError(
this.token,
'ERC721ForbiddenBatchMint',
);
});
it('simple minting is possible after construction', async function () {
const tokenId = sum(...batches.map(b => b.amount)) + offset;
const tokenId = sum(...this.batches.map(b => b.amount)) + offset;
await expectRevertCustomError(this.token.ownerOf(tokenId), 'ERC721NonexistentToken', [tokenId]);
await expect(this.token.ownerOf(tokenId))
.to.be.revertedWithCustomError(this.token, 'ERC721NonexistentToken')
.withArgs(tokenId);
expectEvent(await this.token.$_mint(user1, tokenId), 'Transfer', {
from: constants.ZERO_ADDRESS,
to: user1,
tokenId: tokenId.toString(),
});
await expect(this.token.$_mint(this.alice, tokenId))
.to.emit(this.token, 'Transfer')
.withArgs(ethers.ZeroAddress, this.alice.address, tokenId);
});
it('cannot mint a token that has been batched minted', async function () {
const tokenId = sum(...batches.map(b => b.amount)) + offset - 1;
const tokenId = sum(...this.batches.map(b => b.amount)) + offset - 1n;
expect(await this.token.ownerOf(tokenId)).to.be.not.equal(constants.ZERO_ADDRESS);
expect(await this.token.ownerOf(tokenId)).to.not.equal(ethers.ZeroAddress);
await expectRevertCustomError(this.token.$_mint(user1, tokenId), 'ERC721InvalidSender', [ZERO_ADDRESS]);
await expect(this.token.$_mint(this.alice, tokenId))
.to.be.revertedWithCustomError(this.token, 'ERC721InvalidSender')
.withArgs(ethers.ZeroAddress);
});
});
describe('ERC721 behavior', function () {
const tokenId = web3.utils.toBN(offset + 1);
const tokenId = offset + 1n;
it('core takes over ownership on transfer', async function () {
await this.token.transferFrom(user1, receiver, tokenId, { from: user1 });
await this.token.connect(this.alice).transferFrom(this.alice, this.receiver, tokenId);
expect(await this.token.ownerOf(tokenId)).to.be.equal(receiver);
expect(await this.token.ownerOf(tokenId)).to.equal(this.receiver.address);
});
it('tokens can be burned and re-minted #1', async function () {
expectEvent(await this.token.$_burn(tokenId, { from: user1 }), 'Transfer', {
from: user1,
to: constants.ZERO_ADDRESS,
tokenId,
});
await expect(this.token.connect(this.alice).$_burn(tokenId))
.to.emit(this.token, 'Transfer')
.withArgs(this.alice.address, ethers.ZeroAddress, tokenId);
await expectRevertCustomError(this.token.ownerOf(tokenId), 'ERC721NonexistentToken', [tokenId]);
await expect(this.token.ownerOf(tokenId))
.to.be.revertedWithCustomError(this.token, 'ERC721NonexistentToken')
.withArgs(tokenId);
expectEvent(await this.token.$_mint(user2, tokenId), 'Transfer', {
from: constants.ZERO_ADDRESS,
to: user2,
tokenId,
});
await expect(this.token.$_mint(this.bruce, tokenId))
.to.emit(this.token, 'Transfer')
.withArgs(ethers.ZeroAddress, this.bruce.address, tokenId);
expect(await this.token.ownerOf(tokenId)).to.be.equal(user2);
expect(await this.token.ownerOf(tokenId)).to.equal(this.bruce.address);
});
it('tokens can be burned and re-minted #2', async function () {
const tokenId = web3.utils.toBN(sum(...batches.map(({ amount }) => amount)) + offset);
const tokenId = sum(...this.batches.map(({ amount }) => amount)) + offset;
await expectRevertCustomError(this.token.ownerOf(tokenId), 'ERC721NonexistentToken', [tokenId]);
await expect(this.token.ownerOf(tokenId))
.to.be.revertedWithCustomError(this.token, 'ERC721NonexistentToken')
.withArgs(tokenId);
// mint
await this.token.$_mint(user1, tokenId);
await expect(this.token.$_mint(this.alice, tokenId))
.to.emit(this.token, 'Transfer')
.withArgs(ethers.ZeroAddress, this.alice.address, tokenId);
expect(await this.token.ownerOf(tokenId), user1);
expect(await this.token.ownerOf(tokenId)).to.equal(this.alice.address);
// burn
expectEvent(await this.token.$_burn(tokenId, { from: user1 }), 'Transfer', {
from: user1,
to: constants.ZERO_ADDRESS,
tokenId,
});
await expect(await this.token.$_burn(tokenId))
.to.emit(this.token, 'Transfer')
.withArgs(this.alice.address, ethers.ZeroAddress, tokenId);
await expectRevertCustomError(this.token.ownerOf(tokenId), 'ERC721NonexistentToken', [tokenId]);
await expect(this.token.ownerOf(tokenId))
.to.be.revertedWithCustomError(this.token, 'ERC721NonexistentToken')
.withArgs(tokenId);
// re-mint
expectEvent(await this.token.$_mint(user2, tokenId), 'Transfer', {
from: constants.ZERO_ADDRESS,
to: user2,
tokenId,
});
await expect(this.token.$_mint(this.bruce, tokenId))
.to.emit(this.token, 'Transfer')
.withArgs(ethers.ZeroAddress, this.bruce.address, tokenId);
expect(await this.token.ownerOf(tokenId), user2);
expect(await this.token.ownerOf(tokenId)).to.equal(this.bruce.address);
});
});
});
}
describe('invalid use', function () {
const receiver = ethers.Wallet.createRandom();
it('cannot mint a batch larger than 5000', async function () {
await expectRevertCustomError(
ERC721ConsecutiveMock.new(name, symbol, 0, [], [user1], ['5001']),
'ERC721ExceededMaxBatchMint',
[5000, 5001],
);
const { interface } = await ethers.getContractFactory('$ERC721ConsecutiveMock');
await expect(ethers.deployContract('$ERC721ConsecutiveMock', [name, symbol, 0, [], [receiver], [5001n]]))
.to.be.revertedWithCustomError({ interface }, 'ERC721ExceededMaxBatchMint')
.withArgs(5001n, 5000n);
});
it('cannot use single minting during construction', async function () {
await expectRevertCustomError(
ERC721ConsecutiveNoConstructorMintMock.new(name, symbol),
'ERC721ForbiddenMint',
[],
);
const { interface } = await ethers.getContractFactory('$ERC721ConsecutiveNoConstructorMintMock');
await expect(
ethers.deployContract('$ERC721ConsecutiveNoConstructorMintMock', [name, symbol]),
).to.be.revertedWithCustomError({ interface }, 'ERC721ForbiddenMint');
});
it('cannot use single minting during construction', async function () {
await expectRevertCustomError(
ERC721ConsecutiveNoConstructorMintMock.new(name, symbol),
'ERC721ForbiddenMint',
[],
);
const { interface } = await ethers.getContractFactory('$ERC721ConsecutiveNoConstructorMintMock');
await expect(
ethers.deployContract('$ERC721ConsecutiveNoConstructorMintMock', [name, symbol]),
).to.be.revertedWithCustomError({ interface }, 'ERC721ForbiddenMint');
});
it('consecutive mint not compatible with enumerability', async function () {
await expectRevertCustomError(
ERC721ConsecutiveEnumerableMock.new(
name,
symbol,
batches.map(({ receiver }) => receiver),
batches.map(({ amount }) => amount),
),
'ERC721EnumerableForbiddenBatchMint',
[],
);
const { interface } = await ethers.getContractFactory('$ERC721ConsecutiveEnumerableMock');
await expect(
ethers.deployContract('$ERC721ConsecutiveEnumerableMock', [name, symbol, [receiver], [100n]]),
).to.be.revertedWithCustomError({ interface }, 'ERC721EnumerableForbiddenBatchMint');
});
});
});

View File

@ -1,89 +1,80 @@
const { BN, constants } = require('@openzeppelin/test-helpers');
const { ethers } = require('hardhat');
const { expect } = require('chai');
const { expectRevertCustomError } = require('../../../helpers/customError');
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
const ERC721Pausable = artifacts.require('$ERC721Pausable');
const name = 'Non Fungible Token';
const symbol = 'NFT';
const tokenId = 1n;
const otherTokenId = 2n;
const data = ethers.Typed.bytes('0x42');
contract('ERC721Pausable', function (accounts) {
const [owner, receiver, operator] = accounts;
const name = 'Non Fungible Token';
const symbol = 'NFT';
async function fixture() {
const [owner, receiver, operator] = await ethers.getSigners();
const token = await ethers.deployContract('$ERC721Pausable', [name, symbol]);
return { owner, receiver, operator, token };
}
describe('ERC721Pausable', function () {
beforeEach(async function () {
this.token = await ERC721Pausable.new(name, symbol);
Object.assign(this, await loadFixture(fixture));
});
context('when token is paused', function () {
const firstTokenId = new BN(1);
const secondTokenId = new BN(1337);
const mockData = '0x42';
beforeEach(async function () {
await this.token.$_mint(owner, firstTokenId, { from: owner });
await this.token.$_mint(this.owner, tokenId);
await this.token.$_pause();
});
it('reverts when trying to transferFrom', async function () {
await expectRevertCustomError(
this.token.transferFrom(owner, receiver, firstTokenId, { from: owner }),
'EnforcedPause',
[],
);
await expect(
this.token.connect(this.owner).transferFrom(this.owner, this.receiver, tokenId),
).to.be.revertedWithCustomError(this.token, 'EnforcedPause');
});
it('reverts when trying to safeTransferFrom', async function () {
await expectRevertCustomError(
this.token.safeTransferFrom(owner, receiver, firstTokenId, { from: owner }),
'EnforcedPause',
[],
);
await expect(
this.token.connect(this.owner).safeTransferFrom(this.owner, this.receiver, tokenId),
).to.be.revertedWithCustomError(this.token, 'EnforcedPause');
});
it('reverts when trying to safeTransferFrom with data', async function () {
await expectRevertCustomError(
this.token.methods['safeTransferFrom(address,address,uint256,bytes)'](owner, receiver, firstTokenId, mockData, {
from: owner,
}),
'EnforcedPause',
[],
);
await expect(
this.token.connect(this.owner).safeTransferFrom(this.owner, this.receiver, tokenId, data),
).to.be.revertedWithCustomError(this.token, 'EnforcedPause');
});
it('reverts when trying to mint', async function () {
await expectRevertCustomError(this.token.$_mint(receiver, secondTokenId), 'EnforcedPause', []);
await expect(this.token.$_mint(this.receiver, otherTokenId)).to.be.revertedWithCustomError(
this.token,
'EnforcedPause',
);
});
it('reverts when trying to burn', async function () {
await expectRevertCustomError(this.token.$_burn(firstTokenId), 'EnforcedPause', []);
await expect(this.token.$_burn(tokenId)).to.be.revertedWithCustomError(this.token, 'EnforcedPause');
});
describe('getApproved', function () {
it('returns approved address', async function () {
const approvedAccount = await this.token.getApproved(firstTokenId);
expect(approvedAccount).to.equal(constants.ZERO_ADDRESS);
expect(await this.token.getApproved(tokenId)).to.equal(ethers.ZeroAddress);
});
});
describe('balanceOf', function () {
it('returns the amount of tokens owned by the given address', async function () {
const balance = await this.token.balanceOf(owner);
expect(balance).to.be.bignumber.equal('1');
expect(await this.token.balanceOf(this.owner)).to.equal(1n);
});
});
describe('ownerOf', function () {
it('returns the amount of tokens owned by the given address', async function () {
const ownerOfToken = await this.token.ownerOf(firstTokenId);
expect(ownerOfToken).to.equal(owner);
expect(await this.token.ownerOf(tokenId)).to.equal(this.owner.address);
});
});
describe('isApprovedForAll', function () {
it('returns the approval of the operator', async function () {
expect(await this.token.isApprovedForAll(owner, operator)).to.equal(false);
expect(await this.token.isApprovedForAll(this.owner, this.operator)).to.be.false;
});
});
});

View File

@ -1,45 +1,55 @@
require('@openzeppelin/test-helpers');
const { ethers } = require('hardhat');
const { expect } = require('chai');
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
const { shouldBehaveLikeERC2981 } = require('../../common/ERC2981.behavior');
const ERC721Royalty = artifacts.require('$ERC721Royalty');
const name = 'Non Fungible Token';
const symbol = 'NFT';
contract('ERC721Royalty', function (accounts) {
const [account1, account2, recipient] = accounts;
const tokenId1 = web3.utils.toBN('1');
const tokenId2 = web3.utils.toBN('2');
const royalty = web3.utils.toBN('200');
const salePrice = web3.utils.toBN('1000');
const tokenId1 = 1n;
const tokenId2 = 2n;
const royalty = 200n;
const salePrice = 1000n;
async function fixture() {
const [account1, account2, recipient] = await ethers.getSigners();
const token = await ethers.deployContract('$ERC721Royalty', [name, symbol]);
await token.$_mint(account1, tokenId1);
await token.$_mint(account1, tokenId2);
return { account1, account2, recipient, token };
}
describe('ERC721Royalty', function () {
beforeEach(async function () {
this.token = await ERC721Royalty.new('My Token', 'TKN');
await this.token.$_mint(account1, tokenId1);
await this.token.$_mint(account1, tokenId2);
this.account1 = account1;
this.account2 = account2;
this.tokenId1 = tokenId1;
this.tokenId2 = tokenId2;
this.salePrice = salePrice;
Object.assign(
this,
await loadFixture(fixture),
{ tokenId1, tokenId2, royalty, salePrice }, // set for behavior tests
);
});
describe('token specific functions', function () {
beforeEach(async function () {
await this.token.$_setTokenRoyalty(tokenId1, recipient, royalty);
await this.token.$_setTokenRoyalty(tokenId1, this.recipient, royalty);
});
it('royalty information are kept during burn and re-mint', async function () {
await this.token.$_burn(tokenId1);
const tokenInfoA = await this.token.royaltyInfo(tokenId1, salePrice);
expect(tokenInfoA[0]).to.be.equal(recipient);
expect(tokenInfoA[1]).to.be.bignumber.equal(salePrice.mul(royalty).divn(1e4));
expect(await this.token.royaltyInfo(tokenId1, salePrice)).to.deep.equal([
this.recipient.address,
(salePrice * royalty) / 10000n,
]);
await this.token.$_mint(account2, tokenId1);
await this.token.$_mint(this.account2, tokenId1);
const tokenInfoB = await this.token.royaltyInfo(tokenId1, salePrice);
expect(tokenInfoB[0]).to.be.equal(recipient);
expect(tokenInfoB[1]).to.be.bignumber.equal(salePrice.mul(royalty).divn(1e4));
expect(await this.token.royaltyInfo(tokenId1, salePrice)).to.deep.equal([
this.recipient.address,
(salePrice * royalty) / 10000n,
]);
});
});

View File

@ -1,63 +1,64 @@
const { BN, expectEvent } = require('@openzeppelin/test-helpers');
const { ethers } = require('hardhat');
const { expect } = require('chai');
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
const { shouldSupportInterfaces } = require('../../../utils/introspection/SupportsInterface.behavior');
const { expectRevertCustomError } = require('../../../helpers/customError');
const ERC721URIStorageMock = artifacts.require('$ERC721URIStorageMock');
const name = 'Non Fungible Token';
const symbol = 'NFT';
const baseURI = 'https://api.example.com/v1/';
const otherBaseURI = 'https://api.example.com/v2/';
const sampleUri = 'mock://mytoken';
const tokenId = 1n;
const nonExistentTokenId = 2n;
contract('ERC721URIStorage', function (accounts) {
const [owner] = accounts;
const name = 'Non Fungible Token';
const symbol = 'NFT';
const firstTokenId = new BN('5042');
const nonExistentTokenId = new BN('13');
async function fixture() {
const [owner] = await ethers.getSigners();
const token = await ethers.deployContract('$ERC721URIStorageMock', [name, symbol]);
return { owner, token };
}
contract('ERC721URIStorage', function () {
beforeEach(async function () {
this.token = await ERC721URIStorageMock.new(name, symbol);
Object.assign(this, await loadFixture(fixture));
});
shouldSupportInterfaces(['0x49064906']);
describe('token URI', function () {
beforeEach(async function () {
await this.token.$_mint(owner, firstTokenId);
await this.token.$_mint(this.owner, tokenId);
});
const baseURI = 'https://api.example.com/v1/';
const sampleUri = 'mock://mytoken';
it('it is empty by default', async function () {
expect(await this.token.tokenURI(firstTokenId)).to.be.equal('');
expect(await this.token.tokenURI(tokenId)).to.equal('');
});
it('reverts when queried for non existent token id', async function () {
await expectRevertCustomError(this.token.tokenURI(nonExistentTokenId), 'ERC721NonexistentToken', [
nonExistentTokenId,
]);
await expect(this.token.tokenURI(nonExistentTokenId))
.to.be.revertedWithCustomError(this.token, 'ERC721NonexistentToken')
.withArgs(nonExistentTokenId);
});
it('can be set for a token id', async function () {
await this.token.$_setTokenURI(firstTokenId, sampleUri);
expect(await this.token.tokenURI(firstTokenId)).to.be.equal(sampleUri);
await this.token.$_setTokenURI(tokenId, sampleUri);
expect(await this.token.tokenURI(tokenId)).to.equal(sampleUri);
});
it('setting the uri emits an event', async function () {
expectEvent(await this.token.$_setTokenURI(firstTokenId, sampleUri), 'MetadataUpdate', {
_tokenId: firstTokenId,
});
await expect(this.token.$_setTokenURI(tokenId, sampleUri))
.to.emit(this.token, 'MetadataUpdate')
.withArgs(tokenId);
});
it('setting the uri for non existent token id is allowed', async function () {
expectEvent(await this.token.$_setTokenURI(nonExistentTokenId, sampleUri), 'MetadataUpdate', {
_tokenId: nonExistentTokenId,
});
await expect(await this.token.$_setTokenURI(nonExistentTokenId, sampleUri))
.to.emit(this.token, 'MetadataUpdate')
.withArgs(nonExistentTokenId);
// value will be accessible after mint
await this.token.$_mint(owner, nonExistentTokenId);
expect(await this.token.tokenURI(nonExistentTokenId)).to.be.equal(sampleUri);
await this.token.$_mint(this.owner, nonExistentTokenId);
expect(await this.token.tokenURI(nonExistentTokenId)).to.equal(sampleUri);
});
it('base URI can be set', async function () {
@ -67,48 +68,54 @@ contract('ERC721URIStorage', function (accounts) {
it('base URI is added as a prefix to the token URI', async function () {
await this.token.setBaseURI(baseURI);
await this.token.$_setTokenURI(firstTokenId, sampleUri);
await this.token.$_setTokenURI(tokenId, sampleUri);
expect(await this.token.tokenURI(firstTokenId)).to.be.equal(baseURI + sampleUri);
expect(await this.token.tokenURI(tokenId)).to.equal(baseURI + sampleUri);
});
it('token URI can be changed by changing the base URI', async function () {
await this.token.setBaseURI(baseURI);
await this.token.$_setTokenURI(firstTokenId, sampleUri);
await this.token.$_setTokenURI(tokenId, sampleUri);
const newBaseURI = 'https://api.example.com/v2/';
await this.token.setBaseURI(newBaseURI);
expect(await this.token.tokenURI(firstTokenId)).to.be.equal(newBaseURI + sampleUri);
await this.token.setBaseURI(otherBaseURI);
expect(await this.token.tokenURI(tokenId)).to.equal(otherBaseURI + sampleUri);
});
it('tokenId is appended to base URI for tokens with no URI', async function () {
await this.token.setBaseURI(baseURI);
expect(await this.token.tokenURI(firstTokenId)).to.be.equal(baseURI + firstTokenId);
expect(await this.token.tokenURI(tokenId)).to.equal(baseURI + tokenId);
});
it('tokens without URI can be burnt ', async function () {
await this.token.$_burn(firstTokenId);
await this.token.$_burn(tokenId);
await expectRevertCustomError(this.token.tokenURI(firstTokenId), 'ERC721NonexistentToken', [firstTokenId]);
await expect(this.token.tokenURI(tokenId))
.to.be.revertedWithCustomError(this.token, 'ERC721NonexistentToken')
.withArgs(tokenId);
});
it('tokens with URI can be burnt ', async function () {
await this.token.$_setTokenURI(firstTokenId, sampleUri);
await this.token.$_setTokenURI(tokenId, sampleUri);
await this.token.$_burn(firstTokenId);
await this.token.$_burn(tokenId);
await expectRevertCustomError(this.token.tokenURI(firstTokenId), 'ERC721NonexistentToken', [firstTokenId]);
await expect(this.token.tokenURI(tokenId))
.to.be.revertedWithCustomError(this.token, 'ERC721NonexistentToken')
.withArgs(tokenId);
});
it('tokens URI is kept if token is burnt and reminted ', async function () {
await this.token.$_setTokenURI(firstTokenId, sampleUri);
await this.token.$_setTokenURI(tokenId, sampleUri);
await this.token.$_burn(firstTokenId);
await expectRevertCustomError(this.token.tokenURI(firstTokenId), 'ERC721NonexistentToken', [firstTokenId]);
await this.token.$_burn(tokenId);
await this.token.$_mint(owner, firstTokenId);
expect(await this.token.tokenURI(firstTokenId)).to.be.equal(sampleUri);
await expect(this.token.tokenURI(tokenId))
.to.be.revertedWithCustomError(this.token, 'ERC721NonexistentToken')
.withArgs(tokenId);
await this.token.$_mint(this.owner, tokenId);
expect(await this.token.tokenURI(tokenId)).to.equal(sampleUri);
});
});
});

View File

@ -1,26 +1,29 @@
const { BN, expectEvent, constants } = require('@openzeppelin/test-helpers');
const { ethers } = require('hardhat');
const { expect } = require('chai');
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
const { shouldBehaveLikeERC721 } = require('../ERC721.behavior');
const { expectRevertCustomError } = require('../../../helpers/customError');
const ERC721 = artifacts.require('$ERC721');
const ERC721Wrapper = artifacts.require('$ERC721Wrapper');
const name = 'Non Fungible Token';
const symbol = 'NFT';
const tokenId = 1n;
const otherTokenId = 2n;
contract('ERC721Wrapper', function (accounts) {
const [initialHolder, anotherAccount, approvedAccount] = accounts;
async function fixture() {
const accounts = await ethers.getSigners();
const [owner, approved, other] = accounts;
const name = 'My Token';
const symbol = 'MTKN';
const firstTokenId = new BN(1);
const secondTokenId = new BN(2);
const underlying = await ethers.deployContract('$ERC721', [name, symbol]);
await underlying.$_safeMint(owner, tokenId);
await underlying.$_safeMint(owner, otherTokenId);
const token = await ethers.deployContract('$ERC721Wrapper', [`Wrapped ${name}`, `W${symbol}`, underlying]);
return { accounts, owner, approved, other, underlying, token };
}
describe('ERC721Wrapper', function () {
beforeEach(async function () {
this.underlying = await ERC721.new(name, symbol);
this.token = await ERC721Wrapper.new(`Wrapped ${name}`, `W${symbol}`, this.underlying.address);
await this.underlying.$_safeMint(initialHolder, firstTokenId);
await this.underlying.$_safeMint(initialHolder, secondTokenId);
Object.assign(this, await loadFixture(fixture));
});
it('has a name', async function () {
@ -32,258 +35,167 @@ contract('ERC721Wrapper', function (accounts) {
});
it('has underlying', async function () {
expect(await this.token.underlying()).to.be.bignumber.equal(this.underlying.address);
expect(await this.token.underlying()).to.equal(this.underlying.target);
});
describe('depositFor', function () {
it('works with token approval', async function () {
await this.underlying.approve(this.token.address, firstTokenId, { from: initialHolder });
await this.underlying.connect(this.owner).approve(this.token, tokenId);
const { tx } = await this.token.depositFor(initialHolder, [firstTokenId], { from: initialHolder });
await expectEvent.inTransaction(tx, this.underlying, 'Transfer', {
from: initialHolder,
to: this.token.address,
tokenId: firstTokenId,
});
await expectEvent.inTransaction(tx, this.token, 'Transfer', {
from: constants.ZERO_ADDRESS,
to: initialHolder,
tokenId: firstTokenId,
});
await expect(this.token.connect(this.owner).depositFor(this.owner, [tokenId]))
.to.emit(this.underlying, 'Transfer')
.withArgs(this.owner.address, this.token.target, tokenId)
.to.emit(this.token, 'Transfer')
.withArgs(ethers.ZeroAddress, this.owner.address, tokenId);
});
it('works with approval for all', async function () {
await this.underlying.setApprovalForAll(this.token.address, true, { from: initialHolder });
await this.underlying.connect(this.owner).setApprovalForAll(this.token, true);
const { tx } = await this.token.depositFor(initialHolder, [firstTokenId], { from: initialHolder });
await expectEvent.inTransaction(tx, this.underlying, 'Transfer', {
from: initialHolder,
to: this.token.address,
tokenId: firstTokenId,
});
await expectEvent.inTransaction(tx, this.token, 'Transfer', {
from: constants.ZERO_ADDRESS,
to: initialHolder,
tokenId: firstTokenId,
});
await expect(this.token.connect(this.owner).depositFor(this.owner, [tokenId]))
.to.emit(this.underlying, 'Transfer')
.withArgs(this.owner.address, this.token.target, tokenId)
.to.emit(this.token, 'Transfer')
.withArgs(ethers.ZeroAddress, this.owner.address, tokenId);
});
it('works sending to another account', async function () {
await this.underlying.approve(this.token.address, firstTokenId, { from: initialHolder });
await this.underlying.connect(this.owner).approve(this.token, tokenId);
const { tx } = await this.token.depositFor(anotherAccount, [firstTokenId], { from: initialHolder });
await expectEvent.inTransaction(tx, this.underlying, 'Transfer', {
from: initialHolder,
to: this.token.address,
tokenId: firstTokenId,
});
await expectEvent.inTransaction(tx, this.token, 'Transfer', {
from: constants.ZERO_ADDRESS,
to: anotherAccount,
tokenId: firstTokenId,
});
await expect(this.token.connect(this.owner).depositFor(this.other, [tokenId]))
.to.emit(this.underlying, 'Transfer')
.withArgs(this.owner.address, this.token.target, tokenId)
.to.emit(this.token, 'Transfer')
.withArgs(ethers.ZeroAddress, this.other.address, tokenId);
});
it('works with multiple tokens', async function () {
await this.underlying.approve(this.token.address, firstTokenId, { from: initialHolder });
await this.underlying.approve(this.token.address, secondTokenId, { from: initialHolder });
await this.underlying.connect(this.owner).approve(this.token, tokenId);
await this.underlying.connect(this.owner).approve(this.token, otherTokenId);
const { tx } = await this.token.depositFor(initialHolder, [firstTokenId, secondTokenId], { from: initialHolder });
await expectEvent.inTransaction(tx, this.underlying, 'Transfer', {
from: initialHolder,
to: this.token.address,
tokenId: firstTokenId,
});
await expectEvent.inTransaction(tx, this.token, 'Transfer', {
from: constants.ZERO_ADDRESS,
to: initialHolder,
tokenId: firstTokenId,
});
await expectEvent.inTransaction(tx, this.underlying, 'Transfer', {
from: initialHolder,
to: this.token.address,
tokenId: secondTokenId,
});
await expectEvent.inTransaction(tx, this.token, 'Transfer', {
from: constants.ZERO_ADDRESS,
to: initialHolder,
tokenId: secondTokenId,
});
await expect(this.token.connect(this.owner).depositFor(this.owner, [tokenId, otherTokenId]))
.to.emit(this.underlying, 'Transfer')
.withArgs(this.owner.address, this.token.target, tokenId)
.to.emit(this.token, 'Transfer')
.withArgs(ethers.ZeroAddress, this.owner.address, tokenId)
.to.emit(this.underlying, 'Transfer')
.withArgs(this.owner.address, this.token.target, otherTokenId)
.to.emit(this.token, 'Transfer')
.withArgs(ethers.ZeroAddress, this.owner.address, otherTokenId);
});
it('reverts with missing approval', async function () {
await expectRevertCustomError(
this.token.depositFor(initialHolder, [firstTokenId], { from: initialHolder }),
'ERC721InsufficientApproval',
[this.token.address, firstTokenId],
);
await expect(this.token.connect(this.owner).depositFor(this.owner, [tokenId]))
.to.be.revertedWithCustomError(this.token, 'ERC721InsufficientApproval')
.withArgs(this.token.target, tokenId);
});
});
describe('withdrawTo', function () {
beforeEach(async function () {
await this.underlying.approve(this.token.address, firstTokenId, { from: initialHolder });
await this.token.depositFor(initialHolder, [firstTokenId], { from: initialHolder });
await this.underlying.connect(this.owner).approve(this.token, tokenId);
await this.token.connect(this.owner).depositFor(this.owner, [tokenId]);
});
it('works for an owner', async function () {
const { tx } = await this.token.withdrawTo(initialHolder, [firstTokenId], { from: initialHolder });
await expectEvent.inTransaction(tx, this.underlying, 'Transfer', {
from: this.token.address,
to: initialHolder,
tokenId: firstTokenId,
});
await expectEvent.inTransaction(tx, this.token, 'Transfer', {
from: initialHolder,
to: constants.ZERO_ADDRESS,
tokenId: firstTokenId,
});
await expect(this.token.connect(this.owner).withdrawTo(this.owner, [tokenId]))
.to.emit(this.underlying, 'Transfer')
.withArgs(this.token.target, this.owner.address, tokenId)
.to.emit(this.token, 'Transfer')
.withArgs(this.owner.address, ethers.ZeroAddress, tokenId);
});
it('works for an approved', async function () {
await this.token.approve(approvedAccount, firstTokenId, { from: initialHolder });
await this.token.connect(this.owner).approve(this.approved, tokenId);
const { tx } = await this.token.withdrawTo(initialHolder, [firstTokenId], { from: approvedAccount });
await expectEvent.inTransaction(tx, this.underlying, 'Transfer', {
from: this.token.address,
to: initialHolder,
tokenId: firstTokenId,
});
await expectEvent.inTransaction(tx, this.token, 'Transfer', {
from: initialHolder,
to: constants.ZERO_ADDRESS,
tokenId: firstTokenId,
});
await expect(this.token.connect(this.approved).withdrawTo(this.owner, [tokenId]))
.to.emit(this.underlying, 'Transfer')
.withArgs(this.token.target, this.owner.address, tokenId)
.to.emit(this.token, 'Transfer')
.withArgs(this.owner.address, ethers.ZeroAddress, tokenId);
});
it('works for an approved for all', async function () {
await this.token.setApprovalForAll(approvedAccount, true, { from: initialHolder });
await this.token.connect(this.owner).setApprovalForAll(this.approved, true);
const { tx } = await this.token.withdrawTo(initialHolder, [firstTokenId], { from: approvedAccount });
await expectEvent.inTransaction(tx, this.underlying, 'Transfer', {
from: this.token.address,
to: initialHolder,
tokenId: firstTokenId,
});
await expectEvent.inTransaction(tx, this.token, 'Transfer', {
from: initialHolder,
to: constants.ZERO_ADDRESS,
tokenId: firstTokenId,
});
await expect(this.token.connect(this.approved).withdrawTo(this.owner, [tokenId]))
.to.emit(this.underlying, 'Transfer')
.withArgs(this.token.target, this.owner.address, tokenId)
.to.emit(this.token, 'Transfer')
.withArgs(this.owner.address, ethers.ZeroAddress, tokenId);
});
it("doesn't work for a non-owner nor approved", async function () {
await expectRevertCustomError(
this.token.withdrawTo(initialHolder, [firstTokenId], { from: anotherAccount }),
'ERC721InsufficientApproval',
[anotherAccount, firstTokenId],
);
await expect(this.token.connect(this.other).withdrawTo(this.owner, [tokenId]))
.to.be.revertedWithCustomError(this.token, 'ERC721InsufficientApproval')
.withArgs(this.other.address, tokenId);
});
it('works with multiple tokens', async function () {
await this.underlying.approve(this.token.address, secondTokenId, { from: initialHolder });
await this.token.depositFor(initialHolder, [secondTokenId], { from: initialHolder });
await this.underlying.connect(this.owner).approve(this.token, otherTokenId);
await this.token.connect(this.owner).depositFor(this.owner, [otherTokenId]);
const { tx } = await this.token.withdrawTo(initialHolder, [firstTokenId, secondTokenId], { from: initialHolder });
await expectEvent.inTransaction(tx, this.underlying, 'Transfer', {
from: this.token.address,
to: initialHolder,
tokenId: firstTokenId,
});
await expectEvent.inTransaction(tx, this.underlying, 'Transfer', {
from: this.token.address,
to: initialHolder,
tokenId: secondTokenId,
});
await expectEvent.inTransaction(tx, this.token, 'Transfer', {
from: initialHolder,
to: constants.ZERO_ADDRESS,
tokenId: firstTokenId,
});
await expectEvent.inTransaction(tx, this.token, 'Transfer', {
from: initialHolder,
to: constants.ZERO_ADDRESS,
tokenId: secondTokenId,
});
await expect(this.token.connect(this.owner).withdrawTo(this.owner, [tokenId, otherTokenId]))
.to.emit(this.underlying, 'Transfer')
.withArgs(this.token.target, this.owner.address, tokenId)
.to.emit(this.underlying, 'Transfer')
.withArgs(this.token.target, this.owner.address, tokenId)
.to.emit(this.token, 'Transfer')
.withArgs(this.owner.address, ethers.ZeroAddress, tokenId)
.to.emit(this.token, 'Transfer')
.withArgs(this.owner.address, ethers.ZeroAddress, tokenId);
});
it('works to another account', async function () {
const { tx } = await this.token.withdrawTo(anotherAccount, [firstTokenId], { from: initialHolder });
await expectEvent.inTransaction(tx, this.underlying, 'Transfer', {
from: this.token.address,
to: anotherAccount,
tokenId: firstTokenId,
});
await expectEvent.inTransaction(tx, this.token, 'Transfer', {
from: initialHolder,
to: constants.ZERO_ADDRESS,
tokenId: firstTokenId,
});
await expect(this.token.connect(this.owner).withdrawTo(this.other, [tokenId]))
.to.emit(this.underlying, 'Transfer')
.withArgs(this.token.target, this.other.address, tokenId)
.to.emit(this.token, 'Transfer')
.withArgs(this.owner.address, ethers.ZeroAddress, tokenId);
});
});
describe('onERC721Received', function () {
it('only allows calls from underlying', async function () {
await expectRevertCustomError(
this.token.onERC721Received(
initialHolder,
this.token.address,
firstTokenId,
anotherAccount, // Correct data
{ from: anotherAccount },
await expect(
this.token.connect(this.other).onERC721Received(
this.owner,
this.token,
tokenId,
this.other.address, // Correct data
),
'ERC721UnsupportedToken',
[anotherAccount],
);
)
.to.be.revertedWithCustomError(this.token, 'ERC721UnsupportedToken')
.withArgs(this.other.address);
});
it('mints a token to from', async function () {
const { tx } = await this.underlying.safeTransferFrom(initialHolder, this.token.address, firstTokenId, {
from: initialHolder,
});
await expectEvent.inTransaction(tx, this.token, 'Transfer', {
from: constants.ZERO_ADDRESS,
to: initialHolder,
tokenId: firstTokenId,
});
await expect(this.underlying.connect(this.owner).safeTransferFrom(this.owner, this.token, tokenId))
.to.emit(this.token, 'Transfer')
.withArgs(ethers.ZeroAddress, this.owner.address, tokenId);
});
});
describe('_recover', function () {
it('works if there is something to recover', async function () {
// Should use `transferFrom` to avoid `onERC721Received` minting
await this.underlying.transferFrom(initialHolder, this.token.address, firstTokenId, { from: initialHolder });
await this.underlying.connect(this.owner).transferFrom(this.owner, this.token, tokenId);
const { tx } = await this.token.$_recover(anotherAccount, firstTokenId);
await expectEvent.inTransaction(tx, this.token, 'Transfer', {
from: constants.ZERO_ADDRESS,
to: anotherAccount,
tokenId: firstTokenId,
});
await expect(this.token.$_recover(this.other, tokenId))
.to.emit(this.token, 'Transfer')
.withArgs(ethers.ZeroAddress, this.other.address, tokenId);
});
it('reverts if there is nothing to recover', async function () {
const owner = await this.underlying.ownerOf(firstTokenId);
await expectRevertCustomError(this.token.$_recover(initialHolder, firstTokenId), 'ERC721IncorrectOwner', [
this.token.address,
firstTokenId,
owner,
]);
const holder = await this.underlying.ownerOf(tokenId);
await expect(this.token.$_recover(holder, tokenId))
.to.be.revertedWithCustomError(this.token, 'ERC721IncorrectOwner')
.withArgs(this.token.target, tokenId, holder);
});
});
describe('ERC712 behavior', function () {
shouldBehaveLikeERC721(...accounts);
shouldBehaveLikeERC721();
});
});

View File

@ -1,22 +1,20 @@
const { ethers } = require('hardhat');
const { expect } = require('chai');
const ERC721Holder = artifacts.require('$ERC721Holder');
const ERC721 = artifacts.require('$ERC721');
contract('ERC721Holder', function (accounts) {
const [owner] = accounts;
const name = 'Non Fungible Token';
const symbol = 'NFT';
const tokenId = web3.utils.toBN(1);
const name = 'Non Fungible Token';
const symbol = 'NFT';
const tokenId = 1n;
describe('ERC721Holder', function () {
it('receives an ERC721 token', async function () {
const token = await ERC721.new(name, symbol);
const [owner] = await ethers.getSigners();
const token = await ethers.deployContract('$ERC721', [name, symbol]);
await token.$_mint(owner, tokenId);
const receiver = await ERC721Holder.new();
await token.safeTransferFrom(owner, receiver.address, tokenId, { from: owner });
const receiver = await ethers.deployContract('$ERC721Holder');
await token.connect(owner).safeTransferFrom(owner, receiver, tokenId);
expect(await token.ownerOf(tokenId)).to.be.equal(receiver.address);
expect(await token.ownerOf(tokenId)).to.equal(receiver.target);
});
});

View File

@ -1,12 +1,10 @@
const { BN, constants } = require('@openzeppelin/test-helpers');
const { ethers } = require('hardhat');
const { expect } = require('chai');
const { ZERO_ADDRESS } = constants;
const { shouldSupportInterfaces } = require('../../utils/introspection/SupportsInterface.behavior');
const { expectRevertCustomError } = require('../../helpers/customError');
function shouldBehaveLikeERC2981() {
const royaltyFraction = new BN('10');
const royaltyFraction = 10n;
shouldSupportInterfaces(['ERC2981']);
@ -16,64 +14,54 @@ function shouldBehaveLikeERC2981() {
});
it('checks royalty is set', async function () {
const royalty = new BN((this.salePrice * royaltyFraction) / 10000);
const initInfo = await this.token.royaltyInfo(this.tokenId1, this.salePrice);
expect(initInfo[0]).to.be.equal(this.account1);
expect(initInfo[1]).to.be.bignumber.equal(royalty);
expect(await this.token.royaltyInfo(this.tokenId1, this.salePrice)).to.deep.equal([
this.account1.address,
(this.salePrice * royaltyFraction) / 10_000n,
]);
});
it('updates royalty amount', async function () {
const newPercentage = new BN('25');
const newFraction = 25n;
// Updated royalty check
await this.token.$_setDefaultRoyalty(this.account1, newPercentage);
const royalty = new BN((this.salePrice * newPercentage) / 10000);
const newInfo = await this.token.royaltyInfo(this.tokenId1, this.salePrice);
await this.token.$_setDefaultRoyalty(this.account1, newFraction);
expect(newInfo[0]).to.be.equal(this.account1);
expect(newInfo[1]).to.be.bignumber.equal(royalty);
expect(await this.token.royaltyInfo(this.tokenId1, this.salePrice)).to.deep.equal([
this.account1.address,
(this.salePrice * newFraction) / 10_000n,
]);
});
it('holds same royalty value for different tokens', async function () {
const newPercentage = new BN('20');
await this.token.$_setDefaultRoyalty(this.account1, newPercentage);
const newFraction = 20n;
const token1Info = await this.token.royaltyInfo(this.tokenId1, this.salePrice);
const token2Info = await this.token.royaltyInfo(this.tokenId2, this.salePrice);
await this.token.$_setDefaultRoyalty(this.account1, newFraction);
expect(token1Info[1]).to.be.bignumber.equal(token2Info[1]);
expect(await this.token.royaltyInfo(this.tokenId1, this.salePrice)).to.deep.equal(
await this.token.royaltyInfo(this.tokenId2, this.salePrice),
);
});
it('Remove royalty information', async function () {
const newValue = new BN('0');
const newValue = 0n;
await this.token.$_deleteDefaultRoyalty();
const token1Info = await this.token.royaltyInfo(this.tokenId1, this.salePrice);
const token2Info = await this.token.royaltyInfo(this.tokenId2, this.salePrice);
// Test royalty info is still persistent across all tokens
expect(token1Info[0]).to.be.bignumber.equal(token2Info[0]);
expect(token1Info[1]).to.be.bignumber.equal(token2Info[1]);
// Test information was deleted
expect(token1Info[0]).to.be.equal(ZERO_ADDRESS);
expect(token1Info[1]).to.be.bignumber.equal(newValue);
expect(await this.token.royaltyInfo(this.tokenId1, this.salePrice)).to.deep.equal([ethers.ZeroAddress, newValue]);
expect(await this.token.royaltyInfo(this.tokenId2, this.salePrice)).to.deep.equal([ethers.ZeroAddress, newValue]);
});
it('reverts if invalid parameters', async function () {
const royaltyDenominator = await this.token.$_feeDenominator();
await expectRevertCustomError(
this.token.$_setDefaultRoyalty(ZERO_ADDRESS, royaltyFraction),
'ERC2981InvalidDefaultRoyaltyReceiver',
[ZERO_ADDRESS],
);
const anotherRoyaltyFraction = new BN('11000');
await expectRevertCustomError(
this.token.$_setDefaultRoyalty(this.account1, anotherRoyaltyFraction),
'ERC2981InvalidDefaultRoyalty',
[anotherRoyaltyFraction, royaltyDenominator],
);
await expect(this.token.$_setDefaultRoyalty(ethers.ZeroAddress, royaltyFraction))
.to.be.revertedWithCustomError(this.token, 'ERC2981InvalidDefaultRoyaltyReceiver')
.withArgs(ethers.ZeroAddress);
const anotherRoyaltyFraction = 11000n;
await expect(this.token.$_setDefaultRoyalty(this.account1, anotherRoyaltyFraction))
.to.be.revertedWithCustomError(this.token, 'ERC2981InvalidDefaultRoyalty')
.withArgs(anotherRoyaltyFraction, royaltyDenominator);
});
});
@ -83,83 +71,78 @@ function shouldBehaveLikeERC2981() {
});
it('updates royalty amount', async function () {
const newPercentage = new BN('25');
let royalty = new BN((this.salePrice * royaltyFraction) / 10000);
// Initial royalty check
const initInfo = await this.token.royaltyInfo(this.tokenId1, this.salePrice);
const newFraction = 25n;
expect(initInfo[0]).to.be.equal(this.account1);
expect(initInfo[1]).to.be.bignumber.equal(royalty);
expect(await this.token.royaltyInfo(this.tokenId1, this.salePrice)).to.deep.equal([
this.account1.address,
(this.salePrice * royaltyFraction) / 10_000n,
]);
// Updated royalty check
await this.token.$_setTokenRoyalty(this.tokenId1, this.account1, newPercentage);
royalty = new BN((this.salePrice * newPercentage) / 10000);
const newInfo = await this.token.royaltyInfo(this.tokenId1, this.salePrice);
await this.token.$_setTokenRoyalty(this.tokenId1, this.account1, newFraction);
expect(newInfo[0]).to.be.equal(this.account1);
expect(newInfo[1]).to.be.bignumber.equal(royalty);
expect(await this.token.royaltyInfo(this.tokenId1, this.salePrice)).to.deep.equal([
this.account1.address,
(this.salePrice * newFraction) / 10_000n,
]);
});
it('holds different values for different tokens', async function () {
const newPercentage = new BN('20');
await this.token.$_setTokenRoyalty(this.tokenId2, this.account1, newPercentage);
const newFraction = 20n;
const token1Info = await this.token.royaltyInfo(this.tokenId1, this.salePrice);
const token2Info = await this.token.royaltyInfo(this.tokenId2, this.salePrice);
await this.token.$_setTokenRoyalty(this.tokenId2, this.account1, newFraction);
// must be different even at the same this.salePrice
expect(token1Info[1]).to.not.be.bignumber.equal(token2Info[1]);
expect(await this.token.royaltyInfo(this.tokenId1, this.salePrice)).to.not.deep.equal(
await this.token.royaltyInfo(this.tokenId2, this.salePrice),
);
});
it('reverts if invalid parameters', async function () {
const royaltyDenominator = await this.token.$_feeDenominator();
await expectRevertCustomError(
this.token.$_setTokenRoyalty(this.tokenId1, ZERO_ADDRESS, royaltyFraction),
'ERC2981InvalidTokenRoyaltyReceiver',
[this.tokenId1.toString(), ZERO_ADDRESS],
);
const anotherRoyaltyFraction = new BN('11000');
await expectRevertCustomError(
this.token.$_setTokenRoyalty(this.tokenId1, this.account1, anotherRoyaltyFraction),
'ERC2981InvalidTokenRoyalty',
[this.tokenId1.toString(), anotherRoyaltyFraction, royaltyDenominator],
);
await expect(this.token.$_setTokenRoyalty(this.tokenId1, ethers.ZeroAddress, royaltyFraction))
.to.be.revertedWithCustomError(this.token, 'ERC2981InvalidTokenRoyaltyReceiver')
.withArgs(this.tokenId1, ethers.ZeroAddress);
const anotherRoyaltyFraction = 11000n;
await expect(this.token.$_setTokenRoyalty(this.tokenId1, this.account1, anotherRoyaltyFraction))
.to.be.revertedWithCustomError(this.token, 'ERC2981InvalidTokenRoyalty')
.withArgs(this.tokenId1, anotherRoyaltyFraction, royaltyDenominator);
});
it('can reset token after setting royalty', async function () {
const newPercentage = new BN('30');
const royalty = new BN((this.salePrice * newPercentage) / 10000);
await this.token.$_setTokenRoyalty(this.tokenId1, this.account2, newPercentage);
const newFraction = 30n;
const tokenInfo = await this.token.royaltyInfo(this.tokenId1, this.salePrice);
await this.token.$_setTokenRoyalty(this.tokenId1, this.account2, newFraction);
// Tokens must have own information
expect(tokenInfo[1]).to.be.bignumber.equal(royalty);
expect(tokenInfo[0]).to.be.equal(this.account2);
expect(await this.token.royaltyInfo(this.tokenId1, this.salePrice)).to.deep.equal([
this.account2.address,
(this.salePrice * newFraction) / 10_000n,
]);
await this.token.$_setTokenRoyalty(this.tokenId2, this.account1, 0n);
await this.token.$_setTokenRoyalty(this.tokenId2, this.account1, new BN('0'));
const result = await this.token.royaltyInfo(this.tokenId2, this.salePrice);
// Token must not share default information
expect(result[0]).to.be.equal(this.account1);
expect(result[1]).to.be.bignumber.equal(new BN('0'));
expect(await this.token.royaltyInfo(this.tokenId2, this.salePrice)).to.deep.equal([this.account1.address, 0n]);
});
it('can hold default and token royalty information', async function () {
const newPercentage = new BN('30');
const royalty = new BN((this.salePrice * newPercentage) / 10000);
const newFraction = 30n;
await this.token.$_setTokenRoyalty(this.tokenId2, this.account2, newPercentage);
await this.token.$_setTokenRoyalty(this.tokenId2, this.account2, newFraction);
const token1Info = await this.token.royaltyInfo(this.tokenId1, this.salePrice);
const token2Info = await this.token.royaltyInfo(this.tokenId2, this.salePrice);
// Tokens must not have same values
expect(token1Info[1]).to.not.be.bignumber.equal(token2Info[1]);
expect(token1Info[0]).to.not.be.equal(token2Info[0]);
expect(await this.token.royaltyInfo(this.tokenId1, this.salePrice)).to.not.deep.equal([
this.account2.address,
(this.salePrice * newFraction) / 10_000n,
]);
// Updated token must have new values
expect(token2Info[0]).to.be.equal(this.account2);
expect(token2Info[1]).to.be.bignumber.equal(royalty);
expect(await this.token.royaltyInfo(this.tokenId2, this.salePrice)).to.deep.equal([
this.account2.address,
(this.salePrice * newFraction) / 10_000n,
]);
});
});
}