-
Notifications
You must be signed in to change notification settings - Fork 11
/
private_wsclient.go
204 lines (173 loc) · 3.88 KB
/
private_wsclient.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
package swap
import (
"context"
"encoding/json"
"net/http"
"net/url"
"time"
"github.com/NadiaSama/ccexgo/exchange"
"github.com/NadiaSama/ccexgo/exchange/huobi"
"github.com/NadiaSama/ccexgo/internal/rpc"
"github.com/pkg/errors"
)
type (
PrivateCodeC struct {
*huobi.CodeC
}
PrivateWSClient struct {
key string
secret string
*exchange.WSClient
data chan interface{}
}
Response struct {
Op string `json:"op"`
}
subParam struct {
Op string `json:"op"`
Cid string `json:"cid"`
Topic string `json:"topic"`
}
PingResponse struct {
Op string `json:"op"`
TS string `json:"ts"`
}
AuthResponse struct {
Op string `json:"op"`
ErrCode int `json:"err-code"`
TS int64 `json:"ts"`
ErrMsg string `json:"err-msg"`
}
)
const (
SwapPrivateAddr = "wss://api.hbdm.com/swap-notification"
)
func NewPrivateCodeC() *PrivateCodeC {
return &PrivateCodeC{
CodeC: huobi.NewCodeC(),
}
}
func (pcc *PrivateCodeC) Encode(req rpc.Request) ([]byte, error) {
param := req.Params()
r, e := json.Marshal(param)
return r, e
}
func (pcc *PrivateCodeC) Decode(raw []byte) (rpc.Response, error) {
msg, err := pcc.Decompress(raw)
if err != nil {
return nil, err
}
var resp Response
if err := json.Unmarshal(msg, &resp); err != nil {
return nil, err
}
if resp.Op == "ping" {
var pr PingResponse
if err := json.Unmarshal(msg, &pr); err != nil {
return nil, err
}
return &rpc.Notify{
Method: "pong",
Params: pr.TS,
}, nil
}
if resp.Op == "auth" {
var ar AuthResponse
if err := json.Unmarshal(msg, &ar); err != nil {
return nil, err
}
var err error
if ar.ErrCode != 0 {
err = errors.Errorf("error happend %s", string(msg))
}
return &rpc.Result{
ID: "auth",
Error: err,
Result: msg,
}, nil
}
if resp.Op == "notify" {
r, err := ParseOrder(msg)
if err != nil {
return nil, err
}
op := r.Raw.(*OrderNotify)
return &rpc.Notify{
Method: op.Topic,
Params: r,
}, nil
}
return nil, errors.Errorf("unkown op %s", resp.Op)
}
func NewPrivateWSClient(key, secret string, data chan interface{}) *PrivateWSClient {
ret := &PrivateWSClient{
key: key,
secret: secret,
data: data,
}
ret.WSClient = exchange.NewWSClient(SwapPrivateAddr, NewPrivateCodeC(), ret)
return ret
}
func (ws *PrivateWSClient) Run(ctx context.Context) error {
if err := ws.WSClient.Run(ctx); err != nil {
return err
}
return ws.Auth(ctx)
}
func (ws *PrivateWSClient) Auth(ctx context.Context) error {
param := ws.genSignatureParmas()
var resp Response
if err := ws.Call(ctx, "auth", "", param, &resp); err != nil {
return err
}
return nil
}
func (ws *PrivateWSClient) Subscribe(ctx context.Context, channels ...exchange.Channel) error {
if len(channels) != 1 {
return errors.Errorf("only 1 channel can be subcribed")
}
param := subParam{
Op: "sub",
Cid: "123",
Topic: channels[0].String(),
}
err := ws.Call(ctx, "", "", param, nil)
return errors.WithMessage(err, "subscribe fail")
}
func (ws *PrivateWSClient) Handle(ctx context.Context, notify *rpc.Notify) {
if notify.Method == huobi.MethodPong {
go func() {
ws.Call(ctx, "", "", map[string]interface{}{
"op": "pong",
"ts": notify.Params,
}, nil)
}()
}
d := exchange.WSNotify{
Exchange: huobi.Huobi,
Chan: notify.Method,
Data: notify.Params,
}
select {
case ws.data <- &d:
default:
}
}
func (pws *PrivateWSClient) genSignatureParmas() map[string]string {
ts := time.Now().UTC()
ret := map[string]string{
"AccessKeyId": pws.key,
"SignatureMethod": "HmacSHA256",
"SignatureVersion": "2",
"Timestamp": ts.Format("2006-01-02T15:04:05"),
}
values := url.Values{}
for k, v := range ret {
values.Add(k, v)
}
sig := huobi.Signature(pws.secret, http.MethodGet, "api.hbdm.com", "/swap-notification", values.Encode())
ret["Signature"] = sig
ret["type"] = "api"
ret["op"] = "auth"
return ret
}