-
Notifications
You must be signed in to change notification settings - Fork 1
/
protected_requests.go
362 lines (305 loc) · 8.56 KB
/
protected_requests.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
package p2pb2b
import (
"encoding/json"
"net/http"
"time"
)
type currencyBalanceResult struct {
Available string `json:"available"`
Freeze string `json:"freeze"`
}
type CurrencyBalanceJsonRes struct {
Success bool `json:"success"`
Message json.RawMessage `json:"message"`
Result currencyBalanceResult `json:"result"`
}
type CurrencyBalanceJsonBody struct {
RequestUrl string `json:"request"`
Nonce int64 `json:"nonce"`
Currency string `json:"currency"`
}
type CurrencyBalanceParams struct {
Currency string /// e.g "ETH"
}
func (clt *Client) CurrencyBalance(opts CurrencyBalanceParams) (*CurrencyBalanceJsonRes, error) {
endpoint := "/api/v1/account/balance"
postBody := CurrencyBalanceJsonBody{
RequestUrl: endpoint,
Nonce: time.Now().UnixNano(),
Currency: opts.Currency,
}
res, err := clt.AuthAPIRequest(postBody, http.MethodPost, endpoint)
if err != nil {
return nil, err
}
var jsonRes CurrencyBalanceJsonRes
if err := json.Unmarshal(res, &jsonRes); err != nil {
return nil, err
}
return &jsonRes, nil
}
type BalancesJsonBody struct {
RequestUrl string `json:"request"`
Nonce int64 `json:"nonce"`
}
type balancesResult struct {
Available string `json:"available"`
Freeze string `json:"freeze"`
}
type BalancesJsonRes struct {
Success bool `json:"success"`
Message json.RawMessage `json:"message"`
Result map[string]balancesResult `json:"result"`
}
func (clt *Client) Balances() (*BalancesJsonRes, error) {
endpoint := "/api/v1/account/balances"
postBody := BalancesJsonBody{
RequestUrl: endpoint,
Nonce: time.Now().UnixNano(),
}
res, err := clt.AuthAPIRequest(postBody, http.MethodPost, endpoint)
if err != nil {
return nil, err
}
var jsonRes BalancesJsonRes
if err := json.Unmarshal(res, &jsonRes); err != nil {
return nil, err
}
return &jsonRes, nil
}
type CreateOrderParams struct {
Market string `json:"market"`
Side string `json:"side"`
Amount string `json:"amount"`
Price string `json:"price"`
}
type CreateOrderJsonBody struct {
RequestUrl string `json:"request"`
Nonce int64 `json:"nonce"`
Market string `json:"market"`
Side string `json:"side"`
Amount string `json:"amount"`
Price string `json:"price"`
}
type createOrderResult struct {
OrderId int64 `json:"orderId"`
Market string `json:"market"`
Price string `json:"price"`
Side string `json:"side"`
Timestamp float64 `json:"timestamp"`
DealMoney string `json:"dealMoney"`
DealStock string `json:"dealStock"`
Amount string `json:"amount"`
TakerFee string `json:"takerFee"`
MakerFee string `json:"makerFee"`
Left string `json:"left"`
DealFee string `json:"dealFee"`
}
type CreateOrderJsonRes struct {
Success bool `json:"success"`
Message json.RawMessage `json:"message"`
Result createOrderResult `json:"result"`
}
func (clt *Client) CreateOrder(opts CreateOrderParams) (*CreateOrderJsonRes, error) {
endpoint := "/api/v1/order/new"
postBody := CreateOrderJsonBody{
RequestUrl: endpoint,
Nonce: time.Now().UnixNano(),
Market: opts.Market,
Side: opts.Side,
Amount: opts.Amount,
Price: opts.Price,
}
res, err := clt.AuthAPIRequest(postBody, http.MethodPost, endpoint)
if err != nil {
return nil, err
}
var jsonRes CreateOrderJsonRes
if err := json.Unmarshal(res, &jsonRes); err != nil {
return nil, err
}
return &jsonRes, nil
}
type cancelOrderResult struct {
OrderId int64 `json:"orderId"`
Market string `json:"market"`
Price string `json:"price"`
Side string `json:"side"`
Type string `json:"type"`
Timestamp float64 `json:"timestamp"`
DealMoney string `json:"dealMoney"`
DealStock string `json:"dealStock"`
Amount string `json:"amount"`
TakerFee string `json:"takerFee"`
MakerFee string `json:"makerFee"`
Left string `json:"left"`
DealFee string `json:"dealFee"`
}
type CancelOrderJsonRes struct {
Success bool `json:"success"`
Message json.RawMessage `json:"message"`
Result cancelOrderResult `json:"result,omitempty"`
}
type CancelOrderParams struct {
Market string
OrderId int64
}
type CancelOrderJsonBody struct {
RequestUrl string `json:"request"`
Nonce int64 `json:"nonce"`
Market string `json:"market"`
OrderId int64 `json:"orderId"`
}
func (clt *Client) CancelOrder(opts CancelOrderParams) (*CancelOrderJsonRes, error) {
endpoint := "/api/v1/order/cancel"
postBody := CancelOrderJsonBody{
RequestUrl: endpoint,
Nonce: time.Now().UnixNano(),
Market: opts.Market,
OrderId: opts.OrderId,
}
res, err := clt.AuthAPIRequest(postBody, http.MethodPost, endpoint)
if err != nil {
return nil, err
}
var jsonRes CancelOrderJsonRes
if err := json.Unmarshal(res, &jsonRes); err != nil {
return nil, err
}
return &jsonRes, nil
}
type getOrdersResult struct {
OrderId int64 `json:"orderId"`
Left string `json:"left"`
Market string `json:"market"`
Amount string `json:"amount"`
Type string `json:"type"`
Price string `json:"price"`
Timestamp float64 `json:"timestamp"`
Side string `json:"side"`
DealFee string `json:"dealFee"`
TakerFee string `json:"takerFee"`
MakerFee string `json:"makerFee"`
DealStock string `json:"dealStock"`
DealMoney string `json:"dealMoney"`
}
type GetOrdersJsonRes struct {
Success bool `json:"success"`
Message json.RawMessage `json:"message"`
Result []getOrdersResult `json:"result"`
}
type GetOrdersBody struct {
RequestUrl string `json:"request"`
Nonce int64 `json:"nonce"`
Market string `json:"market"`
Offset int64 `json:"offset"`
Limit int64 `json:"limit"`
}
type GetOrdersParams struct {
Market string
Offset int64
Limit int64
}
func (clt *Client) GetOrders(opts GetOrdersParams) (*GetOrdersJsonRes, error) {
endpoint := "/api/v1/orders"
postBody := GetOrdersBody{
RequestUrl: endpoint,
Nonce: time.Now().UnixNano(),
Market: opts.Market,
Offset: opts.Offset,
Limit: opts.Limit,
}
res, err := clt.AuthAPIRequest(postBody, http.MethodPost, endpoint)
if err != nil {
return nil, err
}
var jsonRes GetOrdersJsonRes
if err := json.Unmarshal(res, &jsonRes); err != nil {
return nil, err
}
return &jsonRes, nil
}
type orderHistoryResult struct {
Id int64 `json:"id"`
Amount string `json:"amount"`
Price string `json:"price"`
Type string `json:"type"`
Side string `json:"side"`
CTime float64 `json:"ctime"`
TakerFee string `json:"takerFee"`
FTime float64 `json:"ftime"`
Market string `json:"market"`
MakerFee string `json:"makerFee"`
DealFee string `json:"dealFee"`
DealStock string `json:"dealStock"`
DealMoney string `json:"dealMoney"`
MarketName string `json:"marketName"`
}
type OrderHistoryJsonRes struct {
Success bool
Message json.RawMessage
Result map[string][]orderHistoryResult
}
type OrderHistoryBody struct {
RequestUrl string `json:"request"`
Nonce int64 `json:"nonce"`
Offset int64 `json:"offset"`
Limit int64 `json:"limit"`
}
type OrderHistoryParams struct {
Offset int64
Limit int64
}
func (clt *Client) OrderHistory(opts OrderHistoryParams) (*OrderHistoryJsonRes, error) {
endpoint := "/api/v1/account/order_history"
postBody := OrderHistoryBody{
RequestUrl: endpoint,
Nonce: time.Now().UnixNano(),
Offset: opts.Offset,
Limit: opts.Limit,
}
res, err := clt.AuthAPIRequest(postBody, http.MethodPost, endpoint)
if err != nil {
return nil, err
}
var jsonRes OrderHistoryJsonRes
if err := json.Unmarshal(res, &jsonRes); err != nil {
return nil, err
}
return &jsonRes, nil
}
type GetOrderJsonRes struct {
Success bool `json:"success"`
Message json.RawMessage `json:"message"`
}
type GetOrderBody struct {
RequestUrl string `json:"request"`
Nonce int64 `json:"nonce"`
Offset int64 `json:"offset"`
OrderID int64 `json:"orderId"`
Limit int64 `json:"limit"`
}
type GetOrderParams struct {
OrderID int64
Offset int64
Limit int64
}
func (clt *Client) GetOrder(opts GetOrderParams) (*GetOrderJsonRes, error) {
endpoint := "/api/v1/account/order"
postBody := GetOrderBody{
RequestUrl: endpoint,
Nonce: time.Now().UnixNano(),
OrderID: opts.OrderID,
Offset: opts.Offset,
Limit: opts.Limit,
}
res, err := clt.AuthAPIRequest(postBody, http.MethodPost, endpoint)
if err != nil {
return nil, err
}
var jsonRes GetOrderJsonRes
if err := json.Unmarshal(res, &jsonRes); err != nil {
return nil, err
}
return &jsonRes, nil
}