This commit is contained in:
dexorder
2024-10-17 02:42:28 -04:00
commit 25def69c66
878 changed files with 112489 additions and 0 deletions

View File

@@ -0,0 +1,972 @@
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 baseURI = 'https://api.example.com/v1/';
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);
});
describe('base URI', function () {
beforeEach(function () {
if (!this.token.interface.hasFunction('setBaseURI')) {
this.skip();
}
});
it('base URI can be set', async function () {
await this.token.setBaseURI(baseURI);
expect(await this.token.baseURI()).to.equal(baseURI);
});
it('base URI is added as a prefix to the token URI', async function () {
await this.token.setBaseURI(baseURI);
expect(await this.token.tokenURI(firstTokenId)).to.equal(baseURI + firstTokenId.toString());
});
it('token URI can be changed by changing the base URI', async function () {
await this.token.setBaseURI(baseURI);
const newBaseURI = 'https://api.example.com/v2/';
await this.token.setBaseURI(newBaseURI);
expect(await this.token.tokenURI(firstTokenId)).to.equal(newBaseURI + firstTokenId.toString());
});
});
});
});
}
module.exports = {
shouldBehaveLikeERC721,
shouldBehaveLikeERC721Enumerable,
shouldBehaveLikeERC721Metadata,
};

View File

@@ -0,0 +1,23 @@
const { ethers } = require('hardhat');
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
const { shouldBehaveLikeERC721, shouldBehaveLikeERC721Metadata } = require('./ERC721.behavior');
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 () {
Object.assign(this, await loadFixture(fixture));
});
shouldBehaveLikeERC721();
shouldBehaveLikeERC721Metadata(name, symbol);
});

View File

@@ -0,0 +1,28 @@
const { ethers } = require('hardhat');
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
const {
shouldBehaveLikeERC721,
shouldBehaveLikeERC721Metadata,
shouldBehaveLikeERC721Enumerable,
} = require('./ERC721.behavior');
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 () {
Object.assign(this, await loadFixture(fixture));
});
shouldBehaveLikeERC721();
shouldBehaveLikeERC721Metadata(name, symbol);
shouldBehaveLikeERC721Enumerable();
});

View File

@@ -0,0 +1,77 @@
const { ethers } = require('hardhat');
const { expect } = require('chai');
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
const name = 'Non Fungible Token';
const symbol = 'NFT';
const tokenId = 1n;
const otherTokenId = 2n;
const unknownTokenId = 3n;
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 () {
Object.assign(this, await loadFixture(fixture));
});
describe('like a burnable ERC721', function () {
beforeEach(async function () {
await this.token.$_mint(this.owner, tokenId);
await this.token.$_mint(this.owner, otherTokenId);
});
describe('burn', function () {
describe('when successful', function () {
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);
await expect(this.token.connect(this.owner).burn(tokenId))
.to.emit(this.token, 'Transfer')
.withArgs(this.owner, ethers.ZeroAddress, 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.connect(this.owner).approve(this.approved, tokenId);
await this.token.connect(this.owner).burn(tokenId);
});
describe('getApproved', function () {
it('reverts', async function () {
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 expect(this.token.connect(this.another).burn(tokenId))
.to.be.revertedWithCustomError(this.token, 'ERC721InsufficientApproval')
.withArgs(this.another, tokenId);
});
});
describe('when the given token ID was not tracked by this contract', function () {
it('reverts', async function () {
await expect(this.token.connect(this.owner).burn(unknownTokenId))
.to.be.revertedWithCustomError(this.token, 'ERC721NonexistentToken')
.withArgs(unknownTokenId);
});
});
});
});
});

View File

@@ -0,0 +1,181 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
// solhint-disable func-name-mixedcase
import {ERC721} from "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import {ERC721Consecutive} from "@openzeppelin/contracts/token/ERC721/extensions/ERC721Consecutive.sol";
import {Test, StdUtils} from "@forge-std/Test.sol";
function toSingleton(address account) pure returns (address[] memory) {
address[] memory accounts = new address[](1);
accounts[0] = account;
return accounts;
}
contract ERC721ConsecutiveTarget is StdUtils, ERC721Consecutive {
uint96 private immutable _offset;
uint256 public totalMinted = 0;
constructor(address[] memory receivers, uint256[] memory batches, uint256 startingId) ERC721("", "") {
_offset = uint96(startingId);
for (uint256 i = 0; i < batches.length; i++) {
address receiver = receivers[i % receivers.length];
uint96 batchSize = uint96(bound(batches[i], 0, _maxBatchSize()));
_mintConsecutive(receiver, batchSize);
totalMinted += batchSize;
}
}
function burn(uint256 tokenId) public {
_burn(tokenId);
}
function _firstConsecutiveId() internal view virtual override returns (uint96) {
return _offset;
}
}
contract ERC721ConsecutiveTest is Test {
function test_balance(address receiver, uint256[] calldata batches, uint96 startingId) public {
vm.assume(receiver != address(0));
uint256 startingTokenId = bound(startingId, 0, 5000);
ERC721ConsecutiveTarget token = new ERC721ConsecutiveTarget(toSingleton(receiver), batches, startingTokenId);
assertEq(token.balanceOf(receiver), token.totalMinted());
}
function test_ownership(
address receiver,
uint256[] calldata batches,
uint256[2] calldata unboundedTokenId,
uint96 startingId
) public {
vm.assume(receiver != address(0));
uint256 startingTokenId = bound(startingId, 0, 5000);
ERC721ConsecutiveTarget token = new ERC721ConsecutiveTarget(toSingleton(receiver), batches, startingTokenId);
if (token.totalMinted() > 0) {
uint256 validTokenId = bound(
unboundedTokenId[0],
startingTokenId,
startingTokenId + token.totalMinted() - 1
);
assertEq(token.ownerOf(validTokenId), receiver);
}
uint256 invalidTokenId = bound(
unboundedTokenId[1],
startingTokenId + token.totalMinted(),
startingTokenId + token.totalMinted() + 1
);
vm.expectRevert();
token.ownerOf(invalidTokenId);
}
function test_burn(
address receiver,
uint256[] calldata batches,
uint256 unboundedTokenId,
uint96 startingId
) public {
vm.assume(receiver != address(0));
uint256 startingTokenId = bound(startingId, 0, 5000);
ERC721ConsecutiveTarget token = new ERC721ConsecutiveTarget(toSingleton(receiver), batches, startingTokenId);
// only test if we minted at least one token
uint256 supply = token.totalMinted();
vm.assume(supply > 0);
// burn a token in [0; supply[
uint256 tokenId = bound(unboundedTokenId, startingTokenId, startingTokenId + supply - 1);
token.burn(tokenId);
// balance should have decreased
assertEq(token.balanceOf(receiver), supply - 1);
// token should be burnt
vm.expectRevert();
token.ownerOf(tokenId);
}
function test_transfer(
address[2] calldata accounts,
uint256[2] calldata unboundedBatches,
uint256[2] calldata unboundedTokenId,
uint96 startingId
) public {
vm.assume(accounts[0] != address(0));
vm.assume(accounts[1] != address(0));
vm.assume(accounts[0] != accounts[1]);
uint256 startingTokenId = bound(startingId, 1, 5000);
address[] memory receivers = new address[](2);
receivers[0] = accounts[0];
receivers[1] = accounts[1];
// We assume _maxBatchSize is 5000 (the default). This test will break otherwise.
uint256[] memory batches = new uint256[](2);
batches[0] = bound(unboundedBatches[0], startingTokenId, 5000);
batches[1] = bound(unboundedBatches[1], startingTokenId, 5000);
ERC721ConsecutiveTarget token = new ERC721ConsecutiveTarget(receivers, batches, startingTokenId);
uint256 tokenId0 = bound(unboundedTokenId[0], startingTokenId, batches[0]);
uint256 tokenId1 = bound(unboundedTokenId[1], startingTokenId, batches[1]) + batches[0];
assertEq(token.ownerOf(tokenId0), accounts[0]);
assertEq(token.ownerOf(tokenId1), accounts[1]);
assertEq(token.balanceOf(accounts[0]), batches[0]);
assertEq(token.balanceOf(accounts[1]), batches[1]);
vm.prank(accounts[0]);
token.transferFrom(accounts[0], accounts[1], tokenId0);
assertEq(token.ownerOf(tokenId0), accounts[1]);
assertEq(token.ownerOf(tokenId1), accounts[1]);
assertEq(token.balanceOf(accounts[0]), batches[0] - 1);
assertEq(token.balanceOf(accounts[1]), batches[1] + 1);
vm.prank(accounts[1]);
token.transferFrom(accounts[1], accounts[0], tokenId1);
assertEq(token.ownerOf(tokenId0), accounts[1]);
assertEq(token.ownerOf(tokenId1), accounts[0]);
assertEq(token.balanceOf(accounts[0]), batches[0]);
assertEq(token.balanceOf(accounts[1]), batches[1]);
}
function test_start_consecutive_id(
address receiver,
uint256[2] calldata unboundedBatches,
uint256[2] calldata unboundedTokenId,
uint96 startingId
) public {
vm.assume(receiver != address(0));
uint256 startingTokenId = bound(startingId, 1, 5000);
// We assume _maxBatchSize is 5000 (the default). This test will break otherwise.
uint256[] memory batches = new uint256[](2);
batches[0] = bound(unboundedBatches[0], startingTokenId, 5000);
batches[1] = bound(unboundedBatches[1], startingTokenId, 5000);
ERC721ConsecutiveTarget token = new ERC721ConsecutiveTarget(toSingleton(receiver), batches, startingTokenId);
uint256 tokenId0 = bound(unboundedTokenId[0], startingTokenId, batches[0]);
uint256 tokenId1 = bound(unboundedTokenId[1], startingTokenId, batches[1]);
assertEq(token.ownerOf(tokenId0), receiver);
assertEq(token.ownerOf(tokenId1), receiver);
assertEq(token.balanceOf(receiver), batches[0] + batches[1]);
}
}

View File

@@ -0,0 +1,236 @@
const { ethers } = require('hardhat');
const { expect } = require('chai');
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
const { sum } = require('../../../helpers/math');
const name = 'Non Fungible Token';
const symbol = 'NFT';
describe('ERC721Consecutive', function () {
for (const offset of [0n, 1n, 42n]) {
describe(`with offset ${offset}`, function () {
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 this.batches) {
if (batch.amount > 0) {
await expect(this.token.deploymentTransaction())
.to.emit(this.token, 'ConsecutiveTransfer')
.withArgs(
first /* fromTokenId */,
first + batch.amount - 1n /* toTokenId */,
ethers.ZeroAddress /* fromAddress */,
batch.receiver /* toAddress */,
);
} else {
// ".to.not.emit" only looks at event name, and doesn't check the parameters
}
first += batch.amount;
}
});
it('ownership is set', async function () {
const owners = [
...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] != ethers.ZeroAddress) {
expect(await this.token.ownerOf(tokenId)).to.equal(owners[tokenId]);
}
}
});
it('balance & voting power are set', async function () {
for (const account of this.accounts) {
const balance =
sum(...this.batches.filter(({ receiver }) => receiver === account).map(({ amount }) => amount)) ?? 0n;
expect(await this.token.balanceOf(account)).to.equal(balance);
// If not delegated at construction, check before + do delegation
if (!this.delegates.includes(account)) {
expect(await this.token.getVotes(account)).to.equal(0n);
await this.token.connect(account).delegate(account);
}
// At this point all accounts should have delegated
expect(await this.token.getVotes(account)).to.equal(balance);
}
});
it('reverts on consecutive minting to the zero address', async function () {
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 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(...this.batches.map(b => b.amount)) + offset;
await expect(this.token.ownerOf(tokenId))
.to.be.revertedWithCustomError(this.token, 'ERC721NonexistentToken')
.withArgs(tokenId);
await expect(this.token.$_mint(this.alice, tokenId))
.to.emit(this.token, 'Transfer')
.withArgs(ethers.ZeroAddress, this.alice, tokenId);
});
it('cannot mint a token that has been batched minted', async function () {
const tokenId = sum(...this.batches.map(b => b.amount)) + offset - 1n;
expect(await this.token.ownerOf(tokenId)).to.not.equal(ethers.ZeroAddress);
await expect(this.token.$_mint(this.alice, tokenId))
.to.be.revertedWithCustomError(this.token, 'ERC721InvalidSender')
.withArgs(ethers.ZeroAddress);
});
});
describe('ERC721 behavior', function () {
const tokenId = offset + 1n;
it('core takes over ownership on transfer', async function () {
await this.token.connect(this.alice).transferFrom(this.alice, this.receiver, tokenId);
expect(await this.token.ownerOf(tokenId)).to.equal(this.receiver);
});
it('tokens can be burned and re-minted #1', async function () {
await expect(this.token.connect(this.alice).$_burn(tokenId))
.to.emit(this.token, 'Transfer')
.withArgs(this.alice, ethers.ZeroAddress, tokenId);
await expect(this.token.ownerOf(tokenId))
.to.be.revertedWithCustomError(this.token, 'ERC721NonexistentToken')
.withArgs(tokenId);
await expect(this.token.$_mint(this.bruce, tokenId))
.to.emit(this.token, 'Transfer')
.withArgs(ethers.ZeroAddress, this.bruce, tokenId);
expect(await this.token.ownerOf(tokenId)).to.equal(this.bruce);
});
it('tokens can be burned and re-minted #2', async function () {
const tokenId = sum(...this.batches.map(({ amount }) => amount)) + offset;
await expect(this.token.ownerOf(tokenId))
.to.be.revertedWithCustomError(this.token, 'ERC721NonexistentToken')
.withArgs(tokenId);
// mint
await expect(this.token.$_mint(this.alice, tokenId))
.to.emit(this.token, 'Transfer')
.withArgs(ethers.ZeroAddress, this.alice, tokenId);
expect(await this.token.ownerOf(tokenId)).to.equal(this.alice);
// burn
await expect(await this.token.$_burn(tokenId))
.to.emit(this.token, 'Transfer')
.withArgs(this.alice, ethers.ZeroAddress, tokenId);
await expect(this.token.ownerOf(tokenId))
.to.be.revertedWithCustomError(this.token, 'ERC721NonexistentToken')
.withArgs(tokenId);
// re-mint
await expect(this.token.$_mint(this.bruce, tokenId))
.to.emit(this.token, 'Transfer')
.withArgs(ethers.ZeroAddress, this.bruce, tokenId);
expect(await this.token.ownerOf(tokenId)).to.equal(this.bruce);
});
});
});
}
describe('invalid use', function () {
const receiver = ethers.Wallet.createRandom();
it('cannot mint a batch larger than 5000', async function () {
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 () {
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 () {
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 () {
const { interface } = await ethers.getContractFactory('$ERC721ConsecutiveEnumerableMock');
await expect(
ethers.deployContract('$ERC721ConsecutiveEnumerableMock', [name, symbol, [receiver], [100n]]),
).to.be.revertedWithCustomError({ interface }, 'ERC721EnumerableForbiddenBatchMint');
});
});
});

View File

@@ -0,0 +1,81 @@
const { ethers } = require('hardhat');
const { expect } = require('chai');
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
const name = 'Non Fungible Token';
const symbol = 'NFT';
const tokenId = 1n;
const otherTokenId = 2n;
const data = ethers.Typed.bytes('0x42');
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 () {
Object.assign(this, await loadFixture(fixture));
});
describe('when token is paused', function () {
beforeEach(async function () {
await this.token.$_mint(this.owner, tokenId);
await this.token.$_pause();
});
it('reverts when trying to transferFrom', async function () {
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 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 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 expect(this.token.$_mint(this.receiver, otherTokenId)).to.be.revertedWithCustomError(
this.token,
'EnforcedPause',
);
});
it('reverts when trying to burn', async function () {
await expect(this.token.$_burn(tokenId)).to.be.revertedWithCustomError(this.token, 'EnforcedPause');
});
describe('getApproved', function () {
it('returns approved address', async function () {
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 () {
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 () {
expect(await this.token.ownerOf(tokenId)).to.equal(this.owner);
});
});
describe('isApprovedForAll', function () {
it('returns the approval of the operator', async function () {
expect(await this.token.isApprovedForAll(this.owner, this.operator)).to.be.false;
});
});
});
});

View File

@@ -0,0 +1,57 @@
const { ethers } = require('hardhat');
const { expect } = require('chai');
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
const { shouldBehaveLikeERC2981 } = require('../../common/ERC2981.behavior');
const name = 'Non Fungible Token';
const symbol = 'NFT';
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 () {
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, this.recipient, royalty);
});
it('royalty information are kept during burn and re-mint', async function () {
await this.token.$_burn(tokenId1);
expect(await this.token.royaltyInfo(tokenId1, salePrice)).to.deep.equal([
this.recipient.address,
(salePrice * royalty) / 10000n,
]);
await this.token.$_mint(this.account2, tokenId1);
expect(await this.token.royaltyInfo(tokenId1, salePrice)).to.deep.equal([
this.recipient.address,
(salePrice * royalty) / 10000n,
]);
});
});
shouldBehaveLikeERC2981();
});

View File

@@ -0,0 +1,121 @@
const { ethers } = require('hardhat');
const { expect } = require('chai');
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
const { shouldSupportInterfaces } = require('../../../utils/introspection/SupportsInterface.behavior');
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;
async function fixture() {
const [owner] = await ethers.getSigners();
const token = await ethers.deployContract('$ERC721URIStorageMock', [name, symbol]);
return { owner, token };
}
describe('ERC721URIStorage', function () {
beforeEach(async function () {
Object.assign(this, await loadFixture(fixture));
});
shouldSupportInterfaces(['0x49064906']);
describe('token URI', function () {
beforeEach(async function () {
await this.token.$_mint(this.owner, tokenId);
});
it('it is empty by default', async function () {
expect(await this.token.tokenURI(tokenId)).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);
});
it('can be set for a token id', async function () {
await this.token.$_setTokenURI(tokenId, sampleUri);
expect(await this.token.tokenURI(tokenId)).to.equal(sampleUri);
});
it('setting the uri emits an event', async function () {
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 () {
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(this.owner, nonExistentTokenId);
expect(await this.token.tokenURI(nonExistentTokenId)).to.equal(sampleUri);
});
it('base URI can be set', async function () {
await this.token.setBaseURI(baseURI);
expect(await this.token.$_baseURI()).to.equal(baseURI);
});
it('base URI is added as a prefix to the token URI', async function () {
await this.token.setBaseURI(baseURI);
await this.token.$_setTokenURI(tokenId, 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(tokenId, 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(tokenId)).to.equal(baseURI + tokenId);
});
it('tokens without URI can be burnt ', async function () {
await this.token.$_burn(tokenId);
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(tokenId, sampleUri);
await this.token.$_burn(tokenId);
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(tokenId, sampleUri);
await this.token.$_burn(tokenId);
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

@@ -0,0 +1,194 @@
const { ethers } = require('hardhat');
const { expect } = require('chai');
const { loadFixture, mine } = require('@nomicfoundation/hardhat-network-helpers');
const time = require('../../../helpers/time');
const { shouldBehaveLikeVotes } = require('../../../governance/utils/Votes.behavior');
const TOKENS = [
{ Token: '$ERC721Votes', mode: 'blocknumber' },
// no timestamp mode for ERC721Votes yet
];
const name = 'My Vote';
const symbol = 'MTKN';
const version = '1';
const tokens = [ethers.parseEther('10000000'), 10n, 20n, 30n];
describe('ERC721Votes', function () {
for (const { Token, mode } of TOKENS) {
const fixture = async () => {
// accounts is required by shouldBehaveLikeVotes
const accounts = await ethers.getSigners();
const [holder, recipient, other1, other2] = accounts;
const token = await ethers.deployContract(Token, [name, symbol, name, version]);
return { accounts, holder, recipient, other1, other2, token };
};
describe(`vote with ${mode}`, function () {
beforeEach(async function () {
Object.assign(this, await loadFixture(fixture));
this.votes = this.token;
});
// includes ERC6372 behavior check
shouldBehaveLikeVotes(tokens, { mode, fungible: false });
describe('balanceOf', function () {
beforeEach(async function () {
await this.votes.$_mint(this.holder, tokens[0]);
await this.votes.$_mint(this.holder, tokens[1]);
await this.votes.$_mint(this.holder, tokens[2]);
await this.votes.$_mint(this.holder, tokens[3]);
});
it('grants to initial account', async function () {
expect(await this.votes.balanceOf(this.holder)).to.equal(4n);
});
});
describe('transfers', function () {
beforeEach(async function () {
await this.votes.$_mint(this.holder, tokens[0]);
});
it('no delegation', async function () {
await expect(this.votes.connect(this.holder).transferFrom(this.holder, this.recipient, tokens[0]))
.to.emit(this.token, 'Transfer')
.withArgs(this.holder, this.recipient, tokens[0])
.to.not.emit(this.token, 'DelegateVotesChanged');
this.holderVotes = 0n;
this.recipientVotes = 0n;
});
it('sender delegation', async function () {
await this.votes.connect(this.holder).delegate(this.holder);
const tx = await this.votes.connect(this.holder).transferFrom(this.holder, this.recipient, tokens[0]);
await expect(tx)
.to.emit(this.token, 'Transfer')
.withArgs(this.holder, this.recipient, tokens[0])
.to.emit(this.token, 'DelegateVotesChanged')
.withArgs(this.holder, 1n, 0n);
const { logs } = await tx.wait();
const { index } = logs.find(event => event.fragment.name == 'DelegateVotesChanged');
for (const event of logs.filter(event => event.fragment.name == 'Transfer')) {
expect(event.index).to.lt(index);
}
this.holderVotes = 0n;
this.recipientVotes = 0n;
});
it('receiver delegation', async function () {
await this.votes.connect(this.recipient).delegate(this.recipient);
const tx = await this.votes.connect(this.holder).transferFrom(this.holder, this.recipient, tokens[0]);
await expect(tx)
.to.emit(this.token, 'Transfer')
.withArgs(this.holder, this.recipient, tokens[0])
.to.emit(this.token, 'DelegateVotesChanged')
.withArgs(this.recipient, 0n, 1n);
const { logs } = await tx.wait();
const { index } = logs.find(event => event.fragment.name == 'DelegateVotesChanged');
for (const event of logs.filter(event => event.fragment.name == 'Transfer')) {
expect(event.index).to.lt(index);
}
this.holderVotes = 0n;
this.recipientVotes = 1n;
});
it('full delegation', async function () {
await this.votes.connect(this.holder).delegate(this.holder);
await this.votes.connect(this.recipient).delegate(this.recipient);
const tx = await this.votes.connect(this.holder).transferFrom(this.holder, this.recipient, tokens[0]);
await expect(tx)
.to.emit(this.token, 'Transfer')
.withArgs(this.holder, this.recipient, tokens[0])
.to.emit(this.token, 'DelegateVotesChanged')
.withArgs(this.holder, 1n, 0n)
.to.emit(this.token, 'DelegateVotesChanged')
.withArgs(this.recipient, 0n, 1n);
const { logs } = await tx.wait();
const { index } = logs.find(event => event.fragment.name == 'DelegateVotesChanged');
for (const event of logs.filter(event => event.fragment.name == 'Transfer')) {
expect(event.index).to.lt(index);
}
this.holderVotes = 0;
this.recipientVotes = 1n;
});
it('returns the same total supply on transfers', async function () {
await this.votes.connect(this.holder).delegate(this.holder);
const tx = await this.votes.connect(this.holder).transferFrom(this.holder, this.recipient, tokens[0]);
const timepoint = await time.clockFromReceipt[mode](tx);
await mine(2);
expect(await this.votes.getPastTotalSupply(timepoint - 1n)).to.equal(1n);
expect(await this.votes.getPastTotalSupply(timepoint + 1n)).to.equal(1n);
this.holderVotes = 0n;
this.recipientVotes = 0n;
});
it('generally returns the voting balance at the appropriate checkpoint', async function () {
await this.votes.$_mint(this.holder, tokens[1]);
await this.votes.$_mint(this.holder, tokens[2]);
await this.votes.$_mint(this.holder, tokens[3]);
const total = await this.votes.balanceOf(this.holder);
const t1 = await this.votes.connect(this.holder).delegate(this.other1);
await mine(2);
const t2 = await this.votes.connect(this.holder).transferFrom(this.holder, this.other2, tokens[0]);
await mine(2);
const t3 = await this.votes.connect(this.holder).transferFrom(this.holder, this.other2, tokens[2]);
await mine(2);
const t4 = await this.votes.connect(this.other2).transferFrom(this.other2, this.holder, tokens[2]);
await mine(2);
t1.timepoint = await time.clockFromReceipt[mode](t1);
t2.timepoint = await time.clockFromReceipt[mode](t2);
t3.timepoint = await time.clockFromReceipt[mode](t3);
t4.timepoint = await time.clockFromReceipt[mode](t4);
expect(await this.votes.getPastVotes(this.other1, t1.timepoint - 1n)).to.equal(0n);
expect(await this.votes.getPastVotes(this.other1, t1.timepoint)).to.equal(total);
expect(await this.votes.getPastVotes(this.other1, t1.timepoint + 1n)).to.equal(total);
expect(await this.votes.getPastVotes(this.other1, t2.timepoint)).to.equal(3n);
expect(await this.votes.getPastVotes(this.other1, t2.timepoint + 1n)).to.equal(3n);
expect(await this.votes.getPastVotes(this.other1, t3.timepoint)).to.equal(2n);
expect(await this.votes.getPastVotes(this.other1, t3.timepoint + 1n)).to.equal(2n);
expect(await this.votes.getPastVotes(this.other1, t4.timepoint)).to.equal('3');
expect(await this.votes.getPastVotes(this.other1, t4.timepoint + 1n)).to.equal(3n);
this.holderVotes = 0n;
this.recipientVotes = 0n;
});
afterEach(async function () {
expect(await this.votes.getVotes(this.holder)).to.equal(this.holderVotes);
expect(await this.votes.getVotes(this.recipient)).to.equal(this.recipientVotes);
// need to advance 2 blocks to see the effect of a transfer on "getPastVotes"
const timepoint = await time.clock[mode]();
await mine();
expect(await this.votes.getPastVotes(this.holder, timepoint)).to.equal(this.holderVotes);
expect(await this.votes.getPastVotes(this.recipient, timepoint)).to.equal(this.recipientVotes);
});
});
});
}
});

View File

@@ -0,0 +1,201 @@
const { ethers } = require('hardhat');
const { expect } = require('chai');
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
const { shouldBehaveLikeERC721 } = require('../ERC721.behavior');
const name = 'Non Fungible Token';
const symbol = 'NFT';
const tokenId = 1n;
const otherTokenId = 2n;
async function fixture() {
const accounts = await ethers.getSigners();
const [owner, approved, other] = accounts;
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 () {
Object.assign(this, await loadFixture(fixture));
});
it('has a name', async function () {
expect(await this.token.name()).to.equal(`Wrapped ${name}`);
});
it('has a symbol', async function () {
expect(await this.token.symbol()).to.equal(`W${symbol}`);
});
it('has underlying', async function () {
expect(await this.token.underlying()).to.equal(this.underlying);
});
describe('depositFor', function () {
it('works with token approval', async function () {
await this.underlying.connect(this.owner).approve(this.token, tokenId);
await expect(this.token.connect(this.owner).depositFor(this.owner, [tokenId]))
.to.emit(this.underlying, 'Transfer')
.withArgs(this.owner, this.token, tokenId)
.to.emit(this.token, 'Transfer')
.withArgs(ethers.ZeroAddress, this.owner, tokenId);
});
it('works with approval for all', async function () {
await this.underlying.connect(this.owner).setApprovalForAll(this.token, true);
await expect(this.token.connect(this.owner).depositFor(this.owner, [tokenId]))
.to.emit(this.underlying, 'Transfer')
.withArgs(this.owner, this.token, tokenId)
.to.emit(this.token, 'Transfer')
.withArgs(ethers.ZeroAddress, this.owner, tokenId);
});
it('works sending to another account', async function () {
await this.underlying.connect(this.owner).approve(this.token, tokenId);
await expect(this.token.connect(this.owner).depositFor(this.other, [tokenId]))
.to.emit(this.underlying, 'Transfer')
.withArgs(this.owner, this.token, tokenId)
.to.emit(this.token, 'Transfer')
.withArgs(ethers.ZeroAddress, this.other, tokenId);
});
it('works with multiple tokens', async function () {
await this.underlying.connect(this.owner).approve(this.token, tokenId);
await this.underlying.connect(this.owner).approve(this.token, otherTokenId);
await expect(this.token.connect(this.owner).depositFor(this.owner, [tokenId, otherTokenId]))
.to.emit(this.underlying, 'Transfer')
.withArgs(this.owner, this.token, tokenId)
.to.emit(this.token, 'Transfer')
.withArgs(ethers.ZeroAddress, this.owner, tokenId)
.to.emit(this.underlying, 'Transfer')
.withArgs(this.owner, this.token, otherTokenId)
.to.emit(this.token, 'Transfer')
.withArgs(ethers.ZeroAddress, this.owner, otherTokenId);
});
it('reverts with missing approval', async function () {
await expect(this.token.connect(this.owner).depositFor(this.owner, [tokenId]))
.to.be.revertedWithCustomError(this.token, 'ERC721InsufficientApproval')
.withArgs(this.token, tokenId);
});
});
describe('withdrawTo', function () {
beforeEach(async function () {
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 () {
await expect(this.token.connect(this.owner).withdrawTo(this.owner, [tokenId]))
.to.emit(this.underlying, 'Transfer')
.withArgs(this.token, this.owner, tokenId)
.to.emit(this.token, 'Transfer')
.withArgs(this.owner, ethers.ZeroAddress, tokenId);
});
it('works for an approved', async function () {
await this.token.connect(this.owner).approve(this.approved, tokenId);
await expect(this.token.connect(this.approved).withdrawTo(this.owner, [tokenId]))
.to.emit(this.underlying, 'Transfer')
.withArgs(this.token, this.owner, tokenId)
.to.emit(this.token, 'Transfer')
.withArgs(this.owner, ethers.ZeroAddress, tokenId);
});
it('works for an approved for all', async function () {
await this.token.connect(this.owner).setApprovalForAll(this.approved, true);
await expect(this.token.connect(this.approved).withdrawTo(this.owner, [tokenId]))
.to.emit(this.underlying, 'Transfer')
.withArgs(this.token, this.owner, tokenId)
.to.emit(this.token, 'Transfer')
.withArgs(this.owner, ethers.ZeroAddress, tokenId);
});
it("doesn't work for a non-owner nor approved", async function () {
await expect(this.token.connect(this.other).withdrawTo(this.owner, [tokenId]))
.to.be.revertedWithCustomError(this.token, 'ERC721InsufficientApproval')
.withArgs(this.other, tokenId);
});
it('works with multiple tokens', async function () {
await this.underlying.connect(this.owner).approve(this.token, otherTokenId);
await this.token.connect(this.owner).depositFor(this.owner, [otherTokenId]);
await expect(this.token.connect(this.owner).withdrawTo(this.owner, [tokenId, otherTokenId]))
.to.emit(this.underlying, 'Transfer')
.withArgs(this.token, this.owner, tokenId)
.to.emit(this.underlying, 'Transfer')
.withArgs(this.token, this.owner, tokenId)
.to.emit(this.token, 'Transfer')
.withArgs(this.owner, ethers.ZeroAddress, tokenId)
.to.emit(this.token, 'Transfer')
.withArgs(this.owner, ethers.ZeroAddress, tokenId);
});
it('works to another account', async function () {
await expect(this.token.connect(this.owner).withdrawTo(this.other, [tokenId]))
.to.emit(this.underlying, 'Transfer')
.withArgs(this.token, this.other, tokenId)
.to.emit(this.token, 'Transfer')
.withArgs(this.owner, ethers.ZeroAddress, tokenId);
});
});
describe('onERC721Received', function () {
it('only allows calls from underlying', async function () {
await expect(
this.token.connect(this.other).onERC721Received(
this.owner,
this.token,
tokenId,
this.other.address, // Correct data
),
)
.to.be.revertedWithCustomError(this.token, 'ERC721UnsupportedToken')
.withArgs(this.other);
});
it('mints a token to from', async function () {
await expect(this.underlying.connect(this.owner).safeTransferFrom(this.owner, this.token, tokenId))
.to.emit(this.token, 'Transfer')
.withArgs(ethers.ZeroAddress, this.owner, tokenId);
});
});
describe('_recover', function () {
it('works if there is something to recover', async function () {
// Should use `transferFrom` to avoid `onERC721Received` minting
await this.underlying.connect(this.owner).transferFrom(this.owner, this.token, tokenId);
await expect(this.token.$_recover(this.other, tokenId))
.to.emit(this.token, 'Transfer')
.withArgs(ethers.ZeroAddress, this.other, tokenId);
});
it('reverts if there is nothing to recover', async function () {
const holder = await this.underlying.ownerOf(tokenId);
await expect(this.token.$_recover(holder, tokenId))
.to.be.revertedWithCustomError(this.token, 'ERC721IncorrectOwner')
.withArgs(this.token, tokenId, holder);
});
});
describe('ERC712 behavior', function () {
shouldBehaveLikeERC721();
});
});

View File

@@ -0,0 +1,20 @@
const { ethers } = require('hardhat');
const { expect } = require('chai');
const name = 'Non Fungible Token';
const symbol = 'NFT';
const tokenId = 1n;
describe('ERC721Holder', function () {
it('receives an ERC721 token', async function () {
const [owner] = await ethers.getSigners();
const token = await ethers.deployContract('$ERC721', [name, symbol]);
await token.$_mint(owner, tokenId);
const receiver = await ethers.deployContract('$ERC721Holder');
await token.connect(owner).safeTransferFrom(owner, receiver, tokenId);
expect(await token.ownerOf(tokenId)).to.equal(receiver);
});
});

View File

@@ -0,0 +1,94 @@
const { ethers } = require('hardhat');
const { expect } = require('chai');
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
const { RevertType } = require('../../../helpers/enums');
const { PANIC_CODES } = require('@nomicfoundation/hardhat-chai-matchers/panic');
const tokenId = 1n;
const RECEIVER_MAGIC_VALUE = '0x150b7a02';
const deployReceiver = (revertType, returnValue = RECEIVER_MAGIC_VALUE) =>
ethers.deployContract('$ERC721ReceiverMock', [returnValue, revertType]);
const fixture = async () => {
const [eoa, operator, owner] = await ethers.getSigners();
const utils = await ethers.deployContract('$ERC721Utils');
const receivers = {
correct: await deployReceiver(RevertType.None),
invalid: await deployReceiver(RevertType.None, '0xdeadbeef'),
message: await deployReceiver(RevertType.RevertWithMessage),
empty: await deployReceiver(RevertType.RevertWithoutMessage),
customError: await deployReceiver(RevertType.RevertWithCustomError),
panic: await deployReceiver(RevertType.Panic),
nonReceiver: await ethers.deployContract('CallReceiverMock'),
eoa,
};
return { operator, owner, utils, receivers };
};
describe('ERC721Utils', function () {
beforeEach(async function () {
Object.assign(this, await loadFixture(fixture));
});
describe('onERC721Received', function () {
it('succeeds when called by an EOA', async function () {
await expect(this.utils.$checkOnERC721Received(this.operator, this.owner, this.receivers.eoa, tokenId, '0x')).to
.not.be.reverted;
});
it('succeeds when data is passed', async function () {
const data = '0x12345678';
await expect(this.utils.$checkOnERC721Received(this.operator, this.owner, this.receivers.correct, tokenId, data))
.to.not.be.reverted;
});
it('succeeds when data is empty', async function () {
await expect(this.utils.$checkOnERC721Received(this.operator, this.owner, this.receivers.correct, tokenId, '0x'))
.to.not.be.reverted;
});
it('reverts when receiver returns invalid value', async function () {
await expect(this.utils.$checkOnERC721Received(this.operator, this.owner, this.receivers.invalid, tokenId, '0x'))
.to.be.revertedWithCustomError(this.utils, 'ERC721InvalidReceiver')
.withArgs(this.receivers.invalid);
});
it('reverts when receiver reverts with message', async function () {
await expect(
this.utils.$checkOnERC721Received(this.operator, this.owner, this.receivers.message, tokenId, '0x'),
).to.be.revertedWith('ERC721ReceiverMock: reverting');
});
it('reverts when receiver reverts without message', async function () {
await expect(this.utils.$checkOnERC721Received(this.operator, this.owner, this.receivers.empty, tokenId, '0x'))
.to.be.revertedWithCustomError(this.utils, 'ERC721InvalidReceiver')
.withArgs(this.receivers.empty);
});
it('reverts when receiver reverts with custom error', async function () {
await expect(
this.utils.$checkOnERC721Received(this.operator, this.owner, this.receivers.customError, tokenId, '0x'),
)
.to.be.revertedWithCustomError(this.receivers.customError, 'CustomError')
.withArgs(RECEIVER_MAGIC_VALUE);
});
it('reverts when receiver panics', async function () {
await expect(
this.utils.$checkOnERC721Received(this.operator, this.owner, this.receivers.panic, tokenId, '0x'),
).to.be.revertedWithPanic(PANIC_CODES.DIVISION_BY_ZERO);
});
it('reverts when receiver does not implement onERC721Received', async function () {
await expect(
this.utils.$checkOnERC721Received(this.operator, this.owner, this.receivers.nonReceiver, tokenId, '0x'),
)
.to.be.revertedWithCustomError(this.utils, 'ERC721InvalidReceiver')
.withArgs(this.receivers.nonReceiver);
});
});
});