-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot-scheduler.js
More file actions
100 lines (84 loc) · 4.4 KB
/
Copy pathbot-scheduler.js
File metadata and controls
100 lines (84 loc) · 4.4 KB
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
const { createBinanceClient, getTickerPrices } = require('./binance-api');
async function botScheduler(context) {
try {
// Get all active bots
const activeBots = await context.entities.BotConfig.filter({ is_active: true });
if (!activeBots || activeBots.length === 0) {
return { success: true, message: 'No active bots' };
}
for (const bot of activeBots) {
try {
// Get user's API connection
const apiConnections = await context.entities.ApiConnection.filter({
created_by: bot.created_by,
is_active: true
});
if (!apiConnections || apiConnections.length === 0) {
continue;
}
const connection = apiConnections[0];
const client = createBinanceClient(
connection.api_key,
connection.api_secret,
connection.is_testnet
);
const prices = await getTickerPrices(client);
// Scan for opportunities
const currencies = bot.enabled_currencies || ['BTC', 'ETH', 'USDT'];
let foundCount = 0;
for (let i = 0; i < currencies.length; i++) {
for (let j = 0; j < currencies.length; j++) {
for (let k = 0; k < currencies.length; k++) {
if (i !== j && j !== k && i !== k) {
const currA = currencies[i];
const currB = currencies[j];
const currC = currencies[k];
const pair1 = `${currA}${currB}`;
const pair2 = `${currB}${currC}`;
const pair3 = `${currC}${currA}`;
if (prices[pair1] && prices[pair2] && prices[pair3]) {
const rate1 = parseFloat(prices[pair1]);
const rate2 = parseFloat(prices[pair2]);
const rate3 = parseFloat(prices[pair3]);
const profit = (1 * rate1 * rate2 * rate3) - 1;
const profitPercentage = profit * 100;
if (profitPercentage >= bot.min_profit_percentage) {
await context.entities.ArbitrageOpportunity.create({
currency_a: currA,
currency_b: currB,
currency_c: currC,
rate_ab: rate1,
rate_bc: rate2,
rate_ca: rate3,
profit_percentage: profitPercentage,
status: 'active',
created_by: bot.created_by
});
foundCount++;
}
}
}
}
}
}
await context.entities.BotLog.create({
bot_config_id: bot.id,
log_type: 'scan',
message: `Scheduled scan completed. Found ${foundCount} opportunities.`,
created_by: bot.created_by
});
} catch (error) {
await context.entities.BotLog.create({
bot_config_id: bot.id,
log_type: 'error',
message: `Scheduler error: ${error.message}`,
created_by: bot.created_by
});
}
}
return { success: true, message: 'Scheduler completed' };
} catch (error) {
return { success: false, error: error.message };
}
}
module.exports = { botScheduler };