-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
105 lines (93 loc) · 3.3 KB
/
index.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
const cliLogger = require('cli-logger')
const fs = require('fs')
const { enforceOrThrow } = require('./util')
const logger = cliLogger({ level: 'info' })
// Contstants
const BUILD_PATH = "./abigenBindings/";
const ABI_PATH = BUILD_PATH + "abi/"
const BIN_PATH = BUILD_PATH + "bin/"
module.exports = async (config) => {
const options = parseConfig(config)
createDirectories(options);
// Set debug logging
if (config.debug) logger.level('debug')
logger.debug('DEBUG logging is turned ON')
let contracts= [];
if (options.contracts.length === 0) {
contracts = await getAllArtifacts(options);
} else {
for (const contractName of options.contracts) {
const c = getArtifact(contractName, options);
const importedContracts = parseImports(options, c);
contracts.push(c);
importedContracts.forEach(x => contracts.push(x));
}
}
contracts.forEach(contract => writeAbigen(contract.contractName, contract.abi, contract.bytecode));
}
const parseImports = (options, blob) => {
split = blob.source.split(";")
let imports = [];
split.forEach(line => {
if(line.indexOf("import") > -1) {
let path = line.split("import")[1].replace(/['"]+/g, '').replace(" ","");
let split = path.split("/");
let name = split[split.length - 1].replace(".sol", "");
imports.push(getArtifact(name, options));
}
})
return imports
}
const parseConfig = (config) => {
let contracts = [];
if (config._.length > 1) {
contracts = config._.slice(1);
logger.info("Contracts supplied!")
} else {
logger.info("No contracts supplied, generating bindings for all built contracts.")
}
const workingDir = config.working_directory
const contractsBuildDir = config.contracts_build_directory
return {
workingDir,
contractsBuildDir,
contracts
}
}
const getArtifact = (contractName, options) => {
// Construct artifact path and read artifact
let artifactPath = `${options.contractsBuildDir}/${contractName}.json`
logger.debug(`Reading artifact file at ${artifactPath}`)
enforceOrThrow(fs.existsSync(artifactPath), `Could not find ${contractName} artifact at ${artifactPath}`)
// Stringify + parse to make a deep copy (to avoid bugs with PR #19)
return JSON.parse(JSON.stringify(require(artifactPath)))
}
const getAllArtifacts = async (options) => {
let files = fs.readdirSync("./build/contracts");
const content = [];
logger.info("here")
files.forEach(file => {
const contractName = file.split(".")[0];
const artifact = getArtifact(contractName, options);
logger.info(artifact)
if (artifact.abi.length !== 0) {
content.push(artifact);
}
});
return content;
}
const createDirectories = () => {
try {
fs.rmdirSync(BUILD_PATH, { recursive: true });
fs.mkdirSync(BUILD_PATH);
fs.mkdirSync(ABI_PATH);
fs.mkdirSync(BIN_PATH);
} catch (e) {
logger.error(e)
}
}
const writeAbigen = (contractName, abi, bytecode) => {
bytecode = bytecode.substring(2);
fs.writeFileSync(ABI_PATH + contractName + ".abi", JSON.stringify(abi))
fs.writeFileSync(BIN_PATH + contractName + ".bin", bytecode)
}