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