ERC721Full.test.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. 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. 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. const sampleUri = 'mock://mytoken';
  61. it('has a name', async function () {
  62. expect(await this.token.name()).to.be.equal(name);
  63. });
  64. it('has a symbol', async function () {
  65. expect(await this.token.symbol()).to.be.equal(symbol);
  66. });
  67. it('sets and returns metadata for a token id', async function () {
  68. await this.token.setTokenURI(firstTokenId, sampleUri);
  69. expect(await this.token.tokenURI(firstTokenId)).to.be.equal(sampleUri);
  70. });
  71. it('reverts when setting metadata for non existent token id', async function () {
  72. await expectRevert(
  73. this.token.setTokenURI(nonExistentTokenId, sampleUri), 'ERC721Metadata: URI set of nonexistent token'
  74. );
  75. });
  76. it('can burn token with metadata', async function () {
  77. await this.token.setTokenURI(firstTokenId, sampleUri);
  78. await this.token.burn(firstTokenId, { from: owner });
  79. expect(await this.token.exists(firstTokenId)).to.equal(false);
  80. });
  81. it('returns empty metadata for token', async function () {
  82. expect(await this.token.tokenURI(firstTokenId)).to.be.equal('');
  83. });
  84. it('reverts when querying metadata for non existent token id', async function () {
  85. await expectRevert(
  86. this.token.tokenURI(nonExistentTokenId), 'ERC721Metadata: URI query for nonexistent token'
  87. );
  88. });
  89. });
  90. describe('tokensOfOwner', function () {
  91. it('returns total tokens of owner', async function () {
  92. const tokenIds = await this.token.tokensOfOwner(owner);
  93. expect(tokenIds.length).to.equal(2);
  94. expect(tokenIds[0]).to.be.bignumber.equal(firstTokenId);
  95. expect(tokenIds[1]).to.be.bignumber.equal(secondTokenId);
  96. });
  97. });
  98. describe('totalSupply', function () {
  99. it('returns total token supply', async function () {
  100. expect(await this.token.totalSupply()).to.be.bignumber.equal('2');
  101. });
  102. });
  103. describe('tokenOfOwnerByIndex', function () {
  104. describe('when the given index is lower than the amount of tokens owned by the given address', function () {
  105. it('returns the token ID placed at the given index', async function () {
  106. expect(await this.token.tokenOfOwnerByIndex(owner, 0)).to.be.bignumber.equal(firstTokenId);
  107. });
  108. });
  109. describe('when the index is greater than or equal to the total tokens owned by the given address', function () {
  110. it('reverts', async function () {
  111. await expectRevert(
  112. this.token.tokenOfOwnerByIndex(owner, 2), 'ERC721Enumerable: owner index out of bounds'
  113. );
  114. });
  115. });
  116. describe('when the given address does not own any token', function () {
  117. it('reverts', async function () {
  118. await expectRevert(
  119. this.token.tokenOfOwnerByIndex(another, 0), 'ERC721Enumerable: owner index out of bounds'
  120. );
  121. });
  122. });
  123. describe('after transferring all tokens to another user', function () {
  124. beforeEach(async function () {
  125. await this.token.transferFrom(owner, another, firstTokenId, { from: owner });
  126. await this.token.transferFrom(owner, another, secondTokenId, { from: owner });
  127. });
  128. it('returns correct token IDs for target', async function () {
  129. expect(await this.token.balanceOf(another)).to.be.bignumber.equal('2');
  130. const tokensListed = await Promise.all(
  131. [0, 1].map(i => this.token.tokenOfOwnerByIndex(another, i))
  132. );
  133. expect(tokensListed.map(t => t.toNumber())).to.have.members([firstTokenId.toNumber(),
  134. secondTokenId.toNumber()]);
  135. });
  136. it('returns empty collection for original owner', async function () {
  137. expect(await this.token.balanceOf(owner)).to.be.bignumber.equal('0');
  138. await expectRevert(
  139. this.token.tokenOfOwnerByIndex(owner, 0), 'ERC721Enumerable: owner index out of bounds'
  140. );
  141. });
  142. });
  143. });
  144. describe('tokenByIndex', function () {
  145. it('should return all tokens', async function () {
  146. const tokensListed = await Promise.all(
  147. [0, 1].map(i => this.token.tokenByIndex(i))
  148. );
  149. expect(tokensListed.map(t => t.toNumber())).to.have.members([firstTokenId.toNumber(),
  150. secondTokenId.toNumber()]);
  151. });
  152. it('should revert if index is greater than supply', async function () {
  153. await expectRevert(
  154. this.token.tokenByIndex(2), 'ERC721Enumerable: global index out of bounds'
  155. );
  156. });
  157. [firstTokenId, secondTokenId].forEach(function (tokenId) {
  158. it(`should return all tokens after burning token ${tokenId} and minting new tokens`, async function () {
  159. const newTokenId = new BN(300);
  160. const anotherNewTokenId = new BN(400);
  161. await this.token.burn(tokenId, { from: owner });
  162. await this.token.mint(newOwner, newTokenId, { from: minter });
  163. await this.token.mint(newOwner, anotherNewTokenId, { from: minter });
  164. expect(await this.token.totalSupply()).to.be.bignumber.equal('3');
  165. const tokensListed = await Promise.all(
  166. [0, 1, 2].map(i => this.token.tokenByIndex(i))
  167. );
  168. const expectedTokens = [firstTokenId, secondTokenId, newTokenId, anotherNewTokenId].filter(
  169. x => (x !== tokenId)
  170. );
  171. expect(tokensListed.map(t => t.toNumber())).to.have.members(expectedTokens.map(t => t.toNumber()));
  172. });
  173. });
  174. });
  175. });
  176. shouldBehaveLikeERC721(creator, minter, accounts);
  177. shouldSupportInterfaces([
  178. 'ERC165',
  179. 'ERC721',
  180. 'ERC721Enumerable',
  181. 'ERC721Metadata',
  182. ]);
  183. });