| Merge 1-add-main-logic into main dded29a rickub 10h 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 | |
| Merge 3-peg-love-to-weth into main ce13509 rickub 9h ago | 8 | /// @title LoveCreate2Test |
| 9 | /// @notice The deployment address derives from the salt, the creation code and |
| 10 | /// the wETH constructor argument. These tests pin that derivation: same |
| 11 | /// salt and same wETH give the same address on any chain, a different |
| 12 | /// wETH gives a different one. |
| Merge 1-add-main-logic into main dded29a rickub 10h ago | 13 | contract LoveCreate2Test is Test { |
| 14 | bytes32 constant SALT = keccak256("LOVE"); |
| Merge 3-peg-love-to-weth into main ce13509 rickub 9h ago | 15 | address constant WETH = 0x4200000000000000000000000000000000000006; |
| Merge 1-add-main-logic into main dded29a rickub 10h ago | 16 | |
| 17 | LoveScript script; |
| 18 | |
| 19 | function setUp() public { |
| 20 | script = new LoveScript(); |
| 21 | |
| 22 | // The canonical deterministic deployer, as it exists on live networks. |
| 23 | assertGt(CREATE2_FACTORY.code.length, 0, "create2 deployer missing"); |
| 24 | } |
| 25 | |
| 26 | function test_DeploysAtPredictedAddress() public { |
| Merge 3-peg-love-to-weth into main ce13509 rickub 9h ago | 27 | address predicted = vm.computeCreate2Address(SALT, keccak256(script.initCode(WETH)), CREATE2_FACTORY); |
| Merge 1-add-main-logic into main dded29a rickub 10h ago | 28 | |
| Merge 3-peg-love-to-weth into main ce13509 rickub 9h ago | 29 | Love deployed = Love(_deploy(SALT, WETH)); |
| Merge 1-add-main-logic into main dded29a rickub 10h ago | 30 | |
| 31 | assertEq(address(deployed), predicted); |
| 32 | assertEq(deployed.symbol(), "LOVE"); |
| Merge 3-peg-love-to-weth into main ce13509 rickub 9h ago | 33 | assertEq(address(deployed.WETH()), WETH); |
| Merge 1-add-main-logic into main dded29a rickub 10h ago | 34 | } |
| 35 | |
| 36 | /// @dev What the deploy script prints must be what the chain gives back. |
| 37 | function test_ScriptPredictionMatchesDeployment() public { |
| Merge 3-peg-love-to-weth into main ce13509 rickub 9h ago | 38 | assertEq(script.predict(SALT, WETH), _deploy(SALT, WETH)); |
| Merge 1-add-main-logic into main dded29a rickub 10h ago | 39 | } |
| 40 | |
| 41 | function test_ScriptUsesKeccakOfLoveAsDefaultSalt() public view { |
| 42 | assertEq(script.DEFAULT_SALT(), SALT); |
| 43 | } |
| 44 | |
| Merge 3-peg-love-to-weth into main ce13509 rickub 9h ago | 45 | function test_ScriptDefaultsToOpStackWeth() public view { |
| 46 | assertEq(script.DEFAULT_WETH(), WETH); |
| 47 | } |
| 48 | |
| 49 | /// @dev Same salt, same code, same wETH, same address. |
| Merge 1-add-main-logic into main dded29a rickub 10h ago | 50 | function test_PredictionIsChainAgnostic() public { |
| Merge 3-peg-love-to-weth into main ce13509 rickub 9h ago | 51 | address onThisChain = script.predict(SALT, WETH); |
| Merge 1-add-main-logic into main dded29a rickub 10h ago | 52 | |
| 53 | vm.chainId(137); |
| Merge 3-peg-love-to-weth into main ce13509 rickub 9h ago | 54 | assertEq(script.predict(SALT, WETH), onThisChain); |
| Merge 1-add-main-logic into main dded29a rickub 10h ago | 55 | |
| 56 | vm.chainId(42_161); |
| Merge 3-peg-love-to-weth into main ce13509 rickub 9h ago | 57 | assertEq(script.predict(SALT, WETH), onThisChain); |
| Merge 1-add-main-logic into main dded29a rickub 10h ago | 58 | } |
| 59 | |
| 60 | function test_DifferentSaltsGiveDifferentAddresses() public view { |
| Merge 3-peg-love-to-weth into main ce13509 rickub 9h ago | 61 | assertTrue(script.predict(SALT, WETH) != script.predict(keccak256("LOVE2"), WETH)); |
| 62 | } |
| 63 | |
| 64 | /// @dev The wETH address is part of the creation code, so chains with their |
| 65 | /// own wETH get their own token address. |
| 66 | function test_DifferentWethGivesDifferentAddress() public view { |
| 67 | assertTrue(script.predict(SALT, WETH) != script.predict(SALT, address(0xBEEF))); |
| Merge 1-add-main-logic into main dded29a rickub 10h ago | 68 | } |
| 69 | |
| 70 | /// @dev Redeploying with the same salt must fail, not silently return the |
| 71 | /// existing token. |
| 72 | function test_RevertWhen_RedeployingWithSameSalt() public { |
| Merge 3-peg-love-to-weth into main ce13509 rickub 9h ago | 73 | _deploy(SALT, WETH); |
| Merge 1-add-main-logic into main dded29a rickub 10h ago | 74 | |
| Merge 3-peg-love-to-weth into main ce13509 rickub 9h ago | 75 | (bool ok,) = CREATE2_FACTORY.call(abi.encodePacked(SALT, script.initCode(WETH))); |
| Merge 1-add-main-logic into main dded29a rickub 10h ago | 76 | assertFalse(ok); |
| 77 | } |
| 78 | |
| Merge 3-peg-love-to-weth into main ce13509 rickub 9h ago | 79 | function testFuzz_PredictionMatchesDeployment(bytes32 salt, address weth) public { |
| 80 | assertEq(script.predict(salt, weth), _deploy(salt, weth)); |
| Merge 1-add-main-logic into main dded29a rickub 10h ago | 81 | } |
| 82 | |
| Merge 3-peg-love-to-weth into main ce13509 rickub 9h ago | 83 | /// @dev Deploys through the canonical deterministic deployer, as the script does. |
| 84 | function _deploy(bytes32 salt, address weth) internal returns (address deployed) { |
| 85 | (bool ok, bytes memory ret) = CREATE2_FACTORY.call(abi.encodePacked(salt, script.initCode(weth))); |
| Merge 1-add-main-logic into main dded29a rickub 10h ago | 86 | require(ok, "create2 deployment failed"); |
| 87 | // casting to 'bytes20' is safe because the deployer returns the 20-byte address, raw |
| 88 | // forge-lint: disable-next-line(unsafe-typecast) |
| 89 | deployed = address(bytes20(ret)); |
| 90 | } |
| 91 | } |