123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534 |
- import assertRevert from '../../helpers/assertRevert';
- const BigNumber = web3.BigNumber;
- const ERC721Token = artifacts.require('ERC721TokenMock.sol');
- require('chai')
- .use(require('chai-as-promised'))
- .use(require('chai-bignumber')(BigNumber))
- .should();
- contract('ERC721Token', accounts => {
- let token = null;
- const _firstTokenId = 1;
- const _secondTokenId = 2;
- const _unknownTokenId = 3;
- const _creator = accounts[0];
- const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000';
- beforeEach(async function () {
- token = await ERC721Token.new({ from: _creator });
- await token.mint(_creator, _firstTokenId, { from: _creator });
- await token.mint(_creator, _secondTokenId, { from: _creator });
- });
- describe('totalSupply', function () {
- it('has a total supply equivalent to the inital supply', async function () {
- const totalSupply = await token.totalSupply();
- totalSupply.should.be.bignumber.equal(2);
- });
- });
- 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('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 () {
- describe('when the given token ID was not tracked by this contract', function () {
- const tokenId = _unknownTokenId;
- describe('when the given address is not the zero address', function () {
- const to = accounts[1];
- it('mints the given token ID to the given address', async function () {
- const previousBalance = await token.balanceOf(to);
- await token.mint(to, tokenId);
- const owner = await token.ownerOf(tokenId);
- owner.should.be.equal(to);
- const balance = await token.balanceOf(to);
- balance.should.be.bignumber.equal(previousBalance + 1);
- });
- it('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 () {
- const { logs } = await token.mint(to, tokenId);
- 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 address is the zero address', function () {
- const to = ZERO_ADDRESS;
- it('reverts', async function () {
- await assertRevert(token.mint(to, tokenId));
- });
- });
- });
- describe('when the given token ID was already tracked by this contract', function () {
- const tokenId = _firstTokenId;
- it('reverts', async function () {
- await assertRevert(token.mint(accounts[1], tokenId));
- });
- });
- });
- describe('burn', function () {
- describe('when the given token ID was tracked by this contract', function () {
- const tokenId = _firstTokenId;
- describe('when the msg.sender owns given token', function () {
- const sender = _creator;
- it('burns the given token ID and adjusts the balance of the owner', async function () {
- const previousBalance = await token.balanceOf(sender);
- await token.burn(tokenId, { from: sender });
- await assertRevert(token.ownerOf(tokenId));
- const balance = await token.balanceOf(sender);
- balance.should.be.bignumber.equal(previousBalance - 1);
- });
- it('removes that token from the token list of the owner', async function () {
- await token.burn(tokenId, { from: sender });
- 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 () {
- const { logs } = await token.burn(tokenId, { from: sender });
- 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 an approval for the given token ID', function () {
- beforeEach(async function () {
- await token.approve(accounts[1], tokenId, { from: sender });
- });
- it('clears the approval', async function () {
- await token.burn(tokenId, { from: sender });
- const approvedAccount = await token.approvedFor(tokenId);
- approvedAccount.should.be.equal(ZERO_ADDRESS);
- });
- it('emits an approval event', async function () {
- const { logs } = await token.burn(tokenId, { from: sender });
- 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);
- });
- });
- });
- describe('when the msg.sender does not own given token', function () {
- const sender = accounts[1];
- it('reverts', async function () {
- await assertRevert(token.burn(tokenId, { from: sender }));
- });
- });
- });
- describe('when the given token ID was not tracked by this contract', function () {
- const tokenID = _unknownTokenId;
- it('reverts', async function () {
- await assertRevert(token.burn(tokenID, { from: _creator }));
- });
- });
- });
- describe('transfer', function () {
- describe('when the address to transfer the token to is not the zero address', function () {
- const to = accounts[1];
- describe('when the given token ID was tracked by this token', function () {
- const tokenId = _firstTokenId;
- describe('when the msg.sender is the owner of the given token ID', function () {
- const sender = _creator;
- it('transfers the ownership of the given token ID to the given address', async function () {
- await token.transfer(to, tokenId, { from: sender });
- const newOwner = await token.ownerOf(tokenId);
- newOwner.should.be.equal(to);
- });
- it('clears the approval for the token ID', async function () {
- await token.approve(accounts[2], tokenId, { from: sender });
- await token.transfer(to, tokenId, { from: sender });
- const approvedAccount = await token.approvedFor(tokenId);
- approvedAccount.should.be.equal(ZERO_ADDRESS);
- });
- it('emits an approval and transfer events', async function () {
- const { logs } = await token.transfer(to, tokenId, { from: sender });
- 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 previousBalance = await token.balanceOf(sender);
- await token.transfer(to, tokenId, { from: sender });
- const newOwnerBalance = await token.balanceOf(to);
- newOwnerBalance.should.be.bignumber.equal(1);
- const previousOwnerBalance = await token.balanceOf(_creator);
- previousOwnerBalance.should.be.bignumber.equal(previousBalance - 1);
- });
- it('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 msg.sender is not the owner of the given token ID', function () {
- const sender = accounts[2];
- it('reverts', async function () {
- await assertRevert(token.transfer(to, tokenId, { from: sender }));
- });
- });
- });
- describe('when the given token ID was not tracked by this token', function () {
- let tokenId = _unknownTokenId;
- it('reverts', async function () {
- await assertRevert(token.transfer(to, tokenId, { from: _creator }));
- });
- });
- });
- describe('when the address to transfer the token to is the zero address', function () {
- const to = ZERO_ADDRESS;
- it('reverts', async function () {
- await assertRevert(token.transfer(to, 0, { from: _creator }));
- });
- });
- });
- describe('approve', function () {
- describe('when the given token ID was already tracked by this contract', function () {
- const tokenId = _firstTokenId;
- describe('when the sender owns the given token ID', function () {
- const sender = _creator;
- describe('when the address that receives the approval is the 0 address', function () {
- const to = ZERO_ADDRESS;
- describe('when there was no approval for the given token ID before', function () {
- it('clears the approval for that token', async function () {
- await token.approve(to, tokenId, { from: sender });
- const approvedAccount = await token.approvedFor(tokenId);
- approvedAccount.should.be.equal(to);
- });
- it('does not emit an approval event', async function () {
- const { logs } = await token.approve(to, tokenId, { from: sender });
- logs.length.should.be.equal(0);
- });
- });
- describe('when the given token ID was approved for another account', function () {
- beforeEach(async function () {
- await token.approve(accounts[2], tokenId, { from: sender });
- });
- it('clears the approval for the token ID', async function () {
- await token.approve(to, tokenId, { from: sender });
- const approvedAccount = await token.approvedFor(tokenId);
- approvedAccount.should.be.equal(to);
- });
- it('emits an approval event', async function () {
- const { logs } = await token.approve(to, tokenId, { from: sender });
- 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 not the 0 address', function () {
- describe('when the address that receives the approval is different than the owner', function () {
- const to = accounts[1];
- describe('when there was no approval for the given token ID before', function () {
- it('approves the token ID to the given address', async function () {
- await token.approve(to, tokenId, { from: sender });
- const approvedAccount = await token.approvedFor(tokenId);
- approvedAccount.should.be.equal(to);
- });
- it('emits an approval event', async function () {
- const { logs } = await token.approve(to, tokenId, { from: sender });
- 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 given token ID was approved for the same account', function () {
- beforeEach(async function () {
- await token.approve(to, tokenId, { from: sender });
- });
- it('keeps the approval to the given address', async function () {
- await token.approve(to, tokenId, { from: sender });
- const approvedAccount = await token.approvedFor(tokenId);
- approvedAccount.should.be.equal(to);
- });
- it('emits an approval event', async function () {
- const { logs } = await token.approve(to, tokenId, { from: sender });
- 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 given token ID was approved for another account', function () {
- beforeEach(async function () {
- await token.approve(accounts[2], tokenId, { from: sender });
- });
- it('changes the approval to the given address', async function () {
- await token.approve(to, tokenId, { from: sender });
- const approvedAccount = await token.approvedFor(tokenId);
- approvedAccount.should.be.equal(to);
- });
- it('emits an approval event', async function () {
- const { logs } = await token.approve(to, tokenId, { from: sender });
- 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 () {
- const to = _creator;
- describe('when there was no approval for the given token ID before', function () {
- it('reverts', async function () {
- await assertRevert(token.approve(to, tokenId, { from: sender }));
- });
- });
- describe('when the given token ID was approved for another account', function () {
- beforeEach(async function () {
- await token.approve(accounts[2], tokenId, { from: sender });
- });
- it('reverts', async function () {
- await assertRevert(token.approve(to, tokenId, { from: sender }));
- });
- });
- });
- });
- });
- describe('when the sender does not own the given token ID', function () {
- const sender = accounts[1];
- it('reverts', async function () {
- await assertRevert(token.approve(accounts[2], tokenId, { from: sender }));
- });
- });
- });
- describe('when the given token ID was not tracked by the contract before', function () {
- const tokenId = _unknownTokenId;
- it('reverts', async function () {
- await assertRevert(token.approve(accounts[1], tokenId, { from: _creator }));
- });
- });
- });
- describe('takeOwnership', function () {
- describe('when the given token ID was already tracked by this contract', function () {
- const tokenId = _firstTokenId;
- describe('when the sender has the approval for the token ID', function () {
- const sender = accounts[1];
- beforeEach(async function () {
- await token.approve(sender, tokenId, { from: _creator });
- });
- it('transfers the ownership of the given token ID to the given address', async function () {
- await token.takeOwnership(tokenId, { from: sender });
- const newOwner = await token.ownerOf(tokenId);
- newOwner.should.be.equal(sender);
- });
- it('clears the approval for the token ID', async function () {
- await token.takeOwnership(tokenId, { from: sender });
- const approvedAccount = await token.approvedFor(tokenId);
- approvedAccount.should.be.equal(ZERO_ADDRESS);
- });
- it('emits an approval and transfer events', async function () {
- const { logs } = await token.takeOwnership(tokenId, { from: sender });
- logs.length.should.be.equal(2);
- logs[0].event.should.be.eq('Approval');
- logs[0].args._owner.should.be.equal(_creator);
- 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(_creator);
- logs[1].args._to.should.be.equal(sender);
- logs[1].args._tokenId.should.be.bignumber.equal(tokenId);
- });
- it('adjusts owners balances', async function () {
- const previousBalance = await token.balanceOf(_creator);
- await token.takeOwnership(tokenId, { from: sender });
- const newOwnerBalance = await token.balanceOf(sender);
- newOwnerBalance.should.be.bignumber.equal(1);
- const previousOwnerBalance = await token.balanceOf(_creator);
- previousOwnerBalance.should.be.bignumber.equal(previousBalance - 1);
- });
- it('adds the token to the tokens list of the new owner', async function () {
- await token.takeOwnership(tokenId, { from: sender });
- const tokenIDs = await token.tokensOf(sender);
- tokenIDs.length.should.be.equal(1);
- tokenIDs[0].should.be.bignumber.equal(tokenId);
- });
- });
- describe('when the sender does not have an approval for the token ID', function () {
- const sender = accounts[1];
- it('reverts', async function () {
- await assertRevert(token.takeOwnership(tokenId, { from: sender }));
- });
- });
- describe('when the sender is already the owner of the token', function () {
- const sender = _creator;
- it('reverts', async function () {
- await assertRevert(token.takeOwnership(tokenId, { from: sender }));
- });
- });
- });
- describe('when the given token ID was not tracked by the contract before', function () {
- const tokenId = _unknownTokenId;
- it('reverts', async function () {
- await assertRevert(token.takeOwnership(tokenId, { from: _creator }));
- });
- });
- });
- });
|