forked from bitontop/gored
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
567 lines (480 loc) · 17.9 KB
/
api.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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
package probit
// Copyright (c) 2015-2019 Bitontop Technologies Inc.
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"math"
"net/http"
"sort"
"strconv"
"time"
"github.com/chz8494/gored/coin"
"github.com/chz8494/gored/exchange"
"github.com/chz8494/gored/pair"
)
/*The Base Endpoint URL*/
const (
API_URL = "https://api.probit.com/api/exchange/v1"
)
/*API Base Knowledge
Path: API function. Usually after the base endpoint URL
Method:
Get - Call a URL, API return a response
Post - Call a URL & send a request, API return a response
Public API:
It doesn't need authorization/signature , can be called by browser to get response.
using exchange.HttpGetRequest/exchange.HttpPostRequest
Private API:
Authorization/Signature is requried. The signature request should look at Exchange API Document.
using ApiKeyGet/ApiKeyPost
Response:
Response is a json structure.
Copy the json to https://transform.now.sh/json-to-go/ convert to go Struct.
Add the go Struct to model.go
ex. Get /api/v1/depth
Get - Method
/api/v1/depth - Path*/
/*************** Public API ***************/
/*Get Coins Information (If API provide)
Step 1: Change Instance Name (e *<exchange Instance Name>)
Step 2: Add Model of API Response
Step 3: Modify API Path(strRequestUrl)*/
func (e *Probit) GetCoinsData() error {
coinsData := CoinsData{}
strRequestUrl := "/currency"
strUrl := API_URL + strRequestUrl
jsonCurrencyReturn := exchange.HttpGetRequest(strUrl, nil)
if err := json.Unmarshal([]byte(jsonCurrencyReturn), &coinsData); err != nil {
return fmt.Errorf("%s Get Coins Json Unmarshal Err: %v %v", e.GetName(), err, jsonCurrencyReturn)
}
for _, data := range coinsData.Data {
c := &coin.Coin{}
switch e.Source {
case exchange.EXCHANGE_API:
c = coin.GetCoin(data.ID)
if c == nil {
c = &coin.Coin{}
c.Code = data.ID
c.Name = data.Name
coin.AddCoin(c)
}
case exchange.JSON_FILE:
c = e.GetCoinBySymbol(data.ID)
}
txFee, err := strconv.ParseFloat(data.WithdrawalFee, 64)
if err != nil {
log.Printf("%s Get Coins txfee parse Err: %v, %v", e.GetName(), err, data.WithdrawalFee)
}
if c != nil {
coinConstraint := e.GetCoinConstraint(c)
if coinConstraint == nil {
coinConstraint = &exchange.CoinConstraint{
CoinID: c.ID,
Coin: c,
ExSymbol: data.ID,
ChainType: exchange.MAINNET,
TxFee: txFee,
Withdraw: !data.WithdrawalSuspended,
Deposit: !data.DepositSuspended,
Confirmation: data.MinConfirmationCount,
Listed: true,
}
} else {
coinConstraint.ExSymbol = data.ID
coinConstraint.TxFee = txFee
coinConstraint.Withdraw = !data.WithdrawalSuspended
coinConstraint.Deposit = !data.DepositSuspended
coinConstraint.Confirmation = data.MinConfirmationCount
}
e.SetCoinConstraint(coinConstraint)
}
}
return nil
}
/* GetPairsData - Get Pairs Information (If API provide)
Step 1: Change Instance Name (e *<exchange Instance Name>)
Step 2: Add Model of API Response
Step 3: Modify API Path(strRequestUrl)*/
func (e *Probit) GetPairsData() error {
pairsData := &PairsData{}
strRequestUrl := "/market"
strUrl := API_URL + strRequestUrl
jsonSymbolsReturn := exchange.HttpGetRequest(strUrl, nil)
if err := json.Unmarshal([]byte(jsonSymbolsReturn), &pairsData); err != nil {
return fmt.Errorf("%s Get Pairs Json Unmarshal Err: %v %v", e.GetName(), err, jsonSymbolsReturn)
}
for _, data := range pairsData.Data {
p := &pair.Pair{}
switch e.Source {
case exchange.EXCHANGE_API:
base := coin.GetCoin(data.QuoteCurrencyID)
target := coin.GetCoin(data.BaseCurrencyID)
if base != nil && target != nil {
p = pair.GetPair(base, target)
}
case exchange.JSON_FILE:
p = e.GetPairBySymbol(data.ID)
}
if p != nil && !data.Closed {
makerFee, _ := strconv.ParseFloat(data.MakerFeeRate, 64)
takerFee, _ := strconv.ParseFloat(data.TakerFeeRate, 64)
pairConstraint := e.GetPairConstraint(p)
if pairConstraint == nil {
pairConstraint = &exchange.PairConstraint{
PairID: p.ID,
Pair: p,
ExSymbol: data.ID,
MakerFee: makerFee,
TakerFee: takerFee,
LotSize: math.Pow10(-1 * data.QuantityPrecision),
PriceFilter: math.Pow10(-1 * data.CostPrecision),
Listed: !data.Closed,
}
} else {
pairConstraint.ExSymbol = data.ID
pairConstraint.MakerFee = makerFee
pairConstraint.TakerFee = takerFee
pairConstraint.LotSize = math.Pow10(-1 * data.QuantityPrecision)
pairConstraint.PriceFilter = math.Pow10(-1 * data.CostPrecision)
pairConstraint.Listed = !data.Closed
}
e.SetPairConstraint(pairConstraint)
}
}
return nil
}
/*Get Pair Market Depth
Step 1: Change Instance Name (e *<exchange Instance Name>)
Step 2: Add Model of API Response
Step 3: Get Exchange Pair Code ex. symbol := e.GetPairCode(p)
Step 4: Modify API Path(strRequestUrl)
Step 5: Add Params - Depend on API request
Step 6: Convert the response to Standard Maker struct*/
func (e *Probit) OrderBook(p *pair.Pair) (*exchange.Maker, error) {
orderBook := OrderBook{}
symbol := e.GetSymbolByPair(p)
strRequestUrl := "/order_book"
strUrl := API_URL + strRequestUrl
mapParams := make(map[string]string)
mapParams["market_id"] = symbol
maker := &exchange.Maker{
WorkerIP: exchange.GetExternalIP(),
Source: exchange.EXCHANGE_API,
BeforeTimestamp: float64(time.Now().UnixNano() / 1e6),
}
jsonOrderbook := exchange.HttpGetRequest(strUrl, mapParams)
if err := json.Unmarshal([]byte(jsonOrderbook), &orderBook); err != nil {
return nil, fmt.Errorf("%s OrderBook json Unmarshal error: %v %v", e.GetName(), err, jsonOrderbook)
}
maker.AfterTimestamp = float64(time.Now().UnixNano() / 1e6)
var err error
for _, order := range orderBook.Data {
if order.Side == "buy" {
buydata := exchange.Order{}
buydata.Quantity, err = strconv.ParseFloat(order.Quantity, 64)
if err != nil {
return nil, fmt.Errorf("%s OrderBook strconv.ParseFloat Quantity error:%v", e.GetName(), err)
}
buydata.Rate, err = strconv.ParseFloat(order.Price, 64)
if err != nil {
return nil, fmt.Errorf("%s OrderBook strconv.ParseFloat Rate error:%v", e.GetName(), err)
}
maker.Bids = append(maker.Bids, buydata)
} else if order.Side == "sell" {
selldata := exchange.Order{}
selldata.Quantity, err = strconv.ParseFloat(order.Quantity, 64)
if err != nil {
return nil, fmt.Errorf("%s OrderBook strconv.ParseFloat Quantity error:%v", e.GetName(), err)
}
selldata.Rate, err = strconv.ParseFloat(order.Price, 64)
if err != nil {
return nil, fmt.Errorf("%s OrderBook strconv.ParseFloat Rate error:%v", e.GetName(), err)
}
maker.Asks = append(maker.Asks, selldata)
}
}
// sort maker's price
Sort(maker.Bids[:], func(i, j int) bool {
return maker.Bids[i].Rate > maker.Bids[j].Rate
})
Sort(maker.Asks[:], func(i, j int) bool {
return maker.Asks[i].Rate < maker.Asks[j].Rate
})
return maker, err
}
func Sort(slice interface{}, less func(i, j int) bool) {
sort.Slice(slice, less)
}
func (e *Probit) LoadPublicData(operation *exchange.PublicOperation) error {
return fmt.Errorf("LoadPublicData :: Operation type invalid: %+v", operation.Type)
}
/*************** Private API ***************/
func (e *Probit) DoAccountOperation(operation *exchange.AccountOperation) error {
return nil
}
func (e *Probit) UpdateAllBalances() {
if e.API_KEY == "" || e.API_SECRET == "" {
log.Printf("%s API Key or Secret Key are nil.", e.GetName())
return
}
accountBalance := AccountBalances{}
strRequest := "/api/v3/account"
jsonBalanceReturn := e.ApiKeyGet(make(map[string]string), strRequest)
if err := json.Unmarshal([]byte(jsonBalanceReturn), &accountBalance); err != nil {
log.Printf("%s UpdateAllBalances json Unmarshal error: %v %s", e.GetName(), err, jsonBalanceReturn)
return
} else {
for _, balance := range accountBalance.Balances {
freeamount, err := strconv.ParseFloat(balance.Free, 64)
if err != nil {
log.Printf("%s UpdateAllBalances err: %+v %v", e.GetName(), balance, err)
return
} else {
c := e.GetCoinBySymbol(balance.Asset)
if c != nil {
balanceMap.Set(c.Code, freeamount)
}
}
}
}
}
/* Withdraw(coin *coin.Coin, quantity float64, addr, tag string) */
func (e *Probit) Withdraw(coin *coin.Coin, quantity float64, addr, tag string) bool {
if e.API_KEY == "" || e.API_SECRET == "" {
log.Printf("%s API Key or Secret Key are nil.", e.GetName())
return false
}
withdraw := WithdrawResponse{}
strRequest := "/wapi/v3/withdraw.html"
mapParams := make(map[string]string)
mapParams["asset"] = e.GetSymbolByCoin(coin)
mapParams["address"] = addr
if tag != "" { //this part is not working yet
mapParams["addressTag"] = tag
}
mapParams["amount"] = strconv.FormatFloat(quantity, 'f', -1, 64)
mapParams["timestamp"] = fmt.Sprintf("%d", time.Now().UnixNano()/1e6)
jsonSubmitWithdraw := e.WApiKeyRequest("POST", mapParams, strRequest)
if err := json.Unmarshal([]byte(jsonSubmitWithdraw), &withdraw); err != nil {
log.Printf("%s Withdraw Json Unmarshal Error: %v %v", e.GetName(), err, jsonSubmitWithdraw)
return false
}
if !withdraw.Success {
log.Printf("%s Withdraw Failed: %s", e.GetName(), withdraw.Msg)
return false
}
return true
}
func (e *Probit) LimitSell(pair *pair.Pair, quantity, rate float64) (*exchange.Order, error) {
if e.API_KEY == "" || e.API_SECRET == "" {
return nil, fmt.Errorf("%s API Key or Secret Key are nil.", e.GetName())
}
placeOrder := PlaceOrder{}
strRequest := "/api/v3/order"
priceFilter := int(math.Round(math.Log10(e.GetPriceFilter(pair)) * -1))
lotSize := int(math.Round(math.Log10(e.GetLotSize(pair)) * -1))
mapParams := make(map[string]string)
mapParams["symbol"] = e.GetSymbolByPair(pair)
mapParams["side"] = "SELL"
mapParams["type"] = "LIMIT"
mapParams["timeInForce"] = "GTC"
mapParams["price"] = strconv.FormatFloat(rate, 'f', priceFilter, 64)
mapParams["quantity"] = strconv.FormatFloat(quantity, 'f', lotSize, 64)
jsonPlaceReturn := e.ApiKeyRequest("POST", mapParams, strRequest)
if err := json.Unmarshal([]byte(jsonPlaceReturn), &placeOrder); err != nil {
return nil, fmt.Errorf("%s LimitSell Unmarshal Err: %v %v", e.GetName(), err, jsonPlaceReturn)
} else if placeOrder.Code != 0 {
return nil, fmt.Errorf("%s LimitSell failed:%v Message:%v", e.GetName(), placeOrder.Code, placeOrder.Msg)
}
order := &exchange.Order{
Pair: pair,
OrderID: fmt.Sprintf("%d", placeOrder.OrderID),
Rate: rate,
Quantity: quantity,
Direction: exchange.Sell,
Status: exchange.New,
JsonResponse: jsonPlaceReturn,
}
return order, nil
}
func (e *Probit) LimitBuy(pair *pair.Pair, quantity, rate float64) (*exchange.Order, error) {
if e.API_KEY == "" || e.API_SECRET == "" {
return nil, fmt.Errorf("%s API Key or Secret Key are nil.", e.GetName())
}
placeOrder := PlaceOrder{}
strRequest := "/api/v3/order"
priceFilter := int(math.Round(math.Log10(e.GetPriceFilter(pair)) * -1))
lotSize := int(math.Round(math.Log10(e.GetLotSize(pair)) * -1))
mapParams := make(map[string]string)
mapParams["symbol"] = e.GetSymbolByPair(pair)
mapParams["side"] = "BUY"
mapParams["type"] = "LIMIT"
mapParams["timeInForce"] = "GTC"
mapParams["price"] = strconv.FormatFloat(rate, 'f', priceFilter, 64)
mapParams["quantity"] = strconv.FormatFloat(quantity, 'f', lotSize, 64)
jsonPlaceReturn := e.ApiKeyRequest("POST", mapParams, strRequest)
if err := json.Unmarshal([]byte(jsonPlaceReturn), &placeOrder); err != nil {
return nil, fmt.Errorf("%s LimitBuy Unmarshal Err: %v %v", e.GetName(), err, jsonPlaceReturn)
} else if placeOrder.Code != 0 {
return nil, fmt.Errorf("%s LimitBuy failed:%v Message:%v", e.GetName(), placeOrder.Code, placeOrder.Msg)
}
order := &exchange.Order{
Pair: pair,
OrderID: fmt.Sprintf("%d", placeOrder.OrderID),
Rate: rate,
Quantity: quantity,
Direction: exchange.Buy,
Status: exchange.New,
JsonResponse: jsonPlaceReturn,
}
return order, nil
}
func (e *Probit) OrderStatus(order *exchange.Order) error {
if e.API_KEY == "" || e.API_SECRET == "" {
return fmt.Errorf("%s API Key or Secret Key are nil.", e.GetName())
}
orderStatus := PlaceOrder{}
strRequest := "/api/v3/order"
mapParams := make(map[string]string)
mapParams["symbol"] = e.GetSymbolByPair(order.Pair)
mapParams["orderId"] = order.OrderID
jsonOrderStatus := e.ApiKeyGet(mapParams, strRequest)
if err := json.Unmarshal([]byte(jsonOrderStatus), &orderStatus); err != nil {
return fmt.Errorf("%s OrderStatus Unmarshal Err: %v %v", e.GetName(), err, jsonOrderStatus)
} else if orderStatus.Code != 0 {
return fmt.Errorf("%s Get OrderStatus Error: %v %s", e.GetName(), orderStatus.Code, orderStatus.Msg)
}
if orderStatus.Status == "CANCELED" {
order.Status = exchange.Cancelled
} else if orderStatus.Status == "FILLED" {
order.Status = exchange.Filled
} else if orderStatus.Status == "PARTIALLY_FILLED" {
order.Status = exchange.Partial
} else if orderStatus.Status == "REJECTED" {
order.Status = exchange.Rejected
} else if orderStatus.Status == "Expired" {
order.Status = exchange.Expired
} else if orderStatus.Status == "NEW" {
order.Status = exchange.New
} else {
order.Status = exchange.Other
}
order.DealRate, _ = strconv.ParseFloat(orderStatus.Price, 64)
order.DealQuantity, _ = strconv.ParseFloat(orderStatus.ExecutedQty, 64)
return nil
}
func (e *Probit) ListOrders() ([]*exchange.Order, error) {
return nil, nil
}
func (e *Probit) CancelOrder(order *exchange.Order) error {
if e.API_KEY == "" || e.API_SECRET == "" {
return fmt.Errorf("%s API Key or Secret Key are nil.", e.GetName())
}
cancelOrder := PlaceOrder{}
strRequest := "/api/v3/order"
mapParams := make(map[string]string)
mapParams["symbol"] = e.GetSymbolByPair(order.Pair)
mapParams["orderId"] = order.OrderID
jsonCancelOrder := e.ApiKeyRequest("DELETE", mapParams, strRequest)
if err := json.Unmarshal([]byte(jsonCancelOrder), &cancelOrder); err != nil {
return fmt.Errorf("%s CancelOrder Unmarshal Err: %v %v", e.GetName(), err, jsonCancelOrder)
} else if cancelOrder.Code != 0 {
return fmt.Errorf("%s CancelOrder Error: %v %s", e.GetName(), cancelOrder.Code, cancelOrder.Msg)
}
order.Status = exchange.Canceling
order.CancelStatus = jsonCancelOrder
return nil
}
func (e *Probit) CancelAllOrder() error {
return nil
}
/*************** Signature Http Request ***************/
/*Method: API Request and Signature is required
Step 1: Change Instance Name (e *<exchange Instance Name>)
Step 2: Create mapParams Depend on API Signature request
Step 3: Add HttpGetRequest below strUrl if API has different requests*/
func (e *Probit) ApiKeyGet(mapParams map[string]string, strRequestPath string) string {
mapParams["recvWindow"] = "50000" //"50000000"
mapParams["timestamp"] = fmt.Sprintf("%d", time.Now().UTC().UnixNano()/int64(time.Millisecond))
mapParams["signature"] = exchange.ComputeHmac256NoDecode(exchange.Map2UrlQuery(mapParams), e.API_SECRET)
payload := exchange.Map2UrlQuery(mapParams)
strUrl := API_URL + strRequestPath + "?" + payload
request, err := http.NewRequest("GET", strUrl, nil)
if nil != err {
return err.Error()
}
request.Header.Add("Content-Type", "application/json; charset=utf-8")
request.Header.Add("X-MBX-APIKEY", e.API_KEY)
httpClient := &http.Client{}
response, err := httpClient.Do(request)
if nil != err {
return err.Error()
}
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
if nil != err {
return err.Error()
}
return string(body)
}
/*Method: API Request and Signature is required
Step 1: Change Instance Name (e *<exchange Instance Name>)
Step 2: Create mapParams Depend on API Signature request
Step 3: Add HttpGetRequest below strUrl if API has different requests*/
func (e *Probit) ApiKeyRequest(strMethod string, mapParams map[string]string, strRequestPath string) string {
mapParams["recvWindow"] = "50000" //"50000000"
mapParams["timestamp"] = fmt.Sprintf("%d", time.Now().UTC().UnixNano()/int64(time.Millisecond))
mapParams["signature"] = exchange.ComputeHmac256NoDecode(exchange.Map2UrlQuery(mapParams), e.API_SECRET)
payload := exchange.Map2UrlQuery(mapParams)
strUrl := API_URL + strRequestPath
request, err := http.NewRequest(strMethod, strUrl, bytes.NewBuffer([]byte(payload)))
if nil != err {
return err.Error()
}
request.Header.Add("Content-Type", "application/json; charset=utf-8")
request.Header.Add("X-MBX-APIKEY", e.API_KEY)
httpClient := &http.Client{}
response, err := httpClient.Do(request)
if nil != err {
return err.Error()
}
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
if nil != err {
return err.Error()
}
return string(body)
}
/*Method: API Request and Signature is required
Step 1: Change Instance Name (e *<exchange Instance Name>)
Step 2: Create mapParams Depend on API Signature request
Step 3: Add HttpGetRequest below strUrl if API has different requests*/
func (e *Probit) WApiKeyRequest(strMethod string, mapParams map[string]string, strRequestPath string) string {
mapParams["recvWindow"] = "50000" //"50000000"
mapParams["timestamp"] = fmt.Sprintf("%d", time.Now().UTC().UnixNano()/int64(time.Millisecond))
signature := exchange.ComputeHmac256NoDecode(exchange.Map2UrlQuery(mapParams), e.API_SECRET)
payload := fmt.Sprintf("%s&signature=%s", exchange.Map2UrlQuery(mapParams), signature)
strUrl := API_URL + strRequestPath
request, err := http.NewRequest(strMethod, strUrl, bytes.NewBuffer([]byte(payload)))
if nil != err {
return err.Error()
}
request.Header.Add("Content-Type", "application/json; charset=utf-8")
request.Header.Add("X-MBX-APIKEY", e.API_KEY)
httpClient := &http.Client{}
response, err := httpClient.Do(request)
if nil != err {
return err.Error()
}
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
if nil != err {
return err.Error()
}
return string(body)
}