mirror of
https://github.com/th30d4y/OpenLearnX.git
synced 2026-05-26 19:26:33 +00:00
Fix .gitignore: stop tracking ignored files
This commit is contained in:
@@ -0,0 +1,893 @@
|
||||
const { BN, constants, expectEvent, expectRevert } = require('@openzeppelin/test-helpers');
|
||||
const { expect } = require('chai');
|
||||
const { ZERO_ADDRESS } = constants;
|
||||
|
||||
const { shouldSupportInterfaces } = require('../../utils/introspection/SupportsInterface.behavior');
|
||||
|
||||
const ERC721ReceiverMock = artifacts.require('ERC721ReceiverMock');
|
||||
const NonERC721ReceiverMock = artifacts.require('CallReceiverMock');
|
||||
|
||||
const Error = ['None', 'RevertWithMessage', 'RevertWithoutMessage', 'Panic'].reduce(
|
||||
(acc, entry, idx) => Object.assign({ [entry]: idx }, acc),
|
||||
{},
|
||||
);
|
||||
|
||||
const firstTokenId = new BN('5042');
|
||||
const secondTokenId = new BN('79217');
|
||||
const nonExistentTokenId = new BN('13');
|
||||
const fourthTokenId = new BN(4);
|
||||
const baseURI = 'https://api.example.com/v1/';
|
||||
|
||||
const RECEIVER_MAGIC_VALUE = '0x150b7a02';
|
||||
|
||||
function shouldBehaveLikeERC721(errorPrefix, owner, newOwner, approved, anotherApproved, operator, other) {
|
||||
shouldSupportInterfaces(['ERC165', 'ERC721']);
|
||||
|
||||
context('with minted tokens', function () {
|
||||
beforeEach(async function () {
|
||||
await this.token.$_mint(owner, firstTokenId);
|
||||
await this.token.$_mint(owner, secondTokenId);
|
||||
this.toWhom = other; // default to other for toWhom in context-dependent tests
|
||||
});
|
||||
|
||||
describe('balanceOf', function () {
|
||||
context('when the given address owns some tokens', function () {
|
||||
it('returns the amount of tokens owned by the given address', async function () {
|
||||
expect(await this.token.balanceOf(owner)).to.be.bignumber.equal('2');
|
||||
});
|
||||
});
|
||||
|
||||
context('when the given address does not own any tokens', function () {
|
||||
it('returns 0', async function () {
|
||||
expect(await this.token.balanceOf(other)).to.be.bignumber.equal('0');
|
||||
});
|
||||
});
|
||||
|
||||
context('when querying the zero address', function () {
|
||||
it('throws', async function () {
|
||||
await expectRevert(this.token.balanceOf(ZERO_ADDRESS), 'ERC721: address zero is not a valid owner');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('ownerOf', function () {
|
||||
context('when the given token ID was tracked by this token', function () {
|
||||
const tokenId = firstTokenId;
|
||||
|
||||
it('returns the owner of the given token ID', async function () {
|
||||
expect(await this.token.ownerOf(tokenId)).to.be.equal(owner);
|
||||
});
|
||||
});
|
||||
|
||||
context('when the given token ID was not tracked by this token', function () {
|
||||
const tokenId = nonExistentTokenId;
|
||||
|
||||
it('reverts', async function () {
|
||||
await expectRevert(this.token.ownerOf(tokenId), 'ERC721: invalid token ID');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('transfers', function () {
|
||||
const tokenId = firstTokenId;
|
||||
const data = '0x42';
|
||||
|
||||
let receipt = null;
|
||||
|
||||
beforeEach(async function () {
|
||||
await this.token.approve(approved, tokenId, { from: owner });
|
||||
await this.token.setApprovalForAll(operator, true, { from: owner });
|
||||
});
|
||||
|
||||
const transferWasSuccessful = function ({ owner, tokenId }) {
|
||||
it('transfers the ownership of the given token ID to the given address', async function () {
|
||||
expect(await this.token.ownerOf(tokenId)).to.be.equal(this.toWhom);
|
||||
});
|
||||
|
||||
it('emits a Transfer event', async function () {
|
||||
expectEvent(receipt, 'Transfer', { from: owner, to: this.toWhom, tokenId: tokenId });
|
||||
});
|
||||
|
||||
it('clears the approval for the token ID', async function () {
|
||||
expect(await this.token.getApproved(tokenId)).to.be.equal(ZERO_ADDRESS);
|
||||
});
|
||||
|
||||
it('adjusts owners balances', async function () {
|
||||
expect(await this.token.balanceOf(owner)).to.be.bignumber.equal('1');
|
||||
});
|
||||
|
||||
it('adjusts owners tokens by index', async function () {
|
||||
if (!this.token.tokenOfOwnerByIndex) return;
|
||||
|
||||
expect(await this.token.tokenOfOwnerByIndex(this.toWhom, 0)).to.be.bignumber.equal(tokenId);
|
||||
|
||||
expect(await this.token.tokenOfOwnerByIndex(owner, 0)).to.be.bignumber.not.equal(tokenId);
|
||||
});
|
||||
};
|
||||
|
||||
const shouldTransferTokensByUsers = function (transferFunction) {
|
||||
context('when called by the owner', function () {
|
||||
beforeEach(async function () {
|
||||
receipt = await transferFunction.call(this, owner, this.toWhom, tokenId, { from: owner });
|
||||
});
|
||||
transferWasSuccessful({ owner, tokenId, approved });
|
||||
});
|
||||
|
||||
context('when called by the approved individual', function () {
|
||||
beforeEach(async function () {
|
||||
receipt = await transferFunction.call(this, owner, this.toWhom, tokenId, { from: approved });
|
||||
});
|
||||
transferWasSuccessful({ owner, tokenId, approved });
|
||||
});
|
||||
|
||||
context('when called by the operator', function () {
|
||||
beforeEach(async function () {
|
||||
receipt = await transferFunction.call(this, owner, this.toWhom, tokenId, { from: operator });
|
||||
});
|
||||
transferWasSuccessful({ owner, tokenId, approved });
|
||||
});
|
||||
|
||||
context('when called by the owner without an approved user', function () {
|
||||
beforeEach(async function () {
|
||||
await this.token.approve(ZERO_ADDRESS, tokenId, { from: owner });
|
||||
receipt = await transferFunction.call(this, owner, this.toWhom, tokenId, { from: operator });
|
||||
});
|
||||
transferWasSuccessful({ owner, tokenId, approved: null });
|
||||
});
|
||||
|
||||
context('when sent to the owner', function () {
|
||||
beforeEach(async function () {
|
||||
receipt = await transferFunction.call(this, owner, owner, tokenId, { from: owner });
|
||||
});
|
||||
|
||||
it('keeps ownership of the token', async function () {
|
||||
expect(await this.token.ownerOf(tokenId)).to.be.equal(owner);
|
||||
});
|
||||
|
||||
it('clears the approval for the token ID', async function () {
|
||||
expect(await this.token.getApproved(tokenId)).to.be.equal(ZERO_ADDRESS);
|
||||
});
|
||||
|
||||
it('emits only a transfer event', async function () {
|
||||
expectEvent(receipt, 'Transfer', {
|
||||
from: owner,
|
||||
to: owner,
|
||||
tokenId: tokenId,
|
||||
});
|
||||
});
|
||||
|
||||
it('keeps the owner balance', async function () {
|
||||
expect(await this.token.balanceOf(owner)).to.be.bignumber.equal('2');
|
||||
});
|
||||
|
||||
it('keeps same tokens by index', async function () {
|
||||
if (!this.token.tokenOfOwnerByIndex) return;
|
||||
const tokensListed = await Promise.all([0, 1].map(i => this.token.tokenOfOwnerByIndex(owner, i)));
|
||||
expect(tokensListed.map(t => t.toNumber())).to.have.members([
|
||||
firstTokenId.toNumber(),
|
||||
secondTokenId.toNumber(),
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
context('when the address of the previous owner is incorrect', function () {
|
||||
it('reverts', async function () {
|
||||
await expectRevert(
|
||||
transferFunction.call(this, other, other, tokenId, { from: owner }),
|
||||
'ERC721: transfer from incorrect owner',
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
context('when the sender is not authorized for the token id', function () {
|
||||
it('reverts', async function () {
|
||||
await expectRevert(
|
||||
transferFunction.call(this, owner, other, tokenId, { from: other }),
|
||||
'ERC721: caller is not token owner or approved',
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
context('when the given token ID does not exist', function () {
|
||||
it('reverts', async function () {
|
||||
await expectRevert(
|
||||
transferFunction.call(this, owner, other, nonExistentTokenId, { from: owner }),
|
||||
'ERC721: invalid token ID',
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
context('when the address to transfer the token to is the zero address', function () {
|
||||
it('reverts', async function () {
|
||||
await expectRevert(
|
||||
transferFunction.call(this, owner, ZERO_ADDRESS, tokenId, { from: owner }),
|
||||
'ERC721: transfer to the zero address',
|
||||
);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
describe('via transferFrom', function () {
|
||||
shouldTransferTokensByUsers(function (from, to, tokenId, opts) {
|
||||
return this.token.transferFrom(from, to, tokenId, opts);
|
||||
});
|
||||
});
|
||||
|
||||
describe('via safeTransferFrom', function () {
|
||||
const safeTransferFromWithData = function (from, to, tokenId, opts) {
|
||||
return this.token.methods['safeTransferFrom(address,address,uint256,bytes)'](from, to, tokenId, data, opts);
|
||||
};
|
||||
|
||||
const safeTransferFromWithoutData = function (from, to, tokenId, opts) {
|
||||
return this.token.methods['safeTransferFrom(address,address,uint256)'](from, to, tokenId, opts);
|
||||
};
|
||||
|
||||
const shouldTransferSafely = function (transferFun, data) {
|
||||
describe('to a user account', function () {
|
||||
shouldTransferTokensByUsers(transferFun);
|
||||
});
|
||||
|
||||
describe('to a valid receiver contract', function () {
|
||||
beforeEach(async function () {
|
||||
this.receiver = await ERC721ReceiverMock.new(RECEIVER_MAGIC_VALUE, Error.None);
|
||||
this.toWhom = this.receiver.address;
|
||||
});
|
||||
|
||||
shouldTransferTokensByUsers(transferFun);
|
||||
|
||||
it('calls onERC721Received', async function () {
|
||||
const receipt = await transferFun.call(this, owner, this.receiver.address, tokenId, { from: owner });
|
||||
|
||||
await expectEvent.inTransaction(receipt.tx, ERC721ReceiverMock, 'Received', {
|
||||
operator: owner,
|
||||
from: owner,
|
||||
tokenId: tokenId,
|
||||
data: data,
|
||||
});
|
||||
});
|
||||
|
||||
it('calls onERC721Received from approved', async function () {
|
||||
const receipt = await transferFun.call(this, owner, this.receiver.address, tokenId, { from: approved });
|
||||
|
||||
await expectEvent.inTransaction(receipt.tx, ERC721ReceiverMock, 'Received', {
|
||||
operator: approved,
|
||||
from: owner,
|
||||
tokenId: tokenId,
|
||||
data: data,
|
||||
});
|
||||
});
|
||||
|
||||
describe('with an invalid token id', function () {
|
||||
it('reverts', async function () {
|
||||
await expectRevert(
|
||||
transferFun.call(this, owner, this.receiver.address, nonExistentTokenId, { from: owner }),
|
||||
'ERC721: invalid token ID',
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
describe('with data', function () {
|
||||
shouldTransferSafely(safeTransferFromWithData, data);
|
||||
});
|
||||
|
||||
describe('without data', function () {
|
||||
shouldTransferSafely(safeTransferFromWithoutData, null);
|
||||
});
|
||||
|
||||
describe('to a receiver contract returning unexpected value', function () {
|
||||
it('reverts', async function () {
|
||||
const invalidReceiver = await ERC721ReceiverMock.new('0x42', Error.None);
|
||||
await expectRevert(
|
||||
this.token.safeTransferFrom(owner, invalidReceiver.address, tokenId, { from: owner }),
|
||||
'ERC721: transfer to non ERC721Receiver implementer',
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('to a receiver contract that reverts with message', function () {
|
||||
it('reverts', async function () {
|
||||
const revertingReceiver = await ERC721ReceiverMock.new(RECEIVER_MAGIC_VALUE, Error.RevertWithMessage);
|
||||
await expectRevert(
|
||||
this.token.safeTransferFrom(owner, revertingReceiver.address, tokenId, { from: owner }),
|
||||
'ERC721ReceiverMock: reverting',
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('to a receiver contract that reverts without message', function () {
|
||||
it('reverts', async function () {
|
||||
const revertingReceiver = await ERC721ReceiverMock.new(RECEIVER_MAGIC_VALUE, Error.RevertWithoutMessage);
|
||||
await expectRevert(
|
||||
this.token.safeTransferFrom(owner, revertingReceiver.address, tokenId, { from: owner }),
|
||||
'ERC721: transfer to non ERC721Receiver implementer',
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('to a receiver contract that panics', function () {
|
||||
it('reverts', async function () {
|
||||
const revertingReceiver = await ERC721ReceiverMock.new(RECEIVER_MAGIC_VALUE, Error.Panic);
|
||||
await expectRevert.unspecified(
|
||||
this.token.safeTransferFrom(owner, revertingReceiver.address, tokenId, { from: owner }),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('to a contract that does not implement the required function', function () {
|
||||
it('reverts', async function () {
|
||||
const nonReceiver = await NonERC721ReceiverMock.new();
|
||||
await expectRevert(
|
||||
this.token.safeTransferFrom(owner, nonReceiver.address, tokenId, { from: owner }),
|
||||
'ERC721: transfer to non ERC721Receiver implementer',
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('safe mint', function () {
|
||||
const tokenId = fourthTokenId;
|
||||
const data = '0x42';
|
||||
|
||||
describe('via safeMint', function () {
|
||||
// regular minting is tested in ERC721Mintable.test.js and others
|
||||
it('calls onERC721Received — with data', async function () {
|
||||
this.receiver = await ERC721ReceiverMock.new(RECEIVER_MAGIC_VALUE, Error.None);
|
||||
const receipt = await this.token.$_safeMint(this.receiver.address, tokenId, data);
|
||||
|
||||
await expectEvent.inTransaction(receipt.tx, ERC721ReceiverMock, 'Received', {
|
||||
from: ZERO_ADDRESS,
|
||||
tokenId: tokenId,
|
||||
data: data,
|
||||
});
|
||||
});
|
||||
|
||||
it('calls onERC721Received — without data', async function () {
|
||||
this.receiver = await ERC721ReceiverMock.new(RECEIVER_MAGIC_VALUE, Error.None);
|
||||
const receipt = await this.token.$_safeMint(this.receiver.address, tokenId);
|
||||
|
||||
await expectEvent.inTransaction(receipt.tx, ERC721ReceiverMock, 'Received', {
|
||||
from: ZERO_ADDRESS,
|
||||
tokenId: tokenId,
|
||||
});
|
||||
});
|
||||
|
||||
context('to a receiver contract returning unexpected value', function () {
|
||||
it('reverts', async function () {
|
||||
const invalidReceiver = await ERC721ReceiverMock.new('0x42', Error.None);
|
||||
await expectRevert(
|
||||
this.token.$_safeMint(invalidReceiver.address, tokenId),
|
||||
'ERC721: transfer to non ERC721Receiver implementer',
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
context('to a receiver contract that reverts with message', function () {
|
||||
it('reverts', async function () {
|
||||
const revertingReceiver = await ERC721ReceiverMock.new(RECEIVER_MAGIC_VALUE, Error.RevertWithMessage);
|
||||
await expectRevert(
|
||||
this.token.$_safeMint(revertingReceiver.address, tokenId),
|
||||
'ERC721ReceiverMock: reverting',
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
context('to a receiver contract that reverts without message', function () {
|
||||
it('reverts', async function () {
|
||||
const revertingReceiver = await ERC721ReceiverMock.new(RECEIVER_MAGIC_VALUE, Error.RevertWithoutMessage);
|
||||
await expectRevert(
|
||||
this.token.$_safeMint(revertingReceiver.address, tokenId),
|
||||
'ERC721: transfer to non ERC721Receiver implementer',
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
context('to a receiver contract that panics', function () {
|
||||
it('reverts', async function () {
|
||||
const revertingReceiver = await ERC721ReceiverMock.new(RECEIVER_MAGIC_VALUE, Error.Panic);
|
||||
await expectRevert.unspecified(this.token.$_safeMint(revertingReceiver.address, tokenId));
|
||||
});
|
||||
});
|
||||
|
||||
context('to a contract that does not implement the required function', function () {
|
||||
it('reverts', async function () {
|
||||
const nonReceiver = await NonERC721ReceiverMock.new();
|
||||
await expectRevert(
|
||||
this.token.$_safeMint(nonReceiver.address, tokenId),
|
||||
'ERC721: transfer to non ERC721Receiver implementer',
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('approve', function () {
|
||||
const tokenId = firstTokenId;
|
||||
|
||||
let receipt = null;
|
||||
|
||||
const itClearsApproval = function () {
|
||||
it('clears approval for the token', async function () {
|
||||
expect(await this.token.getApproved(tokenId)).to.be.equal(ZERO_ADDRESS);
|
||||
});
|
||||
};
|
||||
|
||||
const itApproves = function (address) {
|
||||
it('sets the approval for the target address', async function () {
|
||||
expect(await this.token.getApproved(tokenId)).to.be.equal(address);
|
||||
});
|
||||
};
|
||||
|
||||
const itEmitsApprovalEvent = function (address) {
|
||||
it('emits an approval event', async function () {
|
||||
expectEvent(receipt, 'Approval', {
|
||||
owner: owner,
|
||||
approved: address,
|
||||
tokenId: tokenId,
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
context('when clearing approval', function () {
|
||||
context('when there was no prior approval', function () {
|
||||
beforeEach(async function () {
|
||||
receipt = await this.token.approve(ZERO_ADDRESS, tokenId, { from: owner });
|
||||
});
|
||||
|
||||
itClearsApproval();
|
||||
itEmitsApprovalEvent(ZERO_ADDRESS);
|
||||
});
|
||||
|
||||
context('when there was a prior approval', function () {
|
||||
beforeEach(async function () {
|
||||
await this.token.approve(approved, tokenId, { from: owner });
|
||||
receipt = await this.token.approve(ZERO_ADDRESS, tokenId, { from: owner });
|
||||
});
|
||||
|
||||
itClearsApproval();
|
||||
itEmitsApprovalEvent(ZERO_ADDRESS);
|
||||
});
|
||||
});
|
||||
|
||||
context('when approving a non-zero address', function () {
|
||||
context('when there was no prior approval', function () {
|
||||
beforeEach(async function () {
|
||||
receipt = await this.token.approve(approved, tokenId, { from: owner });
|
||||
});
|
||||
|
||||
itApproves(approved);
|
||||
itEmitsApprovalEvent(approved);
|
||||
});
|
||||
|
||||
context('when there was a prior approval to the same address', function () {
|
||||
beforeEach(async function () {
|
||||
await this.token.approve(approved, tokenId, { from: owner });
|
||||
receipt = await this.token.approve(approved, tokenId, { from: owner });
|
||||
});
|
||||
|
||||
itApproves(approved);
|
||||
itEmitsApprovalEvent(approved);
|
||||
});
|
||||
|
||||
context('when there was a prior approval to a different address', function () {
|
||||
beforeEach(async function () {
|
||||
await this.token.approve(anotherApproved, tokenId, { from: owner });
|
||||
receipt = await this.token.approve(anotherApproved, tokenId, { from: owner });
|
||||
});
|
||||
|
||||
itApproves(anotherApproved);
|
||||
itEmitsApprovalEvent(anotherApproved);
|
||||
});
|
||||
});
|
||||
|
||||
context('when the address that receives the approval is the owner', function () {
|
||||
it('reverts', async function () {
|
||||
await expectRevert(this.token.approve(owner, tokenId, { from: owner }), 'ERC721: approval to current owner');
|
||||
});
|
||||
});
|
||||
|
||||
context('when the sender does not own the given token ID', function () {
|
||||
it('reverts', async function () {
|
||||
await expectRevert(
|
||||
this.token.approve(approved, tokenId, { from: other }),
|
||||
'ERC721: approve caller is not token owner or approved',
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
context('when the sender is approved for the given token ID', function () {
|
||||
it('reverts', async function () {
|
||||
await this.token.approve(approved, tokenId, { from: owner });
|
||||
await expectRevert(
|
||||
this.token.approve(anotherApproved, tokenId, { from: approved }),
|
||||
'ERC721: approve caller is not token owner or approved for all',
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
context('when the sender is an operator', function () {
|
||||
beforeEach(async function () {
|
||||
await this.token.setApprovalForAll(operator, true, { from: owner });
|
||||
receipt = await this.token.approve(approved, tokenId, { from: operator });
|
||||
});
|
||||
|
||||
itApproves(approved);
|
||||
itEmitsApprovalEvent(approved);
|
||||
});
|
||||
|
||||
context('when the given token ID does not exist', function () {
|
||||
it('reverts', async function () {
|
||||
await expectRevert(
|
||||
this.token.approve(approved, nonExistentTokenId, { from: operator }),
|
||||
'ERC721: invalid token ID',
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('setApprovalForAll', function () {
|
||||
context('when the operator willing to approve is not the owner', function () {
|
||||
context('when there is no operator approval set by the sender', function () {
|
||||
it('approves the operator', async function () {
|
||||
await this.token.setApprovalForAll(operator, true, { from: owner });
|
||||
|
||||
expect(await this.token.isApprovedForAll(owner, operator)).to.equal(true);
|
||||
});
|
||||
|
||||
it('emits an approval event', async function () {
|
||||
const receipt = await this.token.setApprovalForAll(operator, true, { from: owner });
|
||||
|
||||
expectEvent(receipt, 'ApprovalForAll', {
|
||||
owner: owner,
|
||||
operator: operator,
|
||||
approved: true,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
context('when the operator was set as not approved', function () {
|
||||
beforeEach(async function () {
|
||||
await this.token.setApprovalForAll(operator, false, { from: owner });
|
||||
});
|
||||
|
||||
it('approves the operator', async function () {
|
||||
await this.token.setApprovalForAll(operator, true, { from: owner });
|
||||
|
||||
expect(await this.token.isApprovedForAll(owner, operator)).to.equal(true);
|
||||
});
|
||||
|
||||
it('emits an approval event', async function () {
|
||||
const receipt = await this.token.setApprovalForAll(operator, true, { from: owner });
|
||||
|
||||
expectEvent(receipt, 'ApprovalForAll', {
|
||||
owner: owner,
|
||||
operator: operator,
|
||||
approved: true,
|
||||
});
|
||||
});
|
||||
|
||||
it('can unset the operator approval', async function () {
|
||||
await this.token.setApprovalForAll(operator, false, { from: owner });
|
||||
|
||||
expect(await this.token.isApprovedForAll(owner, operator)).to.equal(false);
|
||||
});
|
||||
});
|
||||
|
||||
context('when the operator was already approved', function () {
|
||||
beforeEach(async function () {
|
||||
await this.token.setApprovalForAll(operator, true, { from: owner });
|
||||
});
|
||||
|
||||
it('keeps the approval to the given address', async function () {
|
||||
await this.token.setApprovalForAll(operator, true, { from: owner });
|
||||
|
||||
expect(await this.token.isApprovedForAll(owner, operator)).to.equal(true);
|
||||
});
|
||||
|
||||
it('emits an approval event', async function () {
|
||||
const receipt = await this.token.setApprovalForAll(operator, true, { from: owner });
|
||||
|
||||
expectEvent(receipt, 'ApprovalForAll', {
|
||||
owner: owner,
|
||||
operator: operator,
|
||||
approved: true,
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
context('when the operator is the owner', function () {
|
||||
it('reverts', async function () {
|
||||
await expectRevert(this.token.setApprovalForAll(owner, true, { from: owner }), 'ERC721: approve to caller');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('getApproved', async function () {
|
||||
context('when token is not minted', async function () {
|
||||
it('reverts', async function () {
|
||||
await expectRevert(this.token.getApproved(nonExistentTokenId), 'ERC721: invalid token ID');
|
||||
});
|
||||
});
|
||||
|
||||
context('when token has been minted ', async function () {
|
||||
it('should return the zero address', async function () {
|
||||
expect(await this.token.getApproved(firstTokenId)).to.be.equal(ZERO_ADDRESS);
|
||||
});
|
||||
|
||||
context('when account has been approved', async function () {
|
||||
beforeEach(async function () {
|
||||
await this.token.approve(approved, firstTokenId, { from: owner });
|
||||
});
|
||||
|
||||
it('returns approved account', async function () {
|
||||
expect(await this.token.getApproved(firstTokenId)).to.be.equal(approved);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('_mint(address, uint256)', function () {
|
||||
it('reverts with a null destination address', async function () {
|
||||
await expectRevert(this.token.$_mint(ZERO_ADDRESS, firstTokenId), 'ERC721: mint to the zero address');
|
||||
});
|
||||
|
||||
context('with minted token', async function () {
|
||||
beforeEach(async function () {
|
||||
this.receipt = await this.token.$_mint(owner, firstTokenId);
|
||||
});
|
||||
|
||||
it('emits a Transfer event', function () {
|
||||
expectEvent(this.receipt, 'Transfer', { from: ZERO_ADDRESS, to: owner, tokenId: firstTokenId });
|
||||
});
|
||||
|
||||
it('creates the token', async function () {
|
||||
expect(await this.token.balanceOf(owner)).to.be.bignumber.equal('1');
|
||||
expect(await this.token.ownerOf(firstTokenId)).to.equal(owner);
|
||||
});
|
||||
|
||||
it('reverts when adding a token id that already exists', async function () {
|
||||
await expectRevert(this.token.$_mint(owner, firstTokenId), 'ERC721: token already minted');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('_burn', function () {
|
||||
it('reverts when burning a non-existent token id', async function () {
|
||||
await expectRevert(this.token.$_burn(nonExistentTokenId), 'ERC721: invalid token ID');
|
||||
});
|
||||
|
||||
context('with minted tokens', function () {
|
||||
beforeEach(async function () {
|
||||
await this.token.$_mint(owner, firstTokenId);
|
||||
await this.token.$_mint(owner, secondTokenId);
|
||||
});
|
||||
|
||||
context('with burnt token', function () {
|
||||
beforeEach(async function () {
|
||||
this.receipt = await this.token.$_burn(firstTokenId);
|
||||
});
|
||||
|
||||
it('emits a Transfer event', function () {
|
||||
expectEvent(this.receipt, 'Transfer', { from: owner, to: ZERO_ADDRESS, tokenId: firstTokenId });
|
||||
});
|
||||
|
||||
it('deletes the token', async function () {
|
||||
expect(await this.token.balanceOf(owner)).to.be.bignumber.equal('1');
|
||||
await expectRevert(this.token.ownerOf(firstTokenId), 'ERC721: invalid token ID');
|
||||
});
|
||||
|
||||
it('reverts when burning a token id that has been deleted', async function () {
|
||||
await expectRevert(this.token.$_burn(firstTokenId), 'ERC721: invalid token ID');
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function shouldBehaveLikeERC721Enumerable(errorPrefix, owner, newOwner, approved, anotherApproved, operator, other) {
|
||||
shouldSupportInterfaces(['ERC721Enumerable']);
|
||||
|
||||
context('with minted tokens', function () {
|
||||
beforeEach(async function () {
|
||||
await this.token.$_mint(owner, firstTokenId);
|
||||
await this.token.$_mint(owner, secondTokenId);
|
||||
this.toWhom = other; // default to other for toWhom in context-dependent tests
|
||||
});
|
||||
|
||||
describe('totalSupply', function () {
|
||||
it('returns total token supply', async function () {
|
||||
expect(await this.token.totalSupply()).to.be.bignumber.equal('2');
|
||||
});
|
||||
});
|
||||
|
||||
describe('tokenOfOwnerByIndex', function () {
|
||||
describe('when the given index is lower than the amount of tokens owned by the given address', function () {
|
||||
it('returns the token ID placed at the given index', async function () {
|
||||
expect(await this.token.tokenOfOwnerByIndex(owner, 0)).to.be.bignumber.equal(firstTokenId);
|
||||
});
|
||||
});
|
||||
|
||||
describe('when the index is greater than or equal to the total tokens owned by the given address', function () {
|
||||
it('reverts', async function () {
|
||||
await expectRevert(this.token.tokenOfOwnerByIndex(owner, 2), 'ERC721Enumerable: owner index out of bounds');
|
||||
});
|
||||
});
|
||||
|
||||
describe('when the given address does not own any token', function () {
|
||||
it('reverts', async function () {
|
||||
await expectRevert(this.token.tokenOfOwnerByIndex(other, 0), 'ERC721Enumerable: owner index out of bounds');
|
||||
});
|
||||
});
|
||||
|
||||
describe('after transferring all tokens to another user', function () {
|
||||
beforeEach(async function () {
|
||||
await this.token.transferFrom(owner, other, firstTokenId, { from: owner });
|
||||
await this.token.transferFrom(owner, other, secondTokenId, { from: owner });
|
||||
});
|
||||
|
||||
it('returns correct token IDs for target', async function () {
|
||||
expect(await this.token.balanceOf(other)).to.be.bignumber.equal('2');
|
||||
const tokensListed = await Promise.all([0, 1].map(i => this.token.tokenOfOwnerByIndex(other, i)));
|
||||
expect(tokensListed.map(t => t.toNumber())).to.have.members([
|
||||
firstTokenId.toNumber(),
|
||||
secondTokenId.toNumber(),
|
||||
]);
|
||||
});
|
||||
|
||||
it('returns empty collection for original owner', async function () {
|
||||
expect(await this.token.balanceOf(owner)).to.be.bignumber.equal('0');
|
||||
await expectRevert(this.token.tokenOfOwnerByIndex(owner, 0), 'ERC721Enumerable: owner index out of bounds');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('tokenByIndex', function () {
|
||||
it('returns all tokens', async function () {
|
||||
const tokensListed = await Promise.all([0, 1].map(i => this.token.tokenByIndex(i)));
|
||||
expect(tokensListed.map(t => t.toNumber())).to.have.members([
|
||||
firstTokenId.toNumber(),
|
||||
secondTokenId.toNumber(),
|
||||
]);
|
||||
});
|
||||
|
||||
it('reverts if index is greater than supply', async function () {
|
||||
await expectRevert(this.token.tokenByIndex(2), 'ERC721Enumerable: global index out of bounds');
|
||||
});
|
||||
|
||||
[firstTokenId, secondTokenId].forEach(function (tokenId) {
|
||||
it(`returns all tokens after burning token ${tokenId} and minting new tokens`, async function () {
|
||||
const newTokenId = new BN(300);
|
||||
const anotherNewTokenId = new BN(400);
|
||||
|
||||
await this.token.$_burn(tokenId);
|
||||
await this.token.$_mint(newOwner, newTokenId);
|
||||
await this.token.$_mint(newOwner, anotherNewTokenId);
|
||||
|
||||
expect(await this.token.totalSupply()).to.be.bignumber.equal('3');
|
||||
|
||||
const tokensListed = await Promise.all([0, 1, 2].map(i => this.token.tokenByIndex(i)));
|
||||
const expectedTokens = [firstTokenId, secondTokenId, newTokenId, anotherNewTokenId].filter(
|
||||
x => x !== tokenId,
|
||||
);
|
||||
expect(tokensListed.map(t => t.toNumber())).to.have.members(expectedTokens.map(t => t.toNumber()));
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('_mint(address, uint256)', function () {
|
||||
it('reverts with a null destination address', async function () {
|
||||
await expectRevert(this.token.$_mint(ZERO_ADDRESS, firstTokenId), 'ERC721: mint to the zero address');
|
||||
});
|
||||
|
||||
context('with minted token', async function () {
|
||||
beforeEach(async function () {
|
||||
this.receipt = await this.token.$_mint(owner, firstTokenId);
|
||||
});
|
||||
|
||||
it('adjusts owner tokens by index', async function () {
|
||||
expect(await this.token.tokenOfOwnerByIndex(owner, 0)).to.be.bignumber.equal(firstTokenId);
|
||||
});
|
||||
|
||||
it('adjusts all tokens list', async function () {
|
||||
expect(await this.token.tokenByIndex(0)).to.be.bignumber.equal(firstTokenId);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('_burn', function () {
|
||||
it('reverts when burning a non-existent token id', async function () {
|
||||
await expectRevert(this.token.$_burn(firstTokenId), 'ERC721: invalid token ID');
|
||||
});
|
||||
|
||||
context('with minted tokens', function () {
|
||||
beforeEach(async function () {
|
||||
await this.token.$_mint(owner, firstTokenId);
|
||||
await this.token.$_mint(owner, secondTokenId);
|
||||
});
|
||||
|
||||
context('with burnt token', function () {
|
||||
beforeEach(async function () {
|
||||
this.receipt = await this.token.$_burn(firstTokenId);
|
||||
});
|
||||
|
||||
it('removes that token from the token list of the owner', async function () {
|
||||
expect(await this.token.tokenOfOwnerByIndex(owner, 0)).to.be.bignumber.equal(secondTokenId);
|
||||
});
|
||||
|
||||
it('adjusts all tokens list', async function () {
|
||||
expect(await this.token.tokenByIndex(0)).to.be.bignumber.equal(secondTokenId);
|
||||
});
|
||||
|
||||
it('burns all tokens', async function () {
|
||||
await this.token.$_burn(secondTokenId, { from: owner });
|
||||
expect(await this.token.totalSupply()).to.be.bignumber.equal('0');
|
||||
await expectRevert(this.token.tokenByIndex(0), 'ERC721Enumerable: global index out of bounds');
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function shouldBehaveLikeERC721Metadata(errorPrefix, name, symbol, owner) {
|
||||
shouldSupportInterfaces(['ERC721Metadata']);
|
||||
|
||||
describe('metadata', function () {
|
||||
it('has a name', async function () {
|
||||
expect(await this.token.name()).to.be.equal(name);
|
||||
});
|
||||
|
||||
it('has a symbol', async function () {
|
||||
expect(await this.token.symbol()).to.be.equal(symbol);
|
||||
});
|
||||
|
||||
describe('token URI', function () {
|
||||
beforeEach(async function () {
|
||||
await this.token.$_mint(owner, firstTokenId);
|
||||
});
|
||||
|
||||
it('return empty string by default', async function () {
|
||||
expect(await this.token.tokenURI(firstTokenId)).to.be.equal('');
|
||||
});
|
||||
|
||||
it('reverts when queried for non existent token id', async function () {
|
||||
await expectRevert(this.token.tokenURI(nonExistentTokenId), 'ERC721: invalid token ID');
|
||||
});
|
||||
|
||||
describe('base URI', function () {
|
||||
beforeEach(function () {
|
||||
if (this.token.setBaseURI === undefined) {
|
||||
this.skip();
|
||||
}
|
||||
});
|
||||
|
||||
it('base URI can be set', async function () {
|
||||
await this.token.setBaseURI(baseURI);
|
||||
expect(await this.token.baseURI()).to.equal(baseURI);
|
||||
});
|
||||
|
||||
it('base URI is added as a prefix to the token URI', async function () {
|
||||
await this.token.setBaseURI(baseURI);
|
||||
expect(await this.token.tokenURI(firstTokenId)).to.be.equal(baseURI + firstTokenId.toString());
|
||||
});
|
||||
|
||||
it('token URI can be changed by changing the base URI', async function () {
|
||||
await this.token.setBaseURI(baseURI);
|
||||
const newBaseURI = 'https://api.example.com/v2/';
|
||||
await this.token.setBaseURI(newBaseURI);
|
||||
expect(await this.token.tokenURI(firstTokenId)).to.be.equal(newBaseURI + firstTokenId.toString());
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
shouldBehaveLikeERC721,
|
||||
shouldBehaveLikeERC721Enumerable,
|
||||
shouldBehaveLikeERC721Metadata,
|
||||
};
|
||||
@@ -0,0 +1,15 @@
|
||||
const { shouldBehaveLikeERC721, shouldBehaveLikeERC721Metadata } = require('./ERC721.behavior');
|
||||
|
||||
const ERC721 = artifacts.require('$ERC721');
|
||||
|
||||
contract('ERC721', function (accounts) {
|
||||
const name = 'Non Fungible Token';
|
||||
const symbol = 'NFT';
|
||||
|
||||
beforeEach(async function () {
|
||||
this.token = await ERC721.new(name, symbol);
|
||||
});
|
||||
|
||||
shouldBehaveLikeERC721('ERC721', ...accounts);
|
||||
shouldBehaveLikeERC721Metadata('ERC721', name, symbol, ...accounts);
|
||||
});
|
||||
@@ -0,0 +1,20 @@
|
||||
const {
|
||||
shouldBehaveLikeERC721,
|
||||
shouldBehaveLikeERC721Metadata,
|
||||
shouldBehaveLikeERC721Enumerable,
|
||||
} = require('./ERC721.behavior');
|
||||
|
||||
const ERC721Enumerable = artifacts.require('$ERC721Enumerable');
|
||||
|
||||
contract('ERC721Enumerable', function (accounts) {
|
||||
const name = 'Non Fungible Token';
|
||||
const symbol = 'NFT';
|
||||
|
||||
beforeEach(async function () {
|
||||
this.token = await ERC721Enumerable.new(name, symbol);
|
||||
});
|
||||
|
||||
shouldBehaveLikeERC721('ERC721', ...accounts);
|
||||
shouldBehaveLikeERC721Metadata('ERC721', name, symbol, ...accounts);
|
||||
shouldBehaveLikeERC721Enumerable('ERC721', ...accounts);
|
||||
});
|
||||
+70
@@ -0,0 +1,70 @@
|
||||
const { BN, constants, expectEvent, expectRevert } = require('@openzeppelin/test-helpers');
|
||||
|
||||
const { expect } = require('chai');
|
||||
|
||||
const ERC721Burnable = artifacts.require('$ERC721Burnable');
|
||||
|
||||
contract('ERC721Burnable', function (accounts) {
|
||||
const [owner, approved] = accounts;
|
||||
|
||||
const firstTokenId = new BN(1);
|
||||
const secondTokenId = new BN(2);
|
||||
const unknownTokenId = new BN(3);
|
||||
|
||||
const name = 'Non Fungible Token';
|
||||
const symbol = 'NFT';
|
||||
|
||||
beforeEach(async function () {
|
||||
this.token = await ERC721Burnable.new(name, symbol);
|
||||
});
|
||||
|
||||
describe('like a burnable ERC721', function () {
|
||||
beforeEach(async function () {
|
||||
await this.token.$_mint(owner, firstTokenId);
|
||||
await this.token.$_mint(owner, secondTokenId);
|
||||
});
|
||||
|
||||
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('burns the given token ID and adjusts the balance of the owner', async function () {
|
||||
await expectRevert(this.token.ownerOf(tokenId), 'ERC721: invalid token ID');
|
||||
expect(await this.token.balanceOf(owner)).to.be.bignumber.equal('1');
|
||||
});
|
||||
|
||||
it('emits a burn event', async function () {
|
||||
expectEvent(receipt, 'Transfer', {
|
||||
from: owner,
|
||||
to: constants.ZERO_ADDRESS,
|
||||
tokenId: tokenId,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
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 });
|
||||
});
|
||||
|
||||
context('getApproved', function () {
|
||||
it('reverts', async function () {
|
||||
await expectRevert(this.token.getApproved(tokenId), 'ERC721: invalid token ID');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('when the given token ID was not tracked by this contract', function () {
|
||||
it('reverts', async function () {
|
||||
await expectRevert(this.token.burn(unknownTokenId, { from: owner }), 'ERC721: invalid token ID');
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
+122
@@ -0,0 +1,122 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
pragma solidity ^0.8.0;
|
||||
|
||||
// solhint-disable func-name-mixedcase
|
||||
|
||||
import "../../../../contracts/token/ERC721/extensions/ERC721Consecutive.sol";
|
||||
import "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 {
|
||||
uint256 public totalMinted = 0;
|
||||
|
||||
constructor(address[] memory receivers, uint256[] memory batches) ERC721("", "") {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
contract ERC721ConsecutiveTest is Test {
|
||||
function test_balance(address receiver, uint256[] calldata batches) public {
|
||||
vm.assume(receiver != address(0));
|
||||
|
||||
ERC721ConsecutiveTarget token = new ERC721ConsecutiveTarget(toSingleton(receiver), batches);
|
||||
|
||||
assertEq(token.balanceOf(receiver), token.totalMinted());
|
||||
}
|
||||
|
||||
function test_ownership(address receiver, uint256[] calldata batches, uint256[2] calldata unboundedTokenId) public {
|
||||
vm.assume(receiver != address(0));
|
||||
|
||||
ERC721ConsecutiveTarget token = new ERC721ConsecutiveTarget(toSingleton(receiver), batches);
|
||||
|
||||
if (token.totalMinted() > 0) {
|
||||
uint256 validTokenId = bound(unboundedTokenId[0], 0, token.totalMinted() - 1);
|
||||
assertEq(token.ownerOf(validTokenId), receiver);
|
||||
}
|
||||
|
||||
uint256 invalidTokenId = bound(unboundedTokenId[1], token.totalMinted(), type(uint256).max);
|
||||
vm.expectRevert();
|
||||
token.ownerOf(invalidTokenId);
|
||||
}
|
||||
|
||||
function test_burn(address receiver, uint256[] calldata batches, uint256 unboundedTokenId) public {
|
||||
vm.assume(receiver != address(0));
|
||||
|
||||
ERC721ConsecutiveTarget token = new ERC721ConsecutiveTarget(toSingleton(receiver), batches);
|
||||
|
||||
// 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, 0, 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
|
||||
) public {
|
||||
vm.assume(accounts[0] != address(0));
|
||||
vm.assume(accounts[1] != address(0));
|
||||
vm.assume(accounts[0] != accounts[1]);
|
||||
|
||||
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], 1, 5000);
|
||||
batches[1] = bound(unboundedBatches[1], 1, 5000);
|
||||
|
||||
ERC721ConsecutiveTarget token = new ERC721ConsecutiveTarget(receivers, batches);
|
||||
|
||||
uint256 tokenId0 = bound(unboundedTokenId[0], 0, batches[0] - 1);
|
||||
uint256 tokenId1 = bound(unboundedTokenId[1], 0, batches[1] - 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]);
|
||||
}
|
||||
}
|
||||
+206
@@ -0,0 +1,206 @@
|
||||
const { constants, expectEvent, expectRevert } = require('@openzeppelin/test-helpers');
|
||||
const { expect } = require('chai');
|
||||
|
||||
const ERC721ConsecutiveMock = artifacts.require('$ERC721ConsecutiveMock');
|
||||
const ERC721ConsecutiveEnumerableMock = artifacts.require('$ERC721ConsecutiveEnumerableMock');
|
||||
const ERC721ConsecutiveNoConstructorMintMock = artifacts.require('$ERC721ConsecutiveNoConstructorMintMock');
|
||||
|
||||
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];
|
||||
|
||||
describe('with valid batches', function () {
|
||||
beforeEach(async function () {
|
||||
this.token = await ERC721ConsecutiveMock.new(
|
||||
name,
|
||||
symbol,
|
||||
delegates,
|
||||
batches.map(({ receiver }) => receiver),
|
||||
batches.map(({ amount }) => amount),
|
||||
);
|
||||
});
|
||||
|
||||
describe('minting during construction', function () {
|
||||
it('events are emitted at construction', async function () {
|
||||
let first = 0;
|
||||
|
||||
for (const batch of 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,
|
||||
});
|
||||
} else {
|
||||
// expectEvent.notEmitted.inConstruction only looks at event name, and doesn't check the parameters
|
||||
}
|
||||
first += batch.amount;
|
||||
}
|
||||
});
|
||||
|
||||
it('ownership is set', async function () {
|
||||
const owners = batches.flatMap(({ receiver, amount }) => Array(amount).fill(receiver));
|
||||
|
||||
for (const tokenId in owners) {
|
||||
expect(await this.token.ownerOf(tokenId)).to.be.equal(owners[tokenId]);
|
||||
}
|
||||
});
|
||||
|
||||
it('balance & voting power are set', async function () {
|
||||
for (const account of accounts) {
|
||||
const balance = batches
|
||||
.filter(({ receiver }) => receiver === account)
|
||||
.map(({ amount }) => amount)
|
||||
.reduce((a, b) => a + b, 0);
|
||||
|
||||
expect(await this.token.balanceOf(account)).to.be.bignumber.equal(web3.utils.toBN(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));
|
||||
|
||||
await this.token.delegate(account, { from: account });
|
||||
}
|
||||
|
||||
// At this point all accounts should have delegated
|
||||
expect(await this.token.getVotes(account)).to.be.bignumber.equal(web3.utils.toBN(balance));
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('minting after construction', function () {
|
||||
it('consecutive minting is not possible after construction', async function () {
|
||||
await expectRevert(
|
||||
this.token.$_mintConsecutive(user1, 10),
|
||||
'ERC721Consecutive: batch minting restricted to constructor',
|
||||
);
|
||||
});
|
||||
|
||||
it('simple minting is possible after construction', async function () {
|
||||
const tokenId = batches.reduce((acc, { amount }) => acc + amount, 0);
|
||||
|
||||
expect(await this.token.$_exists(tokenId)).to.be.equal(false);
|
||||
|
||||
expectEvent(await this.token.$_mint(user1, tokenId), 'Transfer', {
|
||||
from: constants.ZERO_ADDRESS,
|
||||
to: user1,
|
||||
tokenId: tokenId.toString(),
|
||||
});
|
||||
});
|
||||
|
||||
it('cannot mint a token that has been batched minted', async function () {
|
||||
const tokenId = batches.reduce((acc, { amount }) => acc + amount, 0) - 1;
|
||||
|
||||
expect(await this.token.$_exists(tokenId)).to.be.equal(true);
|
||||
|
||||
await expectRevert(this.token.$_mint(user1, tokenId), 'ERC721: token already minted');
|
||||
});
|
||||
});
|
||||
|
||||
describe('ERC721 behavior', function () {
|
||||
it('core takes over ownership on transfer', async function () {
|
||||
await this.token.transferFrom(user1, receiver, 1, { from: user1 });
|
||||
|
||||
expect(await this.token.ownerOf(1)).to.be.equal(receiver);
|
||||
});
|
||||
|
||||
it('tokens can be burned and re-minted #1', async function () {
|
||||
expectEvent(await this.token.$_burn(1, { from: user1 }), 'Transfer', {
|
||||
from: user1,
|
||||
to: constants.ZERO_ADDRESS,
|
||||
tokenId: '1',
|
||||
});
|
||||
|
||||
await expectRevert(this.token.ownerOf(1), 'ERC721: invalid token ID');
|
||||
|
||||
expectEvent(await this.token.$_mint(user2, 1), 'Transfer', {
|
||||
from: constants.ZERO_ADDRESS,
|
||||
to: user2,
|
||||
tokenId: '1',
|
||||
});
|
||||
|
||||
expect(await this.token.ownerOf(1)).to.be.equal(user2);
|
||||
});
|
||||
|
||||
it('tokens can be burned and re-minted #2', async function () {
|
||||
const tokenId = batches.reduce((acc, { amount }) => acc.addn(amount), web3.utils.toBN(0));
|
||||
|
||||
expect(await this.token.$_exists(tokenId)).to.be.equal(false);
|
||||
await expectRevert(this.token.ownerOf(tokenId), 'ERC721: invalid token ID');
|
||||
|
||||
// mint
|
||||
await this.token.$_mint(user1, tokenId);
|
||||
|
||||
expect(await this.token.$_exists(tokenId)).to.be.equal(true);
|
||||
expect(await this.token.ownerOf(tokenId), user1);
|
||||
|
||||
// burn
|
||||
expectEvent(await this.token.$_burn(tokenId, { from: user1 }), 'Transfer', {
|
||||
from: user1,
|
||||
to: constants.ZERO_ADDRESS,
|
||||
tokenId,
|
||||
});
|
||||
|
||||
expect(await this.token.$_exists(tokenId)).to.be.equal(false);
|
||||
await expectRevert(this.token.ownerOf(tokenId), 'ERC721: invalid token ID');
|
||||
|
||||
// re-mint
|
||||
expectEvent(await this.token.$_mint(user2, tokenId), 'Transfer', {
|
||||
from: constants.ZERO_ADDRESS,
|
||||
to: user2,
|
||||
tokenId,
|
||||
});
|
||||
|
||||
expect(await this.token.$_exists(tokenId)).to.be.equal(true);
|
||||
expect(await this.token.ownerOf(tokenId), user2);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('invalid use', function () {
|
||||
it('cannot mint a batch larger than 5000', async function () {
|
||||
await expectRevert(
|
||||
ERC721ConsecutiveMock.new(name, symbol, [], [user1], ['5001']),
|
||||
'ERC721Consecutive: batch too large',
|
||||
);
|
||||
});
|
||||
|
||||
it('cannot use single minting during construction', async function () {
|
||||
await expectRevert(
|
||||
ERC721ConsecutiveNoConstructorMintMock.new(name, symbol),
|
||||
"ERC721Consecutive: can't mint during construction",
|
||||
);
|
||||
});
|
||||
|
||||
it('cannot use single minting during construction', async function () {
|
||||
await expectRevert(
|
||||
ERC721ConsecutiveNoConstructorMintMock.new(name, symbol),
|
||||
"ERC721Consecutive: can't mint during construction",
|
||||
);
|
||||
});
|
||||
|
||||
it('consecutive mint not compatible with enumerability', async function () {
|
||||
await expectRevert(
|
||||
ERC721ConsecutiveEnumerableMock.new(
|
||||
name,
|
||||
symbol,
|
||||
batches.map(({ receiver }) => receiver),
|
||||
batches.map(({ amount }) => amount),
|
||||
),
|
||||
'ERC721Enumerable: consecutive transfers not supported',
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
+92
@@ -0,0 +1,92 @@
|
||||
const { BN, constants, expectRevert } = require('@openzeppelin/test-helpers');
|
||||
|
||||
const { expect } = require('chai');
|
||||
|
||||
const ERC721Pausable = artifacts.require('$ERC721Pausable');
|
||||
|
||||
contract('ERC721Pausable', function (accounts) {
|
||||
const [owner, receiver, operator] = accounts;
|
||||
|
||||
const name = 'Non Fungible Token';
|
||||
const symbol = 'NFT';
|
||||
|
||||
beforeEach(async function () {
|
||||
this.token = await ERC721Pausable.new(name, symbol);
|
||||
});
|
||||
|
||||
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.$_pause();
|
||||
});
|
||||
|
||||
it('reverts when trying to transferFrom', async function () {
|
||||
await expectRevert(
|
||||
this.token.transferFrom(owner, receiver, firstTokenId, { from: owner }),
|
||||
'ERC721Pausable: token transfer while paused',
|
||||
);
|
||||
});
|
||||
|
||||
it('reverts when trying to safeTransferFrom', async function () {
|
||||
await expectRevert(
|
||||
this.token.safeTransferFrom(owner, receiver, firstTokenId, { from: owner }),
|
||||
'ERC721Pausable: token transfer while paused',
|
||||
);
|
||||
});
|
||||
|
||||
it('reverts when trying to safeTransferFrom with data', async function () {
|
||||
await expectRevert(
|
||||
this.token.methods['safeTransferFrom(address,address,uint256,bytes)'](owner, receiver, firstTokenId, mockData, {
|
||||
from: owner,
|
||||
}),
|
||||
'ERC721Pausable: token transfer while paused',
|
||||
);
|
||||
});
|
||||
|
||||
it('reverts when trying to mint', async function () {
|
||||
await expectRevert(this.token.$_mint(receiver, secondTokenId), 'ERC721Pausable: token transfer while paused');
|
||||
});
|
||||
|
||||
it('reverts when trying to burn', async function () {
|
||||
await expectRevert(this.token.$_burn(firstTokenId), 'ERC721Pausable: token transfer while paused');
|
||||
});
|
||||
|
||||
describe('getApproved', function () {
|
||||
it('returns approved address', async function () {
|
||||
const approvedAccount = await this.token.getApproved(firstTokenId);
|
||||
expect(approvedAccount).to.equal(constants.ZERO_ADDRESS);
|
||||
});
|
||||
});
|
||||
|
||||
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');
|
||||
});
|
||||
});
|
||||
|
||||
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);
|
||||
});
|
||||
});
|
||||
|
||||
describe('exists', function () {
|
||||
it('returns token existence', async function () {
|
||||
expect(await this.token.$_exists(firstTokenId)).to.equal(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('isApprovedForAll', function () {
|
||||
it('returns the approval of the operator', async function () {
|
||||
expect(await this.token.isApprovedForAll(owner, operator)).to.equal(false);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,41 @@
|
||||
const { BN, constants } = require('@openzeppelin/test-helpers');
|
||||
|
||||
const { shouldBehaveLikeERC2981 } = require('../../common/ERC2981.behavior');
|
||||
|
||||
const ERC721Royalty = artifacts.require('$ERC721Royalty');
|
||||
|
||||
contract('ERC721Royalty', function (accounts) {
|
||||
const [account1, account2] = accounts;
|
||||
const tokenId1 = new BN('1');
|
||||
const tokenId2 = new BN('2');
|
||||
const royalty = new BN('200');
|
||||
const salePrice = new BN('1000');
|
||||
|
||||
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;
|
||||
});
|
||||
|
||||
describe('token specific functions', function () {
|
||||
beforeEach(async function () {
|
||||
await this.token.$_setTokenRoyalty(tokenId1, account1, royalty);
|
||||
});
|
||||
|
||||
it('removes royalty information after burn', async function () {
|
||||
await this.token.$_burn(tokenId1);
|
||||
const tokenInfo = await this.token.royaltyInfo(tokenId1, salePrice);
|
||||
|
||||
expect(tokenInfo[0]).to.be.equal(constants.ZERO_ADDRESS);
|
||||
expect(tokenInfo[1]).to.be.bignumber.equal(new BN('0'));
|
||||
});
|
||||
});
|
||||
|
||||
shouldBehaveLikeERC2981();
|
||||
});
|
||||
+100
@@ -0,0 +1,100 @@
|
||||
const { BN, expectEvent, expectRevert } = require('@openzeppelin/test-helpers');
|
||||
const { expect } = require('chai');
|
||||
|
||||
const { shouldSupportInterfaces } = require('../../../utils/introspection/SupportsInterface.behavior');
|
||||
|
||||
const ERC721URIStorageMock = artifacts.require('$ERC721URIStorageMock');
|
||||
|
||||
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');
|
||||
|
||||
beforeEach(async function () {
|
||||
this.token = await ERC721URIStorageMock.new(name, symbol);
|
||||
});
|
||||
|
||||
shouldSupportInterfaces(['0x49064906']);
|
||||
|
||||
describe('token URI', function () {
|
||||
beforeEach(async function () {
|
||||
await this.token.$_mint(owner, firstTokenId);
|
||||
});
|
||||
|
||||
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('');
|
||||
});
|
||||
|
||||
it('reverts when queried for non existent token id', async function () {
|
||||
await expectRevert(this.token.tokenURI(nonExistentTokenId), 'ERC721: invalid token ID');
|
||||
});
|
||||
|
||||
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);
|
||||
});
|
||||
|
||||
it('setting the uri emits an event', async function () {
|
||||
expectEvent(await this.token.$_setTokenURI(firstTokenId, sampleUri), 'MetadataUpdate', {
|
||||
_tokenId: firstTokenId,
|
||||
});
|
||||
});
|
||||
|
||||
it('reverts when setting for non existent token id', async function () {
|
||||
await expectRevert(
|
||||
this.token.$_setTokenURI(nonExistentTokenId, sampleUri),
|
||||
'ERC721URIStorage: URI set of nonexistent token',
|
||||
);
|
||||
});
|
||||
|
||||
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(firstTokenId, sampleUri);
|
||||
|
||||
expect(await this.token.tokenURI(firstTokenId)).to.be.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);
|
||||
|
||||
const newBaseURI = 'https://api.example.com/v2/';
|
||||
await this.token.setBaseURI(newBaseURI);
|
||||
expect(await this.token.tokenURI(firstTokenId)).to.be.equal(newBaseURI + 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);
|
||||
});
|
||||
|
||||
it('tokens without URI can be burnt ', async function () {
|
||||
await this.token.$_burn(firstTokenId, { from: owner });
|
||||
|
||||
expect(await this.token.$_exists(firstTokenId)).to.equal(false);
|
||||
await expectRevert(this.token.tokenURI(firstTokenId), 'ERC721: invalid token ID');
|
||||
});
|
||||
|
||||
it('tokens with URI can be burnt ', async function () {
|
||||
await this.token.$_setTokenURI(firstTokenId, sampleUri);
|
||||
|
||||
await this.token.$_burn(firstTokenId, { from: owner });
|
||||
|
||||
expect(await this.token.$_exists(firstTokenId)).to.equal(false);
|
||||
await expectRevert(this.token.tokenURI(firstTokenId), 'ERC721: invalid token ID');
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,184 @@
|
||||
/* eslint-disable */
|
||||
|
||||
const { BN, expectEvent, time } = require('@openzeppelin/test-helpers');
|
||||
const { expect } = require('chai');
|
||||
|
||||
const { getChainId } = require('../../../helpers/chainid');
|
||||
|
||||
const { shouldBehaveLikeVotes } = require('../../../governance/utils/Votes.behavior');
|
||||
|
||||
const ERC721Votes = artifacts.require('$ERC721Votes');
|
||||
|
||||
contract('ERC721Votes', function (accounts) {
|
||||
const [account1, account2, account1Delegatee, other1, other2] = accounts;
|
||||
|
||||
const name = 'My Vote';
|
||||
const symbol = 'MTKN';
|
||||
|
||||
beforeEach(async function () {
|
||||
this.chainId = await getChainId();
|
||||
|
||||
this.votes = await ERC721Votes.new(name, symbol, name, '1');
|
||||
|
||||
this.NFT0 = new BN('10000000000000000000000000');
|
||||
this.NFT1 = new BN('10');
|
||||
this.NFT2 = new BN('20');
|
||||
this.NFT3 = new BN('30');
|
||||
});
|
||||
|
||||
describe('balanceOf', function () {
|
||||
beforeEach(async function () {
|
||||
await this.votes.$_mint(account1, this.NFT0);
|
||||
await this.votes.$_mint(account1, this.NFT1);
|
||||
await this.votes.$_mint(account1, this.NFT2);
|
||||
await this.votes.$_mint(account1, this.NFT3);
|
||||
});
|
||||
|
||||
it('grants to initial account', async function () {
|
||||
expect(await this.votes.balanceOf(account1)).to.be.bignumber.equal('4');
|
||||
});
|
||||
});
|
||||
|
||||
describe('transfers', function () {
|
||||
beforeEach(async function () {
|
||||
await this.votes.$_mint(account1, this.NFT0);
|
||||
});
|
||||
|
||||
it('no delegation', async function () {
|
||||
const { receipt } = await this.votes.transferFrom(account1, account2, this.NFT0, { from: account1 });
|
||||
expectEvent(receipt, 'Transfer', { from: account1, to: account2, tokenId: this.NFT0 });
|
||||
expectEvent.notEmitted(receipt, 'DelegateVotesChanged');
|
||||
|
||||
this.account1Votes = '0';
|
||||
this.account2Votes = '0';
|
||||
});
|
||||
|
||||
it('sender delegation', async function () {
|
||||
await this.votes.delegate(account1, { from: account1 });
|
||||
|
||||
const { receipt } = await this.votes.transferFrom(account1, account2, this.NFT0, { from: account1 });
|
||||
expectEvent(receipt, 'Transfer', { from: account1, to: account2, tokenId: this.NFT0 });
|
||||
expectEvent(receipt, 'DelegateVotesChanged', { delegate: account1, previousBalance: '1', newBalance: '0' });
|
||||
|
||||
const { logIndex: transferLogIndex } = receipt.logs.find(({ event }) => event == 'Transfer');
|
||||
expect(
|
||||
receipt.logs
|
||||
.filter(({ event }) => event == 'DelegateVotesChanged')
|
||||
.every(({ logIndex }) => transferLogIndex < logIndex),
|
||||
).to.be.equal(true);
|
||||
|
||||
this.account1Votes = '0';
|
||||
this.account2Votes = '0';
|
||||
});
|
||||
|
||||
it('receiver delegation', async function () {
|
||||
await this.votes.delegate(account2, { from: account2 });
|
||||
|
||||
const { receipt } = await this.votes.transferFrom(account1, account2, this.NFT0, { from: account1 });
|
||||
expectEvent(receipt, 'Transfer', { from: account1, to: account2, tokenId: this.NFT0 });
|
||||
expectEvent(receipt, 'DelegateVotesChanged', { delegate: account2, previousBalance: '0', newBalance: '1' });
|
||||
|
||||
const { logIndex: transferLogIndex } = receipt.logs.find(({ event }) => event == 'Transfer');
|
||||
expect(
|
||||
receipt.logs
|
||||
.filter(({ event }) => event == 'DelegateVotesChanged')
|
||||
.every(({ logIndex }) => transferLogIndex < logIndex),
|
||||
).to.be.equal(true);
|
||||
|
||||
this.account1Votes = '0';
|
||||
this.account2Votes = '1';
|
||||
});
|
||||
|
||||
it('full delegation', async function () {
|
||||
await this.votes.delegate(account1, { from: account1 });
|
||||
await this.votes.delegate(account2, { from: account2 });
|
||||
|
||||
const { receipt } = await this.votes.transferFrom(account1, account2, this.NFT0, { from: account1 });
|
||||
expectEvent(receipt, 'Transfer', { from: account1, to: account2, tokenId: this.NFT0 });
|
||||
expectEvent(receipt, 'DelegateVotesChanged', { delegate: account1, previousBalance: '1', newBalance: '0' });
|
||||
expectEvent(receipt, 'DelegateVotesChanged', { delegate: account2, previousBalance: '0', newBalance: '1' });
|
||||
|
||||
const { logIndex: transferLogIndex } = receipt.logs.find(({ event }) => event == 'Transfer');
|
||||
expect(
|
||||
receipt.logs
|
||||
.filter(({ event }) => event == 'DelegateVotesChanged')
|
||||
.every(({ logIndex }) => transferLogIndex < logIndex),
|
||||
).to.be.equal(true);
|
||||
|
||||
this.account1Votes = '0';
|
||||
this.account2Votes = '1';
|
||||
});
|
||||
|
||||
it('returns the same total supply on transfers', async function () {
|
||||
await this.votes.delegate(account1, { from: account1 });
|
||||
|
||||
const { receipt } = await this.votes.transferFrom(account1, account2, this.NFT0, { from: account1 });
|
||||
|
||||
await time.advanceBlock();
|
||||
await time.advanceBlock();
|
||||
|
||||
expect(await this.votes.getPastTotalSupply(receipt.blockNumber - 1)).to.be.bignumber.equal('1');
|
||||
expect(await this.votes.getPastTotalSupply(receipt.blockNumber + 1)).to.be.bignumber.equal('1');
|
||||
|
||||
this.account1Votes = '0';
|
||||
this.account2Votes = '0';
|
||||
});
|
||||
|
||||
it('generally returns the voting balance at the appropriate checkpoint', async function () {
|
||||
await this.votes.$_mint(account1, this.NFT1);
|
||||
await this.votes.$_mint(account1, this.NFT2);
|
||||
await this.votes.$_mint(account1, this.NFT3);
|
||||
|
||||
const total = await this.votes.balanceOf(account1);
|
||||
|
||||
const t1 = await this.votes.delegate(other1, { from: account1 });
|
||||
await time.advanceBlock();
|
||||
await time.advanceBlock();
|
||||
const t2 = await this.votes.transferFrom(account1, other2, this.NFT0, { from: account1 });
|
||||
await time.advanceBlock();
|
||||
await time.advanceBlock();
|
||||
const t3 = await this.votes.transferFrom(account1, other2, this.NFT2, { from: account1 });
|
||||
await time.advanceBlock();
|
||||
await time.advanceBlock();
|
||||
const t4 = await this.votes.transferFrom(other2, account1, this.NFT2, { from: other2 });
|
||||
await time.advanceBlock();
|
||||
await time.advanceBlock();
|
||||
|
||||
expect(await this.votes.getPastVotes(other1, t1.receipt.blockNumber - 1)).to.be.bignumber.equal('0');
|
||||
expect(await this.votes.getPastVotes(other1, t1.receipt.blockNumber)).to.be.bignumber.equal(total);
|
||||
expect(await this.votes.getPastVotes(other1, t1.receipt.blockNumber + 1)).to.be.bignumber.equal(total);
|
||||
expect(await this.votes.getPastVotes(other1, t2.receipt.blockNumber)).to.be.bignumber.equal('3');
|
||||
expect(await this.votes.getPastVotes(other1, t2.receipt.blockNumber + 1)).to.be.bignumber.equal('3');
|
||||
expect(await this.votes.getPastVotes(other1, t3.receipt.blockNumber)).to.be.bignumber.equal('2');
|
||||
expect(await this.votes.getPastVotes(other1, t3.receipt.blockNumber + 1)).to.be.bignumber.equal('2');
|
||||
expect(await this.votes.getPastVotes(other1, t4.receipt.blockNumber)).to.be.bignumber.equal('3');
|
||||
expect(await this.votes.getPastVotes(other1, t4.receipt.blockNumber + 1)).to.be.bignumber.equal('3');
|
||||
|
||||
this.account1Votes = '0';
|
||||
this.account2Votes = '0';
|
||||
});
|
||||
|
||||
afterEach(async function () {
|
||||
expect(await this.votes.getVotes(account1)).to.be.bignumber.equal(this.account1Votes);
|
||||
expect(await this.votes.getVotes(account2)).to.be.bignumber.equal(this.account2Votes);
|
||||
|
||||
// need to advance 2 blocks to see the effect of a transfer on "getPastVotes"
|
||||
const blockNumber = await time.latestBlock();
|
||||
await time.advanceBlock();
|
||||
expect(await this.votes.getPastVotes(account1, blockNumber)).to.be.bignumber.equal(this.account1Votes);
|
||||
expect(await this.votes.getPastVotes(account2, blockNumber)).to.be.bignumber.equal(this.account2Votes);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Voting workflow', function () {
|
||||
beforeEach(async function () {
|
||||
this.account1 = account1;
|
||||
this.account1Delegatee = account1Delegatee;
|
||||
this.account2 = account2;
|
||||
this.name = 'My Vote';
|
||||
});
|
||||
|
||||
// includes EIP6372 behavior check
|
||||
shouldBehaveLikeVotes();
|
||||
});
|
||||
});
|
||||
+283
@@ -0,0 +1,283 @@
|
||||
const { BN, expectEvent, constants, expectRevert } = require('@openzeppelin/test-helpers');
|
||||
const { expect } = require('chai');
|
||||
|
||||
const { shouldBehaveLikeERC721 } = require('../ERC721.behavior');
|
||||
|
||||
const ERC721 = artifacts.require('$ERC721');
|
||||
const ERC721Wrapper = artifacts.require('$ERC721Wrapper');
|
||||
|
||||
contract('ERC721Wrapper', function (accounts) {
|
||||
const [initialHolder, anotherAccount, approvedAccount] = accounts;
|
||||
|
||||
const name = 'My Token';
|
||||
const symbol = 'MTKN';
|
||||
const firstTokenId = new BN(1);
|
||||
const secondTokenId = new BN(2);
|
||||
|
||||
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);
|
||||
});
|
||||
|
||||
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.be.bignumber.equal(this.underlying.address);
|
||||
});
|
||||
|
||||
describe('depositFor', function () {
|
||||
it('works with token approval', async function () {
|
||||
await this.underlying.approve(this.token.address, firstTokenId, { from: initialHolder });
|
||||
|
||||
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,
|
||||
});
|
||||
});
|
||||
|
||||
it('works with approval for all', async function () {
|
||||
await this.underlying.setApprovalForAll(this.token.address, true, { from: initialHolder });
|
||||
|
||||
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,
|
||||
});
|
||||
});
|
||||
|
||||
it('works sending to another account', async function () {
|
||||
await this.underlying.approve(this.token.address, firstTokenId, { from: initialHolder });
|
||||
|
||||
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,
|
||||
});
|
||||
});
|
||||
|
||||
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 });
|
||||
|
||||
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,
|
||||
});
|
||||
});
|
||||
|
||||
it('reverts with missing approval', async function () {
|
||||
await expectRevert(
|
||||
this.token.depositFor(initialHolder, [firstTokenId], { from: initialHolder }),
|
||||
'ERC721: caller is not token owner or approved',
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('withdrawTo', function () {
|
||||
beforeEach(async function () {
|
||||
await this.underlying.approve(this.token.address, firstTokenId, { from: initialHolder });
|
||||
await this.token.depositFor(initialHolder, [firstTokenId], { from: initialHolder });
|
||||
});
|
||||
|
||||
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,
|
||||
});
|
||||
});
|
||||
|
||||
it('works for an approved', async function () {
|
||||
await this.token.approve(approvedAccount, firstTokenId, { from: initialHolder });
|
||||
|
||||
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,
|
||||
});
|
||||
});
|
||||
|
||||
it('works for an approved for all', async function () {
|
||||
await this.token.setApprovalForAll(approvedAccount, true, { from: initialHolder });
|
||||
|
||||
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,
|
||||
});
|
||||
});
|
||||
|
||||
it("doesn't work for a non-owner nor approved", async function () {
|
||||
await expectRevert(
|
||||
this.token.withdrawTo(initialHolder, [firstTokenId], { from: anotherAccount }),
|
||||
'ERC721Wrapper: caller is not token owner or approved',
|
||||
);
|
||||
});
|
||||
|
||||
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 });
|
||||
|
||||
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,
|
||||
});
|
||||
});
|
||||
|
||||
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,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('onERC721Received', function () {
|
||||
it('only allows calls from underlying', async function () {
|
||||
await expectRevert(
|
||||
this.token.onERC721Received(
|
||||
initialHolder,
|
||||
this.token.address,
|
||||
firstTokenId,
|
||||
anotherAccount, // Correct data
|
||||
{ from: anotherAccount },
|
||||
),
|
||||
'ERC721Wrapper: caller is not underlying',
|
||||
);
|
||||
});
|
||||
|
||||
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,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
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 });
|
||||
|
||||
const { tx } = await this.token.$_recover(anotherAccount, firstTokenId);
|
||||
|
||||
await expectEvent.inTransaction(tx, this.token, 'Transfer', {
|
||||
from: constants.ZERO_ADDRESS,
|
||||
to: anotherAccount,
|
||||
tokenId: firstTokenId,
|
||||
});
|
||||
});
|
||||
|
||||
it('reverts if there is nothing to recover', async function () {
|
||||
await expectRevert(
|
||||
this.token.$_recover(initialHolder, firstTokenId),
|
||||
'ERC721Wrapper: wrapper is not token owner',
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('ERC712 behavior', function () {
|
||||
shouldBehaveLikeERC721('ERC721', ...accounts);
|
||||
});
|
||||
});
|
||||
+122
@@ -0,0 +1,122 @@
|
||||
const { BN, constants, expectEvent, expectRevert } = require('@openzeppelin/test-helpers');
|
||||
const { ZERO_ADDRESS } = constants;
|
||||
const { shouldSupportInterfaces } = require('../../../utils/introspection/SupportsInterface.behavior');
|
||||
|
||||
const { expect } = require('chai');
|
||||
|
||||
const ERC721PresetMinterPauserAutoId = artifacts.require('ERC721PresetMinterPauserAutoId');
|
||||
|
||||
contract('ERC721PresetMinterPauserAutoId', function (accounts) {
|
||||
const [deployer, other] = accounts;
|
||||
|
||||
const name = 'MinterAutoIDToken';
|
||||
const symbol = 'MAIT';
|
||||
const baseURI = 'my.app/';
|
||||
|
||||
const DEFAULT_ADMIN_ROLE = '0x0000000000000000000000000000000000000000000000000000000000000000';
|
||||
const MINTER_ROLE = web3.utils.soliditySha3('MINTER_ROLE');
|
||||
|
||||
beforeEach(async function () {
|
||||
this.token = await ERC721PresetMinterPauserAutoId.new(name, symbol, baseURI, { from: deployer });
|
||||
});
|
||||
|
||||
shouldSupportInterfaces(['ERC721', 'ERC721Enumerable', 'AccessControl', 'AccessControlEnumerable']);
|
||||
|
||||
it('token has correct name', async function () {
|
||||
expect(await this.token.name()).to.equal(name);
|
||||
});
|
||||
|
||||
it('token has correct symbol', async function () {
|
||||
expect(await this.token.symbol()).to.equal(symbol);
|
||||
});
|
||||
|
||||
it('deployer has the default admin role', async function () {
|
||||
expect(await this.token.getRoleMemberCount(DEFAULT_ADMIN_ROLE)).to.be.bignumber.equal('1');
|
||||
expect(await this.token.getRoleMember(DEFAULT_ADMIN_ROLE, 0)).to.equal(deployer);
|
||||
});
|
||||
|
||||
it('deployer has the minter role', async function () {
|
||||
expect(await this.token.getRoleMemberCount(MINTER_ROLE)).to.be.bignumber.equal('1');
|
||||
expect(await this.token.getRoleMember(MINTER_ROLE, 0)).to.equal(deployer);
|
||||
});
|
||||
|
||||
it('minter role admin is the default admin', async function () {
|
||||
expect(await this.token.getRoleAdmin(MINTER_ROLE)).to.equal(DEFAULT_ADMIN_ROLE);
|
||||
});
|
||||
|
||||
describe('minting', function () {
|
||||
it('deployer can mint tokens', async function () {
|
||||
const tokenId = new BN('0');
|
||||
|
||||
const receipt = await this.token.mint(other, { from: deployer });
|
||||
expectEvent(receipt, 'Transfer', { from: ZERO_ADDRESS, to: other, tokenId });
|
||||
|
||||
expect(await this.token.balanceOf(other)).to.be.bignumber.equal('1');
|
||||
expect(await this.token.ownerOf(tokenId)).to.equal(other);
|
||||
|
||||
expect(await this.token.tokenURI(tokenId)).to.equal(baseURI + tokenId);
|
||||
});
|
||||
|
||||
it('other accounts cannot mint tokens', async function () {
|
||||
await expectRevert(
|
||||
this.token.mint(other, { from: other }),
|
||||
'ERC721PresetMinterPauserAutoId: must have minter role to mint',
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('pausing', function () {
|
||||
it('deployer can pause', async function () {
|
||||
const receipt = await this.token.pause({ from: deployer });
|
||||
expectEvent(receipt, 'Paused', { account: deployer });
|
||||
|
||||
expect(await this.token.paused()).to.equal(true);
|
||||
});
|
||||
|
||||
it('deployer can unpause', async function () {
|
||||
await this.token.pause({ from: deployer });
|
||||
|
||||
const receipt = await this.token.unpause({ from: deployer });
|
||||
expectEvent(receipt, 'Unpaused', { account: deployer });
|
||||
|
||||
expect(await this.token.paused()).to.equal(false);
|
||||
});
|
||||
|
||||
it('cannot mint while paused', async function () {
|
||||
await this.token.pause({ from: deployer });
|
||||
|
||||
await expectRevert(this.token.mint(other, { from: deployer }), 'ERC721Pausable: token transfer while paused');
|
||||
});
|
||||
|
||||
it('other accounts cannot pause', async function () {
|
||||
await expectRevert(
|
||||
this.token.pause({ from: other }),
|
||||
'ERC721PresetMinterPauserAutoId: must have pauser role to pause',
|
||||
);
|
||||
});
|
||||
|
||||
it('other accounts cannot unpause', async function () {
|
||||
await this.token.pause({ from: deployer });
|
||||
|
||||
await expectRevert(
|
||||
this.token.unpause({ from: other }),
|
||||
'ERC721PresetMinterPauserAutoId: must have pauser role to unpause',
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('burning', function () {
|
||||
it('holders can burn their tokens', async function () {
|
||||
const tokenId = new BN('0');
|
||||
|
||||
await this.token.mint(other, { from: deployer });
|
||||
|
||||
const receipt = await this.token.burn(tokenId, { from: other });
|
||||
|
||||
expectEvent(receipt, 'Transfer', { from: other, to: ZERO_ADDRESS, tokenId });
|
||||
|
||||
expect(await this.token.balanceOf(other)).to.be.bignumber.equal('0');
|
||||
expect(await this.token.totalSupply()).to.be.bignumber.equal('0');
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,22 @@
|
||||
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);
|
||||
|
||||
it('receives an ERC721 token', async function () {
|
||||
const token = await ERC721.new(name, symbol);
|
||||
await token.$_mint(owner, tokenId);
|
||||
|
||||
const receiver = await ERC721Holder.new();
|
||||
await token.safeTransferFrom(owner, receiver.address, tokenId, { from: owner });
|
||||
|
||||
expect(await token.ownerOf(tokenId)).to.be.equal(receiver.address);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user