-
Notifications
You must be signed in to change notification settings - Fork 0
/
deployer.go
127 lines (119 loc) · 3.78 KB
/
deployer.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
// Copyright 2017 Sylvain 6120 Laurent
// This file is part of the gethitihteg library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"math/big"
"strings"
"github.com/ethereum/go-ethereum/common/compiler"
"github.com/jessevdk/go-flags"
ethtk "github.com/Magicking/gethitihteg"
)
var opts struct {
RpcURL string `long:"rpc-url" default:"http://localhost:8545" description:"RPC URL for the node"`
PrivateKey string `long:"key" required:"true" description:"Private key used to sign transaction"`
SolF []string `long:"sol" required:"true" description:"Path to the Ethereum contract Solidity source to deploy"`
SolC string `long:"solc" default:"solc" description:"Solidity compiler to use if source builds are requested"`
Excl []string `long:"excl" description:"Contract to exclude from deploying"`
CacheNonce string `long:"cache-nonce" description:"Path to cached nonce"`
}
func ParamsParse(params []string) (ret []interface{}, err error) {
//TODO ret[contract_name][]interface{}
for _, e := range params {
val := strings.SplitN(e, ":", 2)
if len(val) != 2 {
return nil, fmt.Errorf("Bad argument format for %q should be \"type:value\"", e)
}
switch val[0] {
case "string":
buf := val[1]
ret = append(ret, &buf)
case "int":
var v int64
fmt.Sscan(val[1], &v)
ret = append(ret, big.NewInt(v))
//TODO types
default:
return nil, fmt.Errorf("Unknown type %q", val[0])
}
}
return ret, nil
}
func main() {
lst, err := flags.Parse(&opts)
if err != nil {
return
}
var nonce *big.Int
if opts.CacheNonce != "" {
_nonce, err := ioutil.ReadFile(opts.CacheNonce)
if err != nil {
log.Println("ReadFile", err)
} else {
nonce = big.NewInt(0).SetBytes(_nonce)
}
}
client, err := ethtk.NewNodeConnector(opts.RpcURL, 3)
if err != nil {
log.Fatalf("Could not initialize client context: %v", err)
}
params, err := ParamsParse(lst)
if err != nil {
log.Fatal(err)
}
//Deploy contract
contracts, err := compiler.CompileSolidity(opts.SolC, opts.SolF...)
if err != nil {
log.Fatalf("Failed to build Solidity contract: %v\n", err)
}
excludedContracts := make(map[string]bool)
for _, e := range opts.Excl {
excludedContracts[strings.ToLower(e)] = true
}
// Gather all non-excluded contract for binding
var TODO bool
for name, contract := range contracts {
if TODO {
log.Fatal("TODO handle only one contract")
}
if excludedContracts[strings.ToLower(name)] {
continue
}
abi, _ := json.Marshal(contract.Info.AbiDefinition) // Flatten the compiler parse
nameParts := strings.Split(name, ":")
fmt.Println(nameParts)
addr, tx, _, err := ethtk.CreateContractHelper(client, opts.PrivateKey, string(abi), contract.Code, nonce, params...)
if err != nil {
log.Println("main:", err)
continue
}
fmt.Printf("TX(%v): %v [err: %v]\n", tx.Hash().Hex(), addr.Hex(), err)
var buf []byte
if err == nil {
buf = big.NewInt(int64(tx.Nonce()) + 1).Bytes()
}
if opts.CacheNonce != "" {
if err := ioutil.WriteFile(opts.CacheNonce, buf, 0666); err != nil {
log.Println(err)
}
}
TODO = true
}
fmt.Println(TODO)
}