/
utils.go
221 lines (193 loc) · 5.32 KB
/
utils.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
package cli
import (
"context"
"encoding/json"
"errors"
"fmt"
"math/big"
"net/http"
"os"
"regexp"
"strings"
"github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/ethereum/go-ethereum/console"
"github.com/ethereum/go-ethereum/rpc"
"github.com/sirupsen/logrus"
)
var (
big10 = big.NewInt(10)
big1NEWInWEI = new(big.Int).Exp(big10, big.NewInt(18), nil)
errCliTranNil = errors.New("Cli tran error")
errBigSetString = errors.New("conver string to big error")
errIllegalAmount = errors.New("Illegal Amount")
errIllegalUnit = errors.New("Illegal Unit")
errRequiredFromAddress = errors.New(`required flag(s) "from" not set`)
)
var IsDecimalString = regexp.MustCompile(`^[1-9]\d*$|^0$|^0\.\d*$|^[1-9](\d)*\.(\d)*$`).MatchString
func showSuccess(msg string, args ...interface{}) {
fmt.Printf(msg+"\n", args...)
}
func showError(fields logrus.Fields, msg string, args ...interface{}) {
logrus.WithFields(fields).Errorf(msg, args...)
}
// getPassPhrase retrieves the password associated with an account,
// requested interactively from the user.
func getPassPhrase(prompt string, confirmation bool) (string, error) {
// prompt the user for the password
if prompt != "" {
fmt.Println(prompt)
}
password, err := console.Stdin.PromptPassword("Enter passphrase (empty for no passphrase): ")
if err != nil {
return "", err
}
if confirmation {
confirm, err := console.Stdin.PromptPassword("Enter same passphrase again: ")
if err != nil {
return "", err
}
if password != confirm {
return "", fmt.Errorf("Passphrases do not match")
}
}
return password, nil
}
func stringInSlice(str string, list []string) bool {
for _, v := range list {
if v == str {
return true
}
}
return false
}
func getAmountWei(amountStr, unit string) (*big.Int, error) {
switch unit {
case UnitETH:
index := strings.IndexByte(amountStr, '.')
if index <= 0 {
amountWei, ok := new(big.Int).SetString(amountStr, 10)
if !ok {
return nil, errBigSetString
}
return new(big.Int).Mul(amountWei, big1NEWInWEI), nil
}
amountStrInt := amountStr[:index]
amountStrDec := amountStr[index+1:]
amountStrDecLen := len(amountStrDec)
if amountStrDecLen > 18 {
return nil, errIllegalAmount
}
amountStrInt = amountStrInt + strings.Repeat("0", 18)
amountStrDec = amountStrDec + strings.Repeat("0", 18-amountStrDecLen)
amountStrIntBig, ok := new(big.Int).SetString(amountStrInt, 10)
if !ok {
return nil, errBigSetString
}
amountStrDecBig, ok := new(big.Int).SetString(amountStrDec, 10)
if !ok {
return nil, errBigSetString
}
return new(big.Int).Add(amountStrIntBig, amountStrDecBig), nil
case UnitWEI:
amountWei, ok := new(big.Int).SetString(amountStr, 10)
if !ok {
return nil, errBigSetString
}
return amountWei, nil
}
return nil, errIllegalUnit
}
func getWeiAmountTextUnitByUnit(amount *big.Int, unit string) string {
if amount == nil {
return fmt.Sprintf("0 %s", UnitWEI)
}
amountStr := amount.String()
amountStrLen := len(amountStr)
if unit == "" {
if amountStrLen <= 18 {
// show in WEI
unit = UnitWEI
} else {
unit = UnitETH
}
}
return fmt.Sprintf("%s %s", getWeiAmountTextByUnit(amount, unit), unit)
}
func getWeiAmountTextByUnit(amount *big.Int, unit string) string {
if amount == nil {
return "0"
}
amountStr := amount.String()
amountStrLen := len(amountStr)
switch unit {
case UnitETH:
var amountStrDec, amountStrInt string
if amountStrLen <= 18 {
amountStrDec = strings.Repeat("0", 18-amountStrLen) + amountStr
amountStrInt = "0"
} else {
amountStrDec = amountStr[amountStrLen-18:]
amountStrInt = amountStr[:amountStrLen-18]
}
amountStrDec = strings.TrimRight(amountStrDec, "0")
if len(amountStrDec) <= 0 {
return amountStrInt
}
return amountStrInt + "." + amountStrDec
case UnitWEI:
return amountStr
}
return errIllegalUnit.Error()
}
func createNewAccount(walletPath string, numOfNew int) error {
wallet := keystore.NewKeyStore(walletPath,
keystore.LightScryptN, keystore.LightScryptP)
walletPassword, err := getPassPhrase("Your new account is locked with a password. Please give a password. Do not forget this password.", true)
if err != nil {
fmt.Println("Error: ", err)
return err
}
for i := 0; i < numOfNew; i++ {
account, err := wallet.NewAccount(walletPassword)
if err != nil {
fmt.Println("Account error:", err)
return err
}
fmt.Println(account.Address.Hex())
}
return nil
}
func showTransactionReceipt(url, txStr string) {
sendJSONPostAndShow(url, "eth_getTransactionReceipt", txStr)
}
func sendJSONPostAndShow(url, method string, args ...interface{}) {
ctx := context.Background()
client, err := rpc.DialContext(ctx, url)
if err != nil {
fmt.Println("DialContext: ", err)
return
}
var raw json.RawMessage
if err := client.CallContext(ctx, &raw, method, args...); err != nil {
fmt.Println("CallContext Error: ", err)
return
}
rawStr, err := json.MarshalIndent(raw, "", "\t")
if err != nil {
fmt.Println("JSON marshaling failed: ", err)
return
}
fmt.Printf("%s\n", rawStr)
return
}
func getFaucet(rpcURL, address string) {
url := fmt.Sprintf("%s/faucet?address=%s", rpcURL, address)
resp, err := http.Get(url)
if err != nil {
fmt.Fprintf(os.Stderr, "Get error: %v\n", err)
return
}
if resp.StatusCode == 200 {
fmt.Printf("Get faucet for %s\n", address)
}
}