ERC721Full.test.js 7.9 KB

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