ERC721Consecutive.t.sol 6.4 KB

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