ERC721Full.test.js 7.3 KB

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