-
Notifications
You must be signed in to change notification settings - Fork 0
/
contract_compiler.go
43 lines (37 loc) · 1.06 KB
/
contract_compiler.go
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
package main
import (
"bytes"
"encoding/json"
"fmt"
"os/exec"
)
type CompilationResult struct {
Contracts map[string]struct {
ABI json.RawMessage
Bin string
Metadata string
} `json:"contracts"`
}
func compileContract(filePath string) (string, []byte, error) {
cmd := exec.Command("solc", "--combined-json", "abi,bin", filePath)
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
return "", nil, fmt.Errorf("failed to compile contract: %v", err)
}
var result CompilationResult
err = json.Unmarshal(out.Bytes(), &result)
if err != nil {
return "", nil, fmt.Errorf("failed to parse solc output: %v", err)
}
for _, contract := range result.Contracts {
abiBytes, err := json.Marshal(contract.ABI)
if err != nil {
return "", nil, fmt.Errorf("failed to marshal ABI: %v", err)
}
bin := contract.Bin
return bin, abiBytes, nil
}
return "", nil, fmt.Errorf("no contract found in solc output")
}