ERC721Consecutive.test.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. const { constants, expectEvent, expectRevert } = require('@openzeppelin/test-helpers');
  2. const { expect } = require('chai');
  3. const ERC721ConsecutiveMock = artifacts.require('$ERC721ConsecutiveMock');
  4. const ERC721ConsecutiveEnumerableMock = artifacts.require('$ERC721ConsecutiveEnumerableMock');
  5. const ERC721ConsecutiveNoConstructorMintMock = artifacts.require('$ERC721ConsecutiveNoConstructorMintMock');
  6. contract('ERC721Consecutive', function (accounts) {
  7. const [user1, user2, user3, receiver] = accounts;
  8. const name = 'Non Fungible Token';
  9. const symbol = 'NFT';
  10. const batches = [
  11. { receiver: user1, amount: 0 },
  12. { receiver: user1, amount: 1 },
  13. { receiver: user1, amount: 2 },
  14. { receiver: user2, amount: 5 },
  15. { receiver: user3, amount: 0 },
  16. { receiver: user1, amount: 7 },
  17. ];
  18. const delegates = [user1, user3];
  19. for (const offset of [0, 1, 42]) {
  20. describe(`with offset ${offset}`, function () {
  21. beforeEach(async function () {
  22. this.token = await ERC721ConsecutiveMock.new(
  23. name,
  24. symbol,
  25. offset,
  26. delegates,
  27. batches.map(({ receiver }) => receiver),
  28. batches.map(({ amount }) => amount),
  29. );
  30. });
  31. describe('minting during construction', function () {
  32. it('events are emitted at construction', async function () {
  33. let first = offset;
  34. for (const batch of batches) {
  35. if (batch.amount > 0) {
  36. await expectEvent.inConstruction(this.token, 'ConsecutiveTransfer', {
  37. fromTokenId: web3.utils.toBN(first),
  38. toTokenId: web3.utils.toBN(first + batch.amount - 1),
  39. fromAddress: constants.ZERO_ADDRESS,
  40. toAddress: batch.receiver,
  41. });
  42. } else {
  43. // expectEvent.notEmitted.inConstruction only looks at event name, and doesn't check the parameters
  44. }
  45. first += batch.amount;
  46. }
  47. });
  48. it('ownership is set', async function () {
  49. const owners = [
  50. ...Array(offset).fill(constants.ZERO_ADDRESS),
  51. ...batches.flatMap(({ receiver, amount }) => Array(amount).fill(receiver)),
  52. ];
  53. for (const tokenId in owners) {
  54. if (owners[tokenId] != constants.ZERO_ADDRESS) {
  55. expect(await this.token.ownerOf(tokenId)).to.be.equal(owners[tokenId]);
  56. }
  57. }
  58. });
  59. it('balance & voting power are set', async function () {
  60. for (const account of accounts) {
  61. const balance = batches
  62. .filter(({ receiver }) => receiver === account)
  63. .map(({ amount }) => amount)
  64. .reduce((a, b) => a + b, 0);
  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 expectRevert(
  79. this.token.$_mintConsecutive(user1, 10),
  80. 'ERC721Consecutive: batch minting restricted to constructor',
  81. );
  82. });
  83. it('simple minting is possible after construction', async function () {
  84. const tokenId = batches.reduce((acc, { amount }) => acc + amount, offset);
  85. expect(await this.token.$_exists(tokenId)).to.be.equal(false);
  86. expectEvent(await this.token.$_mint(user1, tokenId), 'Transfer', {
  87. from: constants.ZERO_ADDRESS,
  88. to: user1,
  89. tokenId: tokenId.toString(),
  90. });
  91. });
  92. it('cannot mint a token that has been batched minted', async function () {
  93. const tokenId = batches.reduce((acc, { amount }) => acc + amount, offset) - 1;
  94. expect(await this.token.$_exists(tokenId)).to.be.equal(true);
  95. await expectRevert(this.token.$_mint(user1, tokenId), 'ERC721: token already minted');
  96. });
  97. });
  98. describe('ERC721 behavior', function () {
  99. const tokenId = web3.utils.toBN(offset + 1);
  100. it('core takes over ownership on transfer', async function () {
  101. await this.token.transferFrom(user1, receiver, tokenId, { from: user1 });
  102. expect(await this.token.ownerOf(tokenId)).to.be.equal(receiver);
  103. });
  104. it('tokens can be burned and re-minted #1', async function () {
  105. expectEvent(await this.token.$_burn(tokenId, { from: user1 }), 'Transfer', {
  106. from: user1,
  107. to: constants.ZERO_ADDRESS,
  108. tokenId,
  109. });
  110. await expectRevert(this.token.ownerOf(tokenId), 'ERC721: invalid token ID');
  111. expectEvent(await this.token.$_mint(user2, tokenId), 'Transfer', {
  112. from: constants.ZERO_ADDRESS,
  113. to: user2,
  114. tokenId,
  115. });
  116. expect(await this.token.ownerOf(tokenId)).to.be.equal(user2);
  117. });
  118. it('tokens can be burned and re-minted #2', async function () {
  119. const tokenId = web3.utils.toBN(batches.reduce((acc, { amount }) => acc + amount, offset));
  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(await this.token.$_burn(tokenId, { from: user1 }), 'Transfer', {
  128. from: user1,
  129. to: constants.ZERO_ADDRESS,
  130. 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(await this.token.$_mint(user2, tokenId), 'Transfer', {
  136. from: constants.ZERO_ADDRESS,
  137. to: user2,
  138. 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. }
  146. describe('invalid use', function () {
  147. it('cannot mint a batch larger than 5000', async function () {
  148. await expectRevert(
  149. ERC721ConsecutiveMock.new(name, symbol, 0, [], [user1], ['5001']),
  150. 'ERC721Consecutive: batch too large',
  151. );
  152. });
  153. it('cannot use single minting during construction', async function () {
  154. await expectRevert(
  155. ERC721ConsecutiveNoConstructorMintMock.new(name, symbol),
  156. "ERC721Consecutive: can't mint during construction",
  157. );
  158. });
  159. it('cannot use single minting during construction', async function () {
  160. await expectRevert(
  161. ERC721ConsecutiveNoConstructorMintMock.new(name, symbol),
  162. "ERC721Consecutive: can't mint during construction",
  163. );
  164. });
  165. it('consecutive mint not compatible with enumerability', async function () {
  166. await expectRevert(
  167. ERC721ConsecutiveEnumerableMock.new(
  168. name,
  169. symbol,
  170. batches.map(({ receiver }) => receiver),
  171. batches.map(({ amount }) => amount),
  172. ),
  173. 'ERC721Enumerable: consecutive transfers not supported',
  174. );
  175. });
  176. });
  177. });