-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
128 lines (109 loc) · 2.96 KB
/
main.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
package main
import (
"fmt"
"net/http"
"time"
"github.com/sirupsen/logrus"
"github.com/cwntr/go-dex-trading-bot/trading"
)
var (
cfg Config
logger *logrus.Entry
bot *trading.Bot
)
func main() {
logger = logrus.WithFields(logrus.Fields{"context": "main"})
//Read global config
err := readConfig()
if err != nil {
logger.Errorf("error reading global config, err %v", err)
return
}
cfg.LSSDConfig.Timeout, err = time.ParseDuration(cfg.LSSDConfig.TimeoutStr)
if err != nil {
logger.Errorf("unable parsing lssd timeout, err %v", err)
return
}
logger.Infoln("global config loaded")
//Initialize Clients
tpClient, tpConn := createTradingPairClient()
defer tpConn.Close()
oClient, oConn := createOrdersClient()
defer oConn.Close()
cClient, cConn := createCurrencyClient()
defer cConn.Close()
sClient, sConn := createSwapClient()
defer sConn.Close()
logger.Infoln("clients initiated")
//Initialize LNDConfig
tradingCfg := trading.NewConfig()
err = tradingCfg.Add(trading.CurrencyXSN, cfg.XSN.CertPath, cfg.XSN.Host, cfg.XSN.Port)
if err != nil {
logger.Errorf("error adding XSN to trading config, err %v", err)
return
}
err = tradingCfg.Add(trading.CurrencyLTC, cfg.LTC.CertPath, cfg.LTC.Host, cfg.LTC.Port)
if err != nil {
logger.Errorf("error adding LTC to trading config, err %v", err)
return
}
logger.Infoln("trading config loaded")
//Initialize Bot
bot, err = trading.NewBot(oClient, sClient, cClient, tpClient, tradingCfg, cfg.LSSDConfig.Timeout)
if err != nil {
logger.Errorf("error initializing trading bot, err %v", err)
return
}
logger.Infoln("trading bot initialized")
err = checkInfra(trading.CurrencyXSN)
if err != nil {
logger.Errorf("infra check, err %v", err)
return
}
err = checkInfra(trading.CurrencyLTC)
if err != nil {
logger.Errorf("infra check, err %v", err)
return
}
_ = simpleFlow(bot)
//LSSD routes
http.HandleFunc("/xsn_ltc/orderbook", OrderbookXSNLTC)
http.HandleFunc("/xsn_btc/orderbook", OrderbookXSNBTC)
http.HandleFunc("/orders", OrdersFunc)
http.HandleFunc("/orders/cancel", OrdersCancelFunc)
//LND routes
http.HandleFunc("/xsn/balance", XSNBalanceFunc)
http.HandleFunc("/ltc/balance", LTCBalanceFunc)
http.HandleFunc("/btc/balance", BTCBalanceFunc)
//Run HTTP server from bot config
logger.Fatal(http.ListenAndServe(fmt.Sprintf("%s:%d", cfg.Bot.Host, cfg.Bot.Port), nil))
}
func simpleFlow(bot *trading.Bot) error {
//Subscribe Swaps
err := bot.SubscribeSwaps()
if err != nil {
logger.Errorf("error subscribing to swaps, err %v", err)
return err
}
//Subscribe Orders
err = bot.SubscribeOrders()
if err != nil {
logger.Errorf("error subscribing to orders, err %v", err)
return err
}
//Add Currencies
err = bot.AddCurrencies()
if err != nil {
fmt.Printf("err: %v \n", err)
return err
}
/*
//List Orders
_, err = bot.ListOrders(trading.PairXSNLTC, true, true)
if err != nil {
logger.Errorf("err while listing the orderbook %v", err)
return err
}
*/
return nil
}