ERC721Consecutive.t.sol 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.8.20;
  3. // solhint-disable func-name-mixedcase
  4. import {ERC721} from "@openzeppelin/contracts/token/ERC721/ERC721.sol";
  5. import {ERC721Consecutive} from "@openzeppelin/contracts/token/ERC721/extensions/ERC721Consecutive.sol";
  6. import {Test, StdUtils} from "forge-std/Test.sol";
  7. function toSingleton(address account) pure returns (address[] memory) {
  8. address[] memory accounts = new address[](1);
  9. accounts[0] = account;
  10. return accounts;
  11. }
  12. contract ERC721ConsecutiveTarget is StdUtils, ERC721Consecutive {
  13. uint96 private immutable _offset;
  14. uint256 public totalMinted = 0;
  15. constructor(address[] memory receivers, uint256[] memory batches, uint256 startingId) ERC721("", "") {
  16. _offset = uint96(startingId);
  17. for (uint256 i = 0; i < batches.length; i++) {
  18. address receiver = receivers[i % receivers.length];
  19. uint96 batchSize = uint96(bound(batches[i], 0, _maxBatchSize()));
  20. _mintConsecutive(receiver, batchSize);
  21. totalMinted += batchSize;
  22. }
  23. }
  24. function burn(uint256 tokenId) public {
  25. _burn(tokenId);
  26. }
  27. function _firstConsecutiveId() internal view virtual override returns (uint96) {
  28. return _offset;
  29. }
  30. }
  31. contract ERC721ConsecutiveTest is Test {
  32. function test_balance(address receiver, uint256[] calldata batches, uint96 startingId) public {
  33. vm.assume(receiver != address(0));
  34. uint256 startingTokenId = bound(startingId, 0, 5000);
  35. ERC721ConsecutiveTarget token = new ERC721ConsecutiveTarget(toSingleton(receiver), batches, startingTokenId);
  36. assertEq(token.balanceOf(receiver), token.totalMinted());
  37. }
  38. function test_ownership(
  39. address receiver,
  40. uint256[] calldata batches,
  41. uint256[2] calldata unboundedTokenId,
  42. uint96 startingId
  43. ) public {
  44. vm.assume(receiver != address(0));
  45. uint256 startingTokenId = bound(startingId, 0, 5000);
  46. ERC721ConsecutiveTarget token = new ERC721ConsecutiveTarget(toSingleton(receiver), batches, startingTokenId);
  47. if (token.totalMinted() > 0) {
  48. uint256 validTokenId = bound(
  49. unboundedTokenId[0],
  50. startingTokenId,
  51. startingTokenId + token.totalMinted() - 1
  52. );
  53. assertEq(token.ownerOf(validTokenId), receiver);
  54. }
  55. uint256 invalidTokenId = bound(
  56. unboundedTokenId[1],
  57. startingTokenId + token.totalMinted(),
  58. startingTokenId + token.totalMinted() + 1
  59. );
  60. vm.expectRevert();
  61. token.ownerOf(invalidTokenId);
  62. }
  63. function test_burn(
  64. address receiver,
  65. uint256[] calldata batches,
  66. uint256 unboundedTokenId,
  67. uint96 startingId
  68. ) public {
  69. vm.assume(receiver != address(0));
  70. uint256 startingTokenId = bound(startingId, 0, 5000);
  71. ERC721ConsecutiveTarget token = new ERC721ConsecutiveTarget(toSingleton(receiver), batches, startingTokenId);
  72. // only test if we minted at least one token
  73. uint256 supply = token.totalMinted();
  74. vm.assume(supply > 0);
  75. // burn a token in [0; supply[
  76. uint256 tokenId = bound(unboundedTokenId, startingTokenId, startingTokenId + supply - 1);
  77. token.burn(tokenId);
  78. // balance should have decreased
  79. assertEq(token.balanceOf(receiver), supply - 1);
  80. // token should be burnt
  81. vm.expectRevert();
  82. token.ownerOf(tokenId);
  83. }
  84. function test_transfer(
  85. address[2] calldata accounts,
  86. uint256[2] calldata unboundedBatches,
  87. uint256[2] calldata unboundedTokenId,
  88. uint96 startingId
  89. ) public {
  90. vm.assume(accounts[0] != address(0));
  91. vm.assume(accounts[1] != address(0));
  92. vm.assume(accounts[0] != accounts[1]);
  93. uint256 startingTokenId = bound(startingId, 1, 5000);
  94. address[] memory receivers = new address[](2);
  95. receivers[0] = accounts[0];
  96. receivers[1] = accounts[1];
  97. // We assume _maxBatchSize is 5000 (the default). This test will break otherwise.
  98. uint256[] memory batches = new uint256[](2);
  99. batches[0] = bound(unboundedBatches[0], startingTokenId, 5000);
  100. batches[1] = bound(unboundedBatches[1], startingTokenId, 5000);
  101. ERC721ConsecutiveTarget token = new ERC721ConsecutiveTarget(receivers, batches, startingTokenId);
  102. uint256 tokenId0 = bound(unboundedTokenId[0], startingTokenId, batches[0]);
  103. uint256 tokenId1 = bound(unboundedTokenId[1], startingTokenId, batches[1]) + batches[0];
  104. assertEq(token.ownerOf(tokenId0), accounts[0]);
  105. assertEq(token.ownerOf(tokenId1), accounts[1]);
  106. assertEq(token.balanceOf(accounts[0]), batches[0]);
  107. assertEq(token.balanceOf(accounts[1]), batches[1]);
  108. vm.prank(accounts[0]);
  109. token.transferFrom(accounts[0], accounts[1], tokenId0);
  110. assertEq(token.ownerOf(tokenId0), accounts[1]);
  111. assertEq(token.ownerOf(tokenId1), accounts[1]);
  112. assertEq(token.balanceOf(accounts[0]), batches[0] - 1);
  113. assertEq(token.balanceOf(accounts[1]), batches[1] + 1);
  114. vm.prank(accounts[1]);
  115. token.transferFrom(accounts[1], accounts[0], tokenId1);
  116. assertEq(token.ownerOf(tokenId0), accounts[1]);
  117. assertEq(token.ownerOf(tokenId1), accounts[0]);
  118. assertEq(token.balanceOf(accounts[0]), batches[0]);
  119. assertEq(token.balanceOf(accounts[1]), batches[1]);
  120. }
  121. function test_start_consecutive_id(
  122. address receiver,
  123. uint256[2] calldata unboundedBatches,
  124. uint256[2] calldata unboundedTokenId,
  125. uint96 startingId
  126. ) public {
  127. vm.assume(receiver != address(0));
  128. uint256 startingTokenId = bound(startingId, 1, 5000);
  129. // We assume _maxBatchSize is 5000 (the default). This test will break otherwise.
  130. uint256[] memory batches = new uint256[](2);
  131. batches[0] = bound(unboundedBatches[0], startingTokenId, 5000);
  132. batches[1] = bound(unboundedBatches[1], startingTokenId, 5000);
  133. ERC721ConsecutiveTarget token = new ERC721ConsecutiveTarget(toSingleton(receiver), batches, startingTokenId);
  134. uint256 tokenId0 = bound(unboundedTokenId[0], startingTokenId, batches[0]);
  135. uint256 tokenId1 = bound(unboundedTokenId[1], startingTokenId, batches[1]);
  136. assertEq(token.ownerOf(tokenId0), receiver);
  137. assertEq(token.ownerOf(tokenId1), receiver);
  138. assertEq(token.balanceOf(receiver), batches[0] + batches[1]);
  139. }
  140. }