ERC721Consecutive.test.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. expect(await this.token.$_exists(tokenId)).to.be.equal(false);
  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.$_exists(tokenId)).to.be.equal(true);
  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. expect(await this.token.$_exists(tokenId)).to.be.equal(false);
  125. await expectRevertCustomError(this.token.ownerOf(tokenId), 'ERC721NonexistentToken', [tokenId]);
  126. // mint
  127. await this.token.$_mint(user1, tokenId);
  128. expect(await this.token.$_exists(tokenId)).to.be.equal(true);
  129. expect(await this.token.ownerOf(tokenId), user1);
  130. // burn
  131. expectEvent(await this.token.$_burn(tokenId, { from: user1 }), 'Transfer', {
  132. from: user1,
  133. to: constants.ZERO_ADDRESS,
  134. tokenId,
  135. });
  136. expect(await this.token.$_exists(tokenId)).to.be.equal(false);
  137. await expectRevertCustomError(this.token.ownerOf(tokenId), 'ERC721NonexistentToken', [tokenId]);
  138. // re-mint
  139. expectEvent(await this.token.$_mint(user2, tokenId), 'Transfer', {
  140. from: constants.ZERO_ADDRESS,
  141. to: user2,
  142. tokenId,
  143. });
  144. expect(await this.token.$_exists(tokenId)).to.be.equal(true);
  145. expect(await this.token.ownerOf(tokenId), user2);
  146. });
  147. it('reverts burning batches of size != 1', async function () {
  148. const tokenId = batches[0].amount + offset;
  149. const receiver = batches[0].receiver;
  150. await expectRevertCustomError(
  151. this.token.$_afterTokenTransfer(receiver, ZERO_ADDRESS, tokenId, 2),
  152. 'ERC721ForbiddenBatchBurn',
  153. [],
  154. );
  155. });
  156. });
  157. });
  158. }
  159. describe('invalid use', function () {
  160. it('cannot mint a batch larger than 5000', async function () {
  161. await expectRevertCustomError(
  162. ERC721ConsecutiveMock.new(name, symbol, 0, [], [user1], ['5001']),
  163. 'ERC721ExceededMaxBatchMint',
  164. [5000, 5001],
  165. );
  166. });
  167. it('cannot use single minting during construction', async function () {
  168. await expectRevertCustomError(
  169. ERC721ConsecutiveNoConstructorMintMock.new(name, symbol),
  170. 'ERC721ForbiddenMint',
  171. [],
  172. );
  173. });
  174. it('cannot use single minting during construction', async function () {
  175. await expectRevertCustomError(
  176. ERC721ConsecutiveNoConstructorMintMock.new(name, symbol),
  177. 'ERC721ForbiddenMint',
  178. [],
  179. );
  180. });
  181. it('consecutive mint not compatible with enumerability', async function () {
  182. await expectRevertCustomError(
  183. ERC721ConsecutiveEnumerableMock.new(
  184. name,
  185. symbol,
  186. batches.map(({ receiver }) => receiver),
  187. batches.map(({ amount }) => amount),
  188. ),
  189. 'ERC721EnumerableForbiddenBatchMint',
  190. [],
  191. );
  192. });
  193. });
  194. });