-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
110 lines (95 loc) · 3.69 KB
/
index.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
require('dotenv').config();
const express = require('express');
const app = express();
const path = require("path");
const cors = require('cors');
const cron = require('node-cron');
const socketIO = require('socket.io');
const server = require('http').createServer(app); // Create server with Express app
const checkTrades = require('./services/checkTrades.js');
const closeByProfit = require('./services/closeByProfit.js');
const updateFundingRate = require('./services/getFundingRate.js');
const updateAccumulatedFunding = require('./services/updateAccumulatedFunding.js');
const Bots = require('./models/BotsModel.js');
// Check for required environment variables
if (!process.env.PORT) {
console.error("Missing PORT environment variable. Please check your .env file");
process.exit(1);
}
// CORS configuration
console.log("IN",process.env.ENVIRONMENT, "ENVIRONMENT");
let corsOptions = {
origin: [
"https://dualnet-production.up.railway.app",
"http://localhost:3042",
"http://localhost:3000",
"http://dualnet.railway.internal",
"http://172.16.5.4:3000",
],
};
if (process.env.ENVIRONMENT === 'development') {
corsOptions = { origin: '*' }; // Allow all origins in development
}
app.use(cors(corsOptions));
const populateTables = require('./jobs/PopulateTables.js');
const StreamPrices = require('./services/StreamPrices.js');
const router = require("./routes/Routes.js");
const PORT = process.env.PORT || 3042;
app.use(express.json());
app.use('/api', router);
if (process.env.ENVIRONMENT !== 'development') {
// Serve static files from the React frontend app
app.use(express.static(path.join(__dirname, '../FrontendDualnet/build')))
// All other GET requests not handled before will return our React app
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, '../FrontendDualnet/build', 'index.html'));
});
}
// Global error handler
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).send('Something broke!');
});
// Create the WebSocket server
const io = socketIO(server, { cors: corsOptions });
io.on('connection', (socket) => {
// When a client connects, they should emit a 'join' event with their userId
socket.on('join', (userId) => {
// The client joins a room with a name equal to their userId
socket.join(userId);
});
// Listen for 'getBotData' event from the client
socket.on('getBotData', async (userId) => {
// Fetch all bots where isClose is false and userId matches the provided userId
const bots = await Bots.findAll({ where: { isClose: false, userId: userId } });
if (bots.length) {
try {
await closeByProfit(io, bots);
console.log('Completed the Close By profit loop');
} catch (error) {
console.error('Error closing trades:', error);
}
}
});
});
server.listen(PORT, () => {
console.log(`Server running at port ${PORT}`);
StreamPrices(io); // Start streaming prices after the server has started
});
// Schedule the cron job
cron.schedule('* * * * *', async () => {
// Fetch all bots where isClose is false
const bots = await Bots.findAll({ where: { isClose: false } });
if (bots.length) {
try {
await closeByProfit(io, bots);
console.log('Completed the Close By profit loop');
} catch (error) {
console.error('Error closing trades:', error);
}
}
});
cron.schedule('0 0 * * *', populateTables);
cron.schedule('0 */8 * * *', updateAccumulatedFunding);
cron.schedule('*/10 * * * *', updateFundingRate);
checkTrades();