-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
194 lines (169 loc) · 4.76 KB
/
app.js
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
const Binance = require("binance-api-node").default;
const binance = Binance();
const cors = require("cors");
const dotenv = require("dotenv").config();
const express = require("express");
const server = express();
const PORT = process.env.PORT || 3000;
const bodyParser = require("body-parser");
const _ = require("lodash");
const Bot = require("./bot");
let bots = [];
server.use(cors());
server.use(bodyParser.json());
server.set("json spaces", 2);
server.get("/", async function (req, res) {
res.status(200).json(bots);
});
server.get("/tickers", async function (req, res) {
let data = await binance.futuresAllBookTickers();
if (data != undefined) {
res.status(200).json(data);
} else {
res.status(500).json({ message: "failed to fetch tickers" });
}
});
server.get("/:id", async function (req, res) {
let bot = _.find(bots, function (o) {
return o.id == req.params.id;
});
if (bot != null && bot != undefined) {
res.status(200).json(bot);
} else {
res.status(500).json({ message: "bot with given ID not found" });
}
});
server.get("/history/:id", async function (req, res) {
let bot = _.find(bots, function (o) {
return o.id == req.params.id;
});
if (bot != null && bot != undefined) {
res.status(200).json(bot.history);
} else {
res.status(500).json({ message: "bot with given ID not found" });
}
});
server.get("/:id/last", async function (req, res) {
let bot = _.find(bots, function (o) {
return o.id == req.params.id;
});
if (bot != null && bot != undefined) {
res.status(200).json(bot.lastCandle());
} else {
res.status(500).json({ message: "bot with given ID not found" });
}
});
server.get("/start/:id", async function (req, res) {
let bot = _.find(bots, function (o) {
return o.id == req.params.id;
});
if (bot != null && bot != undefined) {
res.status(200);
bot.start();
} else {
res.status(500).json({ message: "bot with given ID not found" });
}
});
server.get("/stop/:id", async function (req, res) {
let bot = _.find(bots, function (o) {
return o.id == req.params.id;
});
if (bot != null && bot != undefined) {
res.status(200);
bot.stop();
} else {
res.status(500).json({ message: "bot with given ID not found" });
}
});
server.get("/signals/:id", async function (req, res) {
let bot = _.find(bots, function (o) {
return o.id == req.params.id;
});
if (bot != null && bot != undefined) {
res.status(200).json({
bullish: bot.bullishSignals,
bearish: bot.bearishSignals,
});
} else {
res.status(500).json({ message: "bot with given ID not found" });
}
});
server.get("/conditions/:id", async function (req, res) {
let bot = _.find(bots, function (o) {
return o.id == req.params.id;
});
if (bot != null && bot != undefined) {
res.status(200).json({
buyConditions: bot.buyConditions,
sellConditions: bot.sellConditions,
});
} else {
res.status(500).json({ message: "bot with given ID not found" });
}
});
server.post("/create", function (req, res) {
let data = req.body;
console.log(data);
if (data.id != null) {
bots.push(new Bot(data.id, data.ticker, data.interval));
res.status(200).json({
message: "bot successfully created",
});
} else {
res.status(500).json({
message: "could not create bot",
});
}
});
server.post("/delete", function (req, res) {
let data = req.body;
if (data.id != null) {
bots = bots.filter((bot) => bot.id !== data.id);
res.status(200).json({
message: "bot successfully deleted",
});
} else {
res.status(500).json({
message: "could not delete bot",
});
}
});
server.post("/buyconditions", function (req, res) {
let data = req.body;
let bot = _.find(bots, function (o) {
return o.id == data.id;
});
if (bot != null && bot != undefined) {
if (!bot.buyConditions.includes(data.condition)) {
bot.buyConditions.push(data.condition);
} else {
bot.buyConditions = bot.buyConditions.filter(function (e) {
return e !== data.condition;
});
}
res.status(200);
} else {
res.status(500).json({ message: "bot with given ID not found" });
}
});
server.post("/sellconditions", function (req, res) {
let data = req.body;
let bot = _.find(bots, function (o) {
return o.id == data.id;
});
if (bot != null && bot != undefined) {
if (!bot.sellConditions.includes(data.condition)) {
bot.sellConditions.push(data.condition);
} else {
bot.sellConditions = bot.sellConditions.filter(function (e) {
return e !== data.condition;
});
}
res.status(200);
} else {
res.status(500).json({ message: "bot with given ID not found" });
}
});
server.listen(PORT, async () => {
console.log(`server started on port ${PORT}`);
});