|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +pragma solidity ^0.8.24; |
| 3 | + |
| 4 | +import {Script, console} from "forge-std/Script.sol"; |
| 5 | +import {FeeVault} from "../src/FeeVault.sol"; |
| 6 | + |
| 7 | +abstract contract FeeVaultAllocBase is Script { |
| 8 | + struct Config { |
| 9 | + address feeVaultAddress; |
| 10 | + address owner; |
| 11 | + uint32 destinationDomain; |
| 12 | + bytes32 recipientAddress; |
| 13 | + uint256 minimumAmount; |
| 14 | + uint256 callFee; |
| 15 | + uint256 bridgeShareBpsRaw; |
| 16 | + uint256 bridgeShareBps; |
| 17 | + address otherRecipient; |
| 18 | + address hypNativeMinter; |
| 19 | + bytes32 salt; |
| 20 | + address deployer; |
| 21 | + } |
| 22 | + |
| 23 | + function loadConfig() internal view returns (Config memory cfg) { |
| 24 | + cfg.owner = vm.envAddress("OWNER"); |
| 25 | + cfg.destinationDomain = uint32(vm.envOr("DESTINATION_DOMAIN", uint256(0))); |
| 26 | + cfg.recipientAddress = vm.envOr("RECIPIENT_ADDRESS", bytes32(0)); |
| 27 | + cfg.minimumAmount = vm.envOr("MINIMUM_AMOUNT", uint256(0)); |
| 28 | + cfg.callFee = vm.envOr("CALL_FEE", uint256(0)); |
| 29 | + cfg.bridgeShareBpsRaw = vm.envOr("BRIDGE_SHARE_BPS", uint256(0)); |
| 30 | + cfg.otherRecipient = vm.envOr("OTHER_RECIPIENT", address(0)); |
| 31 | + cfg.hypNativeMinter = vm.envOr("HYP_NATIVE_MINTER", address(0)); |
| 32 | + cfg.feeVaultAddress = vm.envOr("FEE_VAULT_ADDRESS", address(0)); |
| 33 | + cfg.deployer = vm.envOr("DEPLOYER", address(0)); |
| 34 | + cfg.salt = vm.envOr("SALT", bytes32(0)); |
| 35 | + |
| 36 | + require(cfg.owner != address(0), "OWNER required"); |
| 37 | + require(cfg.bridgeShareBpsRaw <= 10000, "BRIDGE_SHARE_BPS > 10000"); |
| 38 | + |
| 39 | + cfg.bridgeShareBps = cfg.bridgeShareBpsRaw == 0 ? 10000 : cfg.bridgeShareBpsRaw; |
| 40 | + |
| 41 | + if (cfg.feeVaultAddress == address(0) && cfg.deployer != address(0)) { |
| 42 | + bytes32 initCodeHash = keccak256( |
| 43 | + abi.encodePacked( |
| 44 | + type(FeeVault).creationCode, |
| 45 | + abi.encode( |
| 46 | + cfg.owner, |
| 47 | + cfg.destinationDomain, |
| 48 | + cfg.recipientAddress, |
| 49 | + cfg.minimumAmount, |
| 50 | + cfg.callFee, |
| 51 | + cfg.bridgeShareBpsRaw, |
| 52 | + cfg.otherRecipient |
| 53 | + ) |
| 54 | + ) |
| 55 | + ); |
| 56 | + cfg.feeVaultAddress = address( |
| 57 | + uint160(uint256(keccak256(abi.encodePacked(bytes1(0xff), cfg.deployer, cfg.salt, initCodeHash)))) |
| 58 | + ); |
| 59 | + } |
| 60 | + |
| 61 | + require(cfg.feeVaultAddress != address(0), "FEE_VAULT_ADDRESS or DEPLOYER required"); |
| 62 | + } |
| 63 | + |
| 64 | + function computeSlots(Config memory cfg) |
| 65 | + internal |
| 66 | + pure |
| 67 | + returns ( |
| 68 | + bytes32 slot0, |
| 69 | + bytes32 slot1, |
| 70 | + bytes32 slot2, |
| 71 | + bytes32 slot3, |
| 72 | + bytes32 slot4, |
| 73 | + bytes32 slot5, |
| 74 | + bytes32 slot6 |
| 75 | + ) |
| 76 | + { |
| 77 | + slot0 = bytes32(uint256(uint160(cfg.hypNativeMinter))); |
| 78 | + slot1 = bytes32((uint256(cfg.destinationDomain) << 160) | uint256(uint160(cfg.owner))); |
| 79 | + slot2 = cfg.recipientAddress; |
| 80 | + slot3 = bytes32(cfg.minimumAmount); |
| 81 | + slot4 = bytes32(cfg.callFee); |
| 82 | + slot5 = bytes32(uint256(uint160(cfg.otherRecipient))); |
| 83 | + slot6 = bytes32(cfg.bridgeShareBps); |
| 84 | + } |
| 85 | + |
| 86 | + function addressKey(address addr) internal pure returns (string memory) { |
| 87 | + bytes memory full = bytes(vm.toString(addr)); |
| 88 | + bytes memory key = new bytes(40); |
| 89 | + // Fixed-length copy for address key without 0x prefix. |
| 90 | + for (uint256 i = 0; i < 40; i++) { |
| 91 | + key[i] = full[i + 2]; |
| 92 | + } |
| 93 | + return string(key); |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +/// @title GenerateFeeVaultAlloc |
| 98 | +/// @notice Generates genesis alloc JSON for deploying FeeVault at a deterministic address |
| 99 | +/// @dev Run with: OWNER=0x... forge script script/GenerateFeeVaultAlloc.s.sol -vvv |
| 100 | +contract GenerateFeeVaultAlloc is FeeVaultAllocBase { |
| 101 | + function run() external view { |
| 102 | + Config memory cfg = loadConfig(); |
| 103 | + bytes memory runtimeCode = type(FeeVault).runtimeCode; |
| 104 | + |
| 105 | + (bytes32 slot0, bytes32 slot1, bytes32 slot2, bytes32 slot3, bytes32 slot4, bytes32 slot5, bytes32 slot6) = |
| 106 | + computeSlots(cfg); |
| 107 | + |
| 108 | + console.log("========== FeeVault Genesis Alloc =========="); |
| 109 | + console.log("FeeVault address:", cfg.feeVaultAddress); |
| 110 | + console.log("Owner:", cfg.owner); |
| 111 | + console.log("Destination domain:", cfg.destinationDomain); |
| 112 | + console.log("Bridge share bps (raw):", cfg.bridgeShareBpsRaw); |
| 113 | + console.log("Bridge share bps (effective):", cfg.bridgeShareBps); |
| 114 | + console.log(""); |
| 115 | + |
| 116 | + if (cfg.bridgeShareBpsRaw == 0) { |
| 117 | + console.log("NOTE: BRIDGE_SHARE_BPS=0 defaults to 10000 (constructor behavior)."); |
| 118 | + } |
| 119 | + if (cfg.bridgeShareBps < 10000 && cfg.otherRecipient == address(0)) { |
| 120 | + console.log("WARNING: OTHER_RECIPIENT is zero but bridge share < 10000."); |
| 121 | + } |
| 122 | + if (cfg.hypNativeMinter == address(0)) { |
| 123 | + console.log("NOTE: HYP_NATIVE_MINTER is zero; set it before calling sendToCelestia()."); |
| 124 | + } |
| 125 | + console.log(""); |
| 126 | + |
| 127 | + console.log("Add this to your genesis.json 'alloc' section:"); |
| 128 | + console.log(""); |
| 129 | + console.log("{"); |
| 130 | + console.log(' "alloc": {'); |
| 131 | + console.log(' "%s": {', addressKey(cfg.feeVaultAddress)); |
| 132 | + console.log(' "balance": "0x0",'); |
| 133 | + console.log(' "code": "%s",', vm.toString(runtimeCode)); |
| 134 | + console.log(' "storage": {'); |
| 135 | + console.log(' "0x0": "%s",', vm.toString(slot0)); |
| 136 | + console.log(' "0x1": "%s",', vm.toString(slot1)); |
| 137 | + console.log(' "0x2": "%s",', vm.toString(slot2)); |
| 138 | + console.log(' "0x3": "%s",', vm.toString(slot3)); |
| 139 | + console.log(' "0x4": "%s",', vm.toString(slot4)); |
| 140 | + console.log(' "0x5": "%s",', vm.toString(slot5)); |
| 141 | + console.log(' "0x6": "%s"', vm.toString(slot6)); |
| 142 | + console.log(" }"); |
| 143 | + console.log(" }"); |
| 144 | + console.log(" }"); |
| 145 | + console.log("}"); |
| 146 | + console.log(""); |
| 147 | + console.log("Raw bytecode length:", runtimeCode.length); |
| 148 | + console.log("============================================="); |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +/// @title GenerateFeeVaultAllocJSON |
| 153 | +/// @notice Outputs just the JSON snippet for easy copy-paste |
| 154 | +/// @dev Run with: OWNER=0x... forge script script/GenerateFeeVaultAlloc.s.sol:GenerateFeeVaultAllocJSON -vvv |
| 155 | +contract GenerateFeeVaultAllocJSON is FeeVaultAllocBase { |
| 156 | + function run() external view { |
| 157 | + Config memory cfg = loadConfig(); |
| 158 | + bytes memory runtimeCode = type(FeeVault).runtimeCode; |
| 159 | + |
| 160 | + (bytes32 slot0, bytes32 slot1, bytes32 slot2, bytes32 slot3, bytes32 slot4, bytes32 slot5, bytes32 slot6) = |
| 161 | + computeSlots(cfg); |
| 162 | + |
| 163 | + string memory json = string( |
| 164 | + abi.encodePacked( |
| 165 | + '{"', |
| 166 | + addressKey(cfg.feeVaultAddress), |
| 167 | + '":{"balance":"0x0","code":"', |
| 168 | + vm.toString(runtimeCode), |
| 169 | + '","storage":{"0x0":"', |
| 170 | + vm.toString(slot0), |
| 171 | + '","0x1":"', |
| 172 | + vm.toString(slot1), |
| 173 | + '","0x2":"', |
| 174 | + vm.toString(slot2), |
| 175 | + '","0x3":"', |
| 176 | + vm.toString(slot3), |
| 177 | + '","0x4":"', |
| 178 | + vm.toString(slot4), |
| 179 | + '","0x5":"', |
| 180 | + vm.toString(slot5), |
| 181 | + '","0x6":"', |
| 182 | + vm.toString(slot6), |
| 183 | + '"}}}' |
| 184 | + ) |
| 185 | + ); |
| 186 | + |
| 187 | + console.log(json); |
| 188 | + } |
| 189 | +} |
0 commit comments