/
compiler.go
239 lines (211 loc) · 8.09 KB
/
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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package naet
import (
compiler_client "github.com/aeternity/aepp-sdk-go/v6/swagguard/compiler/client"
"github.com/aeternity/aepp-sdk-go/v6/swagguard/compiler/client/operations"
models "github.com/aeternity/aepp-sdk-go/v6/swagguard/compiler/models"
httptransport "github.com/go-openapi/runtime/client"
"github.com/go-openapi/strfmt"
)
// Compiler wraps around the swagger-generated Compiler. Unlike the
// swagger-generated Compiler, this Compiler struct builds the swagger HTTP
// requests from the Golang native arguments that its methods receive, and uses
// the swagger-generated Compiler's endpoints to send these requests off. It
// then parses the swagger response and makes it as native-Go-code-friendly as
// possible.
type Compiler struct {
*compiler_client.Compiler
}
// NewCompiler creates a new Compiler instance from a URL
func NewCompiler(compilerURL string, debug bool) *Compiler {
host, schemas := urlComponents(compilerURL)
transport := httptransport.New(host, "", schemas)
transport.SetDebug(debug)
c := compiler_client.New(transport, strfmt.Default)
compiler := &Compiler{
Compiler: c,
}
return compiler
}
// APIVersioner guarantees that one can run a APIVersion() method on the
// mocked/real network connection to the aesophia compiler
type APIVersioner interface {
APIVersion() (version string, err error)
}
// APIVersion connects to the compiler and returns its version string, e.g.
// 3.1.0
func (c *Compiler) APIVersion() (version string, err error) {
result, err := c.Compiler.Operations.APIVersion(nil)
if err != nil {
return "", err
}
version = *result.Payload.APIVersion
return
}
// CompileContracter guarantees that one can run a CompileContract() method on
// the mocked/real network connection to the aesophia compiler
type CompileContracter interface {
CompileContract(source string, backend string) (bytecode string, err error)
}
// CompileContract abstracts away the swagger specifics of posting to /compile
func (c *Compiler) CompileContract(source string, backend string) (bytecode string, err error) {
contract := &models.Contract{Code: &source, Options: &models.CompileOpts{
Backend: backend,
FileSystem: nil,
SrcFile: "",
}}
params := operations.NewCompileContractParams().WithBody(contract)
result, err := c.Compiler.Operations.CompileContract(params)
if err != nil {
return "", err
}
bytecode = string(result.Payload.Bytecode)
return
}
// DecodeCallResulter guarantees that one can run a DecodeCallResult() method on
// the mocked/real network connection to the aesophia compiler
type DecodeCallResulter interface {
DecodeCallResult(callResult string, callValue string, function string, source string, backend string) (answer interface{}, err error)
}
// DecodeCallResult abstracts away the swagger specifics of posting to
// /decode-call-result
func (c *Compiler) DecodeCallResult(callResult string, callValue string, function string, source string, backend string) (answer interface{}, err error) {
sophiaCallResultInput := &models.SophiaCallResultInput{
CallResult: &callResult,
CallValue: &callValue,
Function: &function,
Options: &models.CompileOpts{
Backend: backend,
FileSystem: nil,
SrcFile: "",
},
Source: &source,
}
params := operations.NewDecodeCallResultParams().WithBody(sophiaCallResultInput)
result, err := c.Compiler.Operations.DecodeCallResult(params)
if err != nil {
return
}
return result.Payload, err
}
// DecodeCalldataBytecoder guarantees that one can run a
// DecodeCalldataBytecode() method on the mocked/real network connection to the
// aesophia compiler
type DecodeCalldataBytecoder interface {
DecodeCalldataBytecode(bytecode string, calldata string, backend string) (decodedCallData *models.DecodedCalldata, err error)
}
// DecodeCalldataBytecode abstracts away the swagger specifics of posting to
// /decode-calldata/bytecode
func (c *Compiler) DecodeCalldataBytecode(bytecode string, calldata string, backend string) (decodedCallData *models.DecodedCalldata, err error) {
decodeCalldataBytecode := &models.DecodeCalldataBytecode{
Bytecode: models.EncodedByteArray(bytecode),
Calldata: models.EncodedByteArray(calldata),
Backend: backend,
}
params := operations.NewDecodeCalldataBytecodeParams().WithBody(decodeCalldataBytecode)
result, err := c.Compiler.Operations.DecodeCalldataBytecode(params)
if err != nil {
return
}
return result.Payload, err
}
// DecodeCalldataSourcer guarantees that one can run a DecodeCalldataSource()
// method on the mocked/real network connection to the aesophia compiler
type DecodeCalldataSourcer interface {
DecodeCalldataSource(source string, function string, callData string, backend string) (decodedCallData *models.DecodedCalldata, err error)
}
// DecodeCalldataSource abstracts away the swagger specifics of posting to
// /decode-calldata/source
func (c *Compiler) DecodeCalldataSource(source string, function string, callData string, backend string) (decodedCallData *models.DecodedCalldata, err error) {
p := &models.DecodeCalldataSource{
Calldata: models.EncodedByteArray(callData),
Function: &function,
Options: &models.CompileOpts{
Backend: backend,
FileSystem: nil,
SrcFile: "",
},
Source: &source,
}
params := operations.NewDecodeCalldataSourceParams().WithBody(p)
result, err := c.Compiler.Operations.DecodeCalldataSource(params)
if err != nil {
return
}
return result.Payload, err
}
// DecodeDataer guarantees that one can run a DecodeData() method on the
// mocked/real network connection to the aesophia compiler
type DecodeDataer interface {
DecodeData(data string, sophiaType string) (decodedData *models.SophiaJSONData, err error)
}
// DecodeData abstracts away the swagger specifics of posting to /decode-data
func (c *Compiler) DecodeData(data string, sophiaType string) (decodedData *models.SophiaJSONData, err error) {
p := &models.SophiaBinaryData{
Data: &data,
SophiaType: &sophiaType,
}
params := operations.NewDecodeDataParams().WithBody(p)
result, err := c.Compiler.Operations.DecodeData(params)
if err != nil {
return
}
return result.Payload, err
}
// EncodeCalldataer guarantees that one can run a EncodeCalldata() method on the
// mocked/real network connection to the aesophia compiler
type EncodeCalldataer interface {
EncodeCalldata(source string, function string, args []string, backend string) (callData string, err error)
}
// EncodeCalldata abstracts away the swagger specifics of posting to
// /encode-calldata
func (c *Compiler) EncodeCalldata(source string, function string, args []string, backend string) (callData string, err error) {
f := &models.FunctionCallInput{
Arguments: args,
Function: &function,
Options: &models.CompileOpts{
Backend: backend,
FileSystem: nil,
SrcFile: "",
},
Source: &source,
}
params := operations.NewEncodeCalldataParams().WithBody(f)
result, err := c.Compiler.Operations.EncodeCalldata(params)
if err != nil {
return
}
s := string(result.Payload.Calldata)
return s, err
}
// GenerateACIer guarantees that one can run a GenerateACI() method on the
// mocked/real network connection to the aesophia compiler
type GenerateACIer interface {
GenerateACI(source string, backend string) (aci *models.ACI, err error)
}
// GenerateACI abstracts away the swagger specifics of posting to /aci
func (c *Compiler) GenerateACI(source string, backend string) (aci *models.ACI, err error) {
contract := &models.Contract{Code: &source, Options: &models.CompileOpts{
Backend: backend,
FileSystem: nil,
SrcFile: "",
}}
params := operations.NewGenerateACIParams().WithBody(contract)
result, err := c.Compiler.Operations.GenerateACI(params)
if err != nil {
return
}
return result.Payload, err
}
// SophiaVersioner guarantees that one can run a SophiaVersion() method on the
// mocked/real network connection to the aesophia compiler
type SophiaVersioner interface {
SophiaVersion() (version string, err error)
}
// SophiaVersion abstracts away the swagger specifics of getting /version
func (c *Compiler) SophiaVersion() (version string, err error) {
result, err := c.Compiler.Operations.Version(nil)
if err != nil {
return
}
return string(*result.Payload.Version), err
}