-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
88 lines (78 loc) · 2.71 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
import {extendConfig} from "hardhat/config"
import {HardhatPluginError} from "hardhat/plugins";
import "@nomiclabs/hardhat-ethers"
import 'hardhat/types/config';
import './tasks/compile';
import './tasks/selectors';
interface FunctionSelectorsUserConfigEntry {
separateContractSelectors?: boolean
orderedByValue?: boolean
pretty?: boolean
runOnCompile?: boolean
includeParams?: boolean
outputPath?: string,
only?: string[]
except?: string[]
skipSelectors?: string[]
}
export interface FunctionSelectorsConfigEntry {
separateContractSelectors: boolean
orderedByValue: boolean
pretty: boolean
runOnCompile: boolean
includeParams: boolean
outputPath: string,
only: string[]
except: string[]
skipSelectors: string[]
}
declare module "hardhat/types/config" {
interface HardhatUserConfig {
functionSelectors?: FunctionSelectorsUserConfigEntry | FunctionSelectorsUserConfigEntry[]
}
interface HardhatConfig {
functionSelectors: FunctionSelectorsConfigEntry[]
}
}
const DEFAULT_CONFIG : FunctionSelectorsConfigEntry = {
separateContractSelectors: true,
orderedByValue: false,
outputPath: "./selectors.json",
pretty: true,
runOnCompile: true,
includeParams: true,
only: [],
except: [],
skipSelectors: [],
}
extendConfig((config, userConfig) => {
config.functionSelectors = [userConfig.functionSelectors].flat().map((el) => {
// if any values not provided by user, go with defaults
const conf = Object.assign({}, DEFAULT_CONFIG, el)
// check if only/except have an overlap
const overlap = conf.only.filter(value => conf.except.includes(value))
if (overlap.length>0) {
throw new HardhatPluginError(
"function-selectors-plugin",
`\`only\` & \`except\` elements cannot overlap: [${overlap}]`,
);
}
// check if any selectors not correct len
const incorrectLen = conf.skipSelectors.filter((sel) => sel.length !== 10)
if(incorrectLen.length > 0){
throw new HardhatPluginError(
"function-selectors-plugin",
`selectors in \`skipSelectors\` must be 10 characters: [${incorrectLen}]`,
);
}
// check if any selectors don't start with "0x"
const incorrectPrefix = conf.skipSelectors.filter((sel) => !sel.startsWith("0x"))
if(incorrectLen.length > 0){
throw new HardhatPluginError(
"function-selectors-plugin",
`selectors in \`skipSelectors\` must be start with a \`0x\`: [${incorrectPrefix}]`,
);
}
return conf as FunctionSelectorsConfigEntry
})
})