|
3 | 3 |
|
4 | 4 | import { SignerWithAddress } from '@nomicfoundation/hardhat-ethers/signers'; |
5 | 5 | import { ContractFactory, FunctionFragment, Interface, ZeroAddress } from 'ethers'; |
6 | | -import { ethers } from 'hardhat'; |
| 6 | +import { deployments, ethers } from 'hardhat'; |
7 | 7 | import { FacetCut, FacetCutAction } from 'hardhat-deploy/dist/types'; |
8 | 8 | import type { IDiamond } from '../typechain'; |
9 | 9 | import { |
@@ -279,7 +279,7 @@ export async function removeFacetsFromDiamond( |
279 | 279 | throw new Error(`Facet ${facet.name} is empty or does not exist on-chain`); |
280 | 280 | } |
281 | 281 | console.log( |
282 | | - `Will remove the whole facet ${facet.name} [address: ${facet.address}, functions:${selectors.length}]`, |
| 282 | + `Will remove facet [name:${facet.name}, address: ${facet.address}, functions:${selectors.length}]`, |
283 | 283 | ); |
284 | 284 | facetCuts.push({ |
285 | 285 | facetAddress: ZeroAddress, |
@@ -350,3 +350,25 @@ export async function removeFunctionsFromDiamond( |
350 | 350 | await tx.wait(); |
351 | 351 | console.log('Functions removed successfully!'); |
352 | 352 | } |
| 353 | + |
| 354 | +/** |
| 355 | + * Removes dangling deployment artifacts for facets that are no longer linked to the diamond proxy. |
| 356 | + * This is not done automatically in `removeFacetsFromDiamond` because we deploy new facets first, |
| 357 | + * then remove old facets, which sometimes overwrites the existing ones. |
| 358 | + * @param proxyAddress address of the diamond proxy |
| 359 | + */ |
| 360 | +export async function removeDanglingFacetDeploymentArtifacts(proxyAddress: string) { |
| 361 | + console.log('\n=== Removing dangling deployment artifacts ==='); |
| 362 | + const allDeployments = await deployments.all(); |
| 363 | + const diamondLoupe = DiamondLoupeFacet__factory.connect(proxyAddress, ethers.provider); |
| 364 | + const onchainFacets = await diamondLoupe.facetAddresses(); |
| 365 | + for (const deploymentName of Object.keys(allDeployments)) { |
| 366 | + const deploymentAddress = allDeployments[deploymentName].address; |
| 367 | + if (deploymentName.endsWith('Facet') && !onchainFacets.includes(deploymentAddress)) { |
| 368 | + console.log( |
| 369 | + `Deleting dangling facet artifact [name:${deploymentName}, address:${deploymentAddress}]`, |
| 370 | + ); |
| 371 | + await deployments.delete(deploymentName); |
| 372 | + } |
| 373 | + } |
| 374 | +} |
0 commit comments