Commit fce0049c by ken

balance

parent 57c1c7e1
const stream = 'wss://api.zb.com:9999/websocket';
const WebSocket = require('ws');
const options = {};
const observerSymbol = ["btcusdt", 'ethusdt', 'ltcusdt', 'eosusdt', 'bccusdt', 'etcusdt', 'qtumusdt', 'hsrusdt', 'xrpusdt'];
const observerSymbol = ["btcusdt", 'ethusdt', 'ltcusdt', 'bccusdt',];
const redis = require('redis');
const redisClient = redis.createClient();
const publishKey = "ZB@2";
const accountInfoChannel = "getaccountinfo";
const _handleSocketError = function(error) {
// Errors ultimately result in a `close` event.
......@@ -22,6 +23,7 @@ const _handleSocketOpen = function(opened_callback) {
const req = {event: 'addChannel', channel: symbol + '_depth'};
this.send(JSON.stringify(req));
}
this.send(JSON.stringify({event: 'addChannel', channel: accountInfoChannel}));
};
const _handleSocketClose = function(code, reason) {
......@@ -50,7 +52,16 @@ const _subscribe = function(callback, opened_callback = false) {
};
function subscribe() {
_subscribe((depth) => {
_subscribe((data) => {
if (data.channel === accountInfoChannel) {
const coins = data.data.coins;
const balances = {};
for (const coin of coins) {
balances[coin.enName] = {"onOrder": coin.freez, "available": coin.available}
}
redisClient.hset("balance", "binance", balances);
} else {
const depth = data;
const key = depth.channel.replace('_', '').toUpperCase() + '@zb';
const symbol = depth.channel.replace('_depth', '').toUpperCase();
console.log(symbol + '@' + depth.timestamp * 1000 + ' latency=' + (Date.now() - depth.timestamp * 1000) + 'ms');
......@@ -58,7 +69,8 @@ function subscribe() {
const depthJson = JSON.stringify({asks: depth.asks.reverse(), bids: depth.bids, symbol})
redisClient.set(key, depthJson, 'EX', 1);
redisClient.publish(publishKey, publishContent);
})
}
});
}
subscribe();
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment