julien/lovepublic Fork 0
dded29a03d3e4395f369c9e2c8fc829fba287322
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 · 76 lines · 2.7 KBSolidity Blame HistoryRaw
Merge 1-add-main-logic into main dded29a rickub 9h 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
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.
11contract 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}