ERC721Token.test.js 8.2 KB

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