ERC721Full.test.js 8.1 KB

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