ERC721Full.test.js 9.4 KB

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