julien/lovepublic Fork 0
5376202c13fb4103a21f603fecc7fd40680b607b
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 · 89 lines · 3.4 KBSolidity Blame HistoryRaw
Merge 1-add-main-logic into main dded29a rickub 10h 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
add weth peg tests and docs 5376202 julienbrg 9h ago8/// @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 ago12contract LoveCreate2Test is Test {
13 bytes32 constant SALT = keccak256("LOVE");
add weth peg tests and docs 5376202 julienbrg 9h ago14 address constant WETH = 0x4200000000000000000000000000000000000006;
Merge 1-add-main-logic into main dded29a rickub 10h ago15
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 ago26 address predicted = vm.computeCreate2Address(SALT, keccak256(script.initCode(WETH)), CREATE2_FACTORY);
Merge 1-add-main-logic into main dded29a rickub 10h ago27
add weth peg tests and docs 5376202 julienbrg 9h ago28 Love deployed = Love(_deploy(SALT, WETH));
Merge 1-add-main-logic into main dded29a rickub 10h ago29
30 assertEq(address(deployed), predicted);
31 assertEq(deployed.symbol(), "LOVE");
add weth peg tests and docs 5376202 julienbrg 9h ago32 assertEq(address(deployed.WETH()), WETH);
Merge 1-add-main-logic into main dded29a rickub 10h ago33 }
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 ago37 assertEq(script.predict(SALT, WETH), _deploy(SALT, WETH));
Merge 1-add-main-logic into main dded29a rickub 10h ago38 }
39
40 function test_ScriptUsesKeccakOfLoveAsDefaultSalt() public view {
41 assertEq(script.DEFAULT_SALT(), SALT);
42 }
43
add weth peg tests and docs 5376202 julienbrg 9h ago44 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 ago49 function test_PredictionIsChainAgnostic() public {
add weth peg tests and docs 5376202 julienbrg 9h ago50 address onThisChain = script.predict(SALT, WETH);
Merge 1-add-main-logic into main dded29a rickub 10h ago51
52 vm.chainId(137);
add weth peg tests and docs 5376202 julienbrg 9h ago53 assertEq(script.predict(SALT, WETH), onThisChain);
Merge 1-add-main-logic into main dded29a rickub 10h ago54
55 vm.chainId(42_161);
add weth peg tests and docs 5376202 julienbrg 9h ago56 assertEq(script.predict(SALT, WETH), onThisChain);
Merge 1-add-main-logic into main dded29a rickub 10h ago57 }
58
59 function test_DifferentSaltsGiveDifferentAddresses() public view {
add weth peg tests and docs 5376202 julienbrg 9h ago60 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 ago67 }
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 ago72 _deploy(SALT, WETH);
Merge 1-add-main-logic into main dded29a rickub 10h ago73
add weth peg tests and docs 5376202 julienbrg 9h ago74 (bool ok,) = CREATE2_FACTORY.call(abi.encodePacked(SALT, script.initCode(WETH)));
Merge 1-add-main-logic into main dded29a rickub 10h ago75 assertFalse(ok);
76 }
77
add weth peg tests and docs 5376202 julienbrg 9h ago78 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 ago80 }
81
add weth peg tests and docs 5376202 julienbrg 9h ago82 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 ago84 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}