-
Notifications
You must be signed in to change notification settings - Fork 2
/
process-contracts.js
75 lines (57 loc) · 1.89 KB
/
process-contracts.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
const fs = require('fs');
const makeDirIfDoesntExist = (dirName) => {
if (!fs.existsSync(dirName)) {
const pathParts = dirName.split('/');
pathParts.reduce((acc, pathPart) => {
if (!acc) {
return pathPart;
}
const dir = `${acc}/${pathPart}`;
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}
return dir;
}, '');
}
};
const abiFolder = './dist/abi';
const contractFolder = './dist/contracts';
const processFile = (sourceFolder, fileName) => {
if (!fileName.includes('.json')) {
return;
}
const contract = require(`${sourceFolder}/${fileName}`);
if (contract && contract.abi) {
console.log(`Processing ${fileName}...`);
const abiJson = JSON.stringify(contract.abi, null, 2);
const abiFileName = `${abiFolder}/${fileName}`;
const contractJson = JSON.stringify(contract, null, 2);
const contractFileName = `${contractFolder}/${fileName}`;
fs.writeFile(abiFileName, abiJson, (err) => {
if (err) {
return console.log(err);
}
});
fs.writeFile(contractFileName, contractJson, (err) => {
if (err) {
return console.log(err);
}
});
}
}
const buildFolder = './build/contracts';
const extraFiles = [{
sourceFolder: './node_modules/@uniswap/v2-periphery/build',
fileName: 'IUniswapV2Router02.json'
}];
makeDirIfDoesntExist(abiFolder);
makeDirIfDoesntExist(contractFolder);
fs.readdir(buildFolder, (err, files) => {
if (!files || !files.length) {
console.log('Compile contracts first!');
return;
}
files.forEach((fileName) => processFile(buildFolder, fileName));
});
extraFiles.forEach(({ sourceFolder, fileName }) => processFile(sourceFolder, fileName));
console.log('Completed.');