-
Notifications
You must be signed in to change notification settings - Fork 9
/
solidity.go
173 lines (148 loc) · 3.55 KB
/
solidity.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package compiler
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"os/exec"
"path/filepath"
"reflect"
"strings"
"github.com/laizy/web3/utils"
)
type solcOutput struct {
Contracts map[string]*solcContract
Version string
}
type solcContract struct {
BinRuntime string `json:"bin-runtime"`
Bin string
Abi interface{}
}
// Solidity is the solidity compiler
type Solidity struct {
Path string
}
// NewSolidityCompiler instantiates a new solidity compiler
func NewSolidityCompiler(path string) Compiler {
return &Solidity{path}
}
// CompileCode compiles a solidity code
func (s *Solidity) CompileCode(code string) (map[string]*Artifact, error) {
if code == "" {
return nil, fmt.Errorf("code is empty")
}
artifacts, err := s.compileImpl(code)
if err != nil {
return nil, err
}
return artifacts, nil
}
// Compile implements the compiler interface
func (s *Solidity) Compile(files ...string) (map[string]*Artifact, error) {
if len(files) == 0 {
return nil, fmt.Errorf("no input files")
}
return s.compileImpl("", files...)
}
func (s *Solidity) compileImpl(code string, files ...string) (map[string]*Artifact, error) {
args := []string{
"--combined-json",
"bin,bin-runtime,abi",
}
if code != "" {
args = append(args, "-")
}
if len(files) != 0 {
args = append(args, files...)
}
var stdout, stderr bytes.Buffer
cmd := exec.Command(s.Path, args...)
if code != "" {
cmd.Stdin = strings.NewReader(code)
}
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 *solcOutput
if err := json.Unmarshal(stdout.Bytes(), &output); err != nil {
return nil, err
}
artifacts := map[string]*Artifact{}
for name, i := range output.Contracts {
_abi := fmt.Sprint(i.Abi)
if reflect.TypeOf(i.Abi).Kind() != reflect.String { //some compiler version set abi to struct, while others sets abi to string
_abi = utils.JsonStr(i.Abi)
}
artifacts[name] = NewArtifact(_abi, i.Bin, i.BinRuntime)
}
return artifacts, nil
}
// DownloadSolidity downloads the solidity compiler
func DownloadSolidity(version string, dst string, renameDst bool) error {
url := "https://github.com/ethereum/solidity/releases/download/v" + version + "/solc-static-linux"
// check if the dst is correct
exists := false
fi, err := os.Stat(dst)
if err == nil {
switch mode := fi.Mode(); {
case mode.IsDir():
exists = true
case mode.IsRegular():
return fmt.Errorf("dst is a file")
}
} else {
if !os.IsNotExist(err) {
return fmt.Errorf("failed to stat dst '%s': %v", dst, err)
}
}
// create the destiny path if does not exists
if !exists {
if err := os.MkdirAll(dst, 0755); err != nil {
return fmt.Errorf("cannot create dst path: %v", err)
}
}
// rename binary
name := "solidity"
if renameDst {
name += "-" + version
}
// tmp folder to download the binary
tmpDir, err := ioutil.TempDir("/tmp", "solc-")
if err != nil {
return err
}
defer os.RemoveAll(tmpDir)
path := filepath.Join(tmpDir, name)
// Get the data
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
// Create the file
out, err := os.Create(path)
if err != nil {
return err
}
defer out.Close()
// Write the body to file
_, err = io.Copy(out, resp.Body)
if err != nil {
return err
}
// make binary executable
if err := os.Chmod(path, 0755); err != nil {
return err
}
// move file to dst
if err := os.Rename(path, filepath.Join(dst, name)); err != nil {
return err
}
return nil
}