|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +pragma solidity ^0.8.4; |
| 3 | + |
| 4 | +import {Test} from "forge-std/Test.sol"; |
| 5 | +import {CodesenSysToken} from "../../src/tokens/CodesenSysToken.sol"; |
| 6 | +import {Constants} from "../../src/core/Constants.sol"; |
| 7 | +import {Errors} from "../../src/core/Errors.sol"; |
| 8 | + |
| 9 | +/// @notice Stateless fuzz properties for CodesenSysToken. |
| 10 | +/// |
| 11 | +/// @dev Unlike the unit tests (which fix inputs) and the invariant suite |
| 12 | +/// (which maintains state across calls), each test here is stateless: |
| 13 | +/// the fuzzer supplies fresh random inputs on every run and the test |
| 14 | +/// asserts a property that must hold universally. |
| 15 | +/// |
| 16 | +/// Properties covered: |
| 17 | +/// - Supply conservation across mint→transfer→burn sequences |
| 18 | +/// - Allowance accounting with arbitrary approve/spend pairs |
| 19 | +/// - Cap enforcement across the full mintable range |
| 20 | +/// - Balance symmetry after round-trip transfers |
| 21 | +contract ERC20FuzzTest is Test { |
| 22 | + CodesenSysToken public token; |
| 23 | + |
| 24 | + address public owner = makeAddr("owner"); |
| 25 | + address public alice = makeAddr("alice"); |
| 26 | + address public bob = makeAddr("bob"); |
| 27 | + address public carol = makeAddr("carol"); |
| 28 | + |
| 29 | + uint256 public constant INITIAL_MINT = Constants.INITIAL_SUPPLY; |
| 30 | + |
| 31 | + function setUp() public { |
| 32 | + token = new CodesenSysToken(owner, INITIAL_MINT); |
| 33 | + } |
| 34 | + |
| 35 | + // ========================================================================= |
| 36 | + // Supply conservation |
| 37 | + // ========================================================================= |
| 38 | + |
| 39 | + /// @notice Minting then burning the same amount leaves totalSupply unchanged. |
| 40 | + function testFuzz_Supply_MintThenBurnIsNeutral(uint256 amount) public { |
| 41 | + uint256 remaining = Constants.MAX_SUPPLY - token.totalSupply(); |
| 42 | + amount = bound(amount, 1, remaining); |
| 43 | + |
| 44 | + uint256 supplyBefore = token.totalSupply(); |
| 45 | + |
| 46 | + vm.prank(owner); |
| 47 | + token.mint(alice, amount); |
| 48 | + |
| 49 | + vm.prank(alice); |
| 50 | + token.burn(amount); |
| 51 | + |
| 52 | + assertEq(token.totalSupply(), supplyBefore, "supply changed after mint+burn"); |
| 53 | + } |
| 54 | + |
| 55 | + /// @notice A chain of transfers never changes totalSupply. |
| 56 | + function testFuzz_Supply_ChainedTransfersAreNeutral(uint256 amount) public { |
| 57 | + amount = bound(amount, 1, INITIAL_MINT); |
| 58 | + |
| 59 | + uint256 supplyBefore = token.totalSupply(); |
| 60 | + |
| 61 | + vm.prank(owner); |
| 62 | + assertTrue(token.transfer(alice, amount)); |
| 63 | + |
| 64 | + vm.prank(alice); |
| 65 | + assertTrue(token.transfer(bob, amount)); |
| 66 | + |
| 67 | + vm.prank(bob); |
| 68 | + assertTrue(token.transfer(carol, amount)); |
| 69 | + |
| 70 | + assertEq(token.totalSupply(), supplyBefore, "supply changed after chained transfers"); |
| 71 | + } |
| 72 | + |
| 73 | + /// @notice After a full round-trip (owner→alice→owner), both balances are restored. |
| 74 | + function testFuzz_Transfer_RoundTripRestoresBalances(uint256 amount) public { |
| 75 | + amount = bound(amount, 1, INITIAL_MINT); |
| 76 | + |
| 77 | + uint256 ownerBefore = token.balanceOf(owner); |
| 78 | + |
| 79 | + vm.prank(owner); |
| 80 | + assertTrue(token.transfer(alice, amount)); |
| 81 | + |
| 82 | + vm.prank(alice); |
| 83 | + assertTrue(token.transfer(owner, amount)); |
| 84 | + |
| 85 | + assertEq(token.balanceOf(owner), ownerBefore, "owner balance not restored"); |
| 86 | + assertEq(token.balanceOf(alice), 0, "alice balance not zero after round-trip"); |
| 87 | + } |
| 88 | + |
| 89 | + // ========================================================================= |
| 90 | + // Allowance accounting |
| 91 | + // ========================================================================= |
| 92 | + |
| 93 | + /// @notice After a partial spend, remaining allowance equals approve - spent. |
| 94 | + function testFuzz_Allowance_PartialSpendReducesExactly(uint256 approval, uint256 spend) public { |
| 95 | + approval = bound(approval, 1, INITIAL_MINT); |
| 96 | + spend = bound(spend, 1, approval); |
| 97 | + |
| 98 | + vm.prank(owner); |
| 99 | + assertTrue(token.approve(alice, approval)); |
| 100 | + |
| 101 | + vm.prank(alice); |
| 102 | + assertTrue(token.transferFrom(owner, bob, spend)); |
| 103 | + |
| 104 | + assertEq(token.allowance(owner, alice), approval - spend, "allowance not reduced correctly"); |
| 105 | + } |
| 106 | + |
| 107 | + /// @notice The last approve always wins, regardless of prior value. |
| 108 | + function testFuzz_Allowance_OverwriteAlwaysReflectsLastCall(uint256 first, uint256 second) public { |
| 109 | + vm.prank(owner); |
| 110 | + assertTrue(token.approve(alice, first)); |
| 111 | + |
| 112 | + vm.prank(owner); |
| 113 | + assertTrue(token.approve(alice, second)); |
| 114 | + |
| 115 | + assertEq(token.allowance(owner, alice), second, "allowance not overwritten"); |
| 116 | + } |
| 117 | + |
| 118 | + /// @notice transferFrom must revert when spend exceeds allowance by any amount. |
| 119 | + function testFuzz_Allowance_ExceedingAllowanceAlwaysReverts(uint256 approval, uint256 excess) public { |
| 120 | + approval = bound(approval, 0, INITIAL_MINT - 1); |
| 121 | + excess = bound(excess, 1, INITIAL_MINT - approval); |
| 122 | + |
| 123 | + vm.prank(owner); |
| 124 | + assertTrue(token.approve(alice, approval)); |
| 125 | + |
| 126 | + vm.prank(alice); |
| 127 | + vm.expectRevert(); |
| 128 | + token.transferFrom(owner, bob, approval + excess); // reverts — return value intentionally unchecked |
| 129 | + } |
| 130 | + |
| 131 | + // ========================================================================= |
| 132 | + // Cap enforcement |
| 133 | + // ========================================================================= |
| 134 | + |
| 135 | + /// @notice Any mint that would push totalSupply past MAX_SUPPLY must revert. |
| 136 | + function testFuzz_Cap_MintOverCapAlwaysReverts(uint256 overBy) public { |
| 137 | + overBy = bound(overBy, 1, type(uint128).max); |
| 138 | + |
| 139 | + uint256 remaining = Constants.MAX_SUPPLY - token.totalSupply(); |
| 140 | + |
| 141 | + vm.prank(owner); |
| 142 | + vm.expectRevert(Errors.ExceedsCap.selector); |
| 143 | + token.mint(alice, remaining + overBy); // reverts — return value intentionally unchecked |
| 144 | + } |
| 145 | + |
| 146 | + /// @notice Minting exactly up to MAX_SUPPLY must succeed. |
| 147 | + function testFuzz_Cap_MintExactlyToCapSucceeds(uint256 seed) public { |
| 148 | + // Deploy a fresh token so we can control the gap to the cap precisely. |
| 149 | + uint256 initial = bound(seed, 1, Constants.MAX_SUPPLY - 1); |
| 150 | + CodesenSysToken fresh = new CodesenSysToken(owner, initial); |
| 151 | + |
| 152 | + uint256 toMint = Constants.MAX_SUPPLY - initial; |
| 153 | + |
| 154 | + vm.prank(owner); |
| 155 | + fresh.mint(alice, toMint); |
| 156 | + |
| 157 | + assertEq(fresh.totalSupply(), Constants.MAX_SUPPLY, "supply did not reach cap"); |
| 158 | + } |
| 159 | + |
| 160 | + // ========================================================================= |
| 161 | + // Access control |
| 162 | + // ========================================================================= |
| 163 | + |
| 164 | + /// @notice Any non-owner address must be unable to mint. |
| 165 | + function testFuzz_Access_NonOwnerMintAlwaysReverts(address caller, uint256 amount) public { |
| 166 | + vm.assume(caller != owner); |
| 167 | + vm.assume(caller != address(0)); |
| 168 | + amount = bound(amount, 1, Constants.MAX_SUPPLY - token.totalSupply()); |
| 169 | + |
| 170 | + vm.prank(caller); |
| 171 | + vm.expectRevert(Errors.NotAuthorized.selector); |
| 172 | + token.mint(alice, amount); // reverts — return value intentionally unchecked |
| 173 | + } |
| 174 | +} |
0 commit comments