ERC721Consecutive.t.sol 4.4 KB

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