ERC721Consecutive.test.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. const { constants, expectEvent, expectRevert } = require('@openzeppelin/test-helpers');
  2. const { expect } = require('chai');
  3. const { sum } = require('../../../helpers/math');
  4. const ERC721ConsecutiveMock = artifacts.require('$ERC721ConsecutiveMock');
  5. const ERC721ConsecutiveEnumerableMock = artifacts.require('$ERC721ConsecutiveEnumerableMock');
  6. const ERC721ConsecutiveNoConstructorMintMock = artifacts.require('$ERC721ConsecutiveNoConstructorMintMock');
  7. contract('ERC721Consecutive', function (accounts) {
  8. const [user1, user2, user3, receiver] = accounts;
  9. const name = 'Non Fungible Token';
  10. const symbol = 'NFT';
  11. const batches = [
  12. { receiver: user1, amount: 0 },
  13. { receiver: user1, amount: 3 },
  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 = sum(...batches.filter(({ receiver }) => receiver === account).map(({ amount }) => amount));
  55. expect(await this.token.balanceOf(account)).to.be.bignumber.equal(web3.utils.toBN(balance));
  56. // If not delegated at construction, check before + do delegation
  57. if (!delegates.includes(account)) {
  58. expect(await this.token.getVotes(account)).to.be.bignumber.equal(web3.utils.toBN(0));
  59. await this.token.delegate(account, { from: account });
  60. }
  61. // At this point all accounts should have delegated
  62. expect(await this.token.getVotes(account)).to.be.bignumber.equal(web3.utils.toBN(balance));
  63. }
  64. });
  65. });
  66. describe('minting after construction', function () {
  67. it('consecutive minting is not possible after construction', async function () {
  68. await expectRevert(
  69. this.token.$_mintConsecutive(user1, 10),
  70. 'ERC721Consecutive: batch minting restricted to constructor',
  71. );
  72. });
  73. it('simple minting is possible after construction', async function () {
  74. const tokenId = sum(...batches.map(b => b.amount));
  75. expect(await this.token.$_exists(tokenId)).to.be.equal(false);
  76. expectEvent(await this.token.$_mint(user1, tokenId), 'Transfer', {
  77. from: constants.ZERO_ADDRESS,
  78. to: user1,
  79. tokenId: tokenId.toString(),
  80. });
  81. });
  82. it('cannot mint a token that has been batched minted', async function () {
  83. const tokenId = sum(...batches.map(b => b.amount)) - 1;
  84. expect(await this.token.$_exists(tokenId)).to.be.equal(true);
  85. await expectRevert(this.token.$_mint(user1, tokenId), 'ERC721: token already minted');
  86. });
  87. });
  88. describe('ERC721 behavior', function () {
  89. it('core takes over ownership on transfer', async function () {
  90. await this.token.transferFrom(user1, receiver, 1, { from: user1 });
  91. expect(await this.token.ownerOf(1)).to.be.equal(receiver);
  92. });
  93. it('tokens can be burned and re-minted #1', async function () {
  94. expectEvent(await this.token.$_burn(1, { from: user1 }), 'Transfer', {
  95. from: user1,
  96. to: constants.ZERO_ADDRESS,
  97. tokenId: '1',
  98. });
  99. await expectRevert(this.token.ownerOf(1), 'ERC721: invalid token ID');
  100. expectEvent(await this.token.$_mint(user2, 1), 'Transfer', {
  101. from: constants.ZERO_ADDRESS,
  102. to: user2,
  103. tokenId: '1',
  104. });
  105. expect(await this.token.ownerOf(1)).to.be.equal(user2);
  106. });
  107. it('tokens can be burned and re-minted #2', async function () {
  108. const tokenId = web3.utils.toBN(sum(...batches.map(({ amount }) => amount)));
  109. expect(await this.token.$_exists(tokenId)).to.be.equal(false);
  110. await expectRevert(this.token.ownerOf(tokenId), 'ERC721: invalid token ID');
  111. // mint
  112. await this.token.$_mint(user1, tokenId);
  113. expect(await this.token.$_exists(tokenId)).to.be.equal(true);
  114. expect(await this.token.ownerOf(tokenId), user1);
  115. // burn
  116. expectEvent(await this.token.$_burn(tokenId, { from: user1 }), 'Transfer', {
  117. from: user1,
  118. to: constants.ZERO_ADDRESS,
  119. tokenId,
  120. });
  121. expect(await this.token.$_exists(tokenId)).to.be.equal(false);
  122. await expectRevert(this.token.ownerOf(tokenId), 'ERC721: invalid token ID');
  123. // re-mint
  124. expectEvent(await this.token.$_mint(user2, tokenId), 'Transfer', {
  125. from: constants.ZERO_ADDRESS,
  126. to: user2,
  127. tokenId,
  128. });
  129. expect(await this.token.$_exists(tokenId)).to.be.equal(true);
  130. expect(await this.token.ownerOf(tokenId), user2);
  131. });
  132. });
  133. });
  134. describe('invalid use', function () {
  135. it('cannot mint a batch larger than 5000', async function () {
  136. await expectRevert(
  137. ERC721ConsecutiveMock.new(name, symbol, [], [user1], ['5001']),
  138. 'ERC721Consecutive: batch too large',
  139. );
  140. });
  141. it('cannot use single minting during construction', async function () {
  142. await expectRevert(
  143. ERC721ConsecutiveNoConstructorMintMock.new(name, symbol),
  144. "ERC721Consecutive: can't mint during construction",
  145. );
  146. });
  147. it('cannot use single minting during construction', async function () {
  148. await expectRevert(
  149. ERC721ConsecutiveNoConstructorMintMock.new(name, symbol),
  150. "ERC721Consecutive: can't mint during construction",
  151. );
  152. });
  153. it('consecutive mint not compatible with enumerability', async function () {
  154. await expectRevert(
  155. ERC721ConsecutiveEnumerableMock.new(
  156. name,
  157. symbol,
  158. batches.map(({ receiver }) => receiver),
  159. batches.map(({ amount }) => amount),
  160. ),
  161. 'ERC721Enumerable: consecutive transfers not supported',
  162. );
  163. });
  164. });
  165. });