-
Notifications
You must be signed in to change notification settings - Fork 0
/
abis.js
57 lines (47 loc) · 1.54 KB
/
abis.js
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
const fs = require("fs");
const path = require("path");
const abiDir = "abis";
const contractsDir = "contracts";
const artifactsDir = path.join(contractsDir, "out");
const contractsToExtract = ["OracleCore"];
if (!fs.existsSync(abiDir)) {
fs.mkdirSync(abiDir);
}
function checkArtifactsDirectory() {
if (!fs.existsSync(artifactsDir)) {
console.error(`The artifacts directory '${artifactsDir}' does not exist.`);
console.log('Please compile your contracts first using "forge build"');
process.exit(1);
}
const files = fs.readdirSync(artifactsDir);
if (files.length === 0) {
console.error(`The artifacts directory '${artifactsDir}' is empty.`);
console.log(
'Please compile your contracts first using "forge build" or confirm the path is correct.'
);
process.exit(1);
}
}
function extractAbi(contractName) {
const outputPath = path.join(
artifactsDir,
`${contractName}.sol`,
`${contractName}.json`
);
const abiOutputPath = path.join(abiDir, `${contractName}.json`);
try {
const contractData = JSON.parse(fs.readFileSync(outputPath, "utf8"));
const abi = JSON.stringify(contractData.abi, null, 2);
fs.writeFileSync(abiOutputPath, abi);
console.log(`Extracted ABI for ${contractName}`);
} catch (error) {
console.error(`Error extracting ABI for ${contractName}:`, error.message);
}
}
checkArtifactsDirectory();
for (const contractName of contractsToExtract) {
extractAbi(contractName);
}
console.log(
'ABI extraction complete. Check the "abis" directory for the output.'
);