ERC721Consecutive.test.js 7.3 KB

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