forked from Dapp-Learning-DAO/Dapp-Learning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compile.js
44 lines (40 loc) · 1.01 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
const fs = require('fs');
const solc = require('solc');
// Get Path and Load Contract
const source = fs.readFileSync('SimpleToken.sol', 'utf8');
function findImports(path) {
if (fs.existsSync(path)) {
return {
contents: fs.readFileSync(path, 'utf8'),
};
} else if (fs.existsSync('./node_modules/' + path)) {
return {
contents: fs.readFileSync('./node_modules/' + path, 'utf8'),
};
} else {
return { error: 'File not found' };
}
}
// Compile Contract
// https://docs.soliditylang.org/en/v0.8.0/using-the-compiler.html#compiler-input-and-output-json-description
const input = {
language: 'Solidity',
sources: {
'SimpleToken.sol': {
content: source,
},
},
settings: {
outputSelection: {
'*': {
'*': ['*'],
},
},
},
};
const tempFile = JSON.parse(
solc.compile(JSON.stringify(input), { import: findImports })
);
const contractFile = tempFile.contracts['SimpleToken.sol']['SimpleToken'];
// Export Contract Data
module.exports = contractFile;