ERC721Full.test.js 9.2 KB

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