-
Notifications
You must be signed in to change notification settings - Fork 41
/
deployContract.go
140 lines (110 loc) · 3.27 KB
/
deployContract.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
package exec
import (
"fmt"
"io/ioutil"
"math/big"
"net/http"
"os"
"path/filepath"
"strings"
"github.com/aergoio/aergo/cmd/brick/context"
"github.com/aergoio/aergo/contract"
)
func init() {
registerExec(&deployContract{})
}
type deployContract struct{}
func (c *deployContract) Command() string {
return "deploy"
}
func (c *deployContract) Syntax() string {
return fmt.Sprintf("%s %s %s %s %s", context.AccountSymbol, context.AmountSymbol,
context.ContractSymbol, context.PathSymbol, context.ContractArgsSymbol)
}
func (c *deployContract) Usage() string {
return fmt.Sprintf("deploy <sender_name> <amount> <contract_name> `<definition_file_path>` `[contructor_json_arg]`")
}
func (c *deployContract) Describe() string {
return "deploy a smart contract"
}
func (c *deployContract) Validate(args string) error {
// check whether chain is loaded
if context.Get() == nil {
return fmt.Errorf("load chain first")
}
_, _, _, _, _, err := c.parse(args)
return err
}
func (c *deployContract) readDefFile(defPath string) ([]byte, error) {
if strings.HasPrefix(defPath, "http") {
// search in the web
req, err := http.NewRequest("GET", defPath, nil)
if err != nil {
return nil, err
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
defByte, _ := ioutil.ReadAll(resp.Body)
return defByte, nil
}
// search in a local file system
if _, err := os.Stat(defPath); os.IsNotExist(err) {
return nil, err
}
defByte, err := ioutil.ReadFile(defPath)
if err != nil {
return nil, err
}
return defByte, nil
}
func (c *deployContract) parse(args string) (string, *big.Int, string, string, string, error) {
splitArgs := context.SplitSpaceAndAccent(args, false)
if len(splitArgs) < 4 {
return "", nil, "", "", "", fmt.Errorf("need 4 arguments. usage: %s", c.Usage())
}
amount, success := new(big.Int).SetString(splitArgs[1].Text, 10)
if success == false {
return "", nil, "", "", "", fmt.Errorf("fail to parse number %s", splitArgs[1].Text)
}
defPath := splitArgs[3].Text
if _, err := c.readDefFile(defPath); err != nil {
return "", nil, "", "", "", fmt.Errorf("fail to read a contrat def file %s: %s", splitArgs[3].Text, err.Error())
}
constuctorArg := "[]"
if len(splitArgs) == 5 {
constuctorArg = splitArgs[4].Text
} else if len(splitArgs) > 5 {
return "", nil, "", "", "", fmt.Errorf("too many arguments. usage: %s", c.Usage())
}
return splitArgs[0].Text, //accountName
amount, // amount
splitArgs[2].Text, // contractName
defPath, // defPath
constuctorArg,
nil
}
func (c *deployContract) Run(args string) (string, error) {
accountName, amount, contractName, defPath, constuctorArg, _ := c.parse(args)
defByte, err := c.readDefFile(defPath)
if err != nil {
return "", err
}
updateContractInfoInterface(contractName, defPath)
err = context.Get().ConnectBlock(
contract.NewRawLuaTxDefBig(accountName, contractName, amount, string(defByte)).Constructor(constuctorArg),
)
if enableWatch && !strings.HasPrefix(defPath, "http") {
absPath, _ := filepath.Abs(defPath)
watcher.Add(absPath)
}
if err != nil {
return "", err
}
Index(context.ContractSymbol, contractName)
Index(context.AccountSymbol, contractName)
return "deploy a smart contract successfully", nil
}