-
Notifications
You must be signed in to change notification settings - Fork 3
/
client.go
200 lines (168 loc) · 4.13 KB
/
client.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
package goquadriga
import (
"bytes"
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strconv"
"strings"
"time"
)
const V2URL = "https://api.quadrigacx.com/v2/"
type Client struct {
RootUrl string
ClientId string
ApiKey string
ApiSecret string
}
func NewClient(id string, key string, secret string) *Client {
return &Client{
RootUrl: V2URL,
ClientId: id,
ApiKey: key,
ApiSecret: secret,
}
}
func (c *Client) URL(res string) string {
return fmt.Sprintf("%s%s", c.RootUrl, res)
}
func (c *Client) GetCurrentTradingInfo() (CurrentTrade, error) {
var current CurrentTrade
body, err := c.get(c.URL("ticker"))
if err != nil {
return current, err
}
err = json.Unmarshal(body, ¤t)
return current, err
}
func (c *Client) GetOrderBook() (OrderBook, error) {
var orders OrderBook
body, err := c.get(c.URL("order_book"))
if err != nil {
return orders, err
}
err = json.Unmarshal(body, &orders)
return orders, err
}
func (c *Client) GetTransactions() (TransactionResponse, error) {
var transactions TransactionResponse
body, err := c.get(c.URL("transactions"))
if err != nil {
return transactions, err
}
err = json.Unmarshal(body, &transactions)
fmt.Printf("Results: %v\n", transactions)
return transactions, err
}
func (c *Client) PostAccountBalance() (AccountBalance, error) {
var balance AccountBalance
auth := c.makeSig()
payload, err := json.Marshal(auth)
if err != nil {
return balance, err
}
fmt.Println(string(payload))
body, err := c.post(c.URL("balance"), payload)
if err != nil {
return balance, err
}
err = json.Unmarshal(body, &balance)
return balance, nil
}
func (c *Client) PostOpenOrders() (OpenOrdersResponse, error) {
var orders OpenOrdersResponse
auth := c.makeSig()
payload, err := json.Marshal(auth)
if err != nil {
return orders, err
}
fmt.Println(string(payload))
body, err := c.post(c.URL("open_orders"), payload)
if err != nil {
return orders, err
}
err = json.Unmarshal(body, &orders)
return orders, nil
}
func (c *Client) PostOrderLookup(id string) (LookupOrderResponse, error) {
var lookup OrderId
var order []LookupOrderResponse
lookup.ID = id
auth := c.makeSig()
payload, err := json.Marshal(struct {
*BaseAuth
OrderId
}{auth, lookup})
if err != nil {
return order[0], err
}
fmt.Println(string(payload))
body, err := c.post(c.URL("lookup_order"), payload)
if err != nil {
return order[0], err
}
err = json.Unmarshal(body, &order)
return order[0], err
}
func (c *Client) PostCancelOrder(id string) (bool, error) {
var cancel OrderId
cancel.ID = id
auth := c.makeSig()
payload, err := json.Marshal(struct {
*BaseAuth
OrderId
}{auth, cancel})
if err != nil {
return false, err
}
fmt.Println(string(payload))
body, err := c.post(c.URL("cancel_order"), payload)
if err != nil {
return false, err
}
cancelSuccess, err := strconv.ParseBool(strings.Trim(string(body), "\""))
if err != nil {
return false, err
}
return cancelSuccess, err
}
func (c *Client) get(url string) ([]byte, error) {
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return body, nil
}
func (c *Client) post(url string, payload []byte) ([]byte, error) {
req, err := http.NewRequest("POST", url, bytes.NewBuffer(payload))
req.Header.Set("Content-Type", "application/json; charset=utf-8")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
fmt.Println("Response status: ", resp.Status)
fmt.Println("Response headers: ", resp.Header)
fmt.Println("Response body: ", string(body))
return body, nil
}
func (c *Client) makeSig() *BaseAuth {
timestamp := strconv.FormatInt(time.Now().UTC().UnixNano(), 10)
message := strings.Join([]string{timestamp, c.ClientId, c.ApiKey}, "")
fmt.Println("The message is ", message)
sig := hmac.New(sha256.New, []byte(c.ApiSecret))
sig.Write([]byte(message))
base := &BaseAuth{ApiKey: c.ApiKey, Signature: hex.EncodeToString(sig.Sum(nil)), Nonce: timestamp}
return base
}