|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +pragma solidity ^0.8.20; |
| 3 | + |
| 4 | +import "forge-std/Script.sol"; |
| 5 | +import "forge-std/console.sol"; |
| 6 | +import { IDiamondCut } from "diamond-3-hardhat/interfaces/IDiamondCut.sol"; |
| 7 | +import { IDiamondLoupe } from "diamond-3-hardhat/interfaces/IDiamondLoupe.sol"; |
| 8 | +import { LibDeployment } from "./DeployFactory.s.sol"; |
| 9 | +import { DiamondLoupeFacet } from "diamond-3-hardhat/facets/DiamondLoupeFacet.sol"; |
| 10 | +import { IssuerFacet } from "@facets/IssuerFacet.sol"; |
| 11 | +import { StakeholderFacet } from "@facets/StakeholderFacet.sol"; |
| 12 | +import { StockClassFacet } from "@facets/StockClassFacet.sol"; |
| 13 | +import { StockFacet } from "@facets/StockFacet.sol"; |
| 14 | +import { ConvertiblesFacet } from "@facets/ConvertiblesFacet.sol"; |
| 15 | +import { EquityCompensationFacet } from "@facets/EquityCompensationFacet.sol"; |
| 16 | +import { StockPlanFacet } from "@facets/StockPlanFacet.sol"; |
| 17 | +import { WarrantFacet } from "@facets/WarrantFacet.sol"; |
| 18 | +import { StakeholderNFTFacet } from "@facets/StakeholderNFTFacet.sol"; |
| 19 | +import { AccessControlFacet } from "@facets/AccessControlFacet.sol"; |
| 20 | + |
| 21 | +contract SyncSingleFacetScript is Script { |
| 22 | + using LibDeployment for *; |
| 23 | + |
| 24 | + // Core facet operations |
| 25 | + function addFacet(address diamond, address newFacet, bytes4[] memory selectors) public { |
| 26 | + IDiamondCut.FacetCut[] memory cut = new IDiamondCut.FacetCut[](1); |
| 27 | + cut[0] = IDiamondCut.FacetCut({ |
| 28 | + facetAddress: newFacet, |
| 29 | + action: IDiamondCut.FacetCutAction.Add, |
| 30 | + functionSelectors: selectors |
| 31 | + }); |
| 32 | + IDiamondCut(diamond).diamondCut(cut, address(0), ""); |
| 33 | + } |
| 34 | + |
| 35 | + function replaceFacet(address diamond, address newFacet, bytes4[] memory selectors) public { |
| 36 | + IDiamondCut.FacetCut[] memory cut = new IDiamondCut.FacetCut[](1); |
| 37 | + cut[0] = IDiamondCut.FacetCut({ |
| 38 | + facetAddress: newFacet, |
| 39 | + action: IDiamondCut.FacetCutAction.Replace, |
| 40 | + functionSelectors: selectors |
| 41 | + }); |
| 42 | + |
| 43 | + try IDiamondCut(diamond).diamondCut(cut, address(0), "") { |
| 44 | + console.log("Facet replaced successfully"); |
| 45 | + } catch Error(string memory reason) { |
| 46 | + console.log("Failed to replace facet:", reason); |
| 47 | + revert(reason); |
| 48 | + } catch (bytes memory) { |
| 49 | + console.log("Failed to replace facet (no reason)"); |
| 50 | + revert("Unknown error during facet replacement"); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + function removeFacet(address diamond, bytes4[] memory selectors) public { |
| 55 | + IDiamondCut.FacetCut[] memory cut = new IDiamondCut.FacetCut[](1); |
| 56 | + cut[0] = IDiamondCut.FacetCut({ |
| 57 | + facetAddress: address(0), |
| 58 | + action: IDiamondCut.FacetCutAction.Remove, |
| 59 | + functionSelectors: selectors |
| 60 | + }); |
| 61 | + IDiamondCut(diamond).diamondCut(cut, address(0), ""); |
| 62 | + } |
| 63 | + |
| 64 | + function upgradeSingleFacet(LibDeployment.FacetType facetType) public { |
| 65 | + address referenceDiamond = vm.envAddress("REFERENCE_DIAMOND"); |
| 66 | + // solhint-disable func-name-mixedcase |
| 67 | + string memory RPC_URL = vm.envOr("RPC_URL", string("http://localhost:8545")); |
| 68 | + console.log("RPC_URL: %s", RPC_URL); |
| 69 | + |
| 70 | + // Get deployed facets |
| 71 | + uint256 fork = vm.createFork(RPC_URL); |
| 72 | + vm.selectFork(fork); |
| 73 | + IDiamondLoupe.Facet[] memory deployedFacets = IDiamondLoupe(referenceDiamond).facets(); |
| 74 | + |
| 75 | + // Find the facet we want to upgrade |
| 76 | + bytes4 targetSelector = LibDeployment.getFacetCutInfo(facetType).selectors[0]; |
| 77 | + address currentFacetAddress = address(0); |
| 78 | + bytes4[] memory currentSelectors; |
| 79 | + |
| 80 | + // Find current facet address and selectors |
| 81 | + for (uint256 i = 0; i < deployedFacets.length; i++) { |
| 82 | + if (deployedFacets[i].functionSelectors[0] == targetSelector) { |
| 83 | + currentFacetAddress = deployedFacets[i].facetAddress; |
| 84 | + currentSelectors = deployedFacets[i].functionSelectors; |
| 85 | + break; |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + if (currentFacetAddress == address(0)) { |
| 90 | + revert("Facet not found in deployed diamond"); |
| 91 | + } |
| 92 | + |
| 93 | + // Deploy new facet on the same chain |
| 94 | + vm.startBroadcast(vm.envUint("PRIVATE_KEY")); |
| 95 | + address newFacet = LibDeployment.deployFacet(facetType); |
| 96 | + vm.stopBroadcast(); |
| 97 | + |
| 98 | + console.log("New facet deployed at:", newFacet); |
| 99 | + |
| 100 | + // Verify the new facet has code |
| 101 | + uint256 codeSize; |
| 102 | + assembly { |
| 103 | + codeSize := extcodesize(newFacet) |
| 104 | + } |
| 105 | + require(codeSize > 0, "New facet has no code"); |
| 106 | + |
| 107 | + // Get all selectors for the facet type |
| 108 | + LibDeployment.FacetCutInfo memory info = LibDeployment.getFacetCutInfo(facetType); |
| 109 | + |
| 110 | + // Find new selectors that don't exist in current selectors |
| 111 | + bytes4[] memory newSelectors = new bytes4[](info.selectors.length); |
| 112 | + uint256 newSelectorCount = 0; |
| 113 | + |
| 114 | + for (uint256 i = 0; i < info.selectors.length; i++) { |
| 115 | + bool exists = false; |
| 116 | + if (currentSelectors.length > 0) { |
| 117 | + for (uint256 j = 0; j < currentSelectors.length; j++) { |
| 118 | + if (info.selectors[i] == currentSelectors[j]) { |
| 119 | + exists = true; |
| 120 | + break; |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + if (!exists) { |
| 125 | + newSelectors[newSelectorCount] = info.selectors[i]; |
| 126 | + newSelectorCount++; |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + // Resize newSelectors array to actual count |
| 131 | + bytes4[] memory finalNewSelectors = new bytes4[](newSelectorCount); |
| 132 | + for (uint256 i = 0; i < newSelectorCount; i++) { |
| 133 | + finalNewSelectors[i] = newSelectors[i]; |
| 134 | + } |
| 135 | + |
| 136 | + if (newSelectorCount > 0) { |
| 137 | + console.log("Adding new selectors..."); |
| 138 | + vm.startBroadcast(vm.envUint("PRIVATE_KEY")); |
| 139 | + addFacet(referenceDiamond, newFacet, finalNewSelectors); |
| 140 | + vm.stopBroadcast(); |
| 141 | + console.log("New selectors added successfully"); |
| 142 | + } |
| 143 | + |
| 144 | + if (currentSelectors.length > 0) { |
| 145 | + console.log("Replacing existing selectors..."); |
| 146 | + vm.startBroadcast(vm.envUint("PRIVATE_KEY")); |
| 147 | + replaceFacet(referenceDiamond, newFacet, currentSelectors); |
| 148 | + vm.stopBroadcast(); |
| 149 | + console.log("Existing selectors replaced successfully"); |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + function _getBytecode(address addr) internal view returns (bytes memory) { |
| 154 | + uint256 size; |
| 155 | + assembly { |
| 156 | + size := extcodesize(addr) |
| 157 | + } |
| 158 | + bytes memory code = new bytes(size); |
| 159 | + assembly { |
| 160 | + extcodecopy(addr, add(code, 0x20), 0, size) |
| 161 | + } |
| 162 | + return code; |
| 163 | + } |
| 164 | + |
| 165 | + function run() external virtual { |
| 166 | + // Example usage: |
| 167 | + // forge script script/SyncSingleFacet.s.sol:SyncSingleFacetScript --sig "upgradeSingleFacet(uint8)" 4 --broadcast --rpc-url $RPC_URL |
| 168 | + // Where 4 is the enum value for StockFacet |
| 169 | + upgradeSingleFacet(LibDeployment.FacetType.Warrant); |
| 170 | + } |
| 171 | +} |
0 commit comments