ERC721Consecutive.test.js 7.8 KB

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