ERC721Consecutive.test.js 7.1 KB

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