ERC721Token.test.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. import assertRevert from '../../helpers/assertRevert';
  2. import shouldBehaveLikeERC721BasicToken from './ERC721BasicToken.behaviour';
  3. import shouldMintAndBurnERC721Token from './ERC721MintBurn.behaviour';
  4. import _ from 'lodash';
  5. const BigNumber = web3.BigNumber;
  6. const ERC721Token = artifacts.require('ERC721TokenMock.sol');
  7. require('chai')
  8. .use(require('chai-as-promised'))
  9. .use(require('chai-bignumber')(BigNumber))
  10. .should();
  11. contract('ERC721Token', function (accounts) {
  12. const name = 'Non Fungible Token';
  13. const symbol = 'NFT';
  14. const firstTokenId = 100;
  15. const secondTokenId = 200;
  16. const creator = accounts[0];
  17. beforeEach(async function () {
  18. this.token = await ERC721Token.new(name, symbol, { from: creator });
  19. });
  20. shouldBehaveLikeERC721BasicToken(accounts);
  21. shouldMintAndBurnERC721Token(accounts);
  22. describe('like a full ERC721', function () {
  23. beforeEach(async function () {
  24. await this.token.mint(creator, firstTokenId, { from: creator });
  25. await this.token.mint(creator, secondTokenId, { from: creator });
  26. });
  27. describe('mint', function () {
  28. const to = accounts[1];
  29. const tokenId = 3;
  30. beforeEach(async function () {
  31. await this.token.mint(to, tokenId);
  32. });
  33. it('adjusts owner tokens by index', async function () {
  34. const token = await this.token.tokenOfOwnerByIndex(to, 0);
  35. token.toNumber().should.be.equal(tokenId);
  36. });
  37. it('adjusts all tokens list', async function () {
  38. const newToken = await this.token.tokenByIndex(2);
  39. newToken.toNumber().should.be.equal(tokenId);
  40. });
  41. });
  42. describe('burn', function () {
  43. const tokenId = firstTokenId;
  44. const sender = creator;
  45. beforeEach(async function () {
  46. await this.token.burn(tokenId, { from: sender });
  47. });
  48. it('removes that token from the token list of the owner', async function () {
  49. const token = await this.token.tokenOfOwnerByIndex(sender, 0);
  50. token.toNumber().should.be.equal(secondTokenId);
  51. });
  52. it('adjusts all tokens list', async function () {
  53. const token = await this.token.tokenByIndex(0);
  54. token.toNumber().should.be.equal(secondTokenId);
  55. });
  56. it('burns all tokens', async function () {
  57. await this.token.burn(secondTokenId, { from: sender });
  58. const total = await this.token.totalSupply();
  59. total.toNumber().should.be.equal(0);
  60. await assertRevert(this.token.tokenByIndex(0));
  61. });
  62. });
  63. describe('metadata', function () {
  64. const sampleUri = 'mock://mytoken';
  65. it('has a name', async function () {
  66. const tokenName = await this.token.name();
  67. tokenName.should.be.equal(name);
  68. });
  69. it('has a symbol', async function () {
  70. const tokenSymbol = await this.token.symbol();
  71. tokenSymbol.should.be.equal(symbol);
  72. });
  73. it('sets and returns metadata for a token id', async function () {
  74. await this.token.setTokenURI(firstTokenId, sampleUri);
  75. const uri = await this.token.tokenURI(firstTokenId);
  76. uri.should.be.equal(sampleUri);
  77. });
  78. it('can burn token with metadata', async function () {
  79. await this.token.setTokenURI(firstTokenId, sampleUri);
  80. await this.token.burn(firstTokenId);
  81. const exists = await this.token.exists(firstTokenId);
  82. exists.should.be.false;
  83. });
  84. it('returns empty metadata for token', async function () {
  85. const uri = await this.token.tokenURI(firstTokenId);
  86. uri.should.be.equal('');
  87. });
  88. it('reverts when querying metadata for non existant token id', async function () {
  89. await assertRevert(this.token.tokenURI(500));
  90. });
  91. });
  92. describe('totalSupply', function () {
  93. it('returns total token supply', async function () {
  94. const totalSupply = await this.token.totalSupply();
  95. totalSupply.should.be.bignumber.equal(2);
  96. });
  97. });
  98. describe('tokenOfOwnerByIndex', function () {
  99. const owner = creator;
  100. const another = accounts[1];
  101. describe('when the given index is lower than the amount of tokens owned by the given address', function () {
  102. it('returns the token ID placed at the given index', async function () {
  103. const tokenId = await this.token.tokenOfOwnerByIndex(owner, 0);
  104. tokenId.should.be.bignumber.equal(firstTokenId);
  105. });
  106. });
  107. describe('when the index is greater than or equal to the total tokens owned by the given address', function () {
  108. it('reverts', async function () {
  109. await assertRevert(this.token.tokenOfOwnerByIndex(owner, 2));
  110. });
  111. });
  112. describe('when the given address does not own any token', function () {
  113. it('reverts', async function () {
  114. await assertRevert(this.token.tokenOfOwnerByIndex(another, 0));
  115. });
  116. });
  117. describe('after transferring all tokens to another user', function () {
  118. beforeEach(async function () {
  119. await this.token.transferFrom(owner, another, firstTokenId, { from: owner });
  120. await this.token.transferFrom(owner, another, secondTokenId, { from: owner });
  121. });
  122. it('returns correct token IDs for target', async function () {
  123. const count = await this.token.balanceOf(another);
  124. count.toNumber().should.be.equal(2);
  125. const tokensListed = await Promise.all(_.range(2).map(i => this.token.tokenOfOwnerByIndex(another, i)));
  126. tokensListed.map(t => t.toNumber()).should.have.members([firstTokenId, secondTokenId]);
  127. });
  128. it('returns empty collection for original owner', async function () {
  129. const count = await this.token.balanceOf(owner);
  130. count.toNumber().should.be.equal(0);
  131. await assertRevert(this.token.tokenOfOwnerByIndex(owner, 0));
  132. });
  133. });
  134. });
  135. describe('tokenByIndex', function () {
  136. it('should return all tokens', async function () {
  137. const tokensListed = await Promise.all(_.range(2).map(i => this.token.tokenByIndex(i)));
  138. tokensListed.map(t => t.toNumber()).should.have.members([firstTokenId, secondTokenId]);
  139. });
  140. it('should revert if index is greater than supply', async function () {
  141. await assertRevert(this.token.tokenByIndex(2));
  142. });
  143. [firstTokenId, secondTokenId].forEach(function (tokenId) {
  144. it(`should return all tokens after burning token ${tokenId} and minting new tokens`, async function () {
  145. const owner = accounts[0];
  146. const newTokenId = 300;
  147. const anotherNewTokenId = 400;
  148. await this.token.burn(tokenId, { from: owner });
  149. await this.token.mint(owner, newTokenId, { from: owner });
  150. await this.token.mint(owner, anotherNewTokenId, { from: owner });
  151. const count = await this.token.totalSupply();
  152. count.toNumber().should.be.equal(3);
  153. const tokensListed = await Promise.all(_.range(3).map(i => this.token.tokenByIndex(i)));
  154. const expectedTokens = _.filter(
  155. [firstTokenId, secondTokenId, newTokenId, anotherNewTokenId],
  156. x => (x !== tokenId)
  157. );
  158. tokensListed.map(t => t.toNumber()).should.have.members(expectedTokens);
  159. });
  160. });
  161. });
  162. });
  163. });