-
Notifications
You must be signed in to change notification settings - Fork 6
/
ws_private.go
130 lines (110 loc) · 2.56 KB
/
ws_private.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
package spotv3
import (
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"fmt"
"time"
"github.com/ginarea/gobybit/bybitv2/transport"
"github.com/msw-x/moon/ulog"
)
type WsPrivate struct {
ws *WsClient
s *WsSection
key string
secret string
ready bool
}
func NewWsPrivate(key string, secret string) *WsPrivate {
o := &WsPrivate{
ws: NewWsClient("spot/private/v3"),
key: key,
secret: secret,
}
o.s = NewWsSection(o)
return o
}
func (o *WsPrivate) Shutdown() {
o.ws.Shutdown()
}
func (o *WsPrivate) Conf() *transport.WsConf {
return o.ws.Conf()
}
func (o *WsPrivate) WithLog(log *ulog.Log) *WsPrivate {
o.ws.WithLog(log)
return o
}
func (o *WsPrivate) WithByTickUrl() *WsPrivate {
o.ws.WithByTickUrl()
return o
}
func (o *WsPrivate) WithProxy(proxy string) *WsPrivate {
o.Conf().SetProxy(proxy)
return o
}
func (o *WsPrivate) SetOnDialError(onDialError func(error) bool) {
o.ws.SetOnDialError(onDialError)
}
func (o *WsPrivate) Connected() bool {
return o.ws.Connected()
}
func (o *WsPrivate) Ready() bool {
return o.ready
}
func (o *WsPrivate) Run() {
o.ws.SetOnConnected(func() {
o.auth()
})
o.ws.SetOnDisconnected(func() {
o.ready = false
})
o.ws.SetOnAuth(func(ok bool) {
o.ready = ok
o.s.subscribeAll()
})
o.ws.SetOnTopicMessage(func(topic TopicMessage) error {
ok, err := o.s.processTopic(topic)
if !ok && err == nil {
err = fmt.Errorf("unknown topic: %s", topic.Topic)
}
return err
})
o.ws.Run()
}
func (o *WsPrivate) Order() *WsExecutor[[]OrderShot] {
return NewWsExecutor[[]OrderShot](o.s, Subscription{Topic: TopicOrder})
}
func (o *WsPrivate) StopOrder() *WsExecutor[[]StopOrderShot] {
return NewWsExecutor[[]StopOrderShot](o.s, Subscription{Topic: TopicStopOrder})
}
func (o *WsPrivate) Ticket() *WsExecutor[[]TicketShot] {
return NewWsExecutor[[]TicketShot](o.s, Subscription{Topic: TopicTicket})
}
func (o *WsPrivate) Outbound() *WsExecutor[[]OutboundShot] {
return NewWsExecutor[[]OutboundShot](o.s, Subscription{Topic: TopicOutbound})
}
func (o *WsPrivate) auth() {
expires := time.Now().Unix()*1000 + 10000
req := fmt.Sprintf("GET/realtime%d", expires)
sig := hmac.New(sha256.New, []byte(o.secret))
sig.Write([]byte(req))
signature := hex.EncodeToString(sig.Sum(nil))
cmd := struct {
Name string `json:"op"`
Args []any `json:"args"`
}{
Name: "auth",
Args: []any{
o.key,
expires,
signature,
},
}
o.ws.Send(cmd)
}
func (o *WsPrivate) subscribe(topic string) bool {
return o.ws.subscribe(topic)
}
func (o *WsPrivate) unsubscribe(topic string) bool {
return o.ws.unsubscribe(topic)
}