Skip to content

Commit

Permalink
Merge pull request #461 from idkravitz/diamond_selectors_kungfu
Browse files Browse the repository at this point in the history
Add filtering of selectors by facets in Diamonds
  • Loading branch information
wighawag committed Jun 24, 2023
2 parents fc8a72b + 551d769 commit 6f10e1b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
14 changes: 10 additions & 4 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import {
} from '../types';
import {PartialExtension} from './internal/types';
import {UnknownSignerError} from './errors';
import {mergeABIs, recode} from './utils';
import {filterABI, mergeABIs, recode} from './utils';
import fs from 'fs-extra';

import OpenZeppelinTransparentProxy from '../extendedArtifacts/TransparentUpgradeableProxy.json';
Expand Down Expand Up @@ -1958,6 +1958,7 @@ Note that in this case, the contract deployment will not behave the same if depl
let abi: any[] = diamondArtifact.abi.concat([]);
const facetCuts: FacetCut[] = [];
let facetFound: string | undefined;
const excludeSelectors: Record<string, string[]> = options.excludeSelectors || {};
for (const facet of facetsSet) {
let deterministicFacet: string | boolean = true;
let facetName;
Expand Down Expand Up @@ -2020,7 +2021,12 @@ Note that in this case, the contract deployment will not behave the same if depl
// reset args for case where facet do not expect any and there was no specific args set on it
facetArgs = [];
}
abi = mergeABIs([abi, artifact.abi], {
let excludeSighashes: Set<string> = new Set();
if (facetName in excludeSelectors) {
const iface = new Interface(artifact.abi);
excludeSighashes = new Set(excludeSelectors[facetName].map(selector => iface.getSighash(selector)));
}
abi = mergeABIs([abi, filterABI(artifact.abi, excludeSighashes)], {
check: true,
skipSupportsInterface: false,
});
Expand All @@ -2045,7 +2051,7 @@ Note that in this case, the contract deployment will not behave the same if depl
facetAddress = implementation.address;
const newFacet = {
facetAddress,
functionSelectors: sigsFromABI(implementation.abi),
functionSelectors: sigsFromABI(filterABI(implementation.abi, excludeSighashes)),
};
facetSnapshot.push(newFacet);
newSelectors.push(...newFacet.functionSelectors);
Expand All @@ -2054,7 +2060,7 @@ Note that in this case, the contract deployment will not behave the same if depl
facetAddress = oldImpl.address;
const newFacet = {
facetAddress,
functionSelectors: sigsFromABI(oldImpl.abi),
functionSelectors: sigsFromABI(filterABI(oldImpl.abi, excludeSighashes)),
};
facetSnapshot.push(newFacet);
newSelectors.push(...newFacet.functionSelectors);
Expand Down
9 changes: 8 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {getAddress, isAddress} from '@ethersproject/address';
import {Interface, FunctionFragment, Fragment} from '@ethersproject/abi';
import {Artifact, HardhatRuntimeEnvironment, Network} from 'hardhat/types';
import {BigNumber} from '@ethersproject/bignumber';
import {Export, ExtendedArtifact, MultiExport} from '../types';
import {ABI, Export, ExtendedArtifact, MultiExport} from '../types';
import {Artifacts} from 'hardhat/internal/artifacts';
import murmur128 from 'murmur-128';
import {Transaction} from '@ethersproject/transactions';
Expand Down Expand Up @@ -563,6 +563,13 @@ export function getDeployPaths(network: Network): string[] {
}
}

export function filterABI(
abi: ABI,
excludeSighashes: Set<string>,
): any[] {
return abi.filter(fragment => fragment.type !== 'function' || !excludeSighashes.has(Interface.getSighash(Fragment.from(fragment) as FunctionFragment)));
}

export function mergeABIs(
abis: any[][],
options: {check: boolean; skipSupportsInterface: boolean}
Expand Down
3 changes: 3 additions & 0 deletions types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ export interface DiamondOptions extends TxOptions {
methodName: string;
args: any[];
};
excludeSelectors?: {
[facetName: string]: string[]
};
deterministicSalt?: string;
facetsArgs?: any[];
}
Expand Down

0 comments on commit 6f10e1b

Please sign in to comment.