-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathactionId.ts
More file actions
329 lines (280 loc) · 13.4 KB
/
actionId.ts
File metadata and controls
329 lines (280 loc) · 13.4 KB
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
328
329
import { FunctionFragment, Interface } from '@ethersproject/abi';
import fs from 'fs';
import lodash from 'lodash';
import path from 'path';
import logger from './logger';
import { request, gql } from 'graphql-request';
import Task, { TaskMode } from './task';
const { padEnd } = lodash;
export const ACTION_ID_DIRECTORY = path.resolve(process.cwd(), 'action-ids');
export type RoleData = {
role: string;
grantee: string;
target: string;
};
// Define a type alias with a more meaningful name
export type ActionIdData = Record<string, string>;
export type TaskActionIds = Record<string, ContractActionIdData>;
export type ContractActionIdData = {
useAdaptor: boolean;
factoryOutput?: string;
factoryName?: string;
actionIds: ActionIdData;
};
export type ActionIdInfo = {
taskId: string;
contractName: string;
signature: string;
useAdaptor: boolean;
};
interface TheGraphPermissionEntry {
id: string;
account: string;
action: {
id: string;
};
txHash: string;
}
function safeReadJsonFile<T>(filePath: string): Record<string, T> {
const fileExists = fs.existsSync(filePath) && fs.statSync(filePath).isFile();
return fileExists ? JSON.parse(fs.readFileSync(filePath).toString()) : {};
}
export function getTaskActionIds(task: Task): TaskActionIds {
const filePath = path.join(ACTION_ID_DIRECTORY, task.network, 'action-ids.json');
const actionIdFileContents = safeReadJsonFile<TaskActionIds>(filePath);
return actionIdFileContents[task.id];
}
export async function saveActionIds(
task: Task,
contractName: string,
factoryOutput?: string,
factoryName?: string
): Promise<void> {
logger.log(`Generating action IDs for ${contractName} of ${task.id}`, '');
const { useAdaptor, actionIds } = await getActionIds(task, contractName, factoryOutput, factoryName);
const actionIdsDir = path.join(ACTION_ID_DIRECTORY, task.network);
if (!fs.existsSync(actionIdsDir)) fs.mkdirSync(actionIdsDir, { recursive: true });
const filePath = path.join(actionIdsDir, 'action-ids.json');
// Load the existing content if any exists.
const newFileContents = safeReadJsonFile<TaskActionIds>(filePath);
// Write the new entry.
newFileContents[task.id] = newFileContents[task.id] ?? {};
newFileContents[task.id][contractName] = { useAdaptor, factoryOutput, actionIds, factoryName };
fs.writeFileSync(filePath, JSON.stringify(newFileContents, null, 2));
}
export async function checkActionIds(task: Task): Promise<void> {
const taskActionIdData = getTaskActionIds(task);
if (taskActionIdData === undefined) return;
for (const [contractName, actionIdData] of Object.entries(taskActionIdData)) {
const { useAdaptor: expectedUseAdaptor, actionIds: expectedActionIds } = await getActionIds(
task,
contractName,
actionIdData.factoryOutput,
actionIdData.factoryName
);
const adaptorUsageMatch = actionIdData.useAdaptor === expectedUseAdaptor;
const actionIdsMatch = Object.entries(expectedActionIds).every(
([signature, expectedActionId]) => actionIdData.actionIds[signature] === expectedActionId
);
if (adaptorUsageMatch && actionIdsMatch) {
logger.success(`Verified recorded action IDs of contract '${contractName}' of task '${task.id}'`);
} else {
throw Error(
`The recorded action IDs for '${contractName}' of task '${task.id}' does not match those calculated from onchain`
);
}
}
}
export function checkActionIdUniqueness(network: string): void {
const actionIdsDir = path.join(ACTION_ID_DIRECTORY, network);
const filePath = path.join(actionIdsDir, 'action-ids.json');
const actionIdFileContents = safeReadJsonFile<TaskActionIds>(filePath);
const duplicateActionIdsMapping = getDuplicateActionIds(actionIdFileContents);
const expectedCollisionsFilePath = path.join(actionIdsDir, 'expected-collisions.json');
const expectedDuplicateActionIdsMapping = safeReadJsonFile<TaskActionIds>(expectedCollisionsFilePath);
if (JSON.stringify(duplicateActionIdsMapping) === JSON.stringify(expectedDuplicateActionIdsMapping)) {
logger.success(`Verified that no contracts unexpectedly share action IDs for ${network}`);
} else {
for (const [actionId, instances] of Object.entries(duplicateActionIdsMapping)) {
if (JSON.stringify(instances) === JSON.stringify(expectedDuplicateActionIdsMapping[actionId])) {
// We expect some collisions of actionIds for cases where contracts share the same signature,
// such as those using the AuthorizerAdaptor. If the collisions *exactly* match those in the
// expected list, we can ignore them.
continue;
}
// If there are unexpected collisions while running `save-action-ids`, this will generate detailed
// warning messages. Follow the instructions below to update the `expected-collisions` file.
logger.warn(`${instances.length} contracts share the action ID: ${actionId}`);
for (const [index, actionIdInfo] of instances.entries()) {
const prefix = ` ${index + 1}: ${actionIdInfo.contractName}::${actionIdInfo.signature}`;
logger.warn(`${padEnd(prefix, 100)}(${actionIdInfo.taskId})`);
}
}
// Write a file called `updated-expected-collisions`, with new entries added to resolve the warnings.
//
// If there is no `expected-collisions` file for this network, simply review the new file to ensure the
// additions are valid, then rename `updated-expected-collisions` to `expected-collisions`.
// If there is already an`expected-collisions` file, check the diff, then replace the old file with this one.
//
// Never make manual changes to the `expected-collisions` file, as this might result in "unsorted"
// entries that cause `save-action-ids` to fail with no warnings.
//
// After renaming or replacing the collisions file, running `save-action-ids` again should
// produce no warnings.
fs.writeFileSync(
path.join(actionIdsDir, 'updated-expected-collisions.json'),
JSON.stringify(duplicateActionIdsMapping, null, 2)
);
throw Error(`There exist two duplicated action IDs across two separate contracts`);
}
}
export async function getActionIds(
task: Task,
contractName: string,
factoryOutput?: string,
factoryName?: string
): Promise<{ useAdaptor: boolean; actionIds: ActionIdData }> {
const artifact = task.artifact(contractName);
const { ignoredFunctions } = safeReadJsonFile<string[]>(path.join(ACTION_ID_DIRECTORY, 'ignored-functions.json'));
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const contractInterface = new Interface(artifact.abi as any);
const contractFunctions = Object.entries(contractInterface.functions)
.filter(([, func]) => ['nonpayable', 'payable'].includes(func.stateMutability))
.filter(([, func]) => !ignoredFunctions.includes(func.format()))
.sort(([sigA], [sigB]) => (sigA < sigB ? -1 : 1)); // Sort functions alphabetically.
const { useAdaptor, actionIdSource } = await getActionIdSource(task, contractName, factoryOutput, factoryName);
const actionIds: ActionIdData =
actionIdSource === undefined ? {} : await getActionIdsFromSource(contractFunctions, actionIdSource);
return { useAdaptor, actionIds };
}
/**
* Returns action ID source and a boolean indicating whether the source is the authorizer adaptor.
* For V3 contracts, it is tolerable to return undefined as the action ID source for permissionless contracts, as
* V3 does not interact with the authorizer adaptor at all.
*/
async function getActionIdSource(
task: Task,
contractName: string,
factoryOutput?: string,
factoryName?: string
): Promise<{ useAdaptor: boolean; actionIdSource: unknown | undefined }> {
const artifact = task.artifact(contractName);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const contractInterface = new Interface(artifact.abi as any);
// An exception is the V3 vault: Vault and extension actually have `getActionId` implemented in the vault admin.
if (task.id === '20241204-v3-vault' && (contractName === 'Vault' || contractName === 'VaultExtension')) {
const contract = await task.deployedInstance(contractName);
const vaultAdmin = await task.deployedInstance('VaultAdmin');
const contractAsAdmin = vaultAdmin.attach(contract.address as string) as any;
return { useAdaptor: false, actionIdSource: contractAsAdmin };
}
// Not all contracts use the Authorizer directly for authentication.
// Only if it has the `getActionId` function does it use the Authorizer directly.
// Contracts without this function either are permissionless or use another method such as the AuthorizerAdaptor.
const contractIsAuthorizerAware = Object.values(contractInterface.functions).some(
(func) => func.name === 'getActionId'
);
if (contractIsAuthorizerAware) {
if (factoryOutput) {
await checkFactoryOutput(task, contractName, factoryOutput, factoryName);
return { useAdaptor: false, actionIdSource: await task.instanceAt(contractName, factoryOutput) };
} else {
return { useAdaptor: false, actionIdSource: await task.deployedInstance(contractName) };
}
} else {
const adaptorTask = new Task('20220325-authorizer-adaptor', TaskMode.READ_ONLY, task.network);
const actionIdSource = await adaptorTask.optionalDeployedInstance('AuthorizerAdaptor');
return { useAdaptor: true, actionIdSource };
}
}
async function getActionIdsFromSource(
contractFunctions: [string, FunctionFragment][],
actionIdSource: any
): Promise<ActionIdData> {
const functionActionIds = await Promise.all(
contractFunctions.map(async ([signature, contractFunction]) => {
const functionSelector = Interface.getSighash(contractFunction);
return [signature, await actionIdSource.getActionId(functionSelector)] as [string, string];
})
);
return Object.fromEntries(functionActionIds);
}
function getDuplicateActionIds(actionIdFileContents: Record<string, TaskActionIds>): Record<string, ActionIdInfo[]> {
// Reverse the mapping of `contractName -> signature -> actionId` to be `actionId -> [contractName, signature][]`.
// This simplifies checking for duplicate actionIds to just reading the length of the arrays.
const actionIdsMapping: Record<string, ActionIdInfo[]> = Object.entries(actionIdFileContents)
.flatMap(([taskId, taskData]) =>
Object.entries(taskData).flatMap(([contractName, contractData]) =>
Object.entries(contractData.actionIds).map<[string, ActionIdInfo]>(([signature, actionId]) => [
actionId,
{ taskId, contractName, signature, useAdaptor: contractData.useAdaptor },
])
)
)
.reduce((acc: Record<string, ActionIdInfo[]>, [actionId, actionIdInfo]) => {
acc[actionId] = acc[actionId] ?? [];
acc[actionId].push(actionIdInfo);
return acc;
}, {});
const duplicateActionIdsMapping = Object.fromEntries(
Object.entries(actionIdsMapping).filter(([, instances]) => instances.length > 1)
);
return duplicateActionIdsMapping;
}
async function checkFactoryOutput(task: Task, contractName: string, factoryOutput: string, factoryName?: string) {
// We must check that the factory output is actually an instance of the expected contract type. This is
// not trivial due to usage of immutable and lack of knowledge of constructor arguments. However, this scenario
// only arises with Pools created from factories, all of which share a useful property: their factory contract
// name is <contractName>Factory, and they all have a function called 'isPoolFromFactory' we can use for this.
factoryName = factoryName ?? `${contractName}Factory`;
const factory = await task.deployedInstance(factoryName);
if (!(await factory.isPoolFromFactory(factoryOutput))) {
throw Error(`The contract at ${factoryOutput} is not an instance of a ${contractName} coming from ${factoryName}`);
}
}
/** Returns full info for a given actionId and network */
export function getActionIdInfo(actionId: string, network: string): ActionIdInfo | undefined {
// read network JSON file from action-ids dir
const tasks = safeReadJsonFile<TaskActionIds>(path.join(ACTION_ID_DIRECTORY, network, 'action-ids.json'));
// filter all the entries which have the same actionId
// and map them to an array of ActionIdInfo
const entries = Object.entries(tasks)
.filter(([, taskData]) =>
Object.values(taskData).some((contractData) =>
Object.entries(contractData.actionIds).some(([, hash]) => hash == actionId)
)
)
.map(([taskId, taskData]) => {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const contracts = Object.entries(taskData).filter(([, contractData]) =>
Object.entries(contractData.actionIds).some(([, hash]) => hash == actionId)
)!;
return contracts.map(([contractName, contractData]) => ({
taskId,
contractName,
useAdaptor: contractData.useAdaptor,
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
signature: Object.entries(contractData.actionIds).find(([, hash]) => hash == actionId)![0],
actionId,
}));
})
.flat();
// we return first entry because all the collisions are verified by scripts
return entries[0];
}
export async function fetchTheGraphPermissions(url: string): Promise<TheGraphPermissionEntry[]> {
const query = gql`
query {
permissions {
id
account
action {
id
}
txHash
}
}
`;
const data = await request<{ permissions: TheGraphPermissionEntry[] }>(url, query);
return data.permissions;
}