-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathcompile-contract.js
More file actions
47 lines (41 loc) · 1.01 KB
/
compile-contract.js
File metadata and controls
47 lines (41 loc) · 1.01 KB
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
function importSolcInternal() {
return import("solc").then((module) => module.default);
}
let promise;
function importSolc() {
promise = promise ?? importSolcInternal();
return promise;
}
async function compileContract(filename, content) {
const solc = await importSolc();
const input = {
language: "Solidity",
sources: {
[filename]: {
content,
},
},
settings: {
metadata: { bytecodeHash: "none" },
outputSelection: {
"*": {
"*": ["*"],
},
},
},
};
const output = JSON.parse(solc.compile(JSON.stringify(input)));
// We throw if the contract doesn't compile.
if (output.errors?.length > 0) {
throw output.errors[0].formattedMessage;
}
const compiledContracts = output.contracts[filename];
return Object.keys(compiledContracts).reduce(
(byteCodes, contractName) => ({
...byteCodes,
[contractName]: compiledContracts[contractName].evm.bytecode.object,
}),
{},
);
}
export default compileContract;