| Merge 1-add-main-logic into main dded29a rickub 6h ago | 1 | // SPDX-License-Identifier: MIT |
| 2 | pragma solidity ^0.8.30; |
| 3 | |
| 4 | import {LoveScript} from "../script/Love.s.sol"; |
| 5 | import {Love} from "../src/Love.sol"; |
| 6 | import {Test} from "forge-std/Test.sol"; |
| 7 | |
| 8 | /// @notice The token is meant to live at the same address on every EVM network, |
| 9 | /// so these tests pin the CREATE2 deployment path and the inputs the |
| 10 | /// address derives from. |
| 11 | contract LoveCreate2Test is Test { |
| 12 | bytes32 constant SALT = keccak256("LOVE"); |
| 13 | |
| 14 | LoveScript script; |
| 15 | |
| 16 | function setUp() public { |
| 17 | script = new LoveScript(); |
| 18 | |
| 19 | // The canonical deterministic deployer, as it exists on live networks. |
| 20 | assertGt(CREATE2_FACTORY.code.length, 0, "create2 deployer missing"); |
| 21 | } |
| 22 | |
| 23 | function test_DeploysAtPredictedAddress() public { |
| 24 | address predicted = vm.computeCreate2Address(SALT, keccak256(type(Love).creationCode), CREATE2_FACTORY); |
| 25 | |
| 26 | Love deployed = Love(_deploy(SALT)); |
| 27 | |
| 28 | assertEq(address(deployed), predicted); |
| 29 | assertEq(deployed.symbol(), "LOVE"); |
| 30 | } |
| 31 | |
| 32 | /// @dev What the deploy script prints must be what the chain gives back. |
| 33 | function test_ScriptPredictionMatchesDeployment() public { |
| 34 | assertEq(script.predict(SALT), _deploy(SALT)); |
| 35 | } |
| 36 | |
| 37 | function test_ScriptUsesKeccakOfLoveAsDefaultSalt() public view { |
| 38 | assertEq(script.DEFAULT_SALT(), SALT); |
| 39 | } |
| 40 | |
| 41 | /// @dev Same salt, same code, same address — that is the whole point. |
| 42 | function test_PredictionIsChainAgnostic() public { |
| 43 | address onThisChain = script.predict(SALT); |
| 44 | |
| 45 | vm.chainId(137); |
| 46 | assertEq(script.predict(SALT), onThisChain); |
| 47 | |
| 48 | vm.chainId(42_161); |
| 49 | assertEq(script.predict(SALT), onThisChain); |
| 50 | } |
| 51 | |
| 52 | function test_DifferentSaltsGiveDifferentAddresses() public view { |
| 53 | assertTrue(script.predict(SALT) != script.predict(keccak256("LOVE2"))); |
| 54 | } |
| 55 | |
| 56 | /// @dev Redeploying with the same salt must fail, not silently return the |
| 57 | /// existing token. |
| 58 | function test_RevertWhen_RedeployingWithSameSalt() public { |
| 59 | _deploy(SALT); |
| 60 | |
| 61 | (bool ok,) = CREATE2_FACTORY.call(abi.encodePacked(SALT, type(Love).creationCode)); |
| 62 | assertFalse(ok); |
| 63 | } |
| 64 | |
| 65 | function testFuzz_PredictionMatchesDeployment(bytes32 salt) public { |
| 66 | assertEq(script.predict(salt), _deploy(salt)); |
| 67 | } |
| 68 | |
| 69 | function _deploy(bytes32 salt) internal returns (address deployed) { |
| 70 | (bool ok, bytes memory ret) = CREATE2_FACTORY.call(abi.encodePacked(salt, type(Love).creationCode)); |
| 71 | require(ok, "create2 deployment failed"); |
| 72 | // casting to 'bytes20' is safe because the deployer returns the 20-byte address, raw |
| 73 | // forge-lint: disable-next-line(unsafe-typecast) |
| 74 | deployed = address(bytes20(ret)); |
| 75 | } |
| 76 | } |