forked from nntaoli-project/goex
-
Notifications
You must be signed in to change notification settings - Fork 1
/
fmex_swap.go
826 lines (709 loc) · 21.5 KB
/
fmex_swap.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
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
package fmex
import (
"crypto/hmac"
"crypto/sha1"
"encoding/base64"
"errors"
"fmt"
. "github.com/GitTsewell/GoEx"
"net/http"
"net/url"
"strings"
"time"
"unsafe"
)
const (
SPOT = "spot"
ASSETS = "assets"
EXCHANGE = "exchange"
baseUrl = "https://api.fmex.com"
)
type FMexTicker struct {
Ticker
SellAmount,
BuyAmount float64
}
type FMexSwap struct {
httpClient *http.Client
baseUrl,
accessKey,
secretKey string
timeoffset int64
tradeSymbols []TradeSymbol
tradeSymbols2 []TradeSymbol2
}
type TradeSymbol struct {
Name string `json:"name"`
BaseCurrency string `json:"base_currency"`
QuoteCurrency string `json:"quote_currency"`
PriceDecimal int `json:"price_decimal"`
AmountDecimal int `json:"amount_decimal"`
Tradeable bool `json:"tradable"`
}
type TradeSymbol2 struct {
TradeSymbol
Category string `json:"category"`
LeveragedMultiple int `json:"leveraged_multiple"`
MarketOrderEnabled bool `json:"market_order_enabled"`
LimitAmountMin float64 `json:"limit_amount_min"`
LimitAmountMax float64 `json:"limit_amount_max"`
MainTag string `json:"main_tag"`
DailyOpenAt string `json:"daily_open_at"`
DailyCloseAt string `json:"daily_close_at"`
}
type Asset struct {
Currency Currency
Avaliable float64
Frozen float64
Finances float64
Lock float64
Total float64
}
type RawTicker struct {
Ticker
SellAmount float64
BuyAmount float64
LastTradeVol float64
}
type OrderFilter struct {
Range string
Symbol string
OffsetId string
Limit string
}
func NewFMexSwap(config *APIConfig) *FMexSwap {
if config.Endpoint == "" {
config.Endpoint = baseUrl
}
fm := &FMexSwap{baseUrl: config.Endpoint, accessKey: config.ApiKey, secretKey: config.ApiSecretKey, httpClient: config.HttpClient}
fm.setTimeOffset()
return fm
}
func (fm *FMexSwap) SetBaseUri(uri string) {
fm.baseUrl = uri
}
func (fm *FMexSwap) GetExchangeName() string {
return FMEX
}
/**
*获取交割预估价
*/
func (fm *FMexSwap) GetFutureEstimatedPrice(currencyPair CurrencyPair) (float64, error) {
panic("not supported.")
}
/**
* 期货行情
* @param currency_pair btc_usd:比特币 ltc_usd :莱特币
* @param contractType 合约类型: this_week:当周 next_week:下周 month:当月 quarter:季度
*/
func (fm *FMexSwap) GetFutureTicker(currencyPair CurrencyPair, contractType string) (*Ticker, error) {
respmap, err := HttpGet(fm.httpClient, fm.baseUrl+fmt.Sprintf("/v2/market/ticker/%s",
adaptContractType(currencyPair)))
if err != nil {
return nil, err
}
if respmap["status"].(float64) != 0 {
return nil, errors.New(respmap["msg"].(string))
}
tick, ok := respmap["data"].(map[string]interface{})
if !ok {
return nil, API_ERR
}
tickmap, ok := tick["ticker"].([]interface{})
if !ok {
return nil, API_ERR
}
ticker := new(RawTicker)
ticker.Pair = currencyPair
ticker.Date = uint64(time.Now().UnixNano() / 1000000)
ticker.Last = ToFloat64(tickmap[0])
ticker.Vol = ToFloat64(tickmap[9])
ticker.Low = ToFloat64(tickmap[8])
ticker.High = ToFloat64(tickmap[7])
ticker.Buy = ToFloat64(tickmap[2])
ticker.Sell = ToFloat64(tickmap[4])
ticker.SellAmount = ToFloat64(tickmap[5])
ticker.BuyAmount = ToFloat64(tickmap[3])
ticker.LastTradeVol = ToFloat64(tickmap[1])
return (*Ticker)(unsafe.Pointer(ticker)), nil
}
/**
* 期货深度
* @param currencyPair btc_usd:比特币 ltc_usd :莱特币
* @param contractType 合约类型: this_week:当周 next_week:下周 month:当月 quarter:季度
* @param size 获取深度档数
* @return
*/
func (fm *FMexSwap) GetFutureDepth(currency CurrencyPair, contractType string, size int) (*Depth, error) {
var uri string
if size <= 20 {
uri = fmt.Sprintf("/v2/market/depth/L20/%s", adaptContractType(currency))
} else {
uri = fmt.Sprintf("/v2/market/depth/L150/%s", adaptContractType(currency))
}
respmap, err := HttpGet(fm.httpClient, fm.baseUrl+uri)
if err != nil {
return nil, err
}
if respmap["status"].(float64) != 0 {
return nil, errors.New(respmap["msg"].(string))
}
datamap := respmap["data"].(map[string]interface{})
bids, ok1 := datamap["bids"].([]interface{})
asks, ok2 := datamap["asks"].([]interface{})
if !ok1 || !ok2 {
return nil, errors.New("depth error")
}
ts := ToInt64(datamap["ts"])
depth := new(Depth)
depth.Pair = currency
depth.UTime = time.Unix(0, ts*1000000)
n := 0
for i := 0; i < len(bids); {
depth.BidList = append(depth.BidList, DepthRecord{ToFloat64(bids[i]), ToFloat64(bids[i+1])})
i += 2
n++
if n == size {
break
}
}
n = 0
for i := 0; i < len(asks); {
depth.AskList = append(depth.AskList, DepthRecord{ToFloat64(asks[i]), ToFloat64(asks[i+1])})
i += 2
n++
if n == size {
break
}
}
return depth, nil
}
func (fm *FMexSwap) GetTrades(contract_type string, currencyPair CurrencyPair, since int64) ([]Trade, error) {
var uri = "/v2/market/trades/" + adaptContractType(currencyPair)
respmap, err := HttpGet(fm.httpClient, fm.baseUrl+uri)
if err != nil {
return nil, err
}
if respmap["status"].(float64) != 0 {
return nil, errors.New(respmap["msg"].(string))
}
datamap := respmap["data"].([]interface{})
trades := make([]Trade, 0)
for _, v := range datamap {
vv := v.(map[string]interface{})
side := BUY
if vv["side"] == "sell" {
side = SELL
}
trades = append(trades, Trade{
Tid: ToInt64(vv["id"]),
Type: side,
Amount: ToFloat64(vv["amount"]),
Price: ToFloat64(vv["price"]),
Date: ToInt64(vv["ts"]),
Pair: currencyPair,
})
}
return trades, nil
}
/**
* 期货指数
* @param currencyPair btc_usd:比特币 ltc_usd :莱特币
*/
func (fm *FMexSwap) GetFutureIndex(currencyPair CurrencyPair) (float64, error) {
var uri = "/v2/market/indexes"
respmap, err := HttpGet(fm.httpClient, fm.baseUrl+uri)
if err != nil {
return 0.0, err
}
if respmap["status"].(float64) != 0 {
return 0.0, errors.New(respmap["msg"].(string))
}
pair := "." + adaptCurrencyPair(currencyPair).ToLower().ToSymbol("") + "_spot"
datamap := respmap["data"].(map[string]interface{})
spot := datamap[pair].([]interface{})
return ToFloat64(spot[1]), nil
}
/**
*全仓账户
*/
func (fm *FMexSwap) GetFutureUserinfo() (*FutureAccount, error) {
r, err := fm.doAuthenticatedRequest("GET", "/v3/contracts/accounts", url.Values{})
if err != nil {
return nil, err
}
acc := new(FutureAccount)
acc.FutureSubAccounts = make(map[Currency]FutureSubAccount)
balances := r.(map[string]interface{})
for k, v := range balances {
vv := v.([]interface{})
currency := NewCurrency(k, "")
acc.FutureSubAccounts[currency] = FutureSubAccount{
Currency: currency,
AccountRights: ToFloat64(vv[0]),
KeepDeposit: ToFloat64(vv[2]),
ProfitReal: 0,
ProfitUnreal: 0,
RiskRate: 0,
}
}
return acc, nil
}
func (fm *FMexSwap) MarginTransferOut(currency Currency, amount float64) (bool, error) {
params := url.Values{}
params.Set("currency", strings.ToLower(currency.String()))
params.Set("amount", fmt.Sprint(amount))
params.Set("transferFrom", "CONTRACTS")
params.Set("transferFrom", "WALLET")
_, err := fm.doAuthenticatedRequest("POST", "/v3/contracts/transfer/out/request", params)
if err != nil {
return false, err
}
return true, nil
}
/**
* @deprecated
* 期货下单
* @param currencyPair btc_usd:比特币 ltc_usd :莱特币
* @param contractType 合约类型: this_week:当周 next_week:下周 month:当月 quarter:季度
* @param price 价格
* @param amount 委托数量
* @param openType 1:开多 2:开空 3:平多 4:平空
* @param matchPrice 是否为对手价 0:不是 1:是 ,当取值为1时,price无效
*/
func (fm *FMexSwap) PlaceFutureOrder(currencyPair CurrencyPair, contractType, price, amount string, openType, matchPrice, leverRate int) (string, error) {
params := url.Values{}
params.Set("source", "GoEx")
params.Set("symbol", adaptContractType(currencyPair))
switch openType {
case OPEN_BUY, CLOSE_SELL:
params.Set("direction", "LONG")
case OPEN_SELL, CLOSE_BUY:
params.Set("direction", "SHORT")
}
if matchPrice == 0 {
params.Set("type", "LIMIT")
params.Set("price", price)
} else {
params.Set("type", "MARKET")
}
params.Set("quantity", amount)
r, err := fm.doAuthenticatedRequest("POST", "/v3/contracts/orders", params)
if err != nil {
return "", err
}
data := r.(map[string]interface{})
return fmt.Sprintf("%d", int64(data["id"].(float64))), nil
}
type OrderParam struct {
Currency CurrencyPair
Type OrderType
Direction int
Price string
Amount string
OType int //1:开多 2:开空 3:平多 4: 平空
TriggerOn string //订单触发价格,如果不填,则立刻执行
TrailingDistance string //止盈止损订单触发距离,如果不填,则不会按止盈止损执行
IsFOK bool //是否设置FOK订单
IsIOC bool //是否设置IOC订单
IsPostOnly bool //是否设置PostOnly订单
IsHidden bool //是否设置Hidden订单
IsReduceOnly bool //是否设置ReduceOnly订单
}
func (fm *FMexSwap) orderParamCheck(param *OrderParam) error {
if param.TrailingDistance != "" && param.TriggerOn != "" {
return errors.New("triggerOn and trailingDistance couldn't be filled at the same time")
}
if param.IsFOK && (param.IsIOC || param.IsPostOnly || param.IsHidden || param.IsReduceOnly) {
return errors.New("IsIOC is true, coundn't set IsFOK | IsPostOnly | IsHidden | IsReduceOnly")
}
if param.IsIOC && (param.IsFOK || param.IsPostOnly || param.IsHidden || param.IsReduceOnly) {
return errors.New("IsIOC is true, coundn't set IsFOK | IsPostOnly | IsHidden | IsReduceOnly")
}
if param.IsPostOnly && (param.IsFOK || param.IsIOC || param.IsReduceOnly) {
return errors.New("IsPostOnly is true, coundn't set IsFOK | IsIOC | IsReduceOnly")
}
if param.IsHidden && (param.IsFOK || param.IsIOC) {
return errors.New("IsHidden is true, coundn't set IsFOK | IsIOC ")
}
// 只有MARKET订单与IOC订单可以设置reduceOnly=true。
return nil
}
func (fm *FMexSwap) PlaceFutureOrder2(ord *OrderParam) (string, error) {
err := fm.orderParamCheck(ord)
if err != nil {
return "", err
}
params := url.Values{}
params.Set("source", "GoEx")
params.Set("symbol", adaptContractType(ord.Currency))
switch ord.Direction {
case OPEN_BUY, CLOSE_SELL:
params.Set("direction", "LONG")
case OPEN_SELL, CLOSE_BUY:
params.Set("direction", "SHORT")
}
if ord.Type == ORDER_TYPE_LIMIT {
params.Set("type", "LIMIT")
params.Set("price", ord.Price)
} else {
params.Set("type", "MARKET")
}
params.Set("quantity", ord.Amount)
if ord.TriggerOn != "" {
params.Set("trigger_on", ord.TriggerOn)
}
if ord.TrailingDistance != "" {
params.Set("trailing_distance", ord.TrailingDistance)
}
params.Set("fill_or_kill", fmt.Sprint(ord.IsFOK))
params.Set("immediate_or_cancel", fmt.Sprint(ord.IsIOC))
params.Set("post_only", fmt.Sprint(ord.IsPostOnly))
params.Set("hidden", fmt.Sprint(ord.IsHidden))
params.Set("reduce_only", fmt.Sprint(ord.IsReduceOnly))
r, err := fm.doAuthenticatedRequest("POST", "/v3/contracts/orders", params)
if err != nil {
return "", err
}
data := r.(map[string]interface{})
return fmt.Sprintf("%d", int64(data["id"].(float64))), nil
}
/**
* 取消订单
* @param symbol btc_usd:比特币 ltc_usd :莱特币
* @param contractType 合约类型: this_week:当周 next_week:下周 month:当月 quarter:季度
* @param orderId 订单ID
*/
func (fm *FMexSwap) FutureCancelOrder(currencyPair CurrencyPair, contractType, orderId string) (bool, error) {
uri := fmt.Sprintf("/v3/contracts/orders/%s/cancel", orderId)
_, err := fm.doAuthenticatedRequest("POST", uri, url.Values{})
if err != nil {
return false, err
}
return true, nil
}
/**
* 用户持仓查询
* @param symbol btc_usd:比特币 ltc_usd :莱特币
* @param contractType 合约类型: this_week:当周 next_week:下周 month:当月 quarter:季度
* @return
*/
func (fm *FMexSwap) GetFuturePosition(currencyPair CurrencyPair, contractType string) ([]FuturePosition, error) {
r, err := fm.doAuthenticatedRequest("GET", "/v3/contracts/positions", url.Values{})
if err != nil {
return nil, err
}
data := r.(map[string]interface{})
result := data["results"].([]interface{})
var positions []FuturePosition
for _, info := range result {
cont := info.(map[string]interface{})
if !cont["closed"].(bool) {
p := FuturePosition{
CreateDate: int64(cont["updated_at"].(float64)),
LeverRate: int(cont["leverage"].(float64)),
Symbol: currencyPair,
ContractId: int64(cont["user_id"].(float64)),
ForceLiquPrice: cont["liquidation_price"].(float64),
}
if cont["direction"] == "long" {
p.BuyAmount = cont["quantity"].(float64)
p.BuyPriceAvg = cont["entry_price"].(float64)
p.BuyPriceCost = cont["margin"].(float64)
p.BuyProfitReal = cont["realized_pnl"].(float64)
} else {
p.SellAmount = cont["quantity"].(float64)
p.SellPriceAvg = cont["entry_price"].(float64)
p.SellPriceCost = cont["margin"].(float64)
p.SellProfitReal = cont["realized_pnl"].(float64)
}
positions = append(positions, p)
}
}
return positions, nil
}
/**
*获取订单信息
*/
func (fm *FMexSwap) GetFutureOrders(orderIds []string, currencyPair CurrencyPair, contractType string) ([]FutureOrder, error) {
if len(orderIds) == 0 {
return nil, errors.New("orderIds is empty")
}
orders := make([]FutureOrder, 0)
for _, orderId := range orderIds {
ord, err := fm.GetFutureOrder(orderId, currencyPair, contractType)
if err != nil {
return orders, err
}
orders = append(orders, *ord)
}
return orders, nil
}
func (fm *FMexSwap) GetFutureOrderHistory(filter *OrderFilter, currencyPair CurrencyPair, contractType string) ([]FutureOrder, error) {
param := url.Values{}
if filter != nil {
if filter.Range != "" {
param.Set("range", filter.Range)
}
if filter.Symbol != "" {
param.Set("symbol", filter.Symbol)
}
if filter.OffsetId != "" {
param.Set("offsetId", filter.OffsetId)
}
if filter.Limit != "" {
param.Set("limit", filter.Limit)
}
}
r, err := fm.doAuthenticatedRequest("GET", "/v3/contracts/orders/closed", param)
if err != nil {
return nil, err
}
data := r.(map[string]interface{})
result := data["results"].([]interface{})
var orders []FutureOrder
for _, info := range result {
ord := fm.parseOrder(info)
ord.Currency = currencyPair
ord.ContractName = contractType
orders = append(orders, ord)
}
return orders, nil
}
/**
*获取单个订单信息
*/
func (fm *FMexSwap) GetFutureOrder(orderId string, currencyPair CurrencyPair, contractType string) (*FutureOrder, error) {
r, err := fm.doAuthenticatedRequest("GET", "/v3/contracts/orders/"+orderId, url.Values{})
if err != nil {
return nil, err
}
data := r.(map[string]interface{})
order := fm.parseOrder(data)
order.Currency = currencyPair
order.ContractName = contractType
return &order, nil
}
func (fm *FMexSwap) parseOrderStatus(sts string) TradeStatus {
orderStatus := ORDER_UNFINISH
switch sts {
case "PARTIAL_FILLED", "partial_filled":
orderStatus = ORDER_PART_FINISH
case "FULLY_FILLED", "fully_filled":
orderStatus = ORDER_FINISH
//case "STOP_CANCELLED":
// orderStatus = ORDER_CANCEL_ING
case "FULLY_CANCELLED", "PARTIAL_CANCELLED", "STOP_CANCELLED", "fully_cancelled", "partial_cancelled", "stop_cancelled":
orderStatus = ORDER_CANCEL
}
return orderStatus
}
func (fm *FMexSwap) parseOrder(ord interface{}) FutureOrder {
order := ord.(map[string]interface{})
return FutureOrder{
OrderID2: fmt.Sprintf("%d", ToInt64(order["id"])),
Amount: ToFloat64(order["quantity"]),
DealAmount: ToFloat64(order["quantity"]) - ToFloat64(order["unfilled_quantity"]),
Price: ToFloat64(order["price"]),
Fee: ToFloat64(order["fee"]),
OrderTime: ToInt64(order["created_at"]),
Status: fm.parseOrderStatus(order["status"].(string)),
}
}
/**
*获取未完成订单信息
*/
func (fm *FMexSwap) GetUnfinishFutureOrders(currencyPair CurrencyPair, contractType string) ([]FutureOrder, error) {
r, err := fm.doAuthenticatedRequest("GET", "/v3/contracts/orders/open", url.Values{})
if err != nil {
return nil, err
}
data := r.(map[string]interface{})
var orders []FutureOrder
for _, info := range data["results"].([]interface{}) {
ord := fm.parseOrder(info)
ord.Currency = currencyPair
ord.ContractName = contractType
orders = append(orders, ord)
}
return orders, nil
}
/**
*获取交易费
*/
func (fm *FMexSwap) GetFee() (float64, error) {
panic("not supported.")
}
/**
*获取每张合约价值
*/
func (fm *FMexSwap) GetContractValue(currencyPair CurrencyPair) (float64, error) {
panic("not supported.")
}
/**
*获取交割时间 星期(0,1,2,3,4,5,6),小时,分,秒
*/
func (fm *FMexSwap) GetDeliveryTime() (int, int, int, int) {
panic("not supported.")
}
/**
* 获取K线数据
*/
func (fm *FMexSwap) GetKlineRecords(contract_type string, currency CurrencyPair, period, size, since int) ([]FutureKline, error) {
uri := fmt.Sprintf("/v2/market/candles/%s/%s?limit=%d", _INERNAL_KLINE_PERIOD_CONVERTER[period], adaptContractType(currency), size)
respmap, err := HttpGet(fm.httpClient, fm.baseUrl+uri)
if err != nil {
return nil, err
}
if respmap["status"].(float64) != 0 {
return nil, errors.New(respmap["msg"].(string))
}
datamap, isOk := respmap["data"].([]interface{})
if !isOk {
return nil, errors.New("kline error")
}
var klineRecords []FutureKline
for _, record := range datamap {
r := record.(map[string]interface{})
klineRecords = append(klineRecords, FutureKline{Kline: &Kline{
Pair: currency,
Timestamp: int64(ToInt(r["id"])),
Open: ToFloat64(r["open"]),
Close: ToFloat64(r["close"]),
High: ToFloat64(r["high"]),
Low: ToFloat64(r["low"]),
Vol: ToFloat64(r["quote_vol"])}})
}
return klineRecords, nil
}
func (fm *FMexSwap) GetServerTime() (int64, error) {
respmap, err := HttpGet(fm.httpClient, fm.baseUrl+"/v2/public/server-time")
if err != nil {
return 0, err
}
stime := int64(ToInt(respmap["data"]))
return stime, nil
}
func (fm *FMexSwap) setTimeOffset() error {
stime, err := fm.GetServerTime()
if err != nil {
return err
}
st := time.Unix(stime/1000, 0)
lt := time.Now()
offset := st.Sub(lt).Seconds()
fm.timeoffset = int64(offset)
return nil
}
func (fm *FMexSwap) doAuthenticatedRequest(method, uri string, params url.Values) (interface{}, error) {
timestamp := time.Now().Unix()*1000 + fm.timeoffset*1000
sign := fm.buildSigned(method, fm.baseUrl+uri, timestamp, params)
header := map[string]string{
"FC-ACCESS-KEY": fm.accessKey,
"FC-ACCESS-SIGNATURE": sign,
"FC-ACCESS-TIMESTAMP": fmt.Sprint(timestamp)}
var (
respmap map[string]interface{}
err error
)
switch method {
case "GET":
if len(params) != 0 {
respmap, err = HttpGet2(fm.httpClient, fm.baseUrl+uri+"?"+params.Encode(), header)
} else {
respmap, err = HttpGet2(fm.httpClient, fm.baseUrl+uri, header)
}
if err != nil {
return nil, err
}
case "POST":
var parammap = make(map[string]string, 1)
for k, v := range params {
parammap[k] = v[0]
}
respbody, err := HttpPostForm4(fm.httpClient, fm.baseUrl+uri, parammap, header)
if err != nil {
return nil, err
}
json.Unmarshal(respbody, &respmap)
}
if ToInt(respmap["status"]) != 0 {
return nil, errors.New(respmap["msg"].(string))
}
return respmap["data"], err
}
func (fm *FMexSwap) doAuthenticatedRequest2(method, uri string, params url.Values) (map[string]interface{}, error) {
timestamp := time.Now().Unix()*1000 + fm.timeoffset*1000
sign := fm.buildSigned(method, fm.baseUrl+uri, timestamp, params)
header := map[string]string{
"FC-ACCESS-KEY": fm.accessKey,
"FC-ACCESS-SIGNATURE": sign,
"FC-ACCESS-TIMESTAMP": fmt.Sprint(timestamp)}
var (
respmap map[string]interface{}
err error
)
switch method {
case "GET":
if len(params) != 0 {
respmap, err = HttpGet2(fm.httpClient, fm.baseUrl+uri+"?"+params.Encode(), header)
} else {
respmap, err = HttpGet2(fm.httpClient, fm.baseUrl+uri, header)
}
if err != nil {
return nil, err
}
case "POST":
var parammap = make(map[string]string, 1)
for k, v := range params {
parammap[k] = v[0]
}
respbody, err1 := HttpPostForm4(fm.httpClient, fm.baseUrl+uri, parammap, header)
if err1 != nil {
return nil, err1
}
err = json.Unmarshal(respbody, &respmap)
if err != nil {
return nil, err
}
}
return respmap, err
}
func (fm *FMexSwap) buildSigned(httpmethod string, apiurl string, timestamp int64, para url.Values) string {
var (
param = ""
err error
)
if para != nil {
param = para.Encode()
}
if "GET" == httpmethod && param != "" {
apiurl += "?" + param
}
signStr := httpmethod + apiurl + fmt.Sprint(timestamp)
if "POST" == httpmethod && param != "" {
signStr += param
}
signStr2, err := url.QueryUnescape(signStr) // 不需要编码
if err != nil {
signStr2 = signStr
}
sign := base64.StdEncoding.EncodeToString([]byte(signStr2))
mac := hmac.New(sha1.New, []byte(fm.secretKey))
mac.Write([]byte(sign))
sum := mac.Sum(nil)
s := base64.StdEncoding.EncodeToString(sum)
return s
}
func adaptCurrencyPair(pair CurrencyPair) CurrencyPair {
if pair.CurrencyA.Eq(BCH) || pair.CurrencyA.Eq(BCC) {
return NewCurrencyPair(NewCurrency("BCHABC", ""), pair.CurrencyB).AdaptUsdToUsdt()
}
if pair.CurrencyA.Symbol == "BSV" {
return NewCurrencyPair(NewCurrency("BCHSV", ""), pair.CurrencyB).AdaptUsdToUsdt()
}
return pair.AdaptUsdtToUsd()
}
func adaptContractType(currencyPair CurrencyPair) string {
return strings.ToLower(adaptCurrencyPair(currencyPair).ToSymbol("")) + "_p"
}