forked from thrasher-corp/gocryptotrader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.go
159 lines (128 loc) · 2.99 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
package main
import (
"net/http"
"hash"
"crypto/md5"
"crypto/hmac"
"crypto/sha512"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"encoding/hex"
"io"
"io/ioutil"
"errors"
"strings"
"math"
"log"
)
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(hash func() hash.Hash, input, key []byte) ([]byte) {
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 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 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" {
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 {
log.Println(err)
return err
}
if res.StatusCode != 200 {
log.Printf("HTTP status code: %d\n", res.StatusCode)
return errors.New("Status code was not 200.")
}
contents, _ := ioutil.ReadAll(res.Body)
//log.Printf("Recieved raw: %s\n", string(contents))
defer res.Body.Close()
if jsonDecode {
err := json.Unmarshal(contents, &result)
if err != nil {
return errors.New("Unable to JSON decode body.")
}
} else {
result = contents
}
return nil
}