| add love contract 9bf25a7 julienbrg 10h ago | 1 | // SPDX-License-Identifier: MIT |
| 2 | pragma solidity ^0.8.30; |
| 3 | |
| 4 | import {Love} from "../src/Love.sol"; |
| make love an erc20 with public mint 4c8048e julienbrg 10h ago | 5 | import {IERC20Errors} from "@openzeppelin/contracts/interfaces/draft-IERC6093.sol"; |
| 6 | import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; |
| add love contract 9bf25a7 julienbrg 10h ago | 7 | import {Test} from "forge-std/Test.sol"; |
| 8 | |
| 9 | contract LoveTest is Test { |
| 10 | Love public love; |
| 11 | |
| 12 | address alice = makeAddr("alice"); |
| 13 | address bob = makeAddr("bob"); |
| make love an erc20 with public mint 4c8048e julienbrg 10h ago | 14 | address stranger = makeAddr("stranger"); |
| add love contract 9bf25a7 julienbrg 10h ago | 15 | |
| 16 | function setUp() public { |
| 17 | love = new Love(); |
| 18 | } |
| 19 | |
| make love an erc20 with public mint 4c8048e julienbrg 10h ago | 20 | /*////////////////////////////////////////////////////////////// |
| 21 | METADATA |
| 22 | //////////////////////////////////////////////////////////////*/ |
| 23 | |
| 24 | function test_Metadata() public view { |
| 25 | assertEq(love.name(), "Love"); |
| 26 | assertEq(love.symbol(), "LOVE"); |
| 27 | assertEq(love.decimals(), 18); |
| 28 | } |
| 29 | |
| 30 | function test_InitialSupplyIsZero() public view { |
| 31 | assertEq(love.totalSupply(), 0); |
| 32 | } |
| 33 | |
| 34 | /*////////////////////////////////////////////////////////////// |
| 35 | MINT |
| 36 | //////////////////////////////////////////////////////////////*/ |
| 37 | |
| 38 | function test_Mint() public { |
| add love contract 9bf25a7 julienbrg 10h ago | 39 | vm.prank(alice); |
| make love an erc20 with public mint 4c8048e julienbrg 10h ago | 40 | love.mint(alice, 100e18); |
| 41 | |
| 42 | assertEq(love.balanceOf(alice), 100e18); |
| 43 | assertEq(love.totalSupply(), 100e18); |
| 44 | } |
| 45 | |
| 46 | /// @dev The point of this contract: `mint` has no access control. |
| 47 | function test_AnyoneCanMintToAnyone() public { |
| 48 | vm.prank(stranger); |
| 49 | love.mint(bob, 1000e18); |
| add love contract 9bf25a7 julienbrg 10h ago | 50 | |
| make love an erc20 with public mint 4c8048e julienbrg 10h ago | 51 | assertEq(love.balanceOf(bob), 1000e18); |
| 52 | assertEq(love.balanceOf(stranger), 0); |
| 53 | assertEq(love.totalSupply(), 1000e18); |
| add love contract 9bf25a7 julienbrg 10h ago | 54 | } |
| 55 | |
| make love an erc20 with public mint 4c8048e julienbrg 10h ago | 56 | function test_MintEmitsTransferFromZero() public { |
| 57 | vm.expectEmit(true, true, false, true); |
| 58 | emit IERC20.Transfer(address(0), alice, 5e18); |
| 59 | |
| 60 | vm.prank(alice); |
| 61 | love.mint(alice, 5e18); |
| 62 | } |
| 63 | |
| 64 | function test_MintAccumulates() public { |
| add love contract 9bf25a7 julienbrg 10h ago | 65 | vm.startPrank(alice); |
| make love an erc20 with public mint 4c8048e julienbrg 10h ago | 66 | love.mint(alice, 1e18); |
| 67 | love.mint(alice, 2e18); |
| add love contract 9bf25a7 julienbrg 10h ago | 68 | vm.stopPrank(); |
| 69 | |
| make love an erc20 with public mint 4c8048e julienbrg 10h ago | 70 | assertEq(love.balanceOf(alice), 3e18); |
| 71 | assertEq(love.totalSupply(), 3e18); |
| add love contract 9bf25a7 julienbrg 10h ago | 72 | } |
| 73 | |
| make love an erc20 with public mint 4c8048e julienbrg 10h ago | 74 | function test_RevertWhen_MintingToZeroAddress() public { |
| 75 | vm.expectRevert(abi.encodeWithSelector(IERC20Errors.ERC20InvalidReceiver.selector, address(0))); |
| 76 | love.mint(address(0), 1e18); |
| 77 | } |
| 78 | |
| 79 | function test_RevertWhen_MintOverflowsTotalSupply() public { |
| 80 | love.mint(alice, type(uint256).max); |
| 81 | |
| 82 | vm.expectRevert(); |
| 83 | love.mint(bob, 1); |
| 84 | } |
| 85 | |
| 86 | /*////////////////////////////////////////////////////////////// |
| 87 | TRANSFER |
| 88 | //////////////////////////////////////////////////////////////*/ |
| 89 | |
| 90 | function test_Transfer() public { |
| 91 | love.mint(alice, 10e18); |
| 92 | |
| 93 | vm.prank(alice); |
| 94 | assertTrue(love.transfer(bob, 4e18)); |
| 95 | |
| 96 | assertEq(love.balanceOf(alice), 6e18); |
| 97 | assertEq(love.balanceOf(bob), 4e18); |
| 98 | } |
| 99 | |
| 100 | function test_RevertWhen_TransferExceedsBalance() public { |
| 101 | love.mint(alice, 1e18); |
| add love contract 9bf25a7 julienbrg 10h ago | 102 | |
| 103 | vm.prank(alice); |
| make love an erc20 with public mint 4c8048e julienbrg 10h ago | 104 | vm.expectRevert(abi.encodeWithSelector(IERC20Errors.ERC20InsufficientBalance.selector, alice, 1e18, 2e18)); |
| 105 | // forge-lint: disable-next-line(erc20-unchecked-transfer) |
| 106 | love.transfer(bob, 2e18); |
| add love contract 9bf25a7 julienbrg 10h ago | 107 | } |
| 108 | |
| make love an erc20 with public mint 4c8048e julienbrg 10h ago | 109 | /*////////////////////////////////////////////////////////////// |
| 110 | APPROVE / TRANSFERFROM |
| 111 | //////////////////////////////////////////////////////////////*/ |
| 112 | |
| 113 | function test_ApproveAndTransferFrom() public { |
| 114 | love.mint(alice, 10e18); |
| 115 | |
| add love contract 9bf25a7 julienbrg 10h ago | 116 | vm.prank(alice); |
| make love an erc20 with public mint 4c8048e julienbrg 10h ago | 117 | assertTrue(love.approve(bob, 6e18)); |
| 118 | assertEq(love.allowance(alice, bob), 6e18); |
| 119 | |
| 120 | vm.prank(bob); |
| 121 | assertTrue(love.transferFrom(alice, stranger, 6e18)); |
| 122 | |
| 123 | assertEq(love.balanceOf(alice), 4e18); |
| 124 | assertEq(love.balanceOf(stranger), 6e18); |
| 125 | assertEq(love.allowance(alice, bob), 0); |
| add love contract 9bf25a7 julienbrg 10h ago | 126 | } |
| 127 | |
| make love an erc20 with public mint 4c8048e julienbrg 10h ago | 128 | function test_RevertWhen_TransferFromExceedsAllowance() public { |
| 129 | love.mint(alice, 10e18); |
| 130 | |
| add love contract 9bf25a7 julienbrg 10h ago | 131 | vm.prank(alice); |
| make love an erc20 with public mint 4c8048e julienbrg 10h ago | 132 | love.approve(bob, 1e18); |
| 133 | |
| 134 | vm.prank(bob); |
| 135 | vm.expectRevert(abi.encodeWithSelector(IERC20Errors.ERC20InsufficientAllowance.selector, bob, 1e18, 2e18)); |
| 136 | // forge-lint: disable-next-line(erc20-unchecked-transfer) |
| 137 | love.transferFrom(alice, stranger, 2e18); |
| 138 | } |
| 139 | |
| 140 | /*////////////////////////////////////////////////////////////// |
| 141 | FUZZ |
| 142 | //////////////////////////////////////////////////////////////*/ |
| 143 | |
| 144 | function testFuzz_MintByAnyCaller(address caller, address to, uint256 amount) public { |
| 145 | vm.assume(to != address(0)); |
| 146 | |
| 147 | vm.prank(caller); |
| 148 | love.mint(to, amount); |
| 149 | |
| 150 | assertEq(love.balanceOf(to), amount); |
| 151 | assertEq(love.totalSupply(), amount); |
| add love contract 9bf25a7 julienbrg 10h ago | 152 | } |
| 153 | |
| make love an erc20 with public mint 4c8048e julienbrg 10h ago | 154 | function testFuzz_Transfer(uint256 minted, uint256 amount) public { |
| 155 | amount = bound(amount, 0, minted); |
| add love contract 9bf25a7 julienbrg 10h ago | 156 | |
| make love an erc20 with public mint 4c8048e julienbrg 10h ago | 157 | love.mint(alice, minted); |
| 158 | |
| 159 | vm.prank(alice); |
| 160 | assertTrue(love.transfer(bob, amount)); |
| add love contract 9bf25a7 julienbrg 10h ago | 161 | |
| make love an erc20 with public mint 4c8048e julienbrg 10h ago | 162 | assertEq(love.balanceOf(alice), minted - amount); |
| 163 | assertEq(love.balanceOf(bob), amount); |
| add love contract 9bf25a7 julienbrg 10h ago | 164 | } |
| 165 | } |