-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
141 lines (120 loc) · 4.19 KB
/
index.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
import { extendEnvironment } from "hardhat/config";
import { task } from "hardhat/config";
import * as fs from "fs";
import * as path from "path";
import "@nomiclabs/hardhat-ethers";
interface CustomHardhatConfig {
separateContractSelectors?: boolean;
orderedByValue?: boolean;
}
declare module "hardhat/types/config" {
interface HardhatUserConfig {
functionSelectors?: CustomHardhatConfig;
}
interface HardhatConfig {
functionSelectors: CustomHardhatConfig;
}
}
extendEnvironment((hre) => {
hre.selectors = async () => {
if (!hre.config.functionSelectors) {
throw new Error(`Missing \`functionSelectors\` configuration in Hardhat config.
An example configuration looks like this:
module.exports = {
functionSelectors: {
separateContractSelectors: true, // separate by contract
orderedByValue: true, // order function selectors by hex value, least to greatest
},
// other configurations...
};
`);
}
const separateContractSelectors =
hre.config.functionSelectors.separateContractSelectors || false;
const orderedByValue = hre.config.functionSelectors.orderedByValue || false;
const artifactsDir = path.join(hre.config.paths.artifacts, "/contracts");
// Initialize an object to store all contracts selectors
let contractsSelectors: {
[contract: string]: { [selector: string]: string };
} = {};
const processDirectory = (directory: string) => {
const files = fs.readdirSync(directory);
for (let file of files) {
const filePath = path.join(directory, file);
if (fs.lstatSync(filePath).isDirectory()) {
processDirectory(filePath);
} else if (
path.extname(file) === ".json" &&
!file.endsWith(".dbg.json")
) {
const contractName = path.basename(file, ".json");
const artifact = JSON.parse(fs.readFileSync(filePath, "utf8"));
const abi = artifact.abi;
if (separateContractSelectors) {
contractsSelectors[contractName] = {};
}
for (let item of abi) {
if (item.type === "function") {
const abiCoder = new hre.ethers.utils.AbiCoder();
const packedData = abiCoder.encode(
["string"],
[
`${item.name}(${item.inputs
.map((i: any) => i.type)
.join(",")})`,
]
);
const signature = hre.ethers.utils.keccak256(packedData);
const selector = signature.slice(0, 10);
// Save the selector under the corresponding contract if enabled
if (separateContractSelectors) {
contractsSelectors[contractName][selector] = item.name;
} else {
contractsSelectors[selector] = item.name;
}
}
}
}
}
};
processDirectory(artifactsDir);
// Ordering function selectors by their hex value if enabled
if (orderedByValue) {
for (let contract in contractsSelectors) {
const unorderedSelectors = contractsSelectors[contract];
const orderedSelectors = Object.keys(unorderedSelectors)
.sort()
.reduce((obj, key) => {
obj[key] = unorderedSelectors[key];
return obj;
}, {} as { [selector: string]: string });
contractsSelectors[contract] = orderedSelectors;
}
}
// Write the contracts' selectors to a file
fs.writeFileSync(
"selectors.json",
JSON.stringify(contractsSelectors, null, 2)
);
console.log("Function selectors have been written to selectors.json");
};
});
// Define a new task that uses your plugin
task(
"selectors",
"Generate a file of function selectors",
async (args, hre) => {
await hre.selectors();
}
);
// This is needed for TypeScript not to complain about the hre.selectors() field
declare module "hardhat/types/config" {
export interface HardhatUserConfig {
selectors?: () => Promise<void>;
}
}
declare module "hardhat/types/runtime" {
interface HardhatRuntimeEnvironment {
selectors: () => Promise<void>;
}
}