-
Notifications
You must be signed in to change notification settings - Fork 90
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Diamond Helper Functions #158
Open
0xCourtney
wants to merge
32
commits into
master
Choose a base branch
from
diamond-helper
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
a7edda4
diamond utils draft
0xCourtney 420964c
Merge branch 'master' into diamond-helper
ItsNickBarry 047964e
remove printDiamond
0xCourtney 9d3a62d
removes composite type
0xCourtney 9d3b060
exports facet cut action
0xCourtney 20bf78e
removes cli-table3 package
0xCourtney b2eda57
imports ethersproject packages
0xCourtney 398ff37
Revert "remove printDiamond"
0xCourtney f9a6c2c
removes printDiamond from external interface
0xCourtney 45b2dc4
throws if add/replace/remove fails
0xCourtney 3d9d456
adds only filter, refactors exclude
0xCourtney e702c36
adds previewFacetCut
0xCourtney 5f34d05
removes printDiamond
0xCourtney 1aa7db8
adds comments
0xCourtney f00863e
capitalize FacetCutAction enum types
ItsNickBarry c8195bc
moves diamond utils to diamond subdirectory
0xCourtney f43c14a
Merge branch 'diamond-helper' of github.com:solidstate-network/solids…
ItsNickBarry c93c0a5
Merge branch 'master' into diamond-helper
ItsNickBarry e5d19a2
use FacetCutAction enum in spec and test files
ItsNickBarry dfac641
fix dependency versions
ItsNickBarry 669a620
Merge pull request #176 from solidstate-network/diamond-helper-enum-e…
ItsNickBarry cef5a3f
reorder declarations
ItsNickBarry ce286c6
uses except instead of exclude
0xCourtney a2a3c19
Update lib/diamond/utils.ts
0xCourtney c7df310
Update lib/diamond/utils.ts
0xCourtney 7d6598e
Update lib/diamond/filters.ts
0xCourtney 9d92ea0
rename
0xCourtney a9b12a5
updates comments
0xCourtney f4c211b
rename
0xCourtney dbf11d5
typo
0xCourtney 871f868
diamonCut returns receipt
0xCourtney 54da442
consolidates only and execpt to single array, adds type and action to…
0xCourtney File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { FacetCutAction } from './utils'; | ||
import { AddressZero } from '@ethersproject/constants'; | ||
|
||
export interface FacetFilter { | ||
type: string; | ||
action: FacetCutAction; | ||
contract: string; | ||
selectors: string[]; | ||
} | ||
|
||
// returns true if the selector is found in the only or except filters | ||
export function selectorIsFiltered( | ||
only: FacetFilter[], | ||
except: FacetFilter[], | ||
contract: string, | ||
selector: string, | ||
): boolean { | ||
if (only.length > 0) { | ||
// include selectors found in only, exclude all others | ||
return includes(only, contract, selector); | ||
} | ||
|
||
if (except.length > 0) { | ||
// exclude selectors found in except, include all others | ||
return !includes(except, contract, selector); | ||
} | ||
|
||
// if neither only or except are used, then include all selectors | ||
return true; | ||
} | ||
|
||
// returns true if the selector is found in the filters | ||
export function includes( | ||
filters: FacetFilter[], | ||
contract: string, | ||
selector: string, | ||
): boolean { | ||
return filters.some( | ||
(filter) => | ||
[filter.contract, AddressZero].includes(contract) && | ||
filter.selectors.includes(selector), | ||
); | ||
} | ||
|
||
// validates that different filter types do not contain the same contract | ||
export function validateFilters(only: FacetFilter[], except: FacetFilter[]) { | ||
if (only.length > 0 && except.length > 0) { | ||
only.forEach((o) => { | ||
except.forEach((e) => { | ||
if (o.contract === e.contract) { | ||
throw new Error( | ||
'only and except filters cannot contain the same contract', | ||
); | ||
} | ||
}); | ||
}); | ||
} | ||
} | ||
|
||
export function destructureFilters( | ||
filters: FacetFilter[], | ||
action?: FacetCutAction, | ||
) { | ||
const only = filters.filter((f) => f.type === 'only'); | ||
const except = filters.filter((f) => f.type === 'except'); | ||
|
||
if (action !== undefined) { | ||
only.filter((f) => f.action === action); | ||
except.filter((f) => f.action === action); | ||
} | ||
|
||
return { only, except }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,296 @@ | ||
import { | ||
FacetFilter, | ||
destructureFilters, | ||
selectorIsFiltered, | ||
validateFilters, | ||
} from './filters'; | ||
import { AddressZero } from '@ethersproject/constants'; | ||
import { Contract, ContractReceipt } from '@ethersproject/contracts'; | ||
import { | ||
IDiamondReadable, | ||
IDiamondWritable, | ||
} from '@solidstate/typechain-types'; | ||
|
||
export enum FacetCutAction { | ||
ADD, | ||
REPLACE, | ||
REMOVE, | ||
} | ||
|
||
export interface Facet { | ||
target: string; | ||
selectors: string[]; | ||
} | ||
|
||
export interface FacetCut extends Facet { | ||
action: FacetCutAction; | ||
} | ||
|
||
// returns a list of function signatures for a contract | ||
export function getFunctionSignatures(contract: Contract): string[] { | ||
return Object.keys(contract.interface.functions); | ||
} | ||
|
||
// returns a list of selectors for a contract | ||
export function getSelectors(contract: Contract): string[] { | ||
return getFunctionSignatures(contract).map((signature) => | ||
contract.interface.getSighash(signature), | ||
); | ||
} | ||
|
||
// returns a list of Facets for a contract | ||
export function getFacets(contracts: Contract[]): Facet[] { | ||
return contracts.map((contract) => { | ||
return { | ||
target: contract.address, | ||
selectors: getSelectors(contract), | ||
}; | ||
}); | ||
} | ||
|
||
// returns a FacetCut | ||
export function getFacetCut( | ||
target: string, | ||
selectors: string[], | ||
action: number = 0, | ||
): FacetCut { | ||
return { | ||
target: target, | ||
action: action, | ||
selectors: selectors, | ||
}; | ||
} | ||
|
||
// returns true if the selector is found in the facets | ||
export function selectorExistsInFacets( | ||
selector: string, | ||
facets: Facet[], | ||
): boolean { | ||
return facets.some((facet) => facet.selectors.includes(selector)); | ||
} | ||
|
||
// preview FacetCut which adds unregistered selectors | ||
export async function addUnregisteredSelectors( | ||
diamond: IDiamondReadable, | ||
contracts: Contract[], | ||
filters: FacetFilter[] = [], | ||
): Promise<FacetCut[]> { | ||
const { only, except } = destructureFilters(filters, FacetCutAction.ADD); | ||
validateFilters(only, except); | ||
|
||
const diamondFacets: Facet[] = await diamond.facets(); | ||
const facets = getFacets(contracts); | ||
|
||
let selectorsAdded = false; | ||
let facetCuts: FacetCut[] = []; | ||
|
||
// if facet selector is unregistered then it should be added to the diamond. | ||
for (const facet of facets) { | ||
for (const selector of facet.selectors) { | ||
const target = facet.target; | ||
|
||
if ( | ||
target !== diamond.address && | ||
selector.length > 0 && | ||
!selectorExistsInFacets(selector, diamondFacets) && | ||
selectorIsFiltered(only, except, target, selector) | ||
) { | ||
facetCuts.push( | ||
getFacetCut(facet.target, [selector], FacetCutAction.ADD), | ||
); | ||
|
||
selectorsAdded = true; | ||
} | ||
} | ||
} | ||
|
||
if (!selectorsAdded) { | ||
throw new Error('No selectors were added to FacetCut'); | ||
} | ||
|
||
return groupFacetCuts(facetCuts); | ||
} | ||
|
||
// preview FacetCut which replaces registered selectors with unregistered selectors | ||
export async function replaceRegisteredSelectors( | ||
diamond: IDiamondReadable, | ||
contracts: Contract[], | ||
filters: FacetFilter[] = [], | ||
): Promise<FacetCut[]> { | ||
const { only, except } = destructureFilters(filters, FacetCutAction.REPLACE); | ||
validateFilters(only, except); | ||
|
||
const diamondFacets: Facet[] = await diamond.facets(); | ||
const facets = getFacets(contracts); | ||
|
||
let selectorsReplaced = false; | ||
let facetCuts: FacetCut[] = []; | ||
|
||
// if a facet selector is registered with a different target address, the target will | ||
// be replaced | ||
for (const facet of facets) { | ||
for (const selector of facet.selectors) { | ||
const target = facet.target; | ||
const oldTarget = await diamond.facetAddress(selector); | ||
|
||
if ( | ||
target != oldTarget && | ||
target != AddressZero && | ||
target != diamond.address && | ||
selector.length > 0 && | ||
selectorExistsInFacets(selector, diamondFacets) && | ||
selectorIsFiltered(only, except, target, selector) | ||
) { | ||
facetCuts.push(getFacetCut(target, [selector], FacetCutAction.REPLACE)); | ||
|
||
selectorsReplaced = true; | ||
} | ||
} | ||
} | ||
|
||
if (!selectorsReplaced) { | ||
throw new Error('No selectors were replaced in FacetCut'); | ||
} | ||
|
||
return groupFacetCuts(facetCuts); | ||
} | ||
|
||
// preview FacetCut which removes registered selectors | ||
export async function removeRegisteredSelectors( | ||
diamond: IDiamondReadable, | ||
contracts: Contract[], | ||
filters: FacetFilter[] = [], | ||
): Promise<FacetCut[]> { | ||
const { only, except } = destructureFilters(filters, FacetCutAction.REMOVE); | ||
validateFilters(only, except); | ||
|
||
const diamondFacets: Facet[] = await diamond.facets(); | ||
const facets = getFacets(contracts); | ||
|
||
let selectorsRemoved = false; | ||
let facetCuts: FacetCut[] = []; | ||
|
||
// if a registered selector is not found in the facets then it should be removed | ||
// from the diamond | ||
for (const diamondFacet of diamondFacets) { | ||
for (const selector of diamondFacet.selectors) { | ||
const target = diamondFacet.target; | ||
|
||
if ( | ||
target != AddressZero && | ||
target != diamond.address && | ||
selector.length > 0 && | ||
!selectorExistsInFacets(selector, facets) && | ||
selectorIsFiltered(only, except, AddressZero, selector) | ||
) { | ||
facetCuts.push( | ||
getFacetCut(AddressZero, [selector], FacetCutAction.REMOVE), | ||
); | ||
|
||
selectorsRemoved = true; | ||
} | ||
} | ||
} | ||
|
||
if (!selectorsRemoved) { | ||
throw new Error('No selectors were removed from FacetCut'); | ||
} | ||
|
||
return groupFacetCuts(facetCuts); | ||
} | ||
|
||
// preview a FacetCut which adds, replaces, or removes selectors, as needed | ||
export async function previewFacetCut( | ||
diamond: IDiamondReadable, | ||
contracts: Contract[], | ||
filters: FacetFilter[] = [], | ||
): Promise<FacetCut[]> { | ||
let addFacetCuts: FacetCut[] = []; | ||
let replaceFacetCuts: FacetCut[] = []; | ||
let removeFacetCuts: FacetCut[] = []; | ||
|
||
try { | ||
addFacetCuts = await addUnregisteredSelectors(diamond, contracts, filters); | ||
} catch (error) { | ||
console.log(`WARNING: ${(error as Error).message}`); | ||
} | ||
|
||
try { | ||
replaceFacetCuts = await replaceRegisteredSelectors( | ||
diamond, | ||
contracts, | ||
filters, | ||
); | ||
} catch (error) { | ||
console.log(`WARNING: ${(error as Error).message}`); | ||
} | ||
|
||
try { | ||
removeFacetCuts = await removeRegisteredSelectors( | ||
diamond, | ||
contracts, | ||
filters, | ||
); | ||
} catch (error) { | ||
console.log(`WARNING: ${(error as Error).message}`); | ||
} | ||
|
||
return groupFacetCuts([ | ||
...addFacetCuts, | ||
...replaceFacetCuts, | ||
...removeFacetCuts, | ||
]); | ||
} | ||
|
||
// executes a DiamondCut using the provided FacetCut | ||
export async function diamondCut( | ||
diamond: IDiamondWritable, | ||
facetCut: FacetCut[], | ||
target: string = AddressZero, | ||
data: string = '0x', | ||
): Promise<ContractReceipt> { | ||
return (await diamond.diamondCut(facetCut, target, data)).wait(); | ||
} | ||
|
||
// groups facet cuts by target address and action type | ||
export function groupFacetCuts(facetCuts: FacetCut[]): FacetCut[] { | ||
const cuts = facetCuts.reduce((acc: FacetCut[], facetCut: FacetCut) => { | ||
if (acc.length == 0) acc.push(facetCut); | ||
|
||
let exists = false; | ||
|
||
acc.forEach((_, i) => { | ||
if ( | ||
acc[i].action == facetCut.action && | ||
acc[i].target == facetCut.target | ||
) { | ||
acc[i].selectors.push(...facetCut.selectors); | ||
// removes duplicates, if there are any | ||
acc[i].selectors = [...new Set(acc[i].selectors)]; | ||
exists = true; | ||
} | ||
}); | ||
|
||
// push facet cut if it does not already exist | ||
if (!exists) acc.push(facetCut); | ||
|
||
return acc; | ||
}, []); | ||
|
||
let cache: any = {}; | ||
|
||
// checks if selector is used multiple times, emits warning | ||
cuts.forEach((cut) => { | ||
cut.selectors.forEach((selector: string) => { | ||
if (cache[selector]) { | ||
console.log( | ||
`WARNING: selector: ${selector}, target: ${cut.target} is defined in multiple cuts`, | ||
); | ||
} else { | ||
cache[selector] = true; | ||
} | ||
}); | ||
}); | ||
|
||
return cuts; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The errors are thrown if all changes are skipped, but I think we'll want to throw if any change is skipped. Need to think about this though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would this be the result of using filters or something else?