-
Notifications
You must be signed in to change notification settings - Fork 11.8k
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
ERC721 pausable token #1154
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 () { | ||
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]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 thebeforeEach
here pausing the token?There was a problem hiding this comment.
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
toshouldBehaveLikeERC721PausedToken
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).
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
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:But I'm not shure which option is more prefferable.
There was a problem hiding this comment.
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 :) )