-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
65 lines (56 loc) · 1.3 KB
/
index.ts
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
/* A simple example of a lambda function written in Typescript, for compiling Solidity contracts. */
import { Handler } from "aws-lambda";
import solc from "solc";
import { Request, Response } from "./types/dtos.model";
export const handler: Handler<Request, Response> = async (event) => {
const {
contractName,
source,
optimize = true,
solcVersion = "0.8.21+commit.d9974bed",
runs = 200,
} = event;
const input = {
language: "Solidity",
sources: {
[contractName]: {
content: source,
},
},
settings: {
outputSelection: {
"*": {
"*": ["abi", "evm.bytecode.object"],
},
},
optimizer: {
enabled: optimize,
runs,
},
},
};
const compiler = await solc.loadRemoteVersion(solcVersion);
const output = JSON.parse(compiler.compile(JSON.stringify(input)));
if (output.errors) {
return {
bytecode: "",
abi: "",
errors: output.errors.map((error: any) => error.formattedMessage),
};
}
const {
evm: {
bytecode: { object: bytecode },
methodsIdentifiers,
gasEstimates,
},
abi,
} = output.contracts[`${contractName}.sol`][contractName];
return {
bytecode,
abi,
gasEstimates,
methodsIdentifiers,
errors: [],
};
};