/
contract.go
171 lines (148 loc) · 4.17 KB
/
contract.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
package cmd
import (
"fmt"
"io/ioutil"
"os"
"github.com/aeternity/aepp-sdk-go/v6/config"
"github.com/aeternity/aepp-sdk-go/v6/naet"
"github.com/spf13/cobra"
)
var compilerURL string
var contractCmd = &cobra.Command{
Use: "contract subcommand",
Short: "Compile, call or deploy smart contracts",
Long: ``,
}
var compileCmd = &cobra.Command{
Use: "compile FILENAME",
Short: "Send a source file to a compiler",
Long: ``,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) (err error) {
compiler := newCompiler()
return compileFunc(compiler, args)
},
SilenceUsage: true,
}
func compileFunc(conn naet.CompileContracter, args []string) (err error) {
s, err := readSource(args[0])
if err != nil {
return err
}
bytecode, err := conn.CompileContract(s, config.Compiler.Backend)
fmt.Println(bytecode)
return err
}
var encodeCalldataCmd = &cobra.Command{
Use: "encodeCalldata SOURCE FUNCTIONNAME [..ARGS]",
Short: "Encode contract function calls. Needs the path to contract source file",
Long: ``,
Args: cobra.MinimumNArgs(2),
RunE: func(cmd *cobra.Command, args []string) (err error) {
compiler := newCompiler()
return encodeCalldataFunc(compiler, args)
},
SilenceUsage: true,
}
func encodeCalldataFunc(conn naet.EncodeCalldataer, args []string) (err error) {
s, err := readSource(args[0])
if err != nil {
return err
}
callData, err := conn.EncodeCalldata(s, args[1], args[2:], config.Compiler.Backend)
if err != nil {
return err
}
fmt.Println(callData)
return
}
type decodeCalldataer interface {
naet.DecodeCalldataBytecoder
naet.DecodeCalldataSourcer
}
var decodeCalldataBytecodeCmd = &cobra.Command{
Use: "decodeCalldataBytecode BYTECODE CALLDATA [..ARGS]",
Short: "Decode contract function calls. Needs the path to contract source file/compiled bytecode",
Long: ``,
Args: cobra.MinimumNArgs(2),
RunE: func(cmd *cobra.Command, args []string) (err error) {
compiler := newCompiler()
return decodeCalldataBytecodeFunc(compiler, args)
},
SilenceUsage: true,
}
func decodeCalldataBytecodeFunc(conn decodeCalldataer, args []string) (err error) {
if !IsBytecode(args[0]) {
return fmt.Errorf("%s is not bytecode", args[0])
}
if !IsBytecode(args[1]) {
return fmt.Errorf("%s is not bytecode", args[0])
}
r, err := conn.DecodeCalldataBytecode(args[0], args[1], config.Compiler.Backend)
if err != nil {
return
}
fmt.Println(*r.Function, r.Arguments)
return nil
}
var decodeCalldataSourceCmd = &cobra.Command{
Use: "decodeCalldataSource SOURCE_FILE FUNCTION_NAME CALLDATA [..ARGS]",
Short: "Decode contract function calls. Needs the path to contract source file/compiled bytecode",
Long: ``,
Args: cobra.MinimumNArgs(2),
RunE: func(cmd *cobra.Command, args []string) (err error) {
compiler := newCompiler()
return decodeCalldataSourceFunc(compiler, args)
},
SilenceUsage: true,
}
func decodeCalldataSourceFunc(conn decodeCalldataer, args []string) (err error) {
source, err := readSource(args[0])
if err != nil {
return err
}
if !IsBytecode(args[2]) {
return fmt.Errorf("%s is not bytecode", args[0])
}
r, err := conn.DecodeCalldataSource(source, args[1], args[2], config.Compiler.Backend)
fmt.Println(*r.Function, r.Arguments)
return
}
var generateAciCmd = &cobra.Command{
Use: "generateaci FILENAME",
Short: "Generate ACI out of source code",
Long: ``,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) (err error) {
compiler := newCompiler()
return generateAciFunc(compiler, args)
},
SilenceUsage: true,
}
func generateAciFunc(conn naet.GenerateACIer, args []string) (err error) {
source, err := readSource(args[0])
if err != nil {
return
}
aci, err := conn.GenerateACI(source, config.Compiler.Backend)
if err != nil {
return
}
PrintObject("ACI", aci)
return nil
}
func readSource(path string) (s string, err error) {
file, err := os.Open(path)
if err != nil {
return "", err
}
b, err := ioutil.ReadAll(file)
return string(b), err
}
func init() {
RootCmd.AddCommand(contractCmd)
contractCmd.AddCommand(compileCmd)
contractCmd.AddCommand(encodeCalldataCmd)
contractCmd.AddCommand(decodeCalldataBytecodeCmd)
contractCmd.AddCommand(decodeCalldataSourceCmd)
}