-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode_helper.js
More file actions
100 lines (86 loc) · 3.14 KB
/
node_helper.js
File metadata and controls
100 lines (86 loc) · 3.14 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
// Node Helper für HTTP API Requests
const NodeHelper = require("node_helper");
const Log = require("logger");
const axios = require("axios");
module.exports = NodeHelper.create({
// Helper starten
start() {
this.instanceIds = {};
},
// Socket-Benachrichtigungen empfangen
socketNotificationReceived(notification, payload) {
// Ticker-Anfrage verarbeiten
if (notification === "BITUNIX_REQUEST_TICKERS") {
this.fetchTickers(payload.stocks);
}
},
// Tickers von API holen (korrekt mit 24h-Kline)
async fetchTickers(stocks) {
try {
// Symbol normalisieren
const normalized = stocks.map(s =>
s.symbol.endsWith("USDT") ? s.symbol : `${s.symbol}USDT`
);
const symbols = normalized.join(",");
// Ticker-Daten holen
const tickerRes = await axios.get(
"https://fapi.bitunix.com/api/v1/futures/market/tickers",
{ params: { symbols } }
);
const tickerData = tickerRes.data?.data || [];
const formattedStocks = await Promise.all(
tickerData.map(async ticker => {
try {
// 24h-Kline holen
const klineRes = await axios.get(
"https://fapi.bitunix.com/api/v1/futures/market/kline",
{
params: {
symbol: ticker.symbol,
interval: "1m",
limit: 1,
endTime: Date.now() - 24 * 60 * 60 * 1000,
type: "LAST_PRICE"
}
}
);
// Format laut API-Doku: data[0].close
const close24h = parseFloat(klineRes.data?.data?.[0]?.close);
const lastPrice = parseFloat(ticker.lastPrice);
const changePercent =
close24h > 0 ? ((lastPrice - close24h) / close24h) * 100 : 0;
//console.log(`[MMM-Bitunix Node] ${ticker.symbol}: close24h=${close24h}, last=${lastPrice}, Δ=${changePercent.toFixed(2)}%`);
return {
symbol: ticker.symbol,
lastPrice,
changePercent: parseFloat(changePercent.toFixed(2)),
highPrice: parseFloat(ticker.high),
lowPrice: parseFloat(ticker.low),
volumeBase: parseFloat(ticker.baseVol),
volumeQuote: parseFloat(ticker.quoteVol)
};
} catch (err) {
console.warn(`[MMM-Bitunix Node] Fehler bei 24h-Kline für ${ticker.symbol}:`, err.message);
return {
symbol: ticker.symbol,
lastPrice: parseFloat(ticker.lastPrice),
changePercent: 0,
highPrice: parseFloat(ticker.high),
lowPrice: parseFloat(ticker.low),
volumeBase: parseFloat(ticker.baseVol),
volumeQuote: parseFloat(ticker.quoteVol)
};
}
})
);
// Antwort zurück an Frontend
this.sendSocketNotification("BITUNIX_TICKERS_RESPONSE", {
stocks: formattedStocks,
lastUpdate: Date.now()
});
//Log.info("[MMM-Bitunix] Tickers aktualisiert");
} catch (error) {
Log.error("[MMM-Bitunix] API Error:", error.message);
}
}
});