-
Notifications
You must be signed in to change notification settings - Fork 1
/
routes.go
180 lines (164 loc) · 4.94 KB
/
routes.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
package main
import (
"encoding/json"
"net/http"
"github.com/cwntr/go-dex-trading-bot/lncli"
"github.com/cwntr/go-dex-trading-bot/lssdrpc"
"github.com/cwntr/go-dex-trading-bot/trading"
)
func OrderbookXSNLTC(w http.ResponseWriter, r *http.Request) {
orders, err := bot.ListOrders(trading.PairXSNLTC, true, true)
if err != nil {
logger.Errorf("err while listing the orderbook %v", err)
return
}
js, err := json.Marshal(orders)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(js)
}
func OrderbookXSNBTC(w http.ResponseWriter, r *http.Request) {
orders, err := bot.ListOrders(trading.PairXSNBTC, true, true)
if err != nil {
logger.Errorf("err while listing the orderbook %v", err)
return
}
js, err := json.Marshal(orders)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(js)
}
func XSNBalanceFunc(w http.ResponseWriter, r *http.Request) {
b, err := lncli.GetWalletBalance(cfg.Bot.LNCLIPath, cfg.XSN.Directory, cfg.XSN.Host, cfg.XSN.Port)
if err != nil {
logger.Errorf("err while GetWalletBalance %v", err)
return
}
js, err := json.Marshal(b)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(js)
}
func LTCBalanceFunc(w http.ResponseWriter, r *http.Request) {
b, err := lncli.GetWalletBalance(cfg.Bot.LNCLIPath, cfg.LTC.Directory, cfg.LTC.Host, cfg.LTC.Port)
if err != nil {
logger.Errorf("err while GetWalletBalance %v", err)
return
}
js, err := json.Marshal(b)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(js)
}
func BTCBalanceFunc(w http.ResponseWriter, r *http.Request) {
b, err := lncli.GetWalletBalance(cfg.Bot.LNCLIPath, cfg.BTC.Directory, cfg.BTC.Host, cfg.BTC.Port)
if err != nil {
logger.Errorf("err while GetWalletBalance %v", err)
return
}
js, err := json.Marshal(b)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(js)
}
func OrdersFunc(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
http.Error(w, "only POST method allowed", http.StatusNotFound)
return
}
decoder := json.NewDecoder(r.Body)
var orders []Order
err := decoder.Decode(&orders)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
for _, order := range orders {
if order.PriceRangeStart == 0 || order.PriceRangeEnd == 0 || order.PriceRangeStepSize == 0 || order.FixedFunding == 0 {
logger.Errorln("price range config: cannot have any '0' value")
continue
}
if order.Side != "sell" && order.Side != "buy" {
logger.Errorln("err: order wrong side - must be either `sell` or `buy`")
continue
}
//Iterate over order price configs
for _, price := range makeRange(order.PriceRangeStart, order.PriceRangeEnd, order.PriceRangeStepSize) {
//resolve side
var side lssdrpc.OrderSide
if order.Side == "sell" {
side = lssdrpc.OrderSide_sell
} else if order.Side == "buy" {
side = lssdrpc.OrderSide_buy
}
//Place the order
res, err := bot.PlaceOrder(order.TradingPair, price, order.FixedFunding, side)
if err != nil {
logger.Errorf("err while placing an order %v", err)
} else {
logger.Infof("Added order, outcome: %v", res.Outcome)
}
}
}
}
func OrdersCancelFunc(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
http.Error(w, "only POST method allowed", http.StatusNotFound)
return
}
decoder := json.NewDecoder(r.Body)
var orderCancel OrderCancel
err := decoder.Decode(&orderCancel)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
for _, cancel := range orderCancel.TradingPair {
for _, id := range cancel.OrderIDs {
//Cancel the order @TODO times out for some reason, but still works -> quick fix run it in parallel
go bot.CancelOrder(cancel.TradingPair, id)
}
if cancel.DeleteAll {
orders, err := bot.ListOrders(cancel.TradingPair, true, false)
if err != nil {
logger.Errorf("err while fetching all orders from orderbook, err %v", err)
return
}
for _, o := range orders {
if o.IsOwnOrder {
//Cancel the order @TODO times out for some reason, but still works -> quick fix run it in parallel
go bot.CancelOrder(cancel.TradingPair, o.OrderId)
//if err != nil {
// logger.Errorf("err while canceling order: %s, err %v", o.OrderId, err)
// } else {
// logger.Infof("canceled orderId: %s", o.OrderId)
// }
}
}
}
}
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte{})
}
func makeRange(min int, max int, step int) []int {
var rangeList []int
for i := min; i <= max; i += step {
rangeList = append(rangeList, i)
}
return rangeList
}