ERC721Consecutive.test.js 7.1 KB

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