Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ERC721 pausable token #1154

Merged
merged 3 commits into from
Aug 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions contracts/mocks/ERC721PausableTokenMock.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
pragma solidity ^0.4.24;

import "../token/ERC721/ERC721PausableToken.sol";


/**
* @title ERC721PausableTokenMock
* This mock just provides a public mint, burn and exists functions for testing purposes
*/
contract ERC721PausableTokenMock is ERC721PausableToken {
function mint(address _to, uint256 _tokenId) public {
super._mint(_to, _tokenId);
}

function burn(uint256 _tokenId) public {
super._burn(ownerOf(_tokenId), _tokenId);
}

function exists(uint256 _tokenId) public view returns (bool) {
return super._exists(_tokenId);
}
}
42 changes: 42 additions & 0 deletions contracts/token/ERC721/ERC721PausableToken.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
pragma solidity ^0.4.24;

import "./ERC721BasicToken.sol";
import "../../lifecycle/Pausable.sol";


/**
* @title ERC721 Non-Fungible Pausable token
* @dev ERC721BasicToken modified with pausable transfers.
**/
contract ERC721PausableToken is ERC721BasicToken, Pausable {
function approve(
address _to,
uint256 _tokenId
)
public
whenNotPaused
{
super.approve(_to, _tokenId);
}

function setApprovalForAll(
address _to,
bool _approved
)
public
whenNotPaused
{
super.setApprovalForAll(_to, _approved);
}

function transferFrom(
address _from,
address _to,
uint256 _tokenId
)
public
whenNotPaused
{
super.transferFrom(_from, _to, _tokenId);
}
}
36 changes: 36 additions & 0 deletions test/token/ERC721/ERC721PausableToken.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const { shouldBehaveLikeERC721PausedToken } = require('./ERC721PausedToken.behavior');
const { shouldBehaveLikeERC721BasicToken } = require('./ERC721BasicToken.behavior');

const BigNumber = web3.BigNumber;
const ERC721PausableToken = artifacts.require('ERC721PausableTokenMock.sol');

require('chai')
.use(require('chai-bignumber')(BigNumber))
.should();

contract('ERC721PausableToken', function ([_, owner, recipient, operator, ...otherAccounts]) {
beforeEach(async function () {
this.token = await ERC721PausableToken.new({ from: owner });
});

context('when token is paused', function () {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks a bit weird, since the test does not pause, it is the behavior that calls the pause function. Shouldn't we have the beforeEach here pausing the token?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the behavior name be changed from shouldBehaveLikeERC721PausableToken to shouldBehaveLikeERC721PausedToken then?

Or maybe just leave 'when token is paused and then unpaused' context, and move out two first entries (as they don't have beforeEach'es).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what you mean by 'moving out the two first entries' (which?), but the behavior renaming sounds like a neat idea!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean just to remove two first context blocks:

shouldBehaveLikeERC721BasicToken(owner, [...otherAccounts]);
shouldBehaveLikeERC721PausableToken(owner, [...otherAccounts]);

context('when token is paused and then unpaused', function () {
   beforeEach(async function () {
      await this.token.pause({ from: owner });
      await this.token.unpause({ from: owner });
   });

   shouldBehaveLikeERC721BasicToken(owner, [...otherAccounts]);
});

But I'm not shure which option is more prefferable.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd go with pausing outside, in a context, and renaming the behavior: in that second example you're testing both behaviors at the same time, as if they were the same thing (you could, in fact, test again for shouldBehaveLikeERC721PausableToken after pausing and unpausing :) )

beforeEach(async function () {
await this.token.pause({ from: owner });
});

shouldBehaveLikeERC721PausedToken(owner, [...otherAccounts]);
});

context('when token is not paused yet', function () {
shouldBehaveLikeERC721BasicToken([owner, ...otherAccounts]);
});

context('when token is paused and then unpaused', function () {
beforeEach(async function () {
await this.token.pause({ from: owner });
await this.token.unpause({ from: owner });
});

shouldBehaveLikeERC721BasicToken([owner, ...otherAccounts]);
});
});
88 changes: 88 additions & 0 deletions test/token/ERC721/ERC721PausedToken.behavior.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
const { assertRevert } = require('../../helpers/assertRevert');
const { sendTransaction } = require('../../helpers/sendTransaction');

const BigNumber = web3.BigNumber;

require('chai')
.use(require('chai-bignumber')(BigNumber))
.should();

function shouldBehaveLikeERC721PausedToken (owner, [recipient, operator]) {
const firstTokenId = 1;
const mintedTokens = 1;
const mockData = '0x42';
const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000';

describe('like a paused ERC721Token', function () {
beforeEach(async function () {
await this.token.mint(owner, firstTokenId, { from: owner });
});

it('reverts when trying to approve', async function () {
await assertRevert(this.token.approve(recipient, firstTokenId, { from: owner }));
});

it('reverts when trying to setApprovalForAll', async function () {
await assertRevert(this.token.setApprovalForAll(operator, true, { from: owner }));
});

it('reverts when trying to transferFrom', async function () {
await assertRevert(this.token.transferFrom(owner, recipient, firstTokenId, { from: owner }));
});

it('reverts when trying to safeTransferFrom', async function () {
await assertRevert(this.token.safeTransferFrom(owner, recipient, firstTokenId, { from: owner }));
});

it('reverts when trying to safeTransferFrom with data', async function () {
await assertRevert(
sendTransaction(
this.token,
'safeTransferFrom',
'address,address,uint256,bytes',
[owner, recipient, firstTokenId, mockData],
{ from: owner }
)
);
});

describe('getApproved', function () {
it('returns approved address', async function () {
const approvedAccount = await this.token.getApproved(firstTokenId);
approvedAccount.should.be.equal(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);
balance.should.be.bignumber.equal(mintedTokens);
});
});

describe('ownerOf', function () {
it('returns the amount of tokens owned by the given address', async function () {
const ownerOfToken = await this.token.ownerOf(firstTokenId);
ownerOfToken.should.be.equal(owner);
});
});

describe('exists', function () {
it('should return token existance', async function () {
const result = await this.token.exists(firstTokenId);
result.should.eq(true);
});
});

describe('isApprovedForAll', function () {
it('returns the approval of the operator', async function () {
const isApproved = await this.token.isApprovedForAll(owner, operator);
isApproved.should.eq(false);
});
});
});
}

module.exports = {
shouldBehaveLikeERC721PausedToken,
};