-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
132 lines (108 loc) · 2.93 KB
/
main.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
package main
import (
"crypto/hmac"
"crypto/sha512"
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"regexp"
"strconv"
"strings"
"time"
)
const apiKey = "your api key"
const privateKey = "your private key in base64"
const baseUrl = "https://api.btcmarkets.net"
const apiPath = "/v3/orders"
func main() {
getOpenOrders()
response, err := addOrder()
if err != nil {
log.Println(err.Error())
} else {
cancelOrder(extractOrderId(response))
}
}
func extractOrderId(order string) string {
re := regexp.MustCompile(`"orderId":\s*"(\w*)"`)
matches := re.FindStringSubmatch(order)
return matches[1]
}
func getOpenOrders() {
log.Println("list orders")
responseBody, _ := makeHttpCall("GET", apiPath, "status=open", "")
log.Println(responseBody)
}
func addOrder() (string, error) {
orderData := map[string]string{
"marketId": "XRP-AUD",
"price": "0.1",
"amount": "0.1",
"side": "Bid",
"type": "Limit",
}
orderJson, _ := json.Marshal(orderData)
log.Println("adding order " + string(orderJson))
responseBody, err := makeHttpCall("POST", apiPath, "", string(orderJson))
log.Println(responseBody)
return responseBody, err
}
func cancelOrder(orderId string) {
log.Println("cancelling order " + orderId)
log.Println(
makeHttpCall("DELETE", apiPath+"/"+orderId, "", ""))
}
func makeHttpCall(method string, path string, query string, body string) (string, error) {
headers := buildAuthHeaders(method, path, body)
url := baseUrl + path
if query != "" {
url += "?" + query
}
var request *http.Request
if body != "" {
request, _ = http.NewRequest(method, url, strings.NewReader(body))
} else {
request, _ = http.NewRequest(method, url, nil)
}
request.Header = headers
response, err := (&http.Client{}).Do(request)
if err != nil {
return "", err
}
log.Println(response)
defer response.Body.Close()
data, err := ioutil.ReadAll(response.Body)
if err != nil {
return "", nil
}
if response.StatusCode < 200 || response.StatusCode >= 300 {
return "", fmt.Errorf("HTTP %d, %s", response.StatusCode, response)
}
responseBody := string(data)
return responseBody, nil
}
func buildAuthHeaders(method string, path string, body string) http.Header {
//getting now() in milliseconds
nowMs := strconv.FormatInt(time.Now().UTC().UnixNano()/1000000, 10)
stringToSign := method + path + nowMs
if body != "" {
stringToSign += body
}
return http.Header{
"Content-Type": []string{"application/json"},
"Accept": []string{"application/json"},
"Accept-Charset": []string{"UTF-8"},
"BM-AUTH-APIKEY": []string{apiKey},
"BM-AUTH-TIMESTAMP": []string{nowMs},
"BM-AUTH-SIGNATURE": []string{signMessage(privateKey, stringToSign)},
}
}
func signMessage(key string, message string) string {
encodedKey, _ := base64.StdEncoding.DecodeString(key)
mac := hmac.New(sha512.New, encodedKey)
mac.Write([]byte(message))
return base64.StdEncoding.EncodeToString(mac.Sum(nil))
}