Skip to content

Commit

Permalink
adds selectors, and diamond cut
Browse files Browse the repository at this point in the history
  • Loading branch information
0xCourtney committed Oct 30, 2022
1 parent 5b75921 commit e238a29
Showing 1 changed file with 49 additions and 10 deletions.
59 changes: 49 additions & 10 deletions lib/diamonds.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { Contract } from 'ethers';
import { Contract, constants } from 'ethers';

// TODO: import IDiamond type

export interface Facet {
target: string;
Expand Down Expand Up @@ -49,16 +51,44 @@ export function printFacetCuts(
};
}

// attempts to add a selector, if selector already exists, function will throw
export async function insertUnregisteredSelector(
// adds unregistered selectors
export async function addUnregisteredSelector(
diamond: Contract,
facets: Contract[],
) {}
contracts: Contract[],
) {
const registeredFacets: Facet[] = await diamond.facets();

let facets: Facet[] = [];
contracts.forEach((contract) => {
facets.push({
target: contract.address,
selectors: getContractSelectors(contract),
});
});

let facetCuts: FacetCut[] = [];
// if selector is not found in the registered selectors then it should be
// added to the diamond. this will not include selectors which need to be
// replaced following an upgrade.
facets.forEach((facet) => {
facet.selectors.forEach((selector) => {
const target = facet.target;
if (
target !== diamond.address &&
!selectorExistsInFacet(selector, registeredFacets)
) {
// TODO: Group selectors into a single object
facetCuts.push(
printFacetCuts(facet.target, [selector], FacetCutAction.Add),
);
}
});
});

// adds selectors, if selector already exists function will update with new address
export async function upsertSelectors(diamond: Contract, facets: Contract[]) {}
return facetCuts;
}

// removes registered selectors if they are not present in the contracts
// removes registered selectors
export async function removeRegisteredSelectors(
diamond: Contract,
contracts: Contract[],
Expand All @@ -82,10 +112,10 @@ export async function removeRegisteredSelectors(
target !== diamond.address &&
!selectorExistsInFacet(selector, facets)
) {
// TODO: Group removed selectors together in a single object
// TODO: Group selectors into a single object
facetCuts.push(
printFacetCuts(
registeredFacet.target,
constants.AddressZero,
[selector],
FacetCutAction.Remove,
),
Expand All @@ -96,3 +126,12 @@ export async function removeRegisteredSelectors(

return facetCuts;
}

export async function diamondCut(
diamond: Contract,
facetCut: FacetCut[],
target: string = constants.AddressZero,
data: string = '0x',
) {
(await diamond.diamondCut(facetCut, target, data)).wait(1);
}

0 comments on commit e238a29

Please sign in to comment.