ERC721Full.test.js 9.3 KB

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