-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtutorial1.js
152 lines (123 loc) · 4.35 KB
/
tutorial1.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Tutorial 1
// Command Line: node tutorial1.js --build example.sol
// The require packages
const path = require('path');
const fs = require('fs');
const solc = require('solc');
const md5File = require('md5-file');
// Retrieve the command line arguments
var argv = require('minimist')(process.argv.slice(2), { string: ['checkminter'] });
// Input parameters for solc
// Refer to https://solidity.readthedocs.io/en/develop/using-the-compiler.html#compiler-input-and-output-json-description
var solcInput = {
language: "Solidity",
sources: { },
settings: {
optimizer: {
enabled: true
},
evmVersion: "byzantium",
outputSelection: {
"*": {
"": [
"legacyAST",
"ast"
],
"*": [
"abi",
"evm.bytecode.object",
"evm.bytecode.sourceMap",
"evm.deployedBytecode.object",
"evm.deployedBytecode.sourceMap",
"evm.gasEstimates"
]
},
}
}
};
// Try to lookup imported sol files in "contracts" folder or "node_modules" folder
function findImports(importFile) {
console.log("Import File:" + importFile);
try {
// Find in contracts folder first
result = fs.readFileSync("contracts/" + importFile, 'utf8');
return { contents: result };
} catch (error) {
// Try to look into node_modules
try {
result = fs.readFileSync("node_modules/" + importFile, 'utf8');
return { contents: result };
} catch (error) {
console.log(error.message);
return { error: 'File not found' };
}
}
}
// Compile the sol file in "contracts" folder and output the built json file to "build/contracts"
function buildContract(contract) {
let contractFile = 'contracts/' + contract;
let jsonOutputName = path.parse(contract).name + '.json';
let jsonOutputFile = './build/contracts/' + jsonOutputName;
let result = false;
try {
result = fs.statSync(contractFile);
} catch (error) {
console.log(error.message);
return false;
}
let contractFileChecksum = md5File.sync(contractFile);
try {
fs.statSync(jsonOutputFile);
let jsonContent = fs.readFileSync(jsonOutputFile, 'utf8');
let jsonObject = JSON.parse(jsonContent);
let buildChecksum = '';
if (typeof jsonObject['contracts'][contract]['checksum'] != 'undefined') {
buildChecksum = jsonObject['contracts'][contract]['checksum'];
console.log('File Checksum: ' + contractFileChecksum);
console.log('Build Checksum: ' + buildChecksum);
if (contractFileChecksum === buildChecksum) {
console.log('No build is required due no change in file.');
console.log('==============================');
return true;
}
}
} catch (error) {
// Any file not found, will continue build
}
let contractContent = fs.readFileSync(contractFile, 'utf8');
console.log('Contract File: ' + contract);
solcInput.sources[contract] = {
"content": contractContent
};
let solcInputString = JSON.stringify(solcInput);
let output = solc.compileStandardWrapper(solcInputString, findImports);
let jsonOutput = JSON.parse(output);
let isError = false;
if (jsonOutput.errors) {
jsonOutput.errors.forEach(error => {
console.log(error.severity + ': ' + error.component + ': ' + error.formattedMessage);
if (error.severity == 'error') {
isError = true;
}
});
}
if (isError) {
// Compilation errors
console.log('Compile error!');
return false;
}
// Update the sol file checksum
jsonOutput['contracts'][contract]['checksum'] = contractFileChecksum;
let formattedJson = JSON.stringify(jsonOutput, null, 4);
// Write the output JSON
fs.writeFileSync('./build/contracts/' + jsonOutputName, formattedJson);
console.log('==============================');
return true;
}
if (typeof argv.build !== 'undefined') {
// Build contract
var contract = argv.build;
let result = buildContract(contract);
return;
}
console.log('End here.');