|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | + |
| 3 | +pragma solidity^0.8.0; |
| 4 | + |
| 5 | +import"../../../../contracts/token/ERC721/extensions/ERC721Consecutive.sol"; |
| 6 | +import"forge-std/Test.sol"; |
| 7 | + |
| 8 | +function toSingleton(addressaccount) purereturns (address[] memory){ |
| 9 | +address[] memory accounts =newaddress[](1); |
| 10 | + accounts[0] = account; |
| 11 | +return accounts; |
| 12 | +} |
| 13 | + |
| 14 | +contractERC721ConsecutiveTargetisStdUtils, ERC721Consecutive{ |
| 15 | +uint256public totalMinted =0; |
| 16 | + |
| 17 | +constructor(address[] memoryreceivers, uint256[] memorybatches) ERC721("", ""){ |
| 18 | +for (uint256 i =0; i < batches.length; i++){ |
| 19 | +address receiver = receivers[i % receivers.length]; |
| 20 | +uint96 batchSize =uint96(bound(batches[i], 0, _maxBatchSize())); |
| 21 | +_mintConsecutive(receiver, batchSize); |
| 22 | + totalMinted += batchSize; |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | +function burn(uint256tokenId) public{ |
| 27 | +_burn(tokenId); |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +contractERC721ConsecutiveTestisTest{ |
| 32 | +function test_balance(addressreceiver, uint256[] calldatabatches) public{ |
| 33 | + vm.assume(receiver !=address(0)); |
| 34 | + |
| 35 | +ERC721ConsecutiveTarget token =newERC721ConsecutiveTarget(toSingleton(receiver), batches); |
| 36 | + |
| 37 | +assertEq(token.balanceOf(receiver), token.totalMinted()); |
| 38 | + } |
| 39 | + |
| 40 | +function test_ownership(addressreceiver, uint256[] calldatabatches, uint256[2] calldataunboundedTokenId) public{ |
| 41 | + vm.assume(receiver !=address(0)); |
| 42 | + |
| 43 | +ERC721ConsecutiveTarget token =newERC721ConsecutiveTarget(toSingleton(receiver), batches); |
| 44 | + |
| 45 | +if (token.totalMinted() >0){ |
| 46 | +uint256 validTokenId =bound(unboundedTokenId[0], 0, token.totalMinted() -1); |
| 47 | +assertEq(token.ownerOf(validTokenId), receiver); |
| 48 | + } |
| 49 | + |
| 50 | +uint256 invalidTokenId =bound(unboundedTokenId[1], token.totalMinted(), type(uint256).max); |
| 51 | + vm.expectRevert(); |
| 52 | + token.ownerOf(invalidTokenId); |
| 53 | + } |
| 54 | + |
| 55 | +function test_burn(addressreceiver, uint256[] calldatabatches, uint256unboundedTokenId) public{ |
| 56 | + vm.assume(receiver !=address(0)); |
| 57 | + |
| 58 | +ERC721ConsecutiveTarget token =newERC721ConsecutiveTarget(toSingleton(receiver), batches); |
| 59 | + |
| 60 | +// only test if we minted at least one token |
| 61 | +uint256 supply = token.totalMinted(); |
| 62 | + vm.assume(supply >0); |
| 63 | + |
| 64 | +// burn a token in [0; supply[ |
| 65 | +uint256 tokenId =bound(unboundedTokenId, 0, supply -1); |
| 66 | + token.burn(tokenId); |
| 67 | + |
| 68 | +// balance should have decreased |
| 69 | +assertEq(token.balanceOf(receiver), supply -1); |
| 70 | + |
| 71 | +// token should be burnt |
| 72 | + vm.expectRevert(); |
| 73 | + token.ownerOf(tokenId); |
| 74 | + } |
| 75 | + |
| 76 | +function test_transfer( |
| 77 | +address[2] calldataaccounts, |
| 78 | +uint256[2] calldataunboundedBatches, |
| 79 | +uint256[2] calldataunboundedTokenId |
| 80 | + ) public{ |
| 81 | + vm.assume(accounts[0] !=address(0)); |
| 82 | + vm.assume(accounts[1] !=address(0)); |
| 83 | + vm.assume(accounts[0] != accounts[1]); |
| 84 | + |
| 85 | +address[] memory receivers =newaddress[](2); |
| 86 | + receivers[0] = accounts[0]; |
| 87 | + receivers[1] = accounts[1]; |
| 88 | + |
| 89 | +// We assume _maxBatchSize is 5000 (the default). This test will break otherwise. |
| 90 | +uint256[] memory batches =newuint256[](2); |
| 91 | + batches[0] =bound(unboundedBatches[0], 1, 5000); |
| 92 | + batches[1] =bound(unboundedBatches[1], 1, 5000); |
| 93 | + |
| 94 | +ERC721ConsecutiveTarget token =newERC721ConsecutiveTarget(receivers, batches); |
| 95 | + |
| 96 | +uint256 tokenId0 =bound(unboundedTokenId[0], 0, batches[0] -1); |
| 97 | +uint256 tokenId1 =bound(unboundedTokenId[1], 0, batches[1] -1) + batches[0]; |
| 98 | + |
| 99 | +assertEq(token.ownerOf(tokenId0), accounts[0]); |
| 100 | +assertEq(token.ownerOf(tokenId1), accounts[1]); |
| 101 | +assertEq(token.balanceOf(accounts[0]), batches[0]); |
| 102 | +assertEq(token.balanceOf(accounts[1]), batches[1]); |
| 103 | + |
| 104 | + vm.prank(accounts[0]); |
| 105 | + token.transferFrom(accounts[0], accounts[1], tokenId0); |
| 106 | + |
| 107 | +assertEq(token.ownerOf(tokenId0), accounts[1]); |
| 108 | +assertEq(token.ownerOf(tokenId1), accounts[1]); |
| 109 | +assertEq(token.balanceOf(accounts[0]), batches[0] -1); |
| 110 | +assertEq(token.balanceOf(accounts[1]), batches[1] +1); |
| 111 | + |
| 112 | + vm.prank(accounts[1]); |
| 113 | + token.transferFrom(accounts[1], accounts[0], tokenId1); |
| 114 | + |
| 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 | +} |
0 commit comments