-
Notifications
You must be signed in to change notification settings - Fork 12k
ERC721 bugfix + gas optimizations #1549
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
Merged
+170
−124
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4abe9f7
Now only swapping when needed.
nventuro e0505b7
Removed _addTokenTo and _removeTokenFrom
nventuro 55bdeda
Removed removeTokenFrom test.
nventuro 35cf659
Merge branch 'master' into erc721-bugfix-optim
nventuro b4dae05
Added tests for ERC721 _mint and _burn
nventuro 5c7f09b
_burn now uses the same swap and pop mechanism as _removeFromOwner
nventuro 6255db8
Gas optimization on burn
nventuro 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 hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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 |
---|---|---|
@@ -1,13 +1,112 @@ | ||
const { shouldBehaveLikeERC721 } = require('./ERC721.behavior'); | ||
require('../../helpers/setup'); | ||
const { ZERO_ADDRESS } = require('../../helpers/constants'); | ||
const expectEvent = require('../../helpers/expectEvent'); | ||
const send = require('../../helpers/send'); | ||
const shouldFail = require('../../helpers/shouldFail'); | ||
|
||
const { shouldBehaveLikeERC721 } = require('./ERC721.behavior'); | ||
const ERC721Mock = artifacts.require('ERC721Mock.sol'); | ||
|
||
require('../../helpers/setup'); | ||
|
||
contract('ERC721', function ([_, creator, ...accounts]) { | ||
contract('ERC721', function ([_, creator, tokenOwner, anyone, ...accounts]) { | ||
beforeEach(async function () { | ||
this.token = await ERC721Mock.new({ from: creator }); | ||
}); | ||
|
||
shouldBehaveLikeERC721(creator, creator, accounts); | ||
|
||
describe('internal functions', function () { | ||
const tokenId = 5042; | ||
|
||
describe('_mint(address, uint256)', function () { | ||
it('reverts with a null destination address', async function () { | ||
await shouldFail.reverting(this.token.mint(ZERO_ADDRESS, tokenId)); | ||
}); | ||
|
||
context('with minted token', async function () { | ||
beforeEach(async function () { | ||
({ logs: this.logs } = await this.token.mint(tokenOwner, tokenId)); | ||
}); | ||
|
||
it('emits a Transfer event', function () { | ||
expectEvent.inLogs(this.logs, 'Transfer', { from: ZERO_ADDRESS, to: tokenOwner, tokenId }); | ||
}); | ||
|
||
it('creates the token', async function () { | ||
(await this.token.balanceOf(tokenOwner)).should.be.bignumber.equal(1); | ||
(await this.token.ownerOf(tokenId)).should.equal(tokenOwner); | ||
}); | ||
|
||
it('reverts when adding a token id that already exists', async function () { | ||
await shouldFail.reverting(this.token.mint(tokenOwner, tokenId)); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('_burn(address, uint256)', function () { | ||
it('reverts when burning a non-existent token id', async function () { | ||
await shouldFail.reverting(send.transaction(this.token, 'burn', 'address,uint256', [tokenOwner, tokenId])); | ||
}); | ||
|
||
context('with minted token', function () { | ||
beforeEach(async function () { | ||
await this.token.mint(tokenOwner, tokenId); | ||
}); | ||
|
||
it('reverts when the account is not the owner', async function () { | ||
await shouldFail.reverting(send.transaction(this.token, 'burn', 'address,uint256', [anyone, tokenId])); | ||
}); | ||
|
||
context('with burnt token', function () { | ||
beforeEach(async function () { | ||
({ logs: this.logs } = | ||
await send.transaction(this.token, 'burn', 'address,uint256', [tokenOwner, tokenId])); | ||
}); | ||
|
||
it('emits a Transfer event', function () { | ||
expectEvent.inLogs(this.logs, 'Transfer', { from: tokenOwner, to: ZERO_ADDRESS, tokenId }); | ||
}); | ||
|
||
it('deletes the token', async function () { | ||
(await this.token.balanceOf(tokenOwner)).should.be.bignumber.equal(0); | ||
await shouldFail.reverting(this.token.ownerOf(tokenId)); | ||
}); | ||
|
||
it('reverts when burning a token id that has been deleted', async function () { | ||
await shouldFail.reverting(send.transaction(this.token, 'burn', 'address,uint256', [tokenOwner, tokenId])); | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('_burn(uint256)', function () { | ||
it('reverts when burning a non-existent token id', async function () { | ||
await shouldFail.reverting(send.transaction(this.token, 'burn', 'uint256', [tokenId])); | ||
}); | ||
|
||
context('with minted token', function () { | ||
beforeEach(async function () { | ||
await this.token.mint(tokenOwner, tokenId); | ||
}); | ||
|
||
context('with burnt token', function () { | ||
beforeEach(async function () { | ||
({ logs: this.logs } = await send.transaction(this.token, 'burn', 'uint256', [tokenId])); | ||
}); | ||
|
||
it('emits a Transfer event', function () { | ||
expectEvent.inLogs(this.logs, 'Transfer', { from: tokenOwner, to: ZERO_ADDRESS, tokenId }); | ||
}); | ||
|
||
it('deletes the token', async function () { | ||
(await this.token.balanceOf(tokenOwner)).should.be.bignumber.equal(0); | ||
await shouldFail.reverting(this.token.ownerOf(tokenId)); | ||
}); | ||
|
||
it('reverts when burning a token id that has been deleted', async function () { | ||
await shouldFail.reverting(send.transaction(this.token, 'burn', 'uint256', [tokenId])); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.