-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
101 lines (88 loc) · 2.82 KB
/
Copy pathclient.go
File metadata and controls
101 lines (88 loc) · 2.82 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
// Package hyperliquid provides a Go client library for the Hyperliquid
// exchange API. It includes support for both REST API and WebSocket
// connections, allowing users to access market data, manage orders, and
// handle user account operations.
package hyperliquid
import (
"crypto/ecdsa"
"errors"
"net/http"
"time"
"github.com/Simon-Busch/hyperliquid-go/info"
"github.com/Simon-Busch/hyperliquid-go/internal/transport"
"github.com/Simon-Busch/hyperliquid-go/stream"
"github.com/Simon-Busch/hyperliquid-go/trade"
"github.com/Simon-Busch/hyperliquid-go/types"
)
// Client is the top-level Hyperliquid client. It exposes three handles:
//
// c.Info — read-only queries
// c.Trade — signed actions (requires a private key)
// c.Stream — websocket subscriptions and POST requests
//
// Construct it with New and a list of options.
type Client struct {
Info *info.Client
Trade *trade.Client
Stream *stream.Client
}
// New builds a Client configured by the supplied options. At minimum either
// WithMainnet, WithTestnet, or WithBaseURL must be specified for non-default
// behaviour. Signed actions on Trade require WithPrivateKey.
func New(opts ...Option) (*Client, error) {
cfg := defaultClientConfig()
for _, o := range opts {
o(cfg)
}
api := transport.New(cfg.baseURL, cfg.httpClient)
infoC := info.New(cfg.baseURL, true, cfg.meta, cfg.spotMeta, cfg.perpDexs, cfg.builderDex)
c := &Client{Info: infoC}
if cfg.privateKey != nil {
c.Trade = trade.New(trade.Config{
Client: api,
PrivateKey: cfg.privateKey,
Vault: cfg.vault,
AccountAddr: cfg.account,
Dex: cfg.builderDex,
Info: infoC,
ExpiresAfter: cfg.expiresAfter,
})
}
if !cfg.skipStream {
s, err := stream.New(cfg.baseURL)
if err != nil {
return nil, err
}
s.SetLogger(cfg.logger)
s.SetMaxReconnectAttempts(cfg.maxReconnectAttempts)
s.SetReconnectWait(cfg.reconnectWait)
c.Stream = s
}
return c, nil
}
// clientConfig holds the options accumulated by New.
type clientConfig struct {
baseURL string
httpClient *http.Client
privateKey *ecdsa.PrivateKey
account string
vault string
builderDex string
meta *info.Meta
spotMeta *info.SpotMeta
perpDexs *types.MixedArray
skipStream bool
logger stream.Logger
maxReconnectAttempts int
reconnectWait time.Duration
expiresAfter *int64
}
func defaultClientConfig() *clientConfig {
return &clientConfig{
baseURL: types.MainnetAPIURL,
logger: nil, // stream.Client falls back to its internal nop logger
}
}
// ErrMissingPrivateKey is returned by Trader methods called on a Client
// constructed without WithPrivateKey.
var ErrMissingPrivateKey = errors.New("hyperliquid: trader requires WithPrivateKey")