-
Notifications
You must be signed in to change notification settings - Fork 92
/
Copy pathdiamonds.ts
327 lines (284 loc) · 7.57 KB
/
diamonds.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
import {
IDiamondReadable,
IDiamondWritable,
} from '@solidstate/typechain-types';
import { Contract, constants } from 'ethers';
const Table = require('cli-table3');
type Diamond = IDiamondReadable & IDiamondWritable;
export interface Facet {
target: string;
selectors: string[];
}
enum FacetCutAction {
Add,
Replace,
Remove,
}
export interface FacetCut extends Facet {
action: FacetCutAction;
}
export function getSignatures(contract: Contract): string[] {
return Object.keys(contract.interface.functions);
}
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;
}, []);
}
export function getFacets(contracts: Contract[]): Facet[] {
return contracts.map((contract) => {
return {
target: contract.address,
selectors: getSelectors(contract),
};
});
}
export function selectorExistsInFacets(
selector: string,
facets: Facet[],
): boolean {
for (const facet of facets) {
if (facet.selectors.includes(selector)) return true;
}
return false;
}
// adds unregistered selectors
export async function addUnregisteredSelectors(
diamond: Diamond,
contracts: Contract[],
exclude: string[] = [],
): Promise<FacetCut[]> {
const diamondFacets: Facet[] = await diamond.facets();
const facets = getFacets(contracts);
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) &&
!exclude.includes(selector)
) {
facetCuts.push(
printFacetCuts(facet.target, [selector], FacetCutAction.Add),
);
}
}
}
return groupFacetCuts(facetCuts);
}
// replace registered selectors
export async function replaceRegisteredSelectors(
diamond: Diamond,
contracts: Contract[],
exclude: string[] = [],
): Promise<FacetCut[]> {
const diamondFacets: Facet[] = await diamond.facets();
const facets = getFacets(contracts);
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 != constants.AddressZero &&
target != diamond.address &&
selector.length > 0 &&
selectorExistsInFacets(selector, diamondFacets) &&
!exclude.includes(selector)
) {
facetCuts.push(
printFacetCuts(target, [selector], FacetCutAction.Replace),
);
}
}
}
return groupFacetCuts(facetCuts);
}
// removes registered selectors
export async function removeRegisteredSelectors(
diamond: Diamond,
contracts: Contract[],
exclude: string[] = [],
): Promise<FacetCut[]> {
const diamondFacets: Facet[] = await diamond.facets();
const facets = getFacets(contracts);
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 != constants.AddressZero &&
target != diamond.address &&
selector.length > 0 &&
!selectorExistsInFacets(selector, facets) &&
!exclude.includes(selector)
) {
facetCuts.push(
printFacetCuts(
constants.AddressZero,
[selector],
FacetCutAction.Remove,
),
);
}
}
}
return groupFacetCuts(facetCuts);
}
export async function diamondCut(
diamond: Diamond,
facetCut: FacetCut[],
target: string = constants.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,
};
}
// generates table of diamond and facet selectors
export async function printDiamond(diamond: Diamond, contracts: Contract[]) {
const padding = 2;
const table = new Table({
style: {
head: [],
border: [],
'padding-left': padding,
'padding-right': padding,
},
chars: {
mid: '·',
'top-mid': '|',
'left-mid': ' ·',
'mid-mid': '|',
'right-mid': '·',
left: ' |',
'top-left': ' ·',
'top-right': '·',
'bottom-left': ' ·',
'bottom-right': '·',
middle: '·',
top: '-',
bottom: '-',
'bottom-mid': '|',
},
});
table.push([
{
hAlign: 'center',
content: `Target`,
},
{
hAlign: 'center',
content: `Signature`,
},
{
hAlign: 'center',
content: `Selector`,
},
{
hAlign: 'center',
content: `Registered`,
},
]);
let diamondTable = [];
const signatures = await getSignatures(diamond);
for (const signature of signatures) {
diamondTable.push({
target: diamond.address,
signature: signature,
selector: diamond.interface.getSighash(signature),
registered: true,
});
}
for (const contract of contracts) {
const signatures = await getSignatures(contract);
for (const signature of signatures) {
diamondTable.push({
target: contract.address,
signature: signature,
selector: contract.interface.getSighash(signature),
registered: false,
});
}
}
const diamondFacets: Facet[] = await diamond.facets();
for (const facet of diamondFacets) {
const target = facet.target;
for (const selector of facet.selectors) {
for (const row of diamondTable) {
if (row.target == target && row.selector == selector) {
row.registered = true;
}
}
}
}
for (const row of diamondTable) {
table.push([
{
content: row.target,
},
{
content: row.signature,
},
{
content: row.selector,
},
{
content: row.registered,
},
]);
}
console.log(table.toString());
}