|
| 1 | +// Full command: |
| 2 | +// HARDHAT_NETWORK=<NETWORK> npx ts-node --files scripts/contractAndDiamondUpgrades/createDiamondManifest.ts \ |
| 3 | +// --deployed-multiple-contracts-json deployedMultipleContracts.json \ |
| 4 | +// --previous-deploy-json deployed-lit-node-contracts-temp.json \ |
| 5 | +// [--manifest-file scripts/diamondContracts/manifests/diamondCutManifest.json] \ |
| 6 | +// [--reset-manifest true] |
| 7 | + |
| 8 | +import hre from 'hardhat'; |
| 9 | +import yargs from 'yargs'; |
| 10 | +import fs from 'fs/promises'; |
| 11 | +import path from 'path'; |
| 12 | + |
| 13 | +import { MANIFESTS_DIR, appendDiamondCutOperationToManifest } from '../diamondContracts/lib/diamondCutManifest'; |
| 14 | +import { FacetCutAction } from '../diamondContracts/lib/types'; |
| 15 | + |
| 16 | +const { ethers } = hre; |
| 17 | + |
| 18 | +type Address = string; |
| 19 | + |
| 20 | +type PreviousDeployJson = { |
| 21 | + stakingContractAddress?: Address; |
| 22 | + pubkeyRouterContractAddress?: Address; |
| 23 | + pkpNftContractAddress?: Address; |
| 24 | + pkpPermissionsContractAddress?: Address; |
| 25 | + backupRecoveryContractAddress?: Address; |
| 26 | + priceFeedContractAddress?: Address; |
| 27 | + facets?: Record<string, Array<{ facetName: string; facetAddress: Address }>>; |
| 28 | +}; |
| 29 | + |
| 30 | +type DiamondAddressKey = Exclude<keyof PreviousDeployJson, 'facets'>; |
| 31 | + |
| 32 | +const DIAMOND_GROUP_TO_ADDRESS_KEY: Record<string, DiamondAddressKey> = { |
| 33 | + Staking: 'stakingContractAddress', |
| 34 | + PubkeyRouter: 'pubkeyRouterContractAddress', |
| 35 | + PKPNFT: 'pkpNftContractAddress', |
| 36 | + PKPPermissions: 'pkpPermissionsContractAddress', |
| 37 | + BackupRecovery: 'backupRecoveryContractAddress', |
| 38 | + PriceFeed: 'priceFeedContractAddress', |
| 39 | +}; |
| 40 | + |
| 41 | +async function run() { |
| 42 | + const inputs = await getInputsFromCliOptions(); |
| 43 | + |
| 44 | + const previousDeploy: PreviousDeployJson = JSON.parse( |
| 45 | + await fs.readFile(inputs.previousDeployJson, 'utf8') |
| 46 | + ); |
| 47 | + const deployedMultiple: Record<string, Address> = JSON.parse( |
| 48 | + await fs.readFile(inputs.deployedMultipleContractsJson, 'utf8') |
| 49 | + ); |
| 50 | + |
| 51 | + if (!previousDeploy.facets) { |
| 52 | + throw new Error( |
| 53 | + `Missing "facets" in ${inputs.previousDeployJson}; can't look up old facet addresses` |
| 54 | + ); |
| 55 | + } |
| 56 | + |
| 57 | + const manifestFilePath = |
| 58 | + inputs.manifestFile ?? |
| 59 | + path.join( |
| 60 | + __dirname, |
| 61 | + '..', |
| 62 | + 'diamondContracts', |
| 63 | + MANIFESTS_DIR, |
| 64 | + 'diamondCutManifest.json' |
| 65 | + ); |
| 66 | + |
| 67 | + if (inputs.resetManifest) { |
| 68 | + await fs.mkdir(path.dirname(manifestFilePath), { recursive: true }); |
| 69 | + await fs.writeFile(manifestFilePath, JSON.stringify({ operations: [] }, null, 2)); |
| 70 | + console.log(`Reset manifest at ${manifestFilePath}`); |
| 71 | + } |
| 72 | + |
| 73 | + const facetToGroup: Record<string, string> = {}; |
| 74 | + const facetToOldAddress: Record<string, Address> = {}; |
| 75 | + |
| 76 | + for (const [groupName, facets] of Object.entries(previousDeploy.facets)) { |
| 77 | + for (const f of facets) { |
| 78 | + facetToGroup[f.facetName] = groupName; |
| 79 | + facetToOldAddress[f.facetName] = f.facetAddress; |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + const facetNames = Object.keys(deployedMultiple); |
| 84 | + console.log(`Preparing manifest for ${facetNames.length} facets...`); |
| 85 | + |
| 86 | + for (const contractName of facetNames) { |
| 87 | + const newFacetAddress = deployedMultiple[contractName]; |
| 88 | + const groupName = facetToGroup[contractName]; |
| 89 | + const oldFacetAddress = facetToOldAddress[contractName]; |
| 90 | + |
| 91 | + if (!groupName) { |
| 92 | + throw new Error( |
| 93 | + `Unable to find diamond group for facet "${contractName}" in ${inputs.previousDeployJson}` |
| 94 | + ); |
| 95 | + } |
| 96 | + const diamondAddressKey = DIAMOND_GROUP_TO_ADDRESS_KEY[groupName]; |
| 97 | + if (!diamondAddressKey) { |
| 98 | + throw new Error( |
| 99 | + `No diamond address mapping for group "${groupName}". Add it to DIAMOND_GROUP_TO_ADDRESS_KEY.` |
| 100 | + ); |
| 101 | + } |
| 102 | + const diamondAddress = previousDeploy[diamondAddressKey]; |
| 103 | + if (!diamondAddress) { |
| 104 | + throw new Error( |
| 105 | + `Missing "${diamondAddressKey}" in ${inputs.previousDeployJson}; needed for facet "${contractName}"` |
| 106 | + ); |
| 107 | + } |
| 108 | + if (!oldFacetAddress) { |
| 109 | + throw new Error( |
| 110 | + `Unable to find old facet address for "${contractName}" in ${inputs.previousDeployJson}` |
| 111 | + ); |
| 112 | + } |
| 113 | + if (!newFacetAddress) { |
| 114 | + throw new Error( |
| 115 | + `Missing new facet address for "${contractName}" in ${inputs.deployedMultipleContractsJson}` |
| 116 | + ); |
| 117 | + } |
| 118 | + |
| 119 | + // We still create a contract instance even for Remove; the manifest builder |
| 120 | + // uses the live diamond + old facet address to discover selectors for removal. |
| 121 | + const newFacet = await ethers.getContractAt(contractName, newFacetAddress); |
| 122 | + |
| 123 | + console.log( |
| 124 | + `\n${contractName}\n diamond: ${diamondAddress}\n old: ${oldFacetAddress}\n new: ${newFacetAddress}` |
| 125 | + ); |
| 126 | + |
| 127 | + // 1) Remove old facet selectors from the diamond (uses oldFacetAddress) |
| 128 | + await appendDiamondCutOperationToManifest( |
| 129 | + manifestFilePath, |
| 130 | + diamondAddress, |
| 131 | + newFacet, |
| 132 | + FacetCutAction.Remove, |
| 133 | + contractName, |
| 134 | + oldFacetAddress |
| 135 | + ); |
| 136 | + |
| 137 | + // 2) Add new facet selectors to the diamond |
| 138 | + await appendDiamondCutOperationToManifest( |
| 139 | + manifestFilePath, |
| 140 | + diamondAddress, |
| 141 | + newFacet, |
| 142 | + FacetCutAction.Add, |
| 143 | + contractName |
| 144 | + ); |
| 145 | + } |
| 146 | + |
| 147 | + console.log(`\nDiamond cut manifest updated at ${manifestFilePath}`); |
| 148 | +} |
| 149 | + |
| 150 | +run() |
| 151 | + .then(() => process.exit(0)) |
| 152 | + .catch((error) => { |
| 153 | + console.error(error); |
| 154 | + process.exit(1); |
| 155 | + }); |
| 156 | + |
| 157 | +async function getInputsFromCliOptions(): Promise<Inputs> { |
| 158 | + const argv = await yargs(process.argv.slice(2)).options({ |
| 159 | + 'deployed-multiple-contracts-json': { |
| 160 | + type: 'string', |
| 161 | + describe: |
| 162 | + 'Path to deployedMultipleContracts.json (new facet addresses keyed by facet name)', |
| 163 | + default: 'deployedMultipleContracts.json', |
| 164 | + }, |
| 165 | + 'previous-deploy-json': { |
| 166 | + type: 'string', |
| 167 | + describe: |
| 168 | + 'Path to previous deployment JSON (must contain top-level diamond addresses + facets map)', |
| 169 | + default: 'deployed-lit-node-contracts-temp.json', |
| 170 | + }, |
| 171 | + 'manifest-file': { |
| 172 | + type: 'string', |
| 173 | + describe: |
| 174 | + 'Output manifest file path (defaults to scripts/diamondContracts/manifests/diamondCutManifest.json)', |
| 175 | + required: false, |
| 176 | + }, |
| 177 | + 'reset-manifest': { |
| 178 | + type: 'boolean', |
| 179 | + describe: |
| 180 | + 'If true, overwrite manifest with an empty operations list before appending', |
| 181 | + default: true, |
| 182 | + }, |
| 183 | + }).argv; |
| 184 | + |
| 185 | + return { |
| 186 | + deployedMultipleContractsJson: argv['deployed-multiple-contracts-json'], |
| 187 | + previousDeployJson: argv['previous-deploy-json'], |
| 188 | + manifestFile: argv['manifest-file'], |
| 189 | + resetManifest: argv['reset-manifest'], |
| 190 | + }; |
| 191 | +} |
| 192 | + |
| 193 | +interface Inputs { |
| 194 | + deployedMultipleContractsJson: string; |
| 195 | + previousDeployJson: string; |
| 196 | + manifestFile?: string; |
| 197 | + resetManifest: boolean; |
| 198 | +} |
0 commit comments