forked from INFURA/demo-eth-tx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompile.js
30 lines (27 loc) · 1.03 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
const fs = require('fs').promises;
const solc = require('solc');
async function main() {
// Load the contract source code
const sourceCode = await fs.readFile('Demo.sol', 'utf8');
// Compile the source code and retrieve the ABI and Bytecode
const { abi, bytecode } = compile(sourceCode, 'Demo');
// Store the ABI and Bytecode into a JSON file
const artifact = JSON.stringify({ abi, bytecode }, null, 2);
await fs.writeFile('Demo.json', artifact);
}
function compile(sourceCode, contractName) {
// Create the Solidity Compiler Standard Input and Output JSON
const input = {
language: 'Solidity',
sources: { main: { content: sourceCode } },
settings: { outputSelection: { '*': { '*': ['abi', 'evm.bytecode'] } } },
};
// Parse the compiler output to retrieve the ABI and Bytecode
const output = solc.compile(JSON.stringify(input));
const artifact = JSON.parse(output).contracts.main[contractName];
return {
abi: artifact.abi,
bytecode: artifact.evm.bytecode.object,
};
}
main().then(() => process.exit(0));