ERC721Consecutive.test.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. it('reverts on consecutive minting to the zero address', async function () {
  76. await expectRevertCustomError(
  77. ERC721ConsecutiveMock.new(name, symbol, offset, delegates, [ZERO_ADDRESS], [10]),
  78. 'ERC721InvalidReceiver',
  79. [ZERO_ADDRESS],
  80. );
  81. });
  82. });
  83. describe('minting after construction', function () {
  84. it('consecutive minting is not possible after construction', async function () {
  85. await expectRevertCustomError(this.token.$_mintConsecutive(user1, 10), 'ERC721ForbiddenBatchMint', []);
  86. });
  87. it('simple minting is possible after construction', async function () {
  88. const tokenId = sum(...batches.map(b => b.amount)) + offset;
  89. await expectRevertCustomError(this.token.ownerOf(tokenId), 'ERC721NonexistentToken', [tokenId]);
  90. expectEvent(await this.token.$_mint(user1, tokenId), 'Transfer', {
  91. from: constants.ZERO_ADDRESS,
  92. to: user1,
  93. tokenId: tokenId.toString(),
  94. });
  95. });
  96. it('cannot mint a token that has been batched minted', async function () {
  97. const tokenId = sum(...batches.map(b => b.amount)) + offset - 1;
  98. expect(await this.token.ownerOf(tokenId)).to.be.not.equal(constants.ZERO_ADDRESS);
  99. await expectRevertCustomError(this.token.$_mint(user1, tokenId), 'ERC721InvalidSender', [ZERO_ADDRESS]);
  100. });
  101. });
  102. describe('ERC721 behavior', function () {
  103. const tokenId = web3.utils.toBN(offset + 1);
  104. it('core takes over ownership on transfer', async function () {
  105. await this.token.transferFrom(user1, receiver, tokenId, { from: user1 });
  106. expect(await this.token.ownerOf(tokenId)).to.be.equal(receiver);
  107. });
  108. it('tokens can be burned and re-minted #1', async function () {
  109. expectEvent(await this.token.$_burn(tokenId, { from: user1 }), 'Transfer', {
  110. from: user1,
  111. to: constants.ZERO_ADDRESS,
  112. tokenId,
  113. });
  114. await expectRevertCustomError(this.token.ownerOf(tokenId), 'ERC721NonexistentToken', [tokenId]);
  115. expectEvent(await this.token.$_mint(user2, tokenId), 'Transfer', {
  116. from: constants.ZERO_ADDRESS,
  117. to: user2,
  118. tokenId,
  119. });
  120. expect(await this.token.ownerOf(tokenId)).to.be.equal(user2);
  121. });
  122. it('tokens can be burned and re-minted #2', async function () {
  123. const tokenId = web3.utils.toBN(sum(...batches.map(({ amount }) => amount)) + offset);
  124. await expectRevertCustomError(this.token.ownerOf(tokenId), 'ERC721NonexistentToken', [tokenId]);
  125. // mint
  126. await this.token.$_mint(user1, tokenId);
  127. expect(await this.token.ownerOf(tokenId), user1);
  128. // burn
  129. expectEvent(await this.token.$_burn(tokenId, { from: user1 }), 'Transfer', {
  130. from: user1,
  131. to: constants.ZERO_ADDRESS,
  132. tokenId,
  133. });
  134. await expectRevertCustomError(this.token.ownerOf(tokenId), 'ERC721NonexistentToken', [tokenId]);
  135. // re-mint
  136. expectEvent(await this.token.$_mint(user2, tokenId), 'Transfer', {
  137. from: constants.ZERO_ADDRESS,
  138. to: user2,
  139. tokenId,
  140. });
  141. expect(await this.token.ownerOf(tokenId), user2);
  142. });
  143. });
  144. });
  145. }
  146. describe('invalid use', function () {
  147. it('cannot mint a batch larger than 5000', async function () {
  148. await expectRevertCustomError(
  149. ERC721ConsecutiveMock.new(name, symbol, 0, [], [user1], ['5001']),
  150. 'ERC721ExceededMaxBatchMint',
  151. [5000, 5001],
  152. );
  153. });
  154. it('cannot use single minting during construction', async function () {
  155. await expectRevertCustomError(
  156. ERC721ConsecutiveNoConstructorMintMock.new(name, symbol),
  157. 'ERC721ForbiddenMint',
  158. [],
  159. );
  160. });
  161. it('cannot use single minting during construction', async function () {
  162. await expectRevertCustomError(
  163. ERC721ConsecutiveNoConstructorMintMock.new(name, symbol),
  164. 'ERC721ForbiddenMint',
  165. [],
  166. );
  167. });
  168. it('consecutive mint not compatible with enumerability', async function () {
  169. await expectRevertCustomError(
  170. ERC721ConsecutiveEnumerableMock.new(
  171. name,
  172. symbol,
  173. batches.map(({ receiver }) => receiver),
  174. batches.map(({ amount }) => amount),
  175. ),
  176. 'ERC721EnumerableForbiddenBatchMint',
  177. [],
  178. );
  179. });
  180. });
  181. });