-
Notifications
You must be signed in to change notification settings - Fork 6
/
ws_private.go
114 lines (97 loc) · 2.54 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
package uperpetual
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
key string
secret string
}
func NewWsPrivate(key string, secret string) *WsPrivate {
return &WsPrivate{
ws: NewWsClient("private", "wss://stream.bybit.com/realtime_private"),
key: key,
secret: secret,
}
}
func (this *WsPrivate) Shutdown() {
this.ws.Shutdown()
}
func (this *WsPrivate) Conf() *transport.WsConf {
return this.ws.Conf()
}
func (this *WsPrivate) WithLog(log *ulog.Log) *WsPrivate {
this.ws.WithLog(log)
return this
}
func (this *WsPrivate) WithProxy(proxy string) *WsPrivate {
this.Conf().SetProxy(proxy)
return this
}
func (this *WsPrivate) Connected() bool {
return this.ws.Connected()
}
func (this *WsPrivate) Run() {
this.ws.SetOnConnected(func() {
this.auth()
})
this.ws.Run()
}
func (this *WsPrivate) SetOnAuth(onAuth func(bool)) {
this.ws.SetOnAuth(onAuth)
}
func (this *WsPrivate) auth() {
expires := time.Now().Unix()*1000 + 10000
req := fmt.Sprintf("GET/realtime%d", expires)
sig := hmac.New(sha256.New, []byte(this.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{
this.key,
expires,
signature,
},
}
this.ws.Send(cmd)
}
func (this *WsPrivate) SubscribePosition() bool {
return this.ws.Subscribe(Subscription{Topic: TopicPosition})
}
func (this *WsPrivate) UnsubscribePosition() bool {
return this.ws.Unsubscribe(Subscription{Topic: TopicPosition})
}
func (this *WsPrivate) SubscribeExecution() bool {
return this.ws.Subscribe(Subscription{Topic: TopicExecution})
}
func (this *WsPrivate) UnsubcribeExecution() bool {
return this.ws.Unsubscribe(Subscription{Topic: TopicExecution})
}
func (this *WsPrivate) SubscribeOrder() bool {
return this.ws.Subscribe(Subscription{Topic: TopicOrder})
}
func (this *WsPrivate) UnsubscribeOrder() bool {
return this.ws.Unsubscribe(Subscription{Topic: TopicOrder})
}
func (this *WsPrivate) SubscribeStopOrder() bool {
return this.ws.Subscribe(Subscription{Topic: TopicStopOrder})
}
func (this *WsPrivate) UnsubscribeStopOrder() bool {
return this.ws.Unsubscribe(Subscription{Topic: TopicStopOrder})
}
func (this *WsPrivate) SubscribeWallet() bool {
return this.ws.Subscribe(Subscription{Topic: TopicWallet})
}
func (this *WsPrivate) UnsubscribeWallet() bool {
return this.ws.Unsubscribe(Subscription{Topic: TopicWallet})
}