ERC721Token.test.js 8.2 KB

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