forked from thrasher-corp/gocryptotrader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.go
310 lines (253 loc) · 5.25 KB
/
common.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
package main
import (
"crypto/hmac"
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"encoding/base64"
"encoding/csv"
"encoding/hex"
"encoding/json"
"errors"
"hash"
"io"
"io/ioutil"
"log"
"math"
"net/http"
"net/url"
"os"
"strconv"
"strings"
)
const (
HASH_SHA1 = iota
HASH_SHA256
HASH_SHA512
HASH_SHA512_384
SATOSHIS_PER_BTC = 100000000
SATOSHIS_PER_LTC = 100000000
)
func GetMD5(input []byte) []byte {
hash := md5.New()
hash.Write(input)
return hash.Sum(nil)
}
func GetSHA512(input []byte) []byte {
sha := sha512.New()
sha.Write(input)
return sha.Sum(nil)
}
func GetSHA256(input []byte) []byte {
sha := sha256.New()
sha.Write(input)
return sha.Sum(nil)
}
func GetHMAC(hashType int, input, key []byte) []byte {
var hash func() hash.Hash
switch hashType {
case HASH_SHA1:
{
hash = sha1.New
}
case HASH_SHA256:
{
hash = sha256.New
}
case HASH_SHA512:
{
hash = sha512.New
}
case HASH_SHA512_384:
{
hash = sha512.New384
}
}
hmac := hmac.New(hash, []byte(key))
hmac.Write(input)
return hmac.Sum(nil)
}
func HexEncodeToString(input []byte) string {
return hex.EncodeToString(input)
}
func Base64Decode(input string) ([]byte, error) {
result, err := base64.StdEncoding.DecodeString(input)
if err != nil {
return nil, err
}
return result, nil
}
func Base64Encode(input []byte) string {
return base64.StdEncoding.EncodeToString(input)
}
func StringSliceDifference(slice1 []string, slice2 []string) []string {
var diff []string
for i := 0; i < 2; i++ {
for _, s1 := range slice1 {
found := false
for _, s2 := range slice2 {
if s1 == s2 {
found = true
break
}
}
if !found {
diff = append(diff, s1)
}
}
if i == 0 {
slice1, slice2 = slice2, slice1
}
}
return diff
}
func StringContains(input, substring string) bool {
return strings.Contains(input, substring)
}
func JoinStrings(input []string, seperator string) string {
return strings.Join(input, seperator)
}
func SplitStrings(input, seperator string) []string {
return strings.Split(input, seperator)
}
func TrimString(input, cutset string) string {
return strings.Trim(input, cutset)
}
func StringToUpper(input string) string {
return strings.ToUpper(input)
}
func StringToLower(input string) string {
return strings.ToLower(input)
}
func RoundFloat(x float64, prec int) float64 {
var rounder float64
pow := math.Pow(10, float64(prec))
intermed := x * pow
_, frac := math.Modf(intermed)
intermed += .5
x = .5
if frac < 0.0 {
x = -.5
intermed -= 1
}
if frac >= x {
rounder = math.Ceil(intermed)
} else {
rounder = math.Floor(intermed)
}
return rounder / pow
}
func IsEnabled(isEnabled bool) string {
if isEnabled {
return "Enabled"
} else {
return "Disabled"
}
}
func CalculateAmountWithFee(amount, fee float64) float64 {
return amount + CalculateFee(amount, fee)
}
func CalculateFee(amount, fee float64) float64 {
return amount * (fee / 100)
}
func CalculatePercentageDifference(amount, secondAmount float64) float64 {
return (secondAmount - amount) / amount * 100
}
func CalculateNetProfit(amount, priceThen, priceNow, costs float64) float64 {
return (priceNow * amount) - (priceThen * amount) - costs
}
func SendHTTPRequest(method, path string, headers map[string]string, body io.Reader) (string, error) {
result := strings.ToUpper(method)
if result != "POST" && result != "GET" && result != "DELETE" {
return "", errors.New("Invalid HTTP method specified.")
}
req, err := http.NewRequest(method, path, body)
if err != nil {
return "", err
}
for k, v := range headers {
req.Header.Add(k, v)
}
httpClient := &http.Client{}
resp, err := httpClient.Do(req)
if err != nil {
return "", err
}
contents, err := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
if err != nil {
return "", err
}
return string(contents), nil
}
func SendHTTPGetRequest(url string, jsonDecode bool, result interface{}) (err error) {
res, err := http.Get(url)
if err != nil {
return err
}
if res.StatusCode != 200 {
log.Printf("HTTP status code: %d\n", res.StatusCode)
return errors.New("Status code was not 200.")
}
contents, err := ioutil.ReadAll(res.Body)
if err != nil {
return err
}
defer res.Body.Close()
if jsonDecode {
err := JSONDecode(contents, &result)
if err != nil {
return err
}
} else {
result = &contents
}
return nil
}
func JSONEncode(v interface{}) ([]byte, error) {
json, err := json.Marshal(&v)
if err != nil {
return nil, err
}
return json, nil
}
func JSONDecode(data []byte, to interface{}) error {
err := json.Unmarshal(data, &to)
if err != nil {
return err
}
return nil
}
func EncodeURLValues(url string, values url.Values) string {
path := url
if len(values) > 0 {
path += "?" + values.Encode()
}
return path
}
func ExtractHost(address string) string {
host := SplitStrings(address, ":")[0]
if host == "" {
return "localhost"
}
return host
}
func ExtractPort(host string) int {
portStr := SplitStrings(host, ":")[1]
port, _ := strconv.Atoi(portStr)
return port
}
func OutputCSV(path string, data [][]string) error {
file, err := os.Create(path)
if err != nil {
return err
}
writer := csv.NewWriter(file)
err = writer.WriteAll(data)
if err != nil {
return err
}
defer writer.Flush()
return nil
}