ERC721Full.test.js 8.2 KB

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