forked from BANKEX/solidity-float-point-calculation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compile.js
96 lines (85 loc) · 2.98 KB
/
compile.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
const assert = require('assert');
var fs = require("fs");
var solc = require('solc');
const Artifactor = require("truffle-artifactor");
const TruffleContract = require('truffle-contract');
const Web3 = require("web3");
const util = require('util');
const artifactor = new Artifactor("./build/contracts");
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
const Contracts = ['Tester:Tester']
function findImports (path) {
try{
var input = fs.readFileSync("./contracts/"+path);
var ret = {contents: input.toString()};
// ret[path] = input.toString();
return ret;
}
catch(error){
return { error: 'File not found' }
}
}
async function main() {
console.log("Compiling contracts...");
var inputs = {};
for (contract of Contracts){
var parts = contract.split(':');
inputs[contract+'.sol'] = fs.readFileSync("./contracts/"+parts[0]+".sol", 'utf8');
}
try{
var output = solc.compile({ sources: inputs }, 1, findImports)
}
catch(error){
console.log(error);
}
for (err in output.errors){
console.log(output.errors[err]);
}
if (!output.contracts || Object.keys(output.contracts).length == 0){
throw("Didn't compile");
}
// new RegExp('__([a-zA-Z.:]+)[_]*', 'g');
// const libraryRE = /__([a-zA-Z.:]+)[_]*/g.compile();
const libraryRE = new RegExp('__([a-zA-Z.:]+)[_]*', 'g');
for (var property in output.contracts) {
if (output.contracts.hasOwnProperty(property)) {
var contract = output.contracts[property];
var bytecode = contract.bytecode;
if (libraryRE.test(bytecode)){
console.log("Linking is necessary!");
var m;
// bytecodeCopy = bytecode;
m = libraryRE.exec(bytecode);
while ((m = libraryRE.exec(bytecode)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === libraryRE.lastIndex) {
libraryRE.lastIndex++;
}
// The result can be accessed through the `m`-variable.
const libName = m[1];
const libObject = {};
libObject[libName] = output.contracts[libName].bytecode;
// for (var property2 in output.contracts) {
// if (output.contracts.hasOwnProperty(property2)) {
// if (property2 == libName){
// libObject[libName] = output.contracts[property2].bytecode;
// }
// }
// }
// bytecode = solc.linkBytecode(bytecode, libObject);
}
}
var meta = JSON.parse(contract.metadata);
if (bytecode == ""){
console.log("Bytecode is empty!");
}
var abi = JSON.parse(contract.interface);
const p = property.split(':');
await artifactor.save({contract_name: p[p.length-1], abi: abi, unlinked_binary: bytecode});
}
}
console.log("Done compiling");
}
module.exports = main;