ERC721Token.test.js 8.8 KB

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