1
- import { Contract } from 'ethers' ;
1
+ import { Contract , constants } from 'ethers' ;
2
+
3
+ // TODO: import IDiamond type
2
4
3
5
export interface Facet {
4
6
target : string ;
@@ -49,16 +51,44 @@ export function printFacetCuts(
49
51
} ;
50
52
}
51
53
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 (
54
56
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
+ } ) ;
57
87
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
+ }
60
90
61
- // removes registered selectors if they are not present in the contracts
91
+ // removes registered selectors
62
92
export async function removeRegisteredSelectors (
63
93
diamond : Contract ,
64
94
contracts : Contract [ ] ,
@@ -82,10 +112,10 @@ export async function removeRegisteredSelectors(
82
112
target !== diamond . address &&
83
113
! selectorExistsInFacet ( selector , facets )
84
114
) {
85
- // TODO: Group removed selectors together in a single object
115
+ // TODO: Group selectors into a single object
86
116
facetCuts . push (
87
117
printFacetCuts (
88
- registeredFacet . target ,
118
+ constants . AddressZero ,
89
119
[ selector ] ,
90
120
FacetCutAction . Remove ,
91
121
) ,
@@ -96,3 +126,12 @@ export async function removeRegisteredSelectors(
96
126
97
127
return facetCuts ;
98
128
}
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