/
bot.go
351 lines (316 loc) · 10.7 KB
/
bot.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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
package trading
import (
"context"
"fmt"
"io/ioutil"
"strconv"
"strings"
"time"
"github.com/sirupsen/logrus"
"github.com/cwntr/go-dex-client/lssdrpc"
)
const (
LSSDConnectionProblem = "connect: connection refused"
)
// Bot struct holds all the gRPC client connections, the resolved config and the logger
type Bot struct {
OrderClient lssdrpc.OrdersClient
SwapClient lssdrpc.SwapsClient
CurrencyClient lssdrpc.CurrenciesClient
TradingPairClient lssdrpc.TradingPairsClient
LNDConfig LNDConfig
Log *logrus.Entry
LogLevel string
OrderLimit int
Timeout time.Duration
SentryDSN string
}
// Init func will read the contents of the given certificate paths
func (t *Bot) Init() error {
t.LNDConfig.Certs = make(map[string]string, 0)
for currency, path := range t.LNDConfig.TLSPaths {
b, err := ReadFile(path)
if err != nil {
return err
}
t.LNDConfig.Certs[currency] = string(b)
}
return nil
}
// AddCurrencies func will register all the currencies by performing a lssd grpc request for all certificates are specified in the config
func (t *Bot) AddCurrencies() error {
ctx, cancel := context.WithTimeout(context.Background(), t.Timeout)
defer cancel()
defer track(runningTime("bot.AddCurrencies"))
for currency, cert := range t.LNDConfig.Certs {
c := t.LNDConfig.Connections
x := c[currency]
cr := &lssdrpc.AddCurrencyRequest{
Currency: currency,
LndChannel: x.Format(),
}
cr.TlsCert = &lssdrpc.AddCurrencyRequest_RawCert{RawCert: cert}
_, err := t.CurrencyClient.AddCurrency(ctx, cr)
if err != nil {
return err
}
}
return nil
}
// SubscribeSwaps func will subscribe to swaps
func (t *Bot) SubscribeSwaps() error {
ctx, cancel := context.WithTimeout(context.Background(), t.Timeout)
defer cancel()
defer track(runningTime("bot.SubscribeSwaps"))
_, err := t.SwapClient.SubscribeSwaps(ctx, &lssdrpc.SubscribeSwapsRequest{})
if err != nil {
if IsLSSDConnProblem(err) {
//@TODO graceful shutdown of active orders
logger.Errorf("err connecting to LSSD, err: %v", err)
} else {
logger.Errorf("err subscribing to swaps : %v", err)
}
}
return err
}
// EnableTradingPair func will enable the given tradingPair
func (t *Bot) EnableTradingPair(pair string) error {
ctx, cancel := context.WithTimeout(context.Background(), t.Timeout)
defer cancel()
defer track(runningTime("bot.EnableTradingPair"))
_, err := bot.TradingPairClient.EnableTradingPair(ctx, &lssdrpc.EnableTradingPairRequest{PairId: pair})
if err != nil {
if IsLSSDConnProblem(err) {
//@TODO graceful shutdown of active orders
logger.Errorf("err connecting to LSSD, err: %v", err)
} else {
logger.Errorf("err enabling trading pairs : %v", err)
}
}
return err
}
// SubscribeOrders func will subscribe to orders
func (t *Bot) SubscribeOrders() error {
ctx, cancel := context.WithTimeout(context.Background(), t.Timeout)
defer cancel()
defer track(runningTime("bot.SubscribeOrders"))
_, err := t.OrderClient.SubscribeOrders(ctx, &lssdrpc.SubscribeOrdersRequest{})
if err != nil {
if IsLSSDConnProblem(err) {
//@TODO graceful shutdown of active orders
logger.Errorf("err connecting to LSSD, err: %v", err)
} else {
logger.Errorf("err subscribing to orders : %v", err)
}
}
return err
}
// ListOrders func will perform a lssd gRPC request to the ListOrders endpoint of the Orders service for a specified trading pair
func (t *Bot) ListOrders(tradingPair string, myOrders bool, printCLI bool) ([]lssdrpc.Order, error) {
ctx, cancel := context.WithTimeout(context.Background(), t.Timeout)
defer cancel()
defer track(runningTime("bot.ListOrders"))
var orders []lssdrpc.Order
var processed uint32
var limit = uint32(t.OrderLimit)
for {
res, err := t.OrderClient.ListOrders(ctx, &lssdrpc.ListOrdersRequest{
PairId: tradingPair,
IncludeOwnOrders: myOrders,
Skip: processed,
Limit: limit,
})
if err != nil {
if IsLSSDConnProblem(err) {
//@TODO graceful shutdown of active orders
logger.Errorf("err connecting to LSSD, err: %v", err)
} else {
logger.Errorf("err listing orders : %v", err)
}
return nil, err
}
if len(res.Orders) == 0 {
break
}
for _, o := range res.Orders {
if o != nil {
orders = append(orders, *o)
}
}
processed += uint32(len(res.Orders))
}
if printCLI {
for i, o := range orders {
fmt.Printf("id: %d | pair: %s | side: %s | orderId: %s | price: %v | funds: %v | isMy: %v \n", i, o.PairId, o.Side, o.OrderId, o.Price, o.Funds, o.IsOwnOrder)
}
}
return orders, nil
}
// PlaceOrder func will perform a lssd gRPC request to the PlaceOrder endpoint of the Orders service for a specified trading pair and order settings
func (t *Bot) PlaceOrder(tradingPair string, price int64, funds int64, side lssdrpc.OrderSide, orderType string, label string) (*lssdrpc.PlaceOrderResponse, error) {
ctx, cancel := context.WithTimeout(context.Background(), t.Timeout)
defer cancel()
defer track(runningTime("bot.PlaceOrder"))
p := strconv.FormatInt(price, 10)
priceBigInt := &lssdrpc.BigInteger{Value: p}
s := strconv.FormatInt(funds, 10)
fundsBigInt := &lssdrpc.BigInteger{Value: s}
order := &lssdrpc.PlaceOrderRequest{
PairId: tradingPair,
Side: side,
Funds: fundsBigInt,
Price: priceBigInt,
}
res, err := t.OrderClient.PlaceOrder(ctx, order)
if err != nil {
if IsLSSDConnProblem(err) {
//@TODO graceful shutdown of active orders
logger.Errorf("err connecting to LSSD, err: %v", err)
} else {
logger.Errorf("err placing order: %v", err)
}
return nil, err
}
// check if the order was placed
outcomeOrder, isPlaced := res.Outcome.(*lssdrpc.PlaceOrderResponse_Order)
if isPlaced {
if outcomeOrder.Order != nil {
_ = updateOrderStatus(myOrders, *outcomeOrder.Order, tradingPair, OrderStatusPlaced, orderType, label)
}
return res, nil
}
// check if the order was swapped already
outcomeSwapped, isSwapped := res.Outcome.(*lssdrpc.PlaceOrderResponse_SwapSuccess)
if isSwapped {
if outcomeSwapped.SwapSuccess != nil {
o := lssdrpc.Order{}
o.OrderId = outcomeSwapped.SwapSuccess.OrderId
o.Funds = outcomeSwapped.SwapSuccess.Funds
o.Price = outcomeSwapped.SwapSuccess.Price
o.CreatedAt = uint64(time.Now().Unix())
o.Side = side
_ = updateOrderStatus(myOrders, o, tradingPair, OrderStatusCompleted, orderType, label)
}
return res, nil
}
// check if the order failed (swap / orderbook error)
outcomeFailure, isFailure := res.Outcome.(*lssdrpc.PlaceOrderResponse_Failure)
if isFailure {
if outcomeFailure.Failure.Failure != nil {
//try OrderbookFailure
orderbookFailure, isSwapFailure := outcomeFailure.Failure.Failure.(*lssdrpc.PlaceOrderFailure_OrderbookFailure)
if isSwapFailure && orderbookFailure.OrderbookFailure != nil {
logger.Errorf(
"order outcome failure [orderbook-failure] (placingOrderDetails: funds: %d, price: %d, side: %s ), failureDetails: (funds: %s, requiredFee: %s, failureReason: %s)",
funds,
price,
side.String(),
orderbookFailure.OrderbookFailure.Funds.Value,
orderbookFailure.OrderbookFailure.RequiredFee.Value,
orderbookFailure.OrderbookFailure.FailureReason,
)
return res, nil
} else if isSwapFailure {
logger.Errorf(
"order outcome failure [orderbook-failure] (placingOrderDetails: funds: %d, price: %d, side: %s ), failureDetails: (empty)",
funds,
price,
side.String(),
)
return res, nil
}
//try SwapFailure
swapFailure, isSwapFailure := outcomeFailure.Failure.Failure.(*lssdrpc.PlaceOrderFailure_SwapFailure)
if isSwapFailure && swapFailure.SwapFailure != nil {
logger.Errorf(
"order outcome failure [swap-failure] (placingOrderDetails: funds: %d, price: %d, side: %s ), failureDetails: (funds: %s, orderId: %s, failureReason: %s)",
funds,
price,
side.String(),
swapFailure.SwapFailure.Funds,
swapFailure.SwapFailure.OrderId,
swapFailure.SwapFailure.FailureReason,
)
if strings.Contains(swapFailure.SwapFailure.FailureReason, "map::at") {
InitLSSD()
time.Sleep(time.Second * 5)
}
return res, nil
} else if isSwapFailure {
logger.Errorf(
"order outcome failure [swap-failure] (placingOrderDetails: funds: %d, price: %d, side: %s ), failureDetails: (empty)",
funds,
price,
side.String(),
)
return res, nil
}
}
// its a failure but failure-type cannot be determined
logger.Errorf("order outcome failure [?-failure] (funds: %d, price: %d, side: %s ), cannot determine failure type", funds, price, side.String())
return res, nil
}
return res, nil
}
// CancelOrder func will perform a lssd gRPC request to the PlaceOrder endpoint of the Orders service for a specified trading pair and order settings
func (t *Bot) CancelOrder(tradingPair string, orderID string) (*lssdrpc.CancelOrderResponse, error) {
ctx, cancel := context.WithTimeout(context.Background(), t.Timeout)
defer cancel()
defer track(runningTime("bot.CancelOrder"))
order := &lssdrpc.CancelOrderRequest{
PairId: tradingPair,
OrderId: orderID,
}
o := lssdrpc.Order{OrderId: orderID}
cacheOrder := GetMyOrderByID(tradingPair, orderID)
res, err := t.OrderClient.CancelOrder(ctx, order)
if err != nil {
if IsLSSDConnProblem(err) {
//@TODO graceful shutdown of active orders
logger.Errorf("err connecting to LSSD, err: %v", err)
} else {
logger.Errorf("err canceling order : %v", err)
}
return nil, err
}
if cacheOrder != nil {
updateOrderStatus(myOrders, o, tradingPair, OrderStatusCanceled, cacheOrder.OrderType, cacheOrder.Label)
}
return res, nil
}
// NewBot func will create a new Bot object based on all the lssd client connections and the resolved config
func NewBot(o lssdrpc.OrdersClient, s lssdrpc.SwapsClient, c lssdrpc.CurrenciesClient, t lssdrpc.TradingPairsClient, lndConfig LNDConfig, timeout time.Duration, orderLimit int) (*Bot, error) {
if lndConfig.IsEmpty() {
return nil, fmt.Errorf("lndConfig is empty")
}
b := &Bot{
OrderClient: o,
SwapClient: s,
CurrencyClient: c,
TradingPairClient: t,
LNDConfig: lndConfig,
Timeout: timeout,
OrderLimit: orderLimit,
}
b.Log = logrus.WithFields(logrus.Fields{"context": "bot"})
err := b.Init()
return b, err
}
// ReadFile will read the content of a given filepath, e.g. the certificate contents
func ReadFile(path string) ([]byte, error) {
return ioutil.ReadFile(path)
}
func runningTime(s string) (string, time.Time) {
logger.Debugf("-- bot time [%s] start", s)
return s, time.Now()
}
func track(s string, startTime time.Time) {
endTime := time.Now()
logger.Debugf("-- bot time [%s] end | took: %s", s, endTime.Sub(startTime))
}
func IsLSSDConnProblem(err error) bool {
if strings.Contains(err.Error(), LSSDConnectionProblem) {
return true
}
return false
}