forked from web3/web3.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompile_contracts.js
executable file
·117 lines (93 loc) · 3.36 KB
/
compile_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
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
#!/usr/bin/env node
/*
This file is part of web3.js.
web3.js is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
web3.js is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with web3.js. If not, see <http://www.gnu.org/licenses/>.
*/
/* eslint-disable header/header */
/* eslint-disable @typescript-eslint/no-var-requires */
const { promisify } = require('util');
const { resolve } = require('path');
const { compile } = require('solc');
const { rm, readdirSync, readFileSync, writeFileSync, mkdirSync, existsSync } = require('fs');
const rmPromise = promisify(rm);
// Fetch path of build
const buildPath = resolve(__dirname, '../fixtures/build');
const contractsPath = resolve(__dirname, '../fixtures/contracts');
const importDir = resolve(__dirname, '../node_modules');
function findImports(path) {
const importPath = resolve(importDir, path);
if (existsSync(importPath)) {
return {
contents: readFileSync(importPath, 'utf8'),
};
}
return { error: 'File not found' };
}
(async () => {
try {
await rmPromise(buildPath, { recursive: true });
} catch (error) {
// Ignore if directory does not exists
if (error.code !== 'ENOENT') {
throw error;
}
}
// Fetch all Contract files in Contracts folder
const fileNames = readdirSync(contractsPath);
// Gets ABI of all contracts into variable input
const input = fileNames.reduce(
(previousValue, fileName) => {
const filePath = resolve(contractsPath, fileName);
const source = readFileSync(filePath, 'utf8');
return { sources: { ...previousValue.sources, [fileName]: { content: source } } };
},
{ sources: {} },
);
const compileInput = {
...input,
language: 'Solidity',
settings: { outputSelection: { '*': { '*': ['abi', 'evm.bytecode.object'] } } },
};
const compileResult = JSON.parse(
compile(JSON.stringify(compileInput), { import: findImports }),
);
if (compileResult.errors) {
console.error(compileResult.errors);
console.log('Error while compiling');
}
// Compile all contracts
const output = compileResult.contracts;
// Re-Create build folder for output files from each contract
mkdirSync(buildPath);
console.log(output);
// Output contains all objects from all contracts
// Write the contents of each to different files
for (let contract in output) {
const contractName = contract.replace('.sol', '');
const contractBuild = output[contract][contractName];
if (!contractBuild || (contractBuild && !contractBuild['abi'])) {
continue;
}
const contractTsInterface = `export const ${contractName}Abi = ${JSON.stringify(
contractBuild['abi'],
)} as const; \n export const ${contractName}Bytecode = '0x${
contractBuild['evm']['bytecode']['object']
}';`;
writeFileSync(
resolve(buildPath, contractName + '.json'),
JSON.stringify(contractBuild, null, '\t'),
);
writeFileSync(resolve(buildPath, contractName + '.ts'), contractTsInterface);
}
console.info('Compiled successfully');
process.exit(0);
})().catch(console.error);