ERC721Full.test.js 7.4 KB

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