julien/lovepublic Fork 0
3-peg-love-to-weth
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 6h 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 natspec 3cc0f3b julienbrg 5h ago8/// @title LoveCreate2Test
add weth peg tests and docs 5376202 julienbrg 5h ago9/// @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 6h ago13contract LoveCreate2Test is Test {
14 bytes32 constant SALT = keccak256("LOVE");
add weth peg tests and docs 5376202 julienbrg 5h ago15 address constant WETH = 0x4200000000000000000000000000000000000006;
Merge 1-add-main-logic into main dded29a rickub 6h 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 {
add weth peg tests and docs 5376202 julienbrg 5h ago27 address predicted = vm.computeCreate2Address(SALT, keccak256(script.initCode(WETH)), CREATE2_FACTORY);
Merge 1-add-main-logic into main dded29a rickub 6h ago28
add weth peg tests and docs 5376202 julienbrg 5h ago29 Love deployed = Love(_deploy(SALT, WETH));
Merge 1-add-main-logic into main dded29a rickub 6h ago30
31 assertEq(address(deployed), predicted);
32 assertEq(deployed.symbol(), "LOVE");
add weth peg tests and docs 5376202 julienbrg 5h ago33 assertEq(address(deployed.WETH()), WETH);
Merge 1-add-main-logic into main dded29a rickub 6h ago34 }
35
36 /// @dev What the deploy script prints must be what the chain gives back.
37 function test_ScriptPredictionMatchesDeployment() public {
add weth peg tests and docs 5376202 julienbrg 5h ago38 assertEq(script.predict(SALT, WETH), _deploy(SALT, WETH));
Merge 1-add-main-logic into main dded29a rickub 6h ago39 }
40
41 function test_ScriptUsesKeccakOfLoveAsDefaultSalt() public view {
42 assertEq(script.DEFAULT_SALT(), SALT);
43 }
44
add weth peg tests and docs 5376202 julienbrg 5h 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 6h ago50 function test_PredictionIsChainAgnostic() public {
add weth peg tests and docs 5376202 julienbrg 5h ago51 address onThisChain = script.predict(SALT, WETH);
Merge 1-add-main-logic into main dded29a rickub 6h ago52
53 vm.chainId(137);
add weth peg tests and docs 5376202 julienbrg 5h ago54 assertEq(script.predict(SALT, WETH), onThisChain);
Merge 1-add-main-logic into main dded29a rickub 6h ago55
56 vm.chainId(42_161);
add weth peg tests and docs 5376202 julienbrg 5h ago57 assertEq(script.predict(SALT, WETH), onThisChain);
Merge 1-add-main-logic into main dded29a rickub 6h ago58 }
59
60 function test_DifferentSaltsGiveDifferentAddresses() public view {
add weth peg tests and docs 5376202 julienbrg 5h 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 6h ago68 }
69
70 /// @dev Redeploying with the same salt must fail, not silently return the
71 /// existing token.
72 function test_RevertWhen_RedeployingWithSameSalt() public {
add weth peg tests and docs 5376202 julienbrg 5h ago73 _deploy(SALT, WETH);
Merge 1-add-main-logic into main dded29a rickub 6h ago74
add weth peg tests and docs 5376202 julienbrg 5h ago75 (bool ok,) = CREATE2_FACTORY.call(abi.encodePacked(SALT, script.initCode(WETH)));
Merge 1-add-main-logic into main dded29a rickub 6h ago76 assertFalse(ok);
77 }
78
add weth peg tests and docs 5376202 julienbrg 5h 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 6h ago81 }
82
add natspec 3cc0f3b julienbrg 5h ago83 /// @dev Deploys through the canonical deterministic deployer, as the script does.
add weth peg tests and docs 5376202 julienbrg 5h ago84 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 6h 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}