-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi-endpoints.js
More file actions
145 lines (119 loc) · 5.13 KB
/
Copy pathapi-endpoints.js
File metadata and controls
145 lines (119 loc) · 5.13 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
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
const { createBinanceClient, getAccountBalance, getTickerPrices } = require('./binance-api');
// Get Balance Endpoint
async function getBalance(context) {
try {
const { userId } = context.request.body;
// Get user's API credentials from database
const apiConnections = await context.entities.ApiConnection.filter({
created_by: userId,
is_active: true
});
if (!apiConnections || apiConnections.length === 0) {
return { success: false, error: 'No active API connection found' };
}
const connection = apiConnections[0];
const client = createBinanceClient(
connection.api_key,
connection.api_secret,
connection.is_testnet
);
const balances = await getAccountBalance(client);
const prices = await getTickerPrices(client);
return {
success: true,
balances: balances,
prices: prices
};
} catch (error) {
return { success: false, error: error.message };
}
}
// Test Connection Endpoint
async function testConnection(context) {
try {
const { userId, connectionId } = context.request.body;
const connection = await context.entities.ApiConnection.get(connectionId);
if (!connection) {
return { success: false, error: 'Connection not found' };
}
const client = createBinanceClient(
connection.api_key,
connection.api_secret,
connection.is_testnet
);
await client.accountInfo();
return { success: true, message: 'Connection successful' };
} catch (error) {
return { success: false, error: error.message };
}
}
// Scan Opportunities Endpoint
async function scanOpportunities(context) {
try {
const { userId, botConfigId } = context.request.body;
const botConfig = await context.entities.BotConfig.get(botConfigId);
if (!botConfig) {
return { success: false, error: 'Bot config not found' };
}
const apiConnections = await context.entities.ApiConnection.filter({
created_by: userId,
is_active: true
});
if (!apiConnections || apiConnections.length === 0) {
return { success: false, error: 'No active API connection' };
}
const connection = apiConnections[0];
const client = createBinanceClient(
connection.api_key,
connection.api_secret,
connection.is_testnet
);
const prices = await getTickerPrices(client);
// Scan for triangular arbitrage opportunities
const opportunities = [];
const currencies = botConfig.enabled_currencies || ['BTC', 'ETH', 'USDT'];
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 >= botConfig.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: userId
});
opportunities.push({ currA, currB, currC, profitPercentage });
}
}
}
}
}
}
return { success: true, count: opportunities.length, opportunities };
} catch (error) {
return { success: false, error: error.message };
}
}
module.exports = {
getBalance,
testConnection,
scanOpportunities
};