Add more tests for ERC721

This commit is contained in:
Santiago Palladino
2018-03-08 19:19:15 -03:00
parent 71cbc511ec
commit 3745025a1e
8 changed files with 764 additions and 532 deletions

View File

@ -0,0 +1,21 @@
pragma solidity ^0.4.18;
import "../token/ERC721/ERC721Receiver.sol";
contract ERC721ReceiverMock is ERC721Receiver {
bytes4 retval;
bool reverts;
event Received(address _address, uint256 _tokenId, bytes _data, uint256 _gas);
function ERC721ReceiverMock(bytes4 _retval, bool _reverts) public {
retval = _retval;
reverts = _reverts;
}
function onERC721Received(address _address, uint256 _tokenId, bytes _data) public returns(bytes4) {
require(!reverts);
Received(_address, _tokenId, _data, msg.gas);
return retval;
}
}

View File

@ -20,13 +20,20 @@ contract ERC721TokenMock is ERC721Token, ERC721BasicTokenMock {
function tokenURI(uint256 _tokenId) public view returns (string) {
require(exists(_tokenId));
bytes memory uri = new bytes(78);
bytes memory uri = new bytes(78 + 7);
uint256 i;
uint256 value = _tokenId;
uri[0] = "m";
uri[1] = "o";
uri[2] = "c";
uri[3] = "k";
uri[4] = ":";
uri[5] = "/";
uri[6] = "/";
for (i = 0; i < 78; i++) {
uri[7 + 78 - i] = byte(value % 10 + 48);
uri[6 + 78 - i] = byte(value % 10 + 48);
value = value / 10;
}

View File

@ -177,9 +177,7 @@ contract ERC721BasicToken is ERC721Basic {
* @param _tokenId uint256 ID of the token being burned by the msg.sender
*/
function doBurn(uint256 _tokenId) onlyOwnerOf(_tokenId) internal {
if (getApproved(_tokenId) != 0) {
clearApproval(msg.sender, _tokenId);
}
clearApproval(msg.sender, _tokenId);
removeToken(msg.sender, _tokenId);
totalTokens = totalTokens.sub(1);
Transfer(msg.sender, 0x0, _tokenId);
@ -214,8 +212,10 @@ contract ERC721BasicToken is ERC721Basic {
*/
function clearApproval(address _owner, uint256 _tokenId) internal {
require(ownerOf(_tokenId) == _owner);
tokenApprovals[_tokenId] = 0;
Approval(_owner, 0, _tokenId);
if (tokenApprovals[_tokenId] != 0) {
tokenApprovals[_tokenId] = 0;
Approval(_owner, 0, _tokenId);
}
}
/**

View File

@ -0,0 +1,17 @@
const _ = require('lodash');
const ethjsABI = require('ethjs-abi');
export function findMethod (abi, name, args) {
for (var i = 0; i < abi.length; i++) {
const methodArgs = _.map(abi[i].inputs, 'type').join(',');
if ((abi[i].name === name) && (methodArgs === args)) {
return abi[i];
}
}
}
export default function sendTransaction (target, name, argsTypes, argsValues, opts) {
const abiMethod = findMethod(target.abi, name, argsTypes);
const encodedData = ethjsABI.encodeMethod(abiMethod, argsValues);
return target.sendTransaction(Object.assign({ data: encodedData }, opts));
}

View File

@ -0,0 +1,503 @@
import assertRevert from '../../helpers/assertRevert';
import decodeLogs from '../../helpers/decodeLogs';
import sendTransaction from '../../helpers/sendTransaction';
const ERC721Receiver = artifacts.require('ERC721ReceiverMock.sol');
const BigNumber = web3.BigNumber;
require('chai')
.use(require('chai-as-promised'))
.use(require('chai-bignumber')(BigNumber))
.should();
export default function shouldBehaveLikeERC721BasicToken (accounts) {
const firstTokenId = 1;
const secondTokenId = 2;
const unknownTokenId = 3;
const creator = accounts[0];
const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000';
const RECEIVER_MAGIC_VALUE = '0xf0b9e5ba';
describe('like a ERC721BasicToken', function () {
beforeEach(async function () {
await this.token.mint(creator, firstTokenId, { from: creator });
await this.token.mint(creator, secondTokenId, { from: creator });
});
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 () {
const balance = await this.token.balanceOf(creator);
balance.should.be.bignumber.equal(2);
});
});
describe('when the given address does not own any tokens', function () {
it('returns 0', async function () {
const balance = await this.token.balanceOf(accounts[1]);
balance.should.be.bignumber.equal(0);
});
});
});
describe('exists', function () {
describe('when the token exists', function () {
const tokenId = firstTokenId;
it('should return true', async function () {
const result = await this.token.exists(tokenId);
result.should.be.true;
});
});
describe('when the token does not exist', function () {
const tokenId = unknownTokenId;
it('should return false', async function () {
const result = await this.token.exists(tokenId);
result.should.be.false;
});
});
});
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 () {
const owner = await this.token.ownerOf(tokenId);
owner.should.be.equal(creator);
});
});
describe('when the given token ID was not tracked by this token', function () {
const tokenId = unknownTokenId;
it('reverts', async function () {
await assertRevert(this.token.ownerOf(tokenId));
});
});
});
describe('transfers', function () {
const owner = accounts[0];
const approved = accounts[2];
const operator = accounts[3];
const unauthorized = accounts[4];
const tokenId = firstTokenId;
const data = '0x42';
let logs = null;
beforeEach(async function () {
this.to = accounts[1];
await this.token.approve(approved, tokenId, { from: owner });
await this.token.setApprovalForAll(operator, true, { from: owner });
});
const transferWasSuccessful = function ({ owner, tokenId, approved }) {
it('transfers the ownership of the given token ID to the given address', async function () {
const newOwner = await this.token.ownerOf(tokenId);
newOwner.should.be.equal(this.to);
});
it('clears the approval for the token ID', async function () {
const approvedAccount = await this.token.getApproved(tokenId);
approvedAccount.should.be.equal(ZERO_ADDRESS);
});
if (approved) {
it('emits an approval and transfer events', async function () {
logs.length.should.be.equal(2);
logs[0].event.should.be.eq('Approval');
logs[0].args._owner.should.be.equal(owner);
logs[0].args._approved.should.be.equal(ZERO_ADDRESS);
logs[0].args._tokenId.should.be.bignumber.equal(tokenId);
logs[1].event.should.be.eq('Transfer');
logs[1].args._from.should.be.equal(owner);
logs[1].args._to.should.be.equal(this.to);
logs[1].args._tokenId.should.be.bignumber.equal(tokenId);
});
} else {
it('emits only a transfer event', async function () {
logs.length.should.be.equal(1);
logs[0].event.should.be.eq('Transfer');
logs[0].args._from.should.be.equal(owner);
logs[0].args._to.should.be.equal(this.to);
logs[0].args._tokenId.should.be.bignumber.equal(tokenId);
});
}
it('adjusts owners balances', async function () {
const newOwnerBalance = await this.token.balanceOf(this.to);
newOwnerBalance.should.be.bignumber.equal(1);
const previousOwnerBalance = await this.token.balanceOf(owner);
previousOwnerBalance.should.be.bignumber.equal(1);
});
it('adjusts owners tokens by index', async function () {
if (!this.token.tokensOfOwnerByIndex) return;
const newOwnerToken = await this.tokensOfOwnerByIndex(this.to, 0);
newOwnerToken.should.be.equal(tokenId);
const previousOwnerToken = await this.tokensOfOwnerByIndex(owner, 0);
previousOwnerToken.should.not.be.equal(tokenId);
});
};
const shouldTransferTokensByUsers = function (transferFunction) {
describe('when called by the owner', function () {
beforeEach(async function () {
({ logs } = await transferFunction.call(this, owner, this.to, tokenId, { from: owner }));
});
transferWasSuccessful({ owner, tokenId, approved });
});
describe('when called by the approved individual', function () {
beforeEach(async function () {
({ logs } = await transferFunction.call(this, owner, this.to, tokenId, { from: approved }));
});
transferWasSuccessful({ owner, tokenId, approved });
});
describe('when called by the operator', function () {
beforeEach(async function () {
({ logs } = await transferFunction.call(this, owner, this.to, tokenId, { from: operator }));
});
transferWasSuccessful({ owner, tokenId, approved });
});
describe('when called by the owner without an approved user', function () {
beforeEach(async function () {
await this.token.approve(ZERO_ADDRESS, tokenId, { from: owner });
({ logs } = await transferFunction.call(this, owner, this.to, tokenId, { from: operator }));
});
transferWasSuccessful({ owner, tokenId, approved: null });
});
describe('when the address of the previous owner is incorrect', function () {
it('reverts', async function () {
await assertRevert(transferFunction.call(this, unauthorized, this.to, tokenId, { from: owner }));
});
});
describe('when the sender is not authorized for the token id', function () {
it('reverts', async function () {
await assertRevert(transferFunction.call(this, owner, this.to, tokenId, { from: unauthorized }));
});
});
describe('when the given token ID does not exist', function () {
it('reverts', async function () {
await assertRevert(transferFunction.call(this, owner, this.to, unknownTokenId, { from: owner }));
});
});
describe('when the address to transfer the token to is the zero address', function () {
it('reverts', async function () {
await assertRevert(transferFunction.call(this, owner, ZERO_ADDRESS, tokenId, { from: owner }));
});
});
};
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 sendTransaction(
this.token,
'safeTransferFrom',
'address,address,uint256,bytes',
[from, to, tokenId, data],
opts
);
};
const safeTransferFromWithoutData = function (from, to, tokenId, opts) {
return this.token.safeTransferFrom(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 ERC721Receiver.new(RECEIVER_MAGIC_VALUE, false);
this.to = this.receiver.address;
});
shouldTransferTokensByUsers(transferFun);
it('should call onERC721Received', async function () {
const result = await transferFun.call(this, owner, this.to, tokenId, { from: owner });
result.receipt.logs.length.should.be.equal(3);
const [log] = decodeLogs([result.receipt.logs[1]], ERC721Receiver, this.receiver.address);
log.event.should.be.eq('Received');
log.args._address.should.be.equal(owner);
log.args._tokenId.toNumber().should.be.equal(tokenId);
log.args._data.should.be.equal(data);
log.args._gas.toNumber().should.be.lessThan(50000);
});
});
};
describe('with data', function () {
shouldTransferSafely(safeTransferFromWithData, data);
});
describe('without data', function () {
shouldTransferSafely(safeTransferFromWithoutData, '0x');
});
describe('to a receiver contract returning unexpected value', function () {
it('reverts', async function () {
const invalidReceiver = await ERC721Receiver.new('0x42', false);
await assertRevert(this.token.safeTransferFrom(owner, invalidReceiver.address, tokenId, { from: owner }));
});
});
describe('to a receiver contract that throws', function () {
it('reverts', async function () {
const invalidReceiver = await ERC721Receiver.new(RECEIVER_MAGIC_VALUE, true);
await assertRevert(this.token.safeTransferFrom(owner, invalidReceiver.address, tokenId, { from: owner }));
});
});
describe('to a contract that does not implement the required function', function () {
it('reverts', async function () {
const invalidReceiver = this.token;
await assertRevert(this.token.safeTransferFrom(owner, invalidReceiver.address, tokenId, { from: owner }));
});
});
});
});
describe('approve', function () {
const tokenId = firstTokenId;
const sender = creator;
const to = accounts[1];
let logs = null;
describe('when clearing approval', function () {
describe('when there was no prior approval', function () {
beforeEach(async function () {
({ logs } = await this.token.approve(ZERO_ADDRESS, tokenId, { from: sender }));
});
it('clears the approval for that token', async function () {
const approvedAccount = await this.token.getApproved(tokenId);
approvedAccount.should.be.equal(ZERO_ADDRESS);
});
it('does not emit an approval event', async function () {
logs.length.should.be.equal(0);
});
});
describe('when there was a prior approval', function () {
beforeEach(async function () {
await this.token.approve(to, tokenId, { from: sender });
({ logs } = await this.token.approve(ZERO_ADDRESS, tokenId, { from: sender }));
});
it('clears the approval for that token', async function () {
const approvedAccount = await this.token.getApproved(tokenId);
approvedAccount.should.be.equal(ZERO_ADDRESS);
});
it('emits an approval event', async function () {
logs.length.should.be.equal(1);
logs[0].event.should.be.eq('Approval');
logs[0].args._owner.should.be.equal(sender);
logs[0].args._approved.should.be.equal(ZERO_ADDRESS);
logs[0].args._tokenId.should.be.bignumber.equal(tokenId);
});
});
});
describe('when approving a non-zero address', function () {
describe('when there was no prior approval', function () {
beforeEach(async function () {
({ logs } = await this.token.approve(to, tokenId, { from: sender }));
});
it('sets the approval for that token', async function () {
const approvedAccount = await this.token.getApproved(tokenId);
approvedAccount.should.be.equal(to);
});
it('emits an approval event', async function () {
logs.length.should.be.equal(1);
logs[0].event.should.be.eq('Approval');
logs[0].args._owner.should.be.equal(sender);
logs[0].args._approved.should.be.equal(to);
logs[0].args._tokenId.should.be.bignumber.equal(tokenId);
});
});
describe('when there was a prior approval to the same address', function () {
beforeEach(async function () {
await this.token.approve(to, tokenId, { from: sender });
({ logs } = await this.token.approve(to, tokenId, { from: sender }));
});
it('keeps the approval for that token', async function () {
const approvedAccount = await this.token.getApproved(tokenId);
approvedAccount.should.be.equal(to);
});
it('emits an approval event', async function () {
logs.length.should.be.equal(1);
logs[0].event.should.be.eq('Approval');
logs[0].args._owner.should.be.equal(sender);
logs[0].args._approved.should.be.equal(to);
logs[0].args._tokenId.should.be.bignumber.equal(tokenId);
});
});
describe('when there was a prior approval to a different address', function () {
beforeEach(async function () {
await this.token.approve(accounts[2], tokenId, { from: sender });
({ logs } = await this.token.approve(to, tokenId, { from: sender }));
});
it('sets the approval for that token', async function () {
const approvedAccount = await this.token.getApproved(tokenId);
approvedAccount.should.be.equal(to);
});
it('emits an approval event', async function () {
logs.length.should.be.equal(1);
logs[0].event.should.be.eq('Approval');
logs[0].args._owner.should.be.equal(sender);
logs[0].args._approved.should.be.equal(to);
logs[0].args._tokenId.should.be.bignumber.equal(tokenId);
});
});
});
describe('when the address that receives the approval is the owner', function () {
it('reverts', async function () {
await assertRevert(this.token.approve(sender, tokenId, { from: sender }));
});
});
describe('when the sender does not own the given token ID', function () {
it('reverts', async function () {
await assertRevert(this.token.approve(to, tokenId, { from: accounts[2] }));
});
});
describe('when the sender is approved for the given token ID', function () {
it('reverts', async function () {
await this.token.approve(accounts[2], tokenId, { from: sender });
await assertRevert(this.token.approve(to, tokenId, { from: accounts[2] }));
});
});
describe('when the given token ID does not exist', function () {
it('reverts', async function () {
await assertRevert(this.token.approve(to, unknownTokenId, { from: sender }));
});
});
});
describe('setApprovalForAll', function () {
const sender = creator;
describe('when the operator willing to approve is not the owner', function () {
const operator = accounts[1];
describe('when there is no operator approval set by the sender', function () {
it('approves the operator', async function () {
await this.token.setApprovalForAll(operator, true, { from: sender });
const isApproved = await this.token.isApprovedForAll(sender, operator);
isApproved.should.be.true;
});
it('emits an approval event', async function () {
const { logs } = await this.token.setApprovalForAll(operator, true, { from: sender });
logs.length.should.be.equal(1);
logs[0].event.should.be.eq('ApprovalForAll');
logs[0].args._owner.should.be.equal(sender);
logs[0].args._operator.should.be.equal(operator);
logs[0].args._approved.should.be.true;
});
});
describe('when the operator was set as not approved', function () {
beforeEach(async function () {
await this.token.setApprovalForAll(operator, false, { from: sender });
});
it('approves the operator', async function () {
await this.token.setApprovalForAll(operator, true, { from: sender });
const isApproved = await this.token.isApprovedForAll(sender, operator);
isApproved.should.be.true;
});
it('emits an approval event', async function () {
const { logs } = await this.token.setApprovalForAll(operator, true, { from: sender });
logs.length.should.be.equal(1);
logs[0].event.should.be.eq('ApprovalForAll');
logs[0].args._owner.should.be.equal(sender);
logs[0].args._operator.should.be.equal(operator);
logs[0].args._approved.should.be.true;
});
it('can unset the operator approval', async function () {
await this.token.setApprovalForAll(operator, false, { from: sender });
const isApproved = await this.token.isApprovedForAll(sender, operator);
isApproved.should.be.false;
});
});
describe('when the operator was already approved', function () {
beforeEach(async function () {
await this.token.setApprovalForAll(operator, true, { from: sender });
});
it('keeps the approval to the given address', async function () {
await this.token.setApprovalForAll(operator, true, { from: sender });
const isApproved = await this.token.isApprovedForAll(sender, operator);
isApproved.should.be.true;
});
it('emits an approval event', async function () {
const { logs } = await this.token.setApprovalForAll(operator, true, { from: sender });
logs.length.should.be.equal(1);
logs[0].event.should.be.eq('ApprovalForAll');
logs[0].args._owner.should.be.equal(sender);
logs[0].args._operator.should.be.equal(operator);
logs[0].args._approved.should.be.true;
});
});
});
describe('when the operator is the owner', function () {
const operator = creator;
it('reverts', async function () {
await assertRevert(this.token.setApprovalForAll(operator, true, { from: sender }));
});
});
});
});
};

View File

@ -1,4 +1,6 @@
import assertRevert from '../../helpers/assertRevert';
import shouldBehaveLikeERC721BasicToken from './ERC721BasicToken.behaviour';
import shouldMintAndBurnERC721Token from './ERC721MintBurn.behaviour';
const BigNumber = web3.BigNumber;
const ERC721BasicToken = artifacts.require('ERC721BasicTokenMock.sol');
@ -7,492 +9,11 @@ require('chai')
.use(require('chai-bignumber')(BigNumber))
.should();
contract('ERC721BasicToken', accounts => {
let token = null;
const _firstTokenId = 1;
const _secondTokenId = 2;
const _unknownTokenId = 3;
const _creator = accounts[0];
const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000';
contract('ERC721BasicToken', function (accounts) {
beforeEach(async function () {
token = await ERC721BasicToken.new({ from: _creator });
await token.mint(_creator, _firstTokenId, { from: _creator });
await token.mint(_creator, _secondTokenId, { from: _creator });
this.token = await ERC721BasicToken.new({ from: accounts[0] });
});
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 () {
const balance = await token.balanceOf(_creator);
balance.should.be.bignumber.equal(2);
});
});
describe('when the given address does not own any tokens', function () {
it('returns 0', async function () {
const balance = await token.balanceOf(accounts[1]);
balance.should.be.bignumber.equal(0);
});
});
});
describe('exists', function () {
describe('when the token exists', function () {
const tokenId = _firstTokenId;
it('should return true', async function () {
const result = await token.exists(tokenId);
result.should.be.true;
});
});
describe('when the token does not exist', function () {
const tokenId = _unknownTokenId;
it('should return false', async function () {
const result = await token.exists(tokenId);
result.should.be.false;
});
});
});
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 () {
const owner = await token.ownerOf(tokenId);
owner.should.be.equal(_creator);
});
});
describe('when the given token ID was not tracked by this token', function () {
const tokenId = _unknownTokenId;
it('reverts', async function () {
await assertRevert(token.ownerOf(tokenId));
});
});
});
describe('mint', function () {
const to = accounts[1];
const tokenId = _unknownTokenId;
let logs = null;
describe('when successful', function () {
beforeEach(async function () {
const result = await token.mint(to, tokenId);
logs = result.logs;
});
it('assigns the token to the new owner', async function () {
const owner = await token.ownerOf(tokenId);
owner.should.be.equal(to);
});
it('increases the balance of its owner', async function () {
const balance = await token.balanceOf(to);
balance.should.be.bignumber.equal(1);
});
it.skip('adds that token to the token list of the owner', async function () {
await token.mint(to, tokenId);
const tokens = await token.tokensOf(to);
tokens.length.should.be.equal(1);
tokens[0].should.be.bignumber.equal(tokenId);
});
it('emits a transfer event', async function () {
logs.length.should.be.equal(1);
logs[0].event.should.be.eq('Transfer');
logs[0].args._from.should.be.equal(ZERO_ADDRESS);
logs[0].args._to.should.be.equal(to);
logs[0].args._tokenId.should.be.bignumber.equal(tokenId);
});
});
describe('when the given owner address is the zero address', function () {
it('reverts', async function () {
await assertRevert(token.mint(ZERO_ADDRESS, tokenId));
});
});
describe('when the given token ID was already tracked by this contract', function () {
it('reverts', async function () {
await assertRevert(token.mint(accounts[1], _firstTokenId));
});
});
});
describe('burn', function () {
const tokenId = _firstTokenId;
const sender = _creator;
let logs = null;
describe('when successful', function () {
beforeEach(async function () {
const result = await token.burn(tokenId, { from: sender });
logs = result.logs;
});
it('burns the given token ID and adjusts the balance of the owner', async function () {
await assertRevert(token.ownerOf(tokenId));
const balance = await token.balanceOf(sender);
balance.should.be.bignumber.equal(1);
});
it.skip('removes that token from the token list of the owner', async function () {
const tokens = await token.tokensOf(sender);
tokens.length.should.be.equal(1);
tokens[0].should.be.bignumber.equal(_secondTokenId);
});
it('emits a burn event', async function () {
logs.length.should.be.equal(1);
logs[0].event.should.be.eq('Transfer');
logs[0].args._from.should.be.equal(sender);
logs[0].args._to.should.be.equal(ZERO_ADDRESS);
logs[0].args._tokenId.should.be.bignumber.equal(tokenId);
});
});
describe('when there is a previous approval', function () {
beforeEach(async function () {
await token.approve(accounts[1], tokenId, { from: sender });
const result = await token.burn(tokenId, { from: sender });
logs = result.logs;
});
it('clears the approval', async function () {
const approvedAccount = await token.getApproved(tokenId);
approvedAccount.should.be.equal(ZERO_ADDRESS);
});
it('emits an approval event', async function () {
logs.length.should.be.equal(2);
logs[0].event.should.be.eq('Approval');
logs[0].args._owner.should.be.equal(sender);
logs[0].args._approved.should.be.equal(ZERO_ADDRESS);
logs[0].args._tokenId.should.be.bignumber.equal(tokenId);
logs[1].event.should.be.eq('Transfer');
});
});
describe('when the msg.sender does not own given token', function () {
it('reverts', async function () {
await assertRevert(token.burn(tokenId, { from: accounts[1] }));
});
});
describe('when the given token ID was not tracked by this contract', function () {
it('reverts', async function () {
await assertRevert(token.burn(_unknownTokenId, { from: _creator }));
});
});
});
describe('transferFrom', function () {
const sender = _creator;
const from = _creator;
const to = accounts[1];
const tokenId = _firstTokenId;
const approved = accounts[2];
let logs = null;
describe('when successful', function () {
beforeEach(async function () {
await token.approve(approved, tokenId, { from: sender });
const result = await token.transferFrom(from, to, tokenId, { from: sender });
logs = result.logs;
});
it('transfers the ownership of the given token ID to the given address', async function () {
const newOwner = await token.ownerOf(tokenId);
newOwner.should.be.equal(to);
});
it('clears the approval for the token ID', async function () {
const approvedAccount = await token.getApproved(tokenId);
approvedAccount.should.be.equal(ZERO_ADDRESS);
});
it('emits an approval and transfer events', async function () {
logs.length.should.be.equal(2);
logs[0].event.should.be.eq('Approval');
logs[0].args._owner.should.be.equal(sender);
logs[0].args._approved.should.be.equal(ZERO_ADDRESS);
logs[0].args._tokenId.should.be.bignumber.equal(tokenId);
logs[1].event.should.be.eq('Transfer');
logs[1].args._from.should.be.equal(sender);
logs[1].args._to.should.be.equal(to);
logs[1].args._tokenId.should.be.bignumber.equal(tokenId);
});
it('adjusts owners balances', async function () {
const newOwnerBalance = await token.balanceOf(to);
newOwnerBalance.should.be.bignumber.equal(1);
const previousOwnerBalance = await token.balanceOf(_creator);
previousOwnerBalance.should.be.bignumber.equal(1);
});
it.skip('adds the token to the tokens list of the new owner', async function () {
await token.transfer(to, tokenId, { from: sender });
const tokenIDs = await token.tokensOf(to);
tokenIDs.length.should.be.equal(1);
tokenIDs[0].should.be.bignumber.equal(tokenId);
});
});
describe('when the address of the previous owner is incorrect', function () {
it('reverts', async function () {
await assertRevert(token.transferFrom(accounts[3], to, tokenId, { from: sender }));
});
});
describe('when the msg.sender is not the owner of the given token ID', function () {
it('reverts', async function () {
await assertRevert(token.transferFrom(from, to, tokenId, { from: accounts[2] }));
});
});
describe('when the given token ID does not exist', function () {
it('reverts', async function () {
await assertRevert(token.transferFrom(from, to, _unknownTokenId, { from: sender }));
});
});
describe('when the address to transfer the token to is the zero address', function () {
it('reverts', async function () {
await assertRevert(token.transferFrom(from, to, _unknownTokenId, { from: sender }));
});
});
});
describe('approve', function () {
const tokenId = _firstTokenId;
const sender = _creator;
const to = accounts[1];
let logs = null;
describe('when clearing approval', function () {
describe('when there was no prior approval', function () {
beforeEach(async function () {
({ logs } = await token.approve(ZERO_ADDRESS, tokenId, { from: sender }));
});
it('clears the approval for that token', async function () {
const approvedAccount = await token.getApproved(tokenId);
approvedAccount.should.be.equal(ZERO_ADDRESS);
});
it('does not emit an approval event', async function () {
logs.length.should.be.equal(0);
});
});
describe('when there was a prior approval', function () {
beforeEach(async function () {
await token.approve(to, tokenId, { from: sender });
({ logs } = await token.approve(ZERO_ADDRESS, tokenId, { from: sender }));
});
it('clears the approval for that token', async function () {
const approvedAccount = await token.getApproved(tokenId);
approvedAccount.should.be.equal(ZERO_ADDRESS);
});
it('emits an approval event', async function () {
logs.length.should.be.equal(1);
logs[0].event.should.be.eq('Approval');
logs[0].args._owner.should.be.equal(sender);
logs[0].args._approved.should.be.equal(ZERO_ADDRESS);
logs[0].args._tokenId.should.be.bignumber.equal(tokenId);
});
});
});
describe('when approving a non-zero address', function () {
describe('when there was no prior approval', function () {
beforeEach(async function () {
({ logs } = await token.approve(to, tokenId, { from: sender }));
});
it('sets the approval for that token', async function () {
const approvedAccount = await token.getApproved(tokenId);
approvedAccount.should.be.equal(to);
});
it('emits an approval event', async function () {
logs.length.should.be.equal(1);
logs[0].event.should.be.eq('Approval');
logs[0].args._owner.should.be.equal(sender);
logs[0].args._approved.should.be.equal(to);
logs[0].args._tokenId.should.be.bignumber.equal(tokenId);
});
});
describe('when there was a prior approval to the same address', function () {
beforeEach(async function () {
await token.approve(to, tokenId, { from: sender });
({ logs } = await token.approve(to, tokenId, { from: sender }));
});
it('keeps the approval for that token', async function () {
const approvedAccount = await token.getApproved(tokenId);
approvedAccount.should.be.equal(to);
});
it('emits an approval event', async function () {
logs.length.should.be.equal(1);
logs[0].event.should.be.eq('Approval');
logs[0].args._owner.should.be.equal(sender);
logs[0].args._approved.should.be.equal(to);
logs[0].args._tokenId.should.be.bignumber.equal(tokenId);
});
});
describe('when there was a prior approval to a different address', function () {
beforeEach(async function () {
await token.approve(accounts[2], tokenId, { from: sender });
({ logs } = await token.approve(to, tokenId, { from: sender }));
});
it('sets the approval for that token', async function () {
const approvedAccount = await token.getApproved(tokenId);
approvedAccount.should.be.equal(to);
});
it('emits an approval event', async function () {
logs.length.should.be.equal(1);
logs[0].event.should.be.eq('Approval');
logs[0].args._owner.should.be.equal(sender);
logs[0].args._approved.should.be.equal(to);
logs[0].args._tokenId.should.be.bignumber.equal(tokenId);
});
});
});
describe('when the address that receives the approval is the owner', function () {
it('reverts', async function () {
await assertRevert(token.approve(sender, tokenId, { from: sender }));
});
});
describe('when the sender does not own the given token ID', function () {
it('reverts', async function () {
await assertRevert(token.approve(to, tokenId, { from: accounts[2] }));
});
});
describe('when the sender is approved for the given token ID', function () {
it('reverts', async function () {
await token.approve(accounts[2], tokenId, { from: sender });
await assertRevert(token.approve(to, tokenId, { from: accounts[2] }));
});
});
describe('when the given token ID does not exist', function () {
it('reverts', async function () {
await assertRevert(token.approve(to, _unknownTokenId, { from: sender }));
});
});
});
describe('setApprovalForAll', function () {
const sender = _creator;
describe('when the operator willing to approve is not the owner', function () {
const operator = accounts[1];
describe('when there is no operator approval set by the sender', function () {
it('approves the operator', async function () {
await token.setApprovalForAll(operator, true, { from: sender });
const isApproved = await token.isApprovedForAll(sender, operator);
isApproved.should.be.true;
});
it('emits an approval event', async function () {
const { logs } = await token.setApprovalForAll(operator, true, { from: sender });
logs.length.should.be.equal(1);
logs[0].event.should.be.eq('ApprovalForAll');
logs[0].args._owner.should.be.equal(sender);
logs[0].args._operator.should.be.equal(operator);
logs[0].args._approved.should.be.true;
});
});
describe('when the operator was set as not approved', function () {
beforeEach(async function () {
await token.setApprovalForAll(operator, false, { from: sender });
});
it('approves the operator', async function () {
await token.setApprovalForAll(operator, true, { from: sender });
const isApproved = await token.isApprovedForAll(sender, operator);
isApproved.should.be.true;
});
it('emits an approval event', async function () {
const { logs } = await token.setApprovalForAll(operator, true, { from: sender });
logs.length.should.be.equal(1);
logs[0].event.should.be.eq('ApprovalForAll');
logs[0].args._owner.should.be.equal(sender);
logs[0].args._operator.should.be.equal(operator);
logs[0].args._approved.should.be.true;
});
it('can unset the operator approval', async function () {
await token.setApprovalForAll(operator, false, { from: sender });
const isApproved = await token.isApprovedForAll(sender, operator);
isApproved.should.be.false;
});
});
describe('when the operator was already approved', function () {
beforeEach(async function () {
await token.setApprovalForAll(operator, true, { from: sender });
});
it('keeps the approval to the given address and does not emit an approval event', async function () {
await token.setApprovalForAll(operator, true, { from: sender });
const isApproved = await token.isApprovedForAll(sender, operator);
isApproved.should.be.true;
});
it('emits an approval event', async function () {
const { logs } = await token.setApprovalForAll(operator, true, { from: sender });
logs.length.should.be.equal(1);
logs[0].event.should.be.eq('ApprovalForAll');
logs[0].args._owner.should.be.equal(sender);
logs[0].args._operator.should.be.equal(operator);
logs[0].args._approved.should.be.true;
});
});
});
describe('when the operator is the owner', function () {
const operator = _creator;
it('reverts', async function () {
await assertRevert(token.setApprovalForAll(operator, true, { from: sender }));
});
});
});
shouldBehaveLikeERC721BasicToken(accounts);
shouldMintAndBurnERC721Token(accounts);
});

View File

@ -0,0 +1,142 @@
import assertRevert from '../../helpers/assertRevert';
const BigNumber = web3.BigNumber;
require('chai')
.use(require('chai-as-promised'))
.use(require('chai-bignumber')(BigNumber))
.should();
export default function shouldMintAndBurnERC721Token (accounts) {
const firstTokenId = 1;
const secondTokenId = 2;
const unknownTokenId = 3;
const creator = accounts[0];
const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000';
describe('like a mintable and burnable ERC721Token', function () {
beforeEach(async function () {
await this.token.mint(creator, firstTokenId, { from: creator });
await this.token.mint(creator, secondTokenId, { from: creator });
});
describe('mint', function () {
const to = accounts[1];
const tokenId = unknownTokenId;
let logs = null;
describe('when successful', function () {
beforeEach(async function () {
const result = await this.token.mint(to, tokenId);
logs = result.logs;
});
it('assigns the token to the new owner', async function () {
const owner = await this.token.ownerOf(tokenId);
owner.should.be.equal(to);
});
it('increases the balance of its owner', async function () {
const balance = await this.token.balanceOf(to);
balance.should.be.bignumber.equal(1);
});
it('adjusts owner tokens by index', async function () {
if (!this.token.tokensOfOwnerByIndex) return;
const token = await this.tokensOfOwnerByIndex(to, 0);
token.should.be.equal(tokenId);
});
it('emits a transfer event', async function () {
logs.length.should.be.equal(1);
logs[0].event.should.be.eq('Transfer');
logs[0].args._from.should.be.equal(ZERO_ADDRESS);
logs[0].args._to.should.be.equal(to);
logs[0].args._tokenId.should.be.bignumber.equal(tokenId);
});
});
describe('when the given owner address is the zero address', function () {
it('reverts', async function () {
await assertRevert(this.token.mint(ZERO_ADDRESS, tokenId));
});
});
describe('when the given token ID was already tracked by this contract', function () {
it('reverts', async function () {
await assertRevert(this.token.mint(accounts[1], firstTokenId));
});
});
});
describe('burn', function () {
const tokenId = firstTokenId;
const sender = creator;
let logs = null;
describe('when successful', function () {
beforeEach(async function () {
const result = await this.token.burn(tokenId, { from: sender });
logs = result.logs;
});
it('burns the given token ID and adjusts the balance of the owner', async function () {
await assertRevert(this.token.ownerOf(tokenId));
const balance = await this.token.balanceOf(sender);
balance.should.be.bignumber.equal(1);
});
it('removes that token from the token list of the owner', async function () {
if (!this.token.tokensOfOwnerByIndex) return;
const token = await this.tokensOfOwnerByIndex(sender, 0);
token.should.not.be.equal(tokenId);
});
it('emits a burn event', async function () {
logs.length.should.be.equal(1);
logs[0].event.should.be.eq('Transfer');
logs[0].args._from.should.be.equal(sender);
logs[0].args._to.should.be.equal(ZERO_ADDRESS);
logs[0].args._tokenId.should.be.bignumber.equal(tokenId);
});
});
describe('when there is a previous approval', function () {
beforeEach(async function () {
await this.token.approve(accounts[1], tokenId, { from: sender });
const result = await this.token.burn(tokenId, { from: sender });
logs = result.logs;
});
it('clears the approval', async function () {
const approvedAccount = await this.token.getApproved(tokenId);
approvedAccount.should.be.equal(ZERO_ADDRESS);
});
it('emits an approval event', async function () {
logs.length.should.be.equal(2);
logs[0].event.should.be.eq('Approval');
logs[0].args._owner.should.be.equal(sender);
logs[0].args._approved.should.be.equal(ZERO_ADDRESS);
logs[0].args._tokenId.should.be.bignumber.equal(tokenId);
logs[1].event.should.be.eq('Transfer');
});
});
describe('when the msg.sender does not own given token', function () {
it('reverts', async function () {
await assertRevert(this.token.burn(tokenId, { from: accounts[1] }));
});
});
describe('when the given token ID was not tracked by this contract', function () {
it('reverts', async function () {
await assertRevert(this.token.burn(unknownTokenId, { from: creator }));
});
});
});
});
};

View File

@ -1,4 +1,7 @@
import assertRevert from '../../helpers/assertRevert';
import shouldBehaveLikeERC721BasicToken from './ERC721BasicToken.behaviour';
import shouldMintAndBurnERC721Token from './ERC721MintBurn.behaviour';
const BigNumber = web3.BigNumber;
const ERC721Token = artifacts.require('ERC721TokenMock.sol');
@ -7,62 +10,80 @@ require('chai')
.use(require('chai-bignumber')(BigNumber))
.should();
contract('ERC721Token', accounts => {
let token = null;
const _name = 'Non Fungible Token';
const _symbol = 'NFT';
const _firstTokenId = 1;
const _secondTokenId = 2;
const _creator = accounts[0];
contract('ERC721Token', function (accounts) {
const name = 'Non Fungible Token';
const symbol = 'NFT';
const firstTokenId = 1;
const secondTokenId = 2;
const creator = accounts[0];
beforeEach(async function () {
token = await ERC721Token.new(_name, _symbol, { from: _creator });
await token.mint(_creator, _firstTokenId, { from: _creator });
await token.mint(_creator, _secondTokenId, { from: _creator });
this.token = await ERC721Token.new(name, symbol, { from: creator });
});
describe('name', function () {
it('has a name', async function () {
const name = await token.name();
name.should.be.equal(_name);
shouldBehaveLikeERC721BasicToken(accounts);
shouldMintAndBurnERC721Token(accounts);
describe('like a full ERC721', function () {
beforeEach(async function () {
await this.token.mint(creator, firstTokenId, { from: creator });
await this.token.mint(creator, secondTokenId, { from: creator });
});
});
describe('symbol', function () {
it('has a symbol', async function () {
const symbol = await token.symbol();
symbol.should.be.equal(_symbol);
describe('metadata', function () {
it('has a name', async function () {
const name = await this.token.name();
name.should.be.equal(name);
});
it('has a symbol', async function () {
const symbol = await this.token.symbol();
symbol.should.be.equal(symbol);
});
it('returns metadata for a token id', async function () {
const uri = await this.token.tokenURI(firstTokenId);
const expected = `mock://${firstTokenId.toString().padStart(78, 0)}`;
uri.should.be.equal(expected);
});
});
});
describe('tokenOfOwnerByIndex', function () {
describe('when the given address owns some tokens', function () {
const owner = _creator;
describe('totalSupply', function () {
it('returns total token supply', async function () {
const totalSupply = await this.token.totalSupply();
totalSupply.should.be.bignumber.equal(2);
});
});
describe('when the given index is lower than the amount of tokens owned by the given address', function () {
const index = 0;
describe('tokenOfOwnerByIndex', function () {
describe('when the given address owns some tokens', function () {
const owner = creator;
it('returns the token ID placed at the given index', async function () {
const tokenId = await token.tokenOfOwnerByIndex(owner, index);
tokenId.should.be.bignumber.equal(_firstTokenId);
describe('when the given index is lower than the amount of tokens owned by the given address', function () {
const index = 0;
it('returns the token ID placed at the given index', async function () {
const tokenId = await this.token.tokenOfOwnerByIndex(owner, index);
tokenId.should.be.bignumber.equal(firstTokenId);
});
});
describe('when the index is greater than or equal to the total tokens owned by the given address', function () {
const index = 2;
it('reverts', async function () {
await assertRevert(this.token.tokenOfOwnerByIndex(owner, index));
});
});
});
describe('when the index is greater than or equal to the total tokens owned by the given address', function () {
const index = 2;
describe('when the given address does not own any token', function () {
const owner = accounts[1];
it('reverts', async function () {
await assertRevert(token.tokenOfOwnerByIndex(owner, index));
await assertRevert(this.token.tokenOfOwnerByIndex(owner, 0));
});
});
});
describe('when the given address does not own any token', function () {
const owner = accounts[1];
it('reverts', async function () {
await assertRevert(token.tokenOfOwnerByIndex(owner, 0));
});
});
});
});