ERC721Full.test.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. const { assertRevert } = require('../../helpers/assertRevert');
  2. const { shouldBehaveLikeERC721 } = require('./ERC721.behavior');
  3. const { shouldSupportInterfaces } = require('../../introspection/SupportsInterface.behavior');
  4. const _ = require('lodash');
  5. const BigNumber = web3.BigNumber;
  6. const ERC721FullMock = artifacts.require('ERC721FullMock.sol');
  7. require('chai')
  8. .use(require('chai-bignumber')(BigNumber))
  9. .should();
  10. contract('ERC721Full', function ([
  11. creator,
  12. ...accounts
  13. ]) {
  14. const name = 'Non Fungible Token';
  15. const symbol = 'NFT';
  16. const firstTokenId = 100;
  17. const secondTokenId = 200;
  18. const thirdTokenId = 300;
  19. const nonExistentTokenId = 999;
  20. const minter = creator;
  21. const [
  22. owner,
  23. newOwner,
  24. another,
  25. anyone,
  26. ] = accounts;
  27. beforeEach(async function () {
  28. this.token = await ERC721FullMock.new(name, symbol, { from: creator });
  29. });
  30. describe('like a full ERC721', function () {
  31. beforeEach(async function () {
  32. await this.token.mint(owner, firstTokenId, { from: minter });
  33. await this.token.mint(owner, secondTokenId, { from: minter });
  34. });
  35. describe('mint', function () {
  36. beforeEach(async function () {
  37. await this.token.mint(newOwner, thirdTokenId, { from: minter });
  38. });
  39. it('adjusts owner tokens by index', async function () {
  40. (await this.token.tokenOfOwnerByIndex(newOwner, 0)).toNumber().should.be.equal(thirdTokenId);
  41. });
  42. it('adjusts all tokens list', async function () {
  43. (await this.token.tokenByIndex(2)).toNumber().should.be.equal(thirdTokenId);
  44. });
  45. });
  46. describe('burn', function () {
  47. beforeEach(async function () {
  48. await this.token.burn(firstTokenId, { from: owner });
  49. });
  50. it('removes that token from the token list of the owner', async function () {
  51. (await this.token.tokenOfOwnerByIndex(owner, 0)).toNumber().should.be.equal(secondTokenId);
  52. });
  53. it('adjusts all tokens list', async function () {
  54. (await this.token.tokenByIndex(0)).toNumber().should.be.equal(secondTokenId);
  55. });
  56. it('burns all tokens', async function () {
  57. await this.token.burn(secondTokenId, { from: owner });
  58. (await this.token.totalSupply()).toNumber().should.be.equal(0);
  59. await assertRevert(this.token.tokenByIndex(0));
  60. });
  61. });
  62. describe('removeTokenFrom', function () {
  63. it('reverts if the correct owner is not passed', async function () {
  64. await assertRevert(
  65. this.token.removeTokenFrom(anyone, firstTokenId, { from: owner })
  66. );
  67. });
  68. context('once removed', function () {
  69. beforeEach(async function () {
  70. await this.token.removeTokenFrom(owner, firstTokenId, { from: owner });
  71. });
  72. it('has been removed', async function () {
  73. await assertRevert(this.token.tokenOfOwnerByIndex(owner, 1));
  74. });
  75. it('adjusts token list', async function () {
  76. (await this.token.tokenOfOwnerByIndex(owner, 0)).toNumber().should.be.equal(secondTokenId);
  77. });
  78. it('adjusts owner count', async function () {
  79. (await this.token.balanceOf(owner)).toNumber().should.be.equal(1);
  80. });
  81. it('does not adjust supply', async function () {
  82. (await this.token.totalSupply()).toNumber().should.be.equal(2);
  83. });
  84. });
  85. });
  86. describe('metadata', function () {
  87. const sampleUri = 'mock://mytoken';
  88. it('has a name', async function () {
  89. (await this.token.name()).should.be.equal(name);
  90. });
  91. it('has a symbol', async function () {
  92. (await this.token.symbol()).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. (await this.token.tokenURI(firstTokenId)).should.be.equal(sampleUri);
  97. });
  98. it('reverts when setting metadata for non existent token id', async function () {
  99. await assertRevert(this.token.setTokenURI(nonExistentTokenId, sampleUri));
  100. });
  101. it('can burn token with metadata', async function () {
  102. await this.token.setTokenURI(firstTokenId, sampleUri);
  103. await this.token.burn(firstTokenId, { from: owner });
  104. (await this.token.exists(firstTokenId)).should.equal(false);
  105. });
  106. it('returns empty metadata for token', async function () {
  107. (await this.token.tokenURI(firstTokenId)).should.be.equal('');
  108. });
  109. it('reverts when querying metadata for non existent token id', async function () {
  110. await assertRevert(this.token.tokenURI(nonExistentTokenId));
  111. });
  112. });
  113. describe('totalSupply', function () {
  114. it('returns total token supply', async function () {
  115. (await this.token.totalSupply()).should.be.bignumber.equal(2);
  116. });
  117. });
  118. describe('tokenOfOwnerByIndex', function () {
  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 newTokenId = 300;
  161. const anotherNewTokenId = 400;
  162. await this.token.burn(tokenId, { from: owner });
  163. await this.token.mint(newOwner, newTokenId, { from: minter });
  164. await this.token.mint(newOwner, anotherNewTokenId, { from: minter });
  165. (await this.token.totalSupply()).toNumber().should.be.equal(3);
  166. const tokensListed = await Promise.all(_.range(3).map(i => this.token.tokenByIndex(i)));
  167. const expectedTokens = _.filter(
  168. [firstTokenId, secondTokenId, newTokenId, anotherNewTokenId],
  169. x => (x !== tokenId)
  170. );
  171. tokensListed.map(t => t.toNumber()).should.have.members(expectedTokens);
  172. });
  173. });
  174. });
  175. });
  176. shouldBehaveLikeERC721(creator, minter, accounts);
  177. shouldSupportInterfaces([
  178. 'ERC165',
  179. 'ERC721',
  180. 'ERC721Enumerable',
  181. 'ERC721Metadata',
  182. ]);
  183. });