ERC721Consecutive.t.sol 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.8.0;
  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. uint256 public totalMinted = 0;
  13. constructor(address[] memory receivers, uint256[] memory batches) ERC721("", "") {
  14. for (uint256 i = 0; i < batches.length; i++) {
  15. address receiver = receivers[i % receivers.length];
  16. uint96 batchSize = uint96(bound(batches[i], 0, _maxBatchSize()));
  17. _mintConsecutive(receiver, batchSize);
  18. totalMinted += batchSize;
  19. }
  20. }
  21. function burn(uint256 tokenId) public {
  22. _burn(tokenId);
  23. }
  24. }
  25. contract ERC721ConsecutiveTest is Test {
  26. function test_balance(address receiver, uint256[] calldata batches) public {
  27. vm.assume(receiver != address(0));
  28. ERC721ConsecutiveTarget token = new ERC721ConsecutiveTarget(toSingleton(receiver), batches);
  29. assertEq(token.balanceOf(receiver), token.totalMinted());
  30. }
  31. function test_ownership(address receiver, uint256[] calldata batches, uint256[2] calldata unboundedTokenId) public {
  32. vm.assume(receiver != address(0));
  33. ERC721ConsecutiveTarget token = new ERC721ConsecutiveTarget(toSingleton(receiver), batches);
  34. if (token.totalMinted() > 0) {
  35. uint256 validTokenId = bound(unboundedTokenId[0], 0, token.totalMinted() - 1);
  36. assertEq(token.ownerOf(validTokenId), receiver);
  37. }
  38. uint256 invalidTokenId = bound(unboundedTokenId[1], token.totalMinted(), type(uint256).max);
  39. vm.expectRevert();
  40. token.ownerOf(invalidTokenId);
  41. }
  42. function test_burn(address receiver, uint256[] calldata batches, uint256 unboundedTokenId) public {
  43. vm.assume(receiver != address(0));
  44. ERC721ConsecutiveTarget token = new ERC721ConsecutiveTarget(toSingleton(receiver), batches);
  45. // only test if we minted at least one token
  46. uint256 supply = token.totalMinted();
  47. vm.assume(supply > 0);
  48. // burn a token in [0; supply[
  49. uint256 tokenId = bound(unboundedTokenId, 0, supply - 1);
  50. token.burn(tokenId);
  51. // balance should have decreased
  52. assertEq(token.balanceOf(receiver), supply - 1);
  53. // token should be burnt
  54. vm.expectRevert();
  55. token.ownerOf(tokenId);
  56. }
  57. function test_transfer(
  58. address[2] calldata accounts,
  59. uint256[2] calldata unboundedBatches,
  60. uint256[2] calldata unboundedTokenId
  61. ) public {
  62. vm.assume(accounts[0] != address(0));
  63. vm.assume(accounts[1] != address(0));
  64. vm.assume(accounts[0] != accounts[1]);
  65. address[] memory receivers = new address[](2);
  66. receivers[0] = accounts[0];
  67. receivers[1] = accounts[1];
  68. // We assume _maxBatchSize is 5000 (the default). This test will break otherwise.
  69. uint256[] memory batches = new uint256[](2);
  70. batches[0] = bound(unboundedBatches[0], 1, 5000);
  71. batches[1] = bound(unboundedBatches[1], 1, 5000);
  72. ERC721ConsecutiveTarget token = new ERC721ConsecutiveTarget(receivers, batches);
  73. uint256 tokenId0 = bound(unboundedTokenId[0], 0, batches[0] - 1);
  74. uint256 tokenId1 = bound(unboundedTokenId[1], 0, batches[1] - 1) + batches[0];
  75. assertEq(token.ownerOf(tokenId0), accounts[0]);
  76. assertEq(token.ownerOf(tokenId1), accounts[1]);
  77. assertEq(token.balanceOf(accounts[0]), batches[0]);
  78. assertEq(token.balanceOf(accounts[1]), batches[1]);
  79. vm.prank(accounts[0]);
  80. token.transferFrom(accounts[0], accounts[1], tokenId0);
  81. assertEq(token.ownerOf(tokenId0), accounts[1]);
  82. assertEq(token.ownerOf(tokenId1), accounts[1]);
  83. assertEq(token.balanceOf(accounts[0]), batches[0] - 1);
  84. assertEq(token.balanceOf(accounts[1]), batches[1] + 1);
  85. vm.prank(accounts[1]);
  86. token.transferFrom(accounts[1], accounts[0], tokenId1);
  87. assertEq(token.ownerOf(tokenId0), accounts[1]);
  88. assertEq(token.ownerOf(tokenId1), accounts[0]);
  89. assertEq(token.balanceOf(accounts[0]), batches[0]);
  90. assertEq(token.balanceOf(accounts[1]), batches[1]);
  91. }
  92. }