ERC721Consecutive.test.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. const { constants, expectEvent, expectRevert } = require('@openzeppelin/test-helpers');
  2. const { expect } = require('chai');
  3. const ERC721ConsecutiveMock = artifacts.require('ERC721ConsecutiveMock');
  4. const ERC721ConsecutiveEnumerableMock = artifacts.require('ERC721ConsecutiveEnumerableMock');
  5. const ERC721ConsecutiveNoConstructorMintMock = artifacts.require('ERC721ConsecutiveNoConstructorMintMock');
  6. contract('ERC721Consecutive', function (accounts) {
  7. const [ user1, user2, user3, receiver ] = accounts;
  8. const name = 'Non Fungible Token';
  9. const symbol = 'NFT';
  10. const batches = [
  11. { receiver: user1, amount: 0 },
  12. { receiver: user1, amount: 3 },
  13. { receiver: user2, amount: 5 },
  14. { receiver: user3, amount: 0 },
  15. { receiver: user1, amount: 7 },
  16. ];
  17. const delegates = [ user1, user3 ];
  18. describe('with valid batches', function () {
  19. beforeEach(async function () {
  20. this.token = await ERC721ConsecutiveMock.new(
  21. name,
  22. symbol,
  23. delegates,
  24. batches.map(({ receiver }) => receiver),
  25. batches.map(({ amount }) => amount),
  26. );
  27. });
  28. describe('minting during construction', function () {
  29. it('events are emitted at construction', async function () {
  30. let first = 0;
  31. for (const batch of batches) {
  32. if (batch.amount > 0) {
  33. await expectEvent.inConstruction(this.token, 'ConsecutiveTransfer', {
  34. fromTokenId: web3.utils.toBN(first),
  35. toTokenId: web3.utils.toBN(first + batch.amount - 1),
  36. fromAddress: constants.ZERO_ADDRESS,
  37. toAddress: batch.receiver,
  38. });
  39. } else {
  40. // expectEvent.notEmitted.inConstruction only looks at event name, and doesn't check the parameters
  41. }
  42. first += batch.amount;
  43. }
  44. });
  45. it('ownership is set', async function () {
  46. const owners = batches.flatMap(({ receiver, amount }) => Array(amount).fill(receiver));
  47. for (const tokenId in owners) {
  48. expect(await this.token.ownerOf(tokenId))
  49. .to.be.equal(owners[tokenId]);
  50. }
  51. });
  52. it('balance & voting power are set', async function () {
  53. for (const account of accounts) {
  54. const balance = batches
  55. .filter(({ receiver }) => receiver === account)
  56. .map(({ amount }) => amount)
  57. .reduce((a, b) => a + b, 0);
  58. expect(await this.token.balanceOf(account))
  59. .to.be.bignumber.equal(web3.utils.toBN(balance));
  60. // If not delegated at construction, check before + do delegation
  61. if (!delegates.includes(account)) {
  62. expect(await this.token.getVotes(account))
  63. .to.be.bignumber.equal(web3.utils.toBN(0));
  64. await this.token.delegate(account, { from: account });
  65. }
  66. // At this point all accounts should have delegated
  67. expect(await this.token.getVotes(account))
  68. .to.be.bignumber.equal(web3.utils.toBN(balance));
  69. }
  70. });
  71. });
  72. describe('minting after construction', function () {
  73. it('consecutive minting is not possible after construction', async function () {
  74. await expectRevert(
  75. this.token.mintConsecutive(user1, 10),
  76. 'ERC721Consecutive: batch minting restricted to constructor',
  77. );
  78. });
  79. it('simple minting is possible after construction', async function () {
  80. const tokenId = batches.reduce((acc, { amount }) => acc + amount, 0);
  81. expect(await this.token.exists(tokenId)).to.be.equal(false);
  82. expectEvent(
  83. await this.token.mint(user1, tokenId),
  84. 'Transfer',
  85. { from: constants.ZERO_ADDRESS, to: user1, tokenId: tokenId.toString() },
  86. );
  87. });
  88. it('cannot mint a token that has been batched minted', async function () {
  89. const tokenId = batches.reduce((acc, { amount }) => acc + amount, 0) - 1;
  90. expect(await this.token.exists(tokenId)).to.be.equal(true);
  91. await expectRevert(
  92. this.token.mint(user1, tokenId),
  93. 'ERC721: token already minted',
  94. );
  95. });
  96. });
  97. describe('ERC721 behavior', function () {
  98. it('core takes over ownership on transfer', async function () {
  99. await this.token.transferFrom(user1, receiver, 1, { from: user1 });
  100. expect(await this.token.ownerOf(1)).to.be.equal(receiver);
  101. });
  102. it('tokens can be burned and re-minted #1', async function () {
  103. expectEvent(
  104. await this.token.burn(1, { from: user1 }),
  105. 'Transfer',
  106. { from: user1, to: constants.ZERO_ADDRESS, tokenId: '1' },
  107. );
  108. await expectRevert(this.token.ownerOf(1), 'ERC721: invalid token ID');
  109. expectEvent(
  110. await this.token.mint(user2, 1),
  111. 'Transfer',
  112. { from: constants.ZERO_ADDRESS, to: user2, tokenId: '1' },
  113. );
  114. expect(await this.token.ownerOf(1)).to.be.equal(user2);
  115. });
  116. it('tokens can be burned and re-minted #2', async function () {
  117. const tokenId = batches.reduce((acc, { amount }) => acc.addn(amount), web3.utils.toBN(0));
  118. expect(await this.token.exists(tokenId)).to.be.equal(false);
  119. await expectRevert(this.token.ownerOf(tokenId), 'ERC721: invalid token ID');
  120. // mint
  121. await this.token.mint(user1, tokenId);
  122. expect(await this.token.exists(tokenId)).to.be.equal(true);
  123. expect(await this.token.ownerOf(tokenId), user1);
  124. // burn
  125. expectEvent(
  126. await this.token.burn(tokenId, { from: user1 }),
  127. 'Transfer',
  128. { from: user1, to: constants.ZERO_ADDRESS, tokenId },
  129. );
  130. expect(await this.token.exists(tokenId)).to.be.equal(false);
  131. await expectRevert(this.token.ownerOf(tokenId), 'ERC721: invalid token ID');
  132. // re-mint
  133. expectEvent(
  134. await this.token.mint(user2, tokenId),
  135. 'Transfer',
  136. { from: constants.ZERO_ADDRESS, to: user2, tokenId },
  137. );
  138. expect(await this.token.exists(tokenId)).to.be.equal(true);
  139. expect(await this.token.ownerOf(tokenId), user2);
  140. });
  141. });
  142. });
  143. describe('invalid use', function () {
  144. it('cannot mint a batch larger than 5000', async function () {
  145. await expectRevert(
  146. ERC721ConsecutiveMock.new(
  147. name,
  148. symbol,
  149. [],
  150. [user1],
  151. ['5001'],
  152. ),
  153. 'ERC721Consecutive: batch too large',
  154. );
  155. });
  156. it('cannot use single minting during construction', async function () {
  157. await expectRevert(
  158. ERC721ConsecutiveNoConstructorMintMock.new(
  159. name,
  160. symbol,
  161. ),
  162. 'ERC721Consecutive: can\'t mint during construction',
  163. );
  164. });
  165. it('cannot use single minting during construction', async function () {
  166. await expectRevert(
  167. ERC721ConsecutiveNoConstructorMintMock.new(
  168. name,
  169. symbol,
  170. ),
  171. 'ERC721Consecutive: can\'t mint during construction',
  172. );
  173. });
  174. it('consecutive mint not compatible with enumerability', async function () {
  175. await expectRevert(
  176. ERC721ConsecutiveEnumerableMock.new(
  177. name,
  178. symbol,
  179. batches.map(({ receiver }) => receiver),
  180. batches.map(({ amount }) => amount),
  181. ),
  182. 'ERC721Enumerable: consecutive transfers not supported',
  183. );
  184. });
  185. });
  186. });