-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.js
113 lines (93 loc) · 2.74 KB
/
build.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
const solc = require('solc')
const fs = require('fs')
const path = require('path')
const CONTRACTS_PATH = path.resolve(__dirname, 'src')
const BUILD_PATH = path.resolve(__dirname, 'dist')
function isSolidity(filename) {
return filename.endsWith('.sol')
}
function isOpenzeppelin(import_path) {
return import_path.startsWith("@openzeppelin")
}
function removeSync(path) {
if (fs.existsSync(path)) {
fs.rmSync(path, { recursive: true})
}
}
function ensureDirSync(path) {
if (!fs.existsSync(path)) {
fs.mkdirSync(path, {recursive : true})
}
}
function findImports(import_path) {
let sourcePath
if (isOpenzeppelin(import_path)) {
sourcePath = path.resolve(__dirname, 'node_modules', import_path);
} else {
sourcePath = path.resolve(contractsPath, import_path);
}
const source = fs.readFileSync(sourcePath, 'utf-8');
return {contents: source};
}
function compile(contractsPath) {
console.log(`Compiling contracts...`)
const sources = {}
fs.readdirSync(contractsPath).forEach(file => {
if (isSolidity(file)) {
const sourcePath = path.resolve(contractsPath, file);
const source = fs.readFileSync(sourcePath, 'utf-8');
sources[file] = {'content': source}
}
});
const input = {
language: 'Solidity',
sources: sources,
settings: {
optimizer: {
enabled: true,
},
outputSelection: {
'*': {
'*': [ '*' ]
}
}
}
}
const output = JSON.parse(solc.compile(JSON.stringify(input), { import: findImports }))
if (output.errors) {
console.error(output.errors)
}
const contracts = []
for (const sourceName in sources) {
for (const contractName in output.contracts[sourceName]) {
contracts.push({
contractName: contractName,
abi: output.contracts[sourceName][contractName].abi,
bytecode: output.contracts[sourceName][contractName].evm.bytecode.object,
deployedBytecode: output.contracts[sourceName][contractName].evm.deployedBytecode.object
})
}
}
return contracts
}
function saveContracts(contracts, buildPath) {
contracts.forEach(({contractName, abi, bytecode, deployedBytecode}) => {
const contractBinary = {
abi,
bytecode,
deployedBytecode
}
const pathname = path.resolve(buildPath, contractName + ".json")
fs.writeFileSync(pathname, JSON.stringify(contractBinary), 'utf-8')
})
}
function main(contractsPath, buildPath) {
// 1. Recreate build folder
removeSync(buildPath);
ensureDirSync(buildPath);
// 2. Compile contracts
const contracts = compile(contractsPath)
// 3. Save compilation
saveContracts(contracts, buildPath)
}
main(CONTRACTS_PATH, BUILD_PATH)