|
| 1 | +// SPDX-License-Identifier: AGPL-3.0 |
| 2 | +pragma solidity >=0.8.0 <0.9.0; |
| 3 | + |
| 4 | +// inspired by https://github.com/aviggiano/halmos-stateful-erc20 |
| 5 | + |
| 6 | +import {ERC20} from "../src/ERC20.sol"; |
| 7 | +import {Test} from "forge-std/Test.sol"; |
| 8 | + |
| 9 | +/// @custom:halmos --solver bitwuzla-abs --loop 3 --solver-timeout-assertion 0 |
| 10 | +contract ERC20Test is Test { |
| 11 | + // token address |
| 12 | + ERC20 internal token; |
| 13 | + |
| 14 | + // token holders |
| 15 | + address[] internal holders; |
| 16 | + |
| 17 | + function setUp() public virtual { |
| 18 | + // deploy token |
| 19 | + token = new ERC20("ERC20", "ERC20"); |
| 20 | + |
| 21 | + // declare token holders |
| 22 | + holders = new address[](3); |
| 23 | + holders[0] = address(0x1001); |
| 24 | + holders[1] = address(0x1002); |
| 25 | + holders[2] = address(0x1003); |
| 26 | + |
| 27 | + // setup initial balances |
| 28 | + for (uint i = 0; i < holders.length; i++) { |
| 29 | + token.mint(holders[i], 1_000_000e18); |
| 30 | + } |
| 31 | + |
| 32 | + // register this contract as the target to call the handler functions |
| 33 | + targetContract(address(this)); |
| 34 | + } |
| 35 | + |
| 36 | + /* |
| 37 | + * handlers |
| 38 | + * |
| 39 | + * handlers are used to track token holders, allowing us to iterate over |
| 40 | + * them when calculating the sum of balances for specifying invariants. |
| 41 | + * for simplicity, in this example, only the initial token holders are |
| 42 | + * allowed as recipients of tokens. |
| 43 | + */ |
| 44 | + |
| 45 | + function transfer(address to, uint256 amount) public asCaller assumeHolder(to) returns (bool) { |
| 46 | + return token.transfer(to, amount); |
| 47 | + } |
| 48 | + |
| 49 | + function transferFrom(address from, address to, uint256 amount) public asCaller assumeHolder(to) returns (bool) { |
| 50 | + return token.transferFrom(from, to, amount); |
| 51 | + } |
| 52 | + |
| 53 | + function mint(address to, uint256 amount) public asCaller assumeHolder(to) { |
| 54 | + token.mint(to, amount); |
| 55 | + } |
| 56 | + |
| 57 | + function burn(address from, uint256 amount) public asCaller { |
| 58 | + token.burn(from, amount); |
| 59 | + } |
| 60 | + |
| 61 | + function approve(address spender, uint256 amount) public asCaller returns (bool) { |
| 62 | + return token.approve(spender, amount); |
| 63 | + } |
| 64 | + |
| 65 | + /* |
| 66 | + * helpers |
| 67 | + */ |
| 68 | + |
| 69 | + modifier asCaller() { |
| 70 | + vm.startPrank(msg.sender); |
| 71 | + _; |
| 72 | + vm.stopPrank(); |
| 73 | + } |
| 74 | + |
| 75 | + modifier assumeHolder(address account) { |
| 76 | + vm.assume(_contains(holders, account)); |
| 77 | + _; |
| 78 | + } |
| 79 | + |
| 80 | + function _contains(address[] storage array, address value) internal view returns (bool) { |
| 81 | + for (uint256 i = 0; i < array.length; i++) { |
| 82 | + if (array[i] == value) { |
| 83 | + return true; |
| 84 | + } |
| 85 | + } |
| 86 | + return false; |
| 87 | + } |
| 88 | + |
| 89 | + // invariant: the sum of balances must equal the total supply |
| 90 | + function invariant_sumOfBalancesEqualsTotalSupply() public view { |
| 91 | + uint256 sumOfBalances = 0; |
| 92 | + for (uint256 i = 0; i < holders.length; i++) { |
| 93 | + sumOfBalances += token.balanceOf(holders[i]); |
| 94 | + } |
| 95 | + assertEq(sumOfBalances, token.totalSupply()); |
| 96 | + } |
| 97 | +} |
0 commit comments