ERC721Full.test.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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('tokensOfOwner', function () {
  113. it('returns total tokens of owner', async function () {
  114. const tokenIds = await this.token.tokensOfOwner(owner);
  115. tokenIds.length.should.equal(2);
  116. tokenIds[0].should.be.bignumber.equal(firstTokenId);
  117. tokenIds[1].should.be.bignumber.equal(secondTokenId);
  118. });
  119. });
  120. describe('totalSupply', function () {
  121. it('returns total token supply', async function () {
  122. (await this.token.totalSupply()).should.be.bignumber.equal(2);
  123. });
  124. });
  125. describe('tokenOfOwnerByIndex', function () {
  126. describe('when the given index is lower than the amount of tokens owned by the given address', function () {
  127. it('returns the token ID placed at the given index', async function () {
  128. (await this.token.tokenOfOwnerByIndex(owner, 0)).should.be.bignumber.equal(firstTokenId);
  129. });
  130. });
  131. describe('when the index is greater than or equal to the total tokens owned by the given address', function () {
  132. it('reverts', async function () {
  133. await shouldFail.reverting(this.token.tokenOfOwnerByIndex(owner, 2));
  134. });
  135. });
  136. describe('when the given address does not own any token', function () {
  137. it('reverts', async function () {
  138. await shouldFail.reverting(this.token.tokenOfOwnerByIndex(another, 0));
  139. });
  140. });
  141. describe('after transferring all tokens to another user', function () {
  142. beforeEach(async function () {
  143. await this.token.transferFrom(owner, another, firstTokenId, { from: owner });
  144. await this.token.transferFrom(owner, another, secondTokenId, { from: owner });
  145. });
  146. it('returns correct token IDs for target', async function () {
  147. (await this.token.balanceOf(another)).toNumber().should.be.equal(2);
  148. const tokensListed = await Promise.all(
  149. [0, 1].map(i => this.token.tokenOfOwnerByIndex(another, i))
  150. );
  151. tokensListed.map(t => t.toNumber()).should.have.members([firstTokenId, secondTokenId]);
  152. });
  153. it('returns empty collection for original owner', async function () {
  154. (await this.token.balanceOf(owner)).toNumber().should.be.equal(0);
  155. await shouldFail.reverting(this.token.tokenOfOwnerByIndex(owner, 0));
  156. });
  157. });
  158. });
  159. describe('tokenByIndex', function () {
  160. it('should return all tokens', async function () {
  161. const tokensListed = await Promise.all(
  162. [0, 1].map(i => this.token.tokenByIndex(i))
  163. );
  164. tokensListed.map(t => t.toNumber()).should.have.members([firstTokenId, secondTokenId]);
  165. });
  166. it('should revert if index is greater than supply', async function () {
  167. await shouldFail.reverting(this.token.tokenByIndex(2));
  168. });
  169. [firstTokenId, secondTokenId].forEach(function (tokenId) {
  170. it(`should return all tokens after burning token ${tokenId} and minting new tokens`, async function () {
  171. const newTokenId = 300;
  172. const anotherNewTokenId = 400;
  173. await this.token.burn(tokenId, { from: owner });
  174. await this.token.mint(newOwner, newTokenId, { from: minter });
  175. await this.token.mint(newOwner, anotherNewTokenId, { from: minter });
  176. (await this.token.totalSupply()).toNumber().should.be.equal(3);
  177. const tokensListed = await Promise.all(
  178. [0, 1, 2].map(i => this.token.tokenByIndex(i))
  179. );
  180. const expectedTokens = [firstTokenId, secondTokenId, newTokenId, anotherNewTokenId].filter(
  181. x => (x !== tokenId)
  182. );
  183. tokensListed.map(t => t.toNumber()).should.have.members(expectedTokens);
  184. });
  185. });
  186. });
  187. });
  188. shouldBehaveLikeERC721(creator, minter, accounts);
  189. shouldSupportInterfaces([
  190. 'ERC165',
  191. 'ERC721',
  192. 'ERC721Enumerable',
  193. 'ERC721Metadata',
  194. ]);
  195. });