-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathemerald-solidity.js
73 lines (64 loc) · 2.17 KB
/
emerald-solidity.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
const fs = require('fs');
const path = require('path');
const shell = require('shelljs');
let solc = require('solc');
function getFiles() {
const files = fs.readdirSync(path.resolve(process.cwd(), 'contracts'));
return files.map((file) => {
const p = path.resolve(process.cwd(), 'contracts', file);
return {
path: p,
contractName: file.split('.sol')[0],
source: fs.readFileSync(p).toString()
}
});
}
const reducer = (memo, {path, source}) => {
memo[path] = source;
return memo;
}
const version = fs.readFileSync(path.resolve(__dirname, '.solidity-version')).toString();
module.exports = () => {
solc = solc.setupMethods(require('./solc'));
const files = getFiles();
const input = {
sources: files.reduce(reducer, {}),
settings: {
evmVersion: "spuriousDragon"
}
};
const output = solc.compile(input, 1);
if (output.errors && output.errors.length > 0) {
return console.log('ERROR: \n', output.errors);
}
const keys = Object.keys(output.contracts);
for (let i = 0; i < keys.length; i++) {
const key = keys[i];
const outputItem = output.contracts[key];
const fileInput = files.find((file) => {
return file.path === key.split(':')[0];
});
const contractName = fileInput.contractName;
const p = path.resolve(process.cwd(), 'build/contracts/', `${contractName}.json`);
// try to be compatible with truffle-contract-schema spec: https://github.com/trufflesuite/truffle/blob/next/packages/truffle-contract-schema/spec/contract-object.spec.json
const artifact = {
contractName: fileInput.contractName,
abi: outputItem.interface,
bytecode: outputItem.bytecode,
deployedBytecode: outputItem.runtimeBytecode,
sourceMap: outputItem.srcmap,
deployedSourceMap: outputItem.srcmapRuntime,
source: fileInput.source,
sourcePath: fileInput.path,
ast: output.sources[fileInput.path].AST,
compiler: {
name: "solc",
version
},
networks: {},
updatedAt: new Date().toISOString()
};
shell.mkdir('-p', path.resolve(process.cwd(), 'build/contracts'));
fs.writeFileSync(p, JSON.stringify(artifact, null, 2), 'utf8');
};
}