julien/lovepublic Fork 0
ce13509fab5b401d787ec19eb5198d7a4a4cbfc9
Commits
Clone
git clone https://git.rickub.com/julien/love.git
git clone ssh://git@rickub.com/julien/love.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

LoveCreate2.t.sol · 91 lines · 3.5 KBSolidity Blame HistoryRaw
Merge 1-add-main-logic into main dded29a rickub 8h ago1// SPDX-License-Identifier: MIT
2pragma solidity ^0.8.30;
3
4import {LoveScript} from "../script/Love.s.sol";
5import {Love} from "../src/Love.sol";
6import {Test} from "forge-std/Test.sol";
7
Merge 3-peg-love-to-weth into main ce13509 rickub 7h ago8/// @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 8h ago13contract LoveCreate2Test is Test {
14 bytes32 constant SALT = keccak256("LOVE");
Merge 3-peg-love-to-weth into main ce13509 rickub 7h ago15 address constant WETH = 0x4200000000000000000000000000000000000006;
Merge 1-add-main-logic into main dded29a rickub 8h ago16
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 7h ago27 address predicted = vm.computeCreate2Address(SALT, keccak256(script.initCode(WETH)), CREATE2_FACTORY);
Merge 1-add-main-logic into main dded29a rickub 8h ago28
Merge 3-peg-love-to-weth into main ce13509 rickub 7h ago29 Love deployed = Love(_deploy(SALT, WETH));
Merge 1-add-main-logic into main dded29a rickub 8h ago30
31 assertEq(address(deployed), predicted);
32 assertEq(deployed.symbol(), "LOVE");
Merge 3-peg-love-to-weth into main ce13509 rickub 7h ago33 assertEq(address(deployed.WETH()), WETH);
Merge 1-add-main-logic into main dded29a rickub 8h ago34 }
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 7h ago38 assertEq(script.predict(SALT, WETH), _deploy(SALT, WETH));
Merge 1-add-main-logic into main dded29a rickub 8h ago39 }
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 7h ago45 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 8h ago50 function test_PredictionIsChainAgnostic() public {
Merge 3-peg-love-to-weth into main ce13509 rickub 7h ago51 address onThisChain = script.predict(SALT, WETH);
Merge 1-add-main-logic into main dded29a rickub 8h ago52
53 vm.chainId(137);
Merge 3-peg-love-to-weth into main ce13509 rickub 7h ago54 assertEq(script.predict(SALT, WETH), onThisChain);
Merge 1-add-main-logic into main dded29a rickub 8h ago55
56 vm.chainId(42_161);
Merge 3-peg-love-to-weth into main ce13509 rickub 7h ago57 assertEq(script.predict(SALT, WETH), onThisChain);
Merge 1-add-main-logic into main dded29a rickub 8h ago58 }
59
60 function test_DifferentSaltsGiveDifferentAddresses() public view {
Merge 3-peg-love-to-weth into main ce13509 rickub 7h ago61 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 8h ago68 }
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 7h ago73 _deploy(SALT, WETH);
Merge 1-add-main-logic into main dded29a rickub 8h ago74
Merge 3-peg-love-to-weth into main ce13509 rickub 7h ago75 (bool ok,) = CREATE2_FACTORY.call(abi.encodePacked(SALT, script.initCode(WETH)));
Merge 1-add-main-logic into main dded29a rickub 8h ago76 assertFalse(ok);
77 }
78
Merge 3-peg-love-to-weth into main ce13509 rickub 7h ago79 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 8h ago81 }
82
Merge 3-peg-love-to-weth into main ce13509 rickub 7h ago83 /// @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 8h ago86 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}