ERC721Consecutive.test.js 7.2 KB

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