ERC721Consecutive.test.js 7.7 KB

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