-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfactory.go
More file actions
173 lines (165 loc) · 5.08 KB
/
factory.go
File metadata and controls
173 lines (165 loc) · 5.08 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
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
package main
import (
"context"
"fmt"
"os"
"strings"
exchanges "github.com/QuantProcessing/exchanges"
"github.com/QuantProcessing/exchanges/aster"
"github.com/QuantProcessing/exchanges/binance"
"github.com/QuantProcessing/exchanges/edgex"
"github.com/QuantProcessing/exchanges/grvt"
"github.com/QuantProcessing/exchanges/hyperliquid"
"github.com/QuantProcessing/exchanges/lighter"
"github.com/QuantProcessing/exchanges/nado"
"github.com/QuantProcessing/exchanges/okx"
"github.com/QuantProcessing/exchanges/standx"
"go.uber.org/zap"
)
// createAdapter creates an exchange adapter by name using env vars for credentials.
func createAdapter(ctx context.Context, name string, marketType exchanges.MarketType, logger *zap.SugaredLogger) (exchanges.Exchange, error) {
name = strings.ToUpper(strings.TrimSpace(name))
prefix := name + "_"
env := func(key string) string { return os.Getenv(prefix + key) }
quote := exchanges.QuoteCurrency(env("QUOTE_CURRENCY"))
switch name {
case "BINANCE":
if marketType == exchanges.MarketTypeSpot {
return binance.NewSpotAdapter(ctx, binance.Options{
APIKey: env("API_KEY"),
SecretKey: env("SECRET_KEY"),
QuoteCurrency: quote,
Logger: logger,
})
}
return binance.NewAdapter(ctx, binance.Options{
APIKey: env("API_KEY"),
SecretKey: env("SECRET_KEY"),
QuoteCurrency: quote,
Logger: logger,
})
case "OKX":
opts := okx.Options{
APIKey: env("API_KEY"),
SecretKey: env("SECRET_KEY"),
Passphrase: env("PASSPHRASE"),
QuoteCurrency: quote,
Logger: logger,
}
if marketType == exchanges.MarketTypeSpot {
return okx.NewSpotAdapter(ctx, opts)
}
return okx.NewAdapter(ctx, opts)
case "ASTER":
opts := aster.Options{
APIKey: env("API_KEY"),
SecretKey: env("SECRET_KEY"),
QuoteCurrency: quote,
Logger: logger,
}
if marketType == exchanges.MarketTypeSpot {
return aster.NewSpotAdapter(ctx, opts)
}
return aster.NewAdapter(ctx, opts)
case "NADO":
opts := nado.Options{
PrivateKey: env("PRIVATE_KEY"),
SubAccountName: env("SUB_ACCOUNT_NAME"),
QuoteCurrency: quote,
Logger: logger,
}
if marketType == exchanges.MarketTypeSpot {
return nado.NewSpotAdapter(ctx, opts)
}
return nado.NewAdapter(ctx, opts)
case "LIGHTER":
opts := lighter.Options{
PrivateKey: env("PRIVATE_KEY"),
AccountIndex: env("ACCOUNT_INDEX"),
KeyIndex: env("KEY_INDEX"),
RoToken: env("RO_TOKEN"),
QuoteCurrency: quote,
Logger: logger,
}
if marketType == exchanges.MarketTypeSpot {
return lighter.NewSpotAdapter(ctx, opts)
}
return lighter.NewAdapter(ctx, opts)
case "HYPERLIQUID":
opts := hyperliquid.Options{
PrivateKey: env("PRIVATE_KEY"),
AccountAddr: env("ACCOUNT_ADDR"),
QuoteCurrency: quote,
Logger: logger,
}
if marketType == exchanges.MarketTypeSpot {
return hyperliquid.NewSpotAdapter(ctx, opts)
}
return hyperliquid.NewAdapter(ctx, opts)
case "STANDX":
return standx.NewAdapter(ctx, standx.Options{
PrivateKey: env("PRIVATE_KEY"),
QuoteCurrency: quote,
Logger: logger,
})
case "EDGEX":
return edgex.NewAdapter(ctx, edgex.Options{
PrivateKey: env("PRIVATE_KEY"),
AccountID: env("ACCOUNT_ID"),
QuoteCurrency: quote,
Logger: logger,
})
case "GRVT":
return grvt.NewAdapter(ctx, grvt.Options{
APIKey: env("API_KEY"),
SubAccountID: env("SUB_ACCOUNT_ID"),
PrivateKey: env("PRIVATE_KEY"),
QuoteCurrency: quote,
Logger: logger,
})
default:
return nil, fmt.Errorf("unsupported exchange: %s", name)
}
}
// createRESTAdapter is kept for CLI compatibility.
// Since exchanges v0.2.13 removed the shared OrderMode toggle, the adapter's
// unsuffixed write methods are already its primary non-WS write path.
func createRESTAdapter(ctx context.Context, name string, marketType exchanges.MarketType, logger *zap.SugaredLogger) (exchanges.Exchange, error) {
return createAdapter(ctx, name, marketType, logger)
}
// credentialChecks maps exchange names to the env key that indicates the exchange is configured.
var credentialChecks = map[string]string{
"BINANCE": "API_KEY",
"OKX": "API_KEY",
"ASTER": "API_KEY",
"LIGHTER": "PRIVATE_KEY",
"EDGEX": "PRIVATE_KEY",
"GRVT": "API_KEY",
"NADO": "PRIVATE_KEY",
"HYPERLIQUID": "PRIVATE_KEY",
"STANDX": "PRIVATE_KEY",
}
// configuredExchanges returns the names of exchanges that have credentials in env.
func configuredExchanges() []string {
var result []string
for name, key := range credentialChecks {
if os.Getenv(name+"_"+key) != "" {
result = append(result, name)
}
}
return result
}
// resolveExchange auto-detects the exchange if only one is configured.
func resolveExchange(explicit string) string {
if explicit != "" {
return strings.ToUpper(explicit)
}
configured := configuredExchanges()
if len(configured) == 1 {
return configured[0]
}
if len(configured) > 1 {
fmt.Fprintf(os.Stderr, "Multiple exchanges configured: %v. Use -e to specify.\n", configured)
}
return ""
}