-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathutils.ts
305 lines (259 loc) · 7.66 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
import { FacetFilter, selectorIsFiltered, validateFilters } from './filters';
import { AddressZero } from '@ethersproject/constants';
import { Contract } from '@ethersproject/contracts';
import {
IDiamondReadable,
IDiamondWritable,
} from '@solidstate/typechain-types';
export interface Facet {
target: string;
selectors: string[];
}
export enum FacetCutAction {
ADD,
REPLACE,
REMOVE,
}
export interface FacetCut extends Facet {
action: FacetCutAction;
}
// returns a list of signatures for a contract
export function getSignatures(contract: Contract): string[] {
return Object.keys(contract.interface.functions);
}
// returns a list of selectors for a contract
export function getSelectors(contract: Contract): string[] {
const signatures = getSignatures(contract);
return signatures.reduce((acc: string[], val: string) => {
acc.push(contract.interface.getSighash(val));
return acc;
}, []);
}
// 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 true if the selector is found in the facets
export function selectorExistsInFacets(
selector: string,
facets: Facet[],
): boolean {
for (const facet of facets) {
if (facet.selectors.includes(selector)) return true;
}
return false;
}
// preview FacetCut which adds unregistered selectors
export async function addUnregisteredSelectors(
diamond: IDiamondReadable,
contracts: Contract[],
only: FacetFilter[] = [],
exclude: FacetFilter[] = [],
): Promise<FacetCut[]> {
validateFilters(only, exclude);
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, exclude, target, selector)
) {
facetCuts.push(
printFacetCuts(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[],
only: FacetFilter[] = [],
exclude: FacetFilter[] = [],
): Promise<FacetCut[]> {
validateFilters(only, exclude);
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, exclude, target, selector)
) {
facetCuts.push(
printFacetCuts(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[],
only: FacetFilter[] = [],
exclude: FacetFilter[] = [],
): Promise<FacetCut[]> {
validateFilters(only, exclude);
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, exclude, AddressZero, selector)
) {
facetCuts.push(
printFacetCuts(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[],
only: FacetFilter[][] = [[], [], []],
exclude: FacetFilter[][] = [[], [], []],
): Promise<FacetCut[]> {
let addFacetCuts: FacetCut[] = [];
let replaceFacetCuts: FacetCut[] = [];
let removeFacetCuts: FacetCut[] = [];
try {
addFacetCuts = await addUnregisteredSelectors(
diamond,
contracts,
only[0],
exclude[0],
);
} catch (error) {
console.log(`WARNING: ${(error as Error).message}`);
}
try {
replaceFacetCuts = await replaceRegisteredSelectors(
diamond,
contracts,
only[1],
exclude[1],
);
} catch (error) {
console.log(`WARNING: ${(error as Error).message}`);
}
try {
removeFacetCuts = await removeRegisteredSelectors(
diamond,
contracts,
only[2],
exclude[2],
);
} 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',
) {
(await diamond.diamondCut(facetCut, target, data)).wait(1);
}
// 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;
}
export function printFacetCuts(
target: string,
selectors: string[],
action: number = 0,
): FacetCut {
return {
target: target,
action: action,
selectors: selectors,
};
}