Skip to content

Commit e238a29

Browse files
committed
adds selectors, and diamond cut
1 parent 5b75921 commit e238a29

File tree

1 file changed

+49
-10
lines changed

1 file changed

+49
-10
lines changed

lib/diamonds.ts

+49-10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import { Contract } from 'ethers';
1+
import { Contract, constants } from 'ethers';
2+
3+
// TODO: import IDiamond type
24

35
export interface Facet {
46
target: string;
@@ -49,16 +51,44 @@ export function printFacetCuts(
4951
};
5052
}
5153

52-
// attempts to add a selector, if selector already exists, function will throw
53-
export async function insertUnregisteredSelector(
54+
// adds unregistered selectors
55+
export async function addUnregisteredSelector(
5456
diamond: Contract,
55-
facets: Contract[],
56-
) {}
57+
contracts: Contract[],
58+
) {
59+
const registeredFacets: Facet[] = await diamond.facets();
60+
61+
let facets: Facet[] = [];
62+
contracts.forEach((contract) => {
63+
facets.push({
64+
target: contract.address,
65+
selectors: getContractSelectors(contract),
66+
});
67+
});
68+
69+
let facetCuts: FacetCut[] = [];
70+
// if selector is not found in the registered selectors then it should be
71+
// added to the diamond. this will not include selectors which need to be
72+
// replaced following an upgrade.
73+
facets.forEach((facet) => {
74+
facet.selectors.forEach((selector) => {
75+
const target = facet.target;
76+
if (
77+
target !== diamond.address &&
78+
!selectorExistsInFacet(selector, registeredFacets)
79+
) {
80+
// TODO: Group selectors into a single object
81+
facetCuts.push(
82+
printFacetCuts(facet.target, [selector], FacetCutAction.Add),
83+
);
84+
}
85+
});
86+
});
5787

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

61-
// removes registered selectors if they are not present in the contracts
91+
// removes registered selectors
6292
export async function removeRegisteredSelectors(
6393
diamond: Contract,
6494
contracts: Contract[],
@@ -82,10 +112,10 @@ export async function removeRegisteredSelectors(
82112
target !== diamond.address &&
83113
!selectorExistsInFacet(selector, facets)
84114
) {
85-
// TODO: Group removed selectors together in a single object
115+
// TODO: Group selectors into a single object
86116
facetCuts.push(
87117
printFacetCuts(
88-
registeredFacet.target,
118+
constants.AddressZero,
89119
[selector],
90120
FacetCutAction.Remove,
91121
),
@@ -96,3 +126,12 @@ export async function removeRegisteredSelectors(
96126

97127
return facetCuts;
98128
}
129+
130+
export async function diamondCut(
131+
diamond: Contract,
132+
facetCut: FacetCut[],
133+
target: string = constants.AddressZero,
134+
data: string = '0x',
135+
) {
136+
(await diamond.diamondCut(facetCut, target, data)).wait(1);
137+
}

0 commit comments

Comments
 (0)