Skip to content

Commit 6dadbe8

Browse files
committed
feat: Remove dangling artifact deployments after upgrade
1 parent f3b47f1 commit 6dadbe8

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

scripts/upgrades/v6.1.0-bulk-processing.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
getUpgradeContext,
1010
linkFacetsToDiamond,
1111
printOnchainProxyFunctions,
12+
removeDanglingFacetDeploymentArtifacts,
1213
removeFacetsFromDiamond,
1314
removeFunctionsFromDiamond,
1415
} from '../../utils/proxy-tools';
@@ -103,6 +104,7 @@ async function main() {
103104
await linkFacetsToDiamond(proxyAddress, proxyOwner, facetsToAdd);
104105
await printOnchainProxyFunctions(proxyAddress);
105106
console.log('Upgrade performed successfully!');
107+
await removeDanglingFacetDeploymentArtifacts(proxyAddress);
106108
await tryVerify(facetsToAdd.map((facet) => facet.name));
107109
}
108110

utils/proxy-tools.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import { SignerWithAddress } from '@nomicfoundation/hardhat-ethers/signers';
55
import { ContractFactory, FunctionFragment, Interface, ZeroAddress } from 'ethers';
6-
import { ethers } from 'hardhat';
6+
import { deployments, ethers } from 'hardhat';
77
import { FacetCut, FacetCutAction } from 'hardhat-deploy/dist/types';
88
import type { IDiamond } from '../typechain';
99
import {
@@ -279,7 +279,7 @@ export async function removeFacetsFromDiamond(
279279
throw new Error(`Facet ${facet.name} is empty or does not exist on-chain`);
280280
}
281281
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}]`,
283283
);
284284
facetCuts.push({
285285
facetAddress: ZeroAddress,
@@ -350,3 +350,25 @@ export async function removeFunctionsFromDiamond(
350350
await tx.wait();
351351
console.log('Functions removed successfully!');
352352
}
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

Comments
 (0)