-
-
Notifications
You must be signed in to change notification settings - Fork 100
Expand file tree
/
Copy pathopt.go
More file actions
138 lines (118 loc) · 3.17 KB
/
Copy pathopt.go
File metadata and controls
138 lines (118 loc) · 3.17 KB
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
package hyperliquid
import (
"net/http"
"os"
"time"
"github.com/gorilla/websocket"
"github.com/sonirico/vago/lol"
)
type Opt[T any] func(*T)
func (o Opt[T]) Apply(opt *T) {
o(opt)
}
type (
ClientOpt = Opt[client]
ExchangeOpt = Opt[Exchange]
InfoOpt = Opt[Info]
WsOpt = Opt[WebsocketClient]
)
func WsOptDebugMode() WsOpt {
return func(w *WebsocketClient) {
w.debug = true
w.logger = lol.NewZerolog(
lol.WithLevel(lol.LevelTrace),
lol.WithWriter(os.Stdout),
lol.WithEnv(lol.EnvDev),
)
}
}
func InfoOptDebugMode() InfoOpt {
return func(i *Info) {
i.debug = true
}
}
func ExchangeOptDebugMode() ExchangeOpt {
return func(e *Exchange) {
e.debug = true
}
}
func clientOptDebugMode() ClientOpt {
return func(c *client) {
c.debug = true
c.logger = lol.NewZerolog(
lol.WithLevel(lol.LevelTrace),
lol.WithWriter(os.Stderr),
lol.WithEnv(lol.EnvDev),
)
}
}
// ExchangeOptClientOptions allows passing of ClientOpt to Client
func ExchangeOptClientOptions(opts ...ClientOpt) ExchangeOpt {
return func(e *Exchange) {
e.clientOpts = append(e.clientOpts, opts...)
}
}
// ExchangeOptInfoOptions allows passing of InfoOpt to Info
func ExchangeOptInfoOptions(opts ...InfoOpt) ExchangeOpt {
return func(e *Exchange) {
e.infoOpts = append(e.infoOpts, opts...)
}
}
func ExchangeOptPerpDex(dex string) ExchangeOpt {
return func(e *Exchange) {
e.dex = dex
if dex != "" {
e.infoOpts = append(e.infoOpts, InfoOptPerpDexName(dex))
}
}
}
func InfoOptPerpDexName(dex string) InfoOpt {
return func(i *Info) {
i.perpDexName = dex
}
}
// ExchangeOptL1Signer injects an L1ActionSigner. When nil, the default ECDSA implementation with privateKey is used.
func ExchangeOptL1Signer(s L1ActionSigner) ExchangeOpt {
return func(e *Exchange) {
e.l1Signer = s
}
}
// ExchangeOptUserSignedSigner injects a UserSignedActionSigner. When nil, the default ECDSA implementation with privateKey is used.
func ExchangeOptUserSignedSigner(s UserSignedActionSigner) ExchangeOpt {
return func(e *Exchange) {
e.userSignedSigner = s
}
}
// ExchangeOptAgentSigner injects an AgentSigner. When nil, the default ECDSA implementation with privateKey is used.
func ExchangeOptAgentSigner(s AgentSigner) ExchangeOpt {
return func(e *Exchange) {
e.agentSigner = s
}
}
// InfoOptClientOptions allows passing of ClientOpt to Info
func InfoOptClientOptions(opts ...ClientOpt) InfoOpt {
return func(i *Info) {
i.clientOpts = append(i.clientOpts, opts...)
}
}
// WsOptReadTimeout sets the maximum duration to wait for a single read from the
// server. If no message is received within the timeout the connection is closed
// and a reconnection is attempted. Must exceed the internal ping interval (50 s).
// Defaults to 90 s.
func WsOptReadTimeout(timeout time.Duration) WsOpt {
return func(w *WebsocketClient) {
w.readTimeout = timeout
}
}
// WsOptDialer allows setting a custom websocket.Dialer
func WsOptDialer(dialer *websocket.Dialer) WsOpt {
return func(w *WebsocketClient) {
w.dialer = dialer
}
}
// ClientOptHTTPClient allows setting a custom http.Client
func ClientOptHTTPClient(httpClient *http.Client) ClientOpt {
return func(c *client) {
c.httpClient = httpClient
}
}