ERC721Full.test.js 8.4 KB

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