| add love contract 9bf25a7 julienbrg 8h ago | 1 | // SPDX-License-Identifier: MIT |
| 2 | pragma solidity ^0.8.30; |
| 3 | |
| 4 | import {Love} from "../src/Love.sol"; |
| Merge 3-peg-love-to-weth into main ce13509 rickub 7h ago | 5 | import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; |
| add love contract 9bf25a7 julienbrg 8h ago | 6 | import {Script, console} from "forge-std/Script.sol"; |
| 7 | |
| Merge 3-peg-love-to-weth into main ce13509 rickub 7h ago | 8 | /// @title LoveScript |
| 9 | /// @author Julien Béranger |
| Merge 1-add-main-logic into main dded29a rickub 8h ago | 10 | /// @notice Deploys `Love` with CREATE2 through the canonical deterministic |
| Merge 3-peg-love-to-weth into main ce13509 rickub 7h ago | 11 | /// deployer. The address depends on the salt and the creation code, and |
| 12 | /// the creation code embeds the wETH address — so the token only gets |
| 13 | /// the same address on chains where wETH sits at the same address. Keep |
| Merge 1-add-main-logic into main dded29a rickub 8h ago | 14 | /// `solc`, the optimizer settings and `bytecode_hash = "none"` as they |
| Merge 3-peg-love-to-weth into main ce13509 rickub 7h ago | 15 | /// are in `foundry.toml`, or the address changes too. |
| add love contract 9bf25a7 julienbrg 8h ago | 16 | contract LoveScript is Script { |
| Merge 3-peg-love-to-weth into main ce13509 rickub 7h ago | 17 | /// @notice Salt used when `SALT` is not set in the environment. |
| Merge 1-add-main-logic into main dded29a rickub 8h ago | 18 | bytes32 public constant DEFAULT_SALT = keccak256("LOVE"); |
| 19 | |
| Merge 3-peg-love-to-weth into main ce13509 rickub 7h ago | 20 | /// @notice wETH address used when `WETH` is not set in the environment. |
| 21 | /// @dev wETH on Base, Optimism and every other OP-Stack chain. |
| 22 | address public constant DEFAULT_WETH = 0x4200000000000000000000000000000000000006; |
| 23 | |
| 24 | /// @notice The token, once `run()` has deployed or found it. |
| add love contract 9bf25a7 julienbrg 8h ago | 25 | Love public love; |
| 26 | |
| Merge 3-peg-love-to-weth into main ce13509 rickub 7h ago | 27 | /// @notice Deploy `Love`, or return it untouched if it is already there. |
| 28 | /// @dev Reads `SALT` and `WETH` from the environment, falling back to the |
| 29 | /// defaults, and asserts the deployed address matches the prediction. |
| 30 | /// @return The deployed token. |
| Merge 1-add-main-logic into main dded29a rickub 8h ago | 31 | function run() public returns (Love) { |
| 32 | bytes32 s = salt(); |
| Merge 3-peg-love-to-weth into main ce13509 rickub 7h ago | 33 | address w = wethAddress(); |
| 34 | address predicted = predict(s, w); |
| Merge 1-add-main-logic into main dded29a rickub 8h ago | 35 | |
| 36 | console.log("deployer ", CREATE2_FACTORY); |
| 37 | console.log("salt ", vm.toString(s)); |
| Merge 3-peg-love-to-weth into main ce13509 rickub 7h ago | 38 | console.log("weth ", w); |
| 39 | console.log("initCodeHash ", vm.toString(initCodeHash(w))); |
| Merge 1-add-main-logic into main dded29a rickub 8h ago | 40 | console.log("predicted ", predicted); |
| 41 | |
| 42 | if (predicted.code.length > 0) { |
| 43 | console.log("already deployed, nothing to do"); |
| 44 | love = Love(predicted); |
| 45 | return love; |
| 46 | } |
| 47 | |
| add love contract 9bf25a7 julienbrg 8h ago | 48 | vm.startBroadcast(); |
| Merge 3-peg-love-to-weth into main ce13509 rickub 7h ago | 49 | love = new Love{salt: s}(IERC20(w)); |
| Merge 1-add-main-logic into main dded29a rickub 8h ago | 50 | vm.stopBroadcast(); |
| 51 | |
| 52 | require(address(love) == predicted, "LoveScript: address mismatch"); |
| add love contract 9bf25a7 julienbrg 8h ago | 53 | |
| 54 | console.log("Love deployed at", address(love)); |
| make love an erc20 with public mint 4c8048e julienbrg 8h ago | 55 | console.log("name ", love.name()); |
| 56 | console.log("symbol ", love.symbol()); |
| Merge 3-peg-love-to-weth into main ce13509 rickub 7h ago | 57 | console.log("weth ", address(love.WETH())); |
| 58 | console.log("rate ", love.RATE()); |
| make love an erc20 with public mint 4c8048e julienbrg 8h ago | 59 | console.log("totalSupply ", love.totalSupply()); |
| add love contract 9bf25a7 julienbrg 8h ago | 60 | |
| Merge 1-add-main-logic into main dded29a rickub 8h ago | 61 | return love; |
| 62 | } |
| 63 | |
| 64 | /// @notice Print the address `run()` would deploy to, without broadcasting. |
| Merge 3-peg-love-to-weth into main ce13509 rickub 7h ago | 65 | /// @return predicted The address the current salt and wETH derive to. |
| Merge 1-add-main-logic into main dded29a rickub 8h ago | 66 | function predict() public view returns (address predicted) { |
| 67 | bytes32 s = salt(); |
| Merge 3-peg-love-to-weth into main ce13509 rickub 7h ago | 68 | address w = wethAddress(); |
| 69 | predicted = predict(s, w); |
| Merge 1-add-main-logic into main dded29a rickub 8h ago | 70 | |
| 71 | console.log("salt ", vm.toString(s)); |
| Merge 3-peg-love-to-weth into main ce13509 rickub 7h ago | 72 | console.log("weth ", w); |
| 73 | console.log("initCodeHash ", vm.toString(initCodeHash(w))); |
| Merge 1-add-main-logic into main dded29a rickub 8h ago | 74 | console.log("predicted ", predicted); |
| 75 | } |
| 76 | |
| Merge 3-peg-love-to-weth into main ce13509 rickub 7h ago | 77 | /// @notice Derive the deployment address from a salt and a wETH address. |
| 78 | /// @param s The CREATE2 salt. |
| 79 | /// @param w The wETH the token would be pegged to. |
| 80 | /// @return The address `Love` would land at. |
| 81 | function predict(bytes32 s, address w) public pure returns (address) { |
| 82 | return vm.computeCreate2Address(s, initCodeHash(w), CREATE2_FACTORY); |
| 83 | } |
| 84 | |
| 85 | /// @notice The creation code CREATE2 is handed, constructor argument included. |
| 86 | /// @param w The wETH the token would be pegged to. |
| 87 | /// @return The creation code, with `w` ABI-encoded onto it. |
| 88 | function initCode(address w) public pure returns (bytes memory) { |
| 89 | return abi.encodePacked(type(Love).creationCode, abi.encode(w)); |
| Merge 1-add-main-logic into main dded29a rickub 8h ago | 90 | } |
| 91 | |
| Merge 3-peg-love-to-weth into main ce13509 rickub 7h ago | 92 | /// @notice Hash of the creation code, the second input to the address derivation. |
| 93 | /// @param w The wETH the token would be pegged to. |
| 94 | /// @return The keccak256 of `initCode(w)`. |
| 95 | function initCodeHash(address w) public pure returns (bytes32) { |
| 96 | return keccak256(initCode(w)); |
| Merge 1-add-main-logic into main dded29a rickub 8h ago | 97 | } |
| 98 | |
| Merge 3-peg-love-to-weth into main ce13509 rickub 7h ago | 99 | /// @notice The salt to deploy with. |
| Merge 1-add-main-logic into main dded29a rickub 8h ago | 100 | /// @dev `SALT` overrides the default, e.g. to mine a vanity address. |
| Merge 3-peg-love-to-weth into main ce13509 rickub 7h ago | 101 | /// @return The configured salt, or `DEFAULT_SALT`. |
| Merge 1-add-main-logic into main dded29a rickub 8h ago | 102 | function salt() public view returns (bytes32) { |
| 103 | return vm.envOr("SALT", DEFAULT_SALT); |
| add love contract 9bf25a7 julienbrg 8h ago | 104 | } |
| Merge 3-peg-love-to-weth into main ce13509 rickub 7h ago | 105 | |
| 106 | /// @notice The wETH address to peg the deployment to. |
| 107 | /// @dev `WETH` overrides the default, which only holds on OP-Stack chains. |
| 108 | /// @return The configured wETH address, or `DEFAULT_WETH`. |
| 109 | function wethAddress() public view returns (address) { |
| 110 | return vm.envOr("WETH", DEFAULT_WETH); |
| 111 | } |
| add love contract 9bf25a7 julienbrg 8h ago | 112 | } |