-
Notifications
You must be signed in to change notification settings - Fork 11
/
symbol.go
84 lines (68 loc) · 1.83 KB
/
symbol.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
package swap
import (
"context"
"net/http"
"github.com/NadiaSama/ccexgo/exchange"
"github.com/NadiaSama/ccexgo/exchange/huobi"
"github.com/pkg/errors"
"github.com/shopspring/decimal"
)
type (
Symbol struct {
*exchange.BaseSwapSymbol
}
Data struct {
Symbol string `json:"symbol"`
ContractCode string `json:"contract_code"`
ContractSize float64 `json:"contract_size"`
PriceTick float64 `json:"price_tick"`
CreateDate string `json:"create_date"`
ContractStatus int `json:"contract_status"`
SettlementDate string `json:"settlement_date"`
}
ContractInfo struct {
Status string `json:"status"`
TS int `json:"ts"`
Data []Data `json:"data"`
}
)
const (
ContractEndPoint = "/swap-api/v1/swap_contract_info"
)
var (
contractMap = map[string]*Symbol{}
)
func (s *Symbol) String() string {
return s.Index()
}
func Init(ctx context.Context) error {
rc := NewRestClient("", "")
var ci ContractInfo
if err := rc.RequestWithRawResp(ctx, http.MethodGet, ContractEndPoint, nil, nil, false, &ci); err != nil {
return errors.WithMessagef(err, "get contract info fail")
}
if ci.Status != huobi.StatusOK {
return errors.Errorf("got huobi contract info fail")
}
for i := range ci.Data {
data := ci.Data[i]
cv := decimal.NewFromFloat(data.ContractSize)
symbol := &Symbol{
exchange.NewBaseSwapSymbolWithCfg(data.ContractCode, cv, exchange.SymbolConfig{
AmountPrecision: decimal.NewFromFloat(1.0),
PricePrecision: decimal.NewFromFloat(data.PriceTick),
AmountMin: decimal.NewFromInt(1),
AmountMax: decimal.Zero,
}, &data),
}
contractMap[data.ContractCode] = symbol
}
return nil
}
func ParseSymbol(sym string) (exchange.SwapSymbol, error) {
s, ok := contractMap[sym]
if !ok {
return nil, errors.Errorf("unsupport symbol %s", sym)
}
return s, nil
}