-
Notifications
You must be signed in to change notification settings - Fork 9
/
vyper.go
57 lines (47 loc) · 1.17 KB
/
vyper.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package compiler
import (
"bytes"
"encoding/json"
"fmt"
"os/exec"
)
// Vyper is the vyper compiler
type Vyper struct {
path string
}
// NewVyperCompiler instantiates a new vyper compiler
func NewVyperCompiler(path string) Compiler {
return &Vyper{path}
}
// Compile implements the compiler interface
func (v *Vyper) Compile(files ...string) (map[string]*Artifact, error) {
args := []string{
"-f",
"combined_json",
}
args = append(args, files...)
var stdout, stderr bytes.Buffer
cmd := exec.Command(v.path, args...)
cmd.Stdout = &stdout
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
return nil, fmt.Errorf("failed to compile: %s", string(stderr.Bytes()))
}
var output map[string]interface{}
if err := json.Unmarshal(stdout.Bytes(), &output); err != nil {
return nil, err
}
artifacts := map[string]*Artifact{}
for name, o := range output {
if name == "version" {
continue
}
contract := o.(map[string]interface{})
abiStr, err := json.Marshal(contract["abi"])
if err != nil {
return nil, err
}
artifacts[name] = NewArtifact(string(abiStr), contract["bytecode"].(string), contract["bytecode_runtime"].(string))
}
return artifacts, nil
}