ERC721Consecutive.test.js 7.9 KB

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