ERC721Token.test.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. const { assertRevert } = require('../../helpers/assertRevert');
  2. const { shouldBehaveLikeERC721BasicToken } = require('./ERC721BasicToken.behaviour');
  3. const { shouldBehaveLikeMintAndBurnERC721Token } = require('./ERC721MintBurn.behaviour');
  4. const { shouldSupportInterfaces } = require('../../introspection/SupportsInterface.behavior');
  5. const _ = require('lodash');
  6. const BigNumber = web3.BigNumber;
  7. const ERC721Token = artifacts.require('ERC721TokenMock.sol');
  8. require('chai')
  9. .use(require('chai-as-promised'))
  10. .use(require('chai-bignumber')(BigNumber))
  11. .should();
  12. contract('ERC721Token', function (accounts) {
  13. const name = 'Non Fungible Token';
  14. const symbol = 'NFT';
  15. const firstTokenId = 100;
  16. const secondTokenId = 200;
  17. const creator = accounts[0];
  18. beforeEach(async function () {
  19. this.token = await ERC721Token.new(name, symbol, { from: creator });
  20. });
  21. shouldBehaveLikeERC721BasicToken(accounts);
  22. shouldBehaveLikeMintAndBurnERC721Token(accounts);
  23. describe('like a full ERC721', function () {
  24. beforeEach(async function () {
  25. await this.token.mint(creator, firstTokenId, { from: creator });
  26. await this.token.mint(creator, secondTokenId, { from: creator });
  27. });
  28. describe('mint', function () {
  29. const to = accounts[1];
  30. const tokenId = 3;
  31. beforeEach(async function () {
  32. await this.token.mint(to, tokenId);
  33. });
  34. it('adjusts owner tokens by index', async function () {
  35. const token = await this.token.tokenOfOwnerByIndex(to, 0);
  36. token.toNumber().should.be.equal(tokenId);
  37. });
  38. it('adjusts all tokens list', async function () {
  39. const newToken = await this.token.tokenByIndex(2);
  40. newToken.toNumber().should.be.equal(tokenId);
  41. });
  42. });
  43. describe('burn', function () {
  44. const tokenId = firstTokenId;
  45. const sender = creator;
  46. beforeEach(async function () {
  47. await this.token.burn(tokenId, { from: sender });
  48. });
  49. it('removes that token from the token list of the owner', async function () {
  50. const token = await this.token.tokenOfOwnerByIndex(sender, 0);
  51. token.toNumber().should.be.equal(secondTokenId);
  52. });
  53. it('adjusts all tokens list', async function () {
  54. const token = await this.token.tokenByIndex(0);
  55. token.toNumber().should.be.equal(secondTokenId);
  56. });
  57. it('burns all tokens', async function () {
  58. await this.token.burn(secondTokenId, { from: sender });
  59. const total = await this.token.totalSupply();
  60. total.toNumber().should.be.equal(0);
  61. await assertRevert(this.token.tokenByIndex(0));
  62. });
  63. });
  64. describe('metadata', function () {
  65. const sampleUri = 'mock://mytoken';
  66. it('has a name', async function () {
  67. const tokenName = await this.token.name();
  68. tokenName.should.be.equal(name);
  69. });
  70. it('has a symbol', async function () {
  71. const tokenSymbol = await this.token.symbol();
  72. tokenSymbol.should.be.equal(symbol);
  73. });
  74. it('sets and returns metadata for a token id', async function () {
  75. await this.token.setTokenURI(firstTokenId, sampleUri);
  76. const uri = await this.token.tokenURI(firstTokenId);
  77. uri.should.be.equal(sampleUri);
  78. });
  79. it('can burn token with metadata', async function () {
  80. await this.token.setTokenURI(firstTokenId, sampleUri);
  81. await this.token.burn(firstTokenId);
  82. const exists = await this.token.exists(firstTokenId);
  83. exists.should.be.false;
  84. });
  85. it('returns empty metadata for token', async function () {
  86. const uri = await this.token.tokenURI(firstTokenId);
  87. uri.should.be.equal('');
  88. });
  89. it('reverts when querying metadata for non existant token id', async function () {
  90. await assertRevert(this.token.tokenURI(500));
  91. });
  92. });
  93. describe('totalSupply', function () {
  94. it('returns total token supply', async function () {
  95. const totalSupply = await this.token.totalSupply();
  96. totalSupply.should.be.bignumber.equal(2);
  97. });
  98. });
  99. describe('tokenOfOwnerByIndex', function () {
  100. const owner = creator;
  101. const another = accounts[1];
  102. describe('when the given index is lower than the amount of tokens owned by the given address', function () {
  103. it('returns the token ID placed at the given index', async function () {
  104. const tokenId = await this.token.tokenOfOwnerByIndex(owner, 0);
  105. tokenId.should.be.bignumber.equal(firstTokenId);
  106. });
  107. });
  108. describe('when the index is greater than or equal to the total tokens owned by the given address', function () {
  109. it('reverts', async function () {
  110. await assertRevert(this.token.tokenOfOwnerByIndex(owner, 2));
  111. });
  112. });
  113. describe('when the given address does not own any token', function () {
  114. it('reverts', async function () {
  115. await assertRevert(this.token.tokenOfOwnerByIndex(another, 0));
  116. });
  117. });
  118. describe('after transferring all tokens to another user', function () {
  119. beforeEach(async function () {
  120. await this.token.transferFrom(owner, another, firstTokenId, { from: owner });
  121. await this.token.transferFrom(owner, another, secondTokenId, { from: owner });
  122. });
  123. it('returns correct token IDs for target', async function () {
  124. const count = await this.token.balanceOf(another);
  125. count.toNumber().should.be.equal(2);
  126. const tokensListed = await Promise.all(_.range(2).map(i => this.token.tokenOfOwnerByIndex(another, i)));
  127. tokensListed.map(t => t.toNumber()).should.have.members([firstTokenId, secondTokenId]);
  128. });
  129. it('returns empty collection for original owner', async function () {
  130. const count = await this.token.balanceOf(owner);
  131. count.toNumber().should.be.equal(0);
  132. await assertRevert(this.token.tokenOfOwnerByIndex(owner, 0));
  133. });
  134. });
  135. });
  136. describe('tokenByIndex', function () {
  137. it('should return all tokens', async function () {
  138. const tokensListed = await Promise.all(_.range(2).map(i => this.token.tokenByIndex(i)));
  139. tokensListed.map(t => t.toNumber()).should.have.members([firstTokenId, secondTokenId]);
  140. });
  141. it('should revert if index is greater than supply', async function () {
  142. await assertRevert(this.token.tokenByIndex(2));
  143. });
  144. [firstTokenId, secondTokenId].forEach(function (tokenId) {
  145. it(`should return all tokens after burning token ${tokenId} and minting new tokens`, async function () {
  146. const owner = accounts[0];
  147. const newTokenId = 300;
  148. const anotherNewTokenId = 400;
  149. await this.token.burn(tokenId, { from: owner });
  150. await this.token.mint(owner, newTokenId, { from: owner });
  151. await this.token.mint(owner, anotherNewTokenId, { from: owner });
  152. const count = await this.token.totalSupply();
  153. count.toNumber().should.be.equal(3);
  154. const tokensListed = await Promise.all(_.range(3).map(i => this.token.tokenByIndex(i)));
  155. const expectedTokens = _.filter(
  156. [firstTokenId, secondTokenId, newTokenId, anotherNewTokenId],
  157. x => (x !== tokenId)
  158. );
  159. tokensListed.map(t => t.toNumber()).should.have.members(expectedTokens);
  160. });
  161. });
  162. });
  163. });
  164. shouldSupportInterfaces([
  165. 'ERC165',
  166. 'ERC721',
  167. 'ERC721Exists',
  168. 'ERC721Enumerable',
  169. 'ERC721Metadata',
  170. ]);
  171. });