ERC721Token.test.js 8.3 KB

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