Commit fce0049c by ken

balance

parent 57c1c7e1
const stream = 'wss://api.zb.com:9999/websocket'; const stream = 'wss://api.zb.com:9999/websocket';
const WebSocket = require('ws'); const WebSocket = require('ws');
const options = {}; const options = {};
const observerSymbol = ["btcusdt", 'ethusdt', 'ltcusdt', 'eosusdt', 'bccusdt', 'etcusdt', 'qtumusdt', 'hsrusdt', 'xrpusdt']; const observerSymbol = ["btcusdt", 'ethusdt', 'ltcusdt', 'bccusdt',];
const redis = require('redis'); const redis = require('redis');
const redisClient = redis.createClient(); const redisClient = redis.createClient();
const publishKey = "ZB@2"; const publishKey = "ZB@2";
const accountInfoChannel = "getaccountinfo";
const _handleSocketError = function(error) { const _handleSocketError = function(error) {
// Errors ultimately result in a `close` event. // Errors ultimately result in a `close` event.
...@@ -22,6 +23,7 @@ const _handleSocketOpen = function(opened_callback) { ...@@ -22,6 +23,7 @@ const _handleSocketOpen = function(opened_callback) {
const req = {event: 'addChannel', channel: symbol + '_depth'}; const req = {event: 'addChannel', channel: symbol + '_depth'};
this.send(JSON.stringify(req)); this.send(JSON.stringify(req));
} }
this.send(JSON.stringify({event: 'addChannel', channel: accountInfoChannel}));
}; };
const _handleSocketClose = function(code, reason) { const _handleSocketClose = function(code, reason) {
...@@ -50,15 +52,25 @@ const _subscribe = function(callback, opened_callback = false) { ...@@ -50,15 +52,25 @@ const _subscribe = function(callback, opened_callback = false) {
}; };
function subscribe() { function subscribe() {
_subscribe((depth) => { _subscribe((data) => {
const key = depth.channel.replace('_', '').toUpperCase() + '@zb'; if (data.channel === accountInfoChannel) {
const symbol = depth.channel.replace('_depth', '').toUpperCase(); const coins = data.data.coins;
console.log(symbol + '@' + depth.timestamp * 1000 + ' latency=' + (Date.now() - depth.timestamp * 1000) + 'ms'); const balances = {};
const publishContent = JSON.stringify({symbol, time: depth.timestamp * 1000}) for (const coin of coins) {
const depthJson = JSON.stringify({asks: depth.asks.reverse(), bids: depth.bids, symbol}) balances[coin.enName] = {"onOrder": coin.freez, "available": coin.available}
redisClient.set(key, depthJson, 'EX', 1); }
redisClient.publish(publishKey, publishContent); 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');
const publishContent = JSON.stringify({symbol, time: depth.timestamp * 1000})
const depthJson = JSON.stringify({asks: depth.asks.reverse(), bids: depth.bids, symbol})
redisClient.set(key, depthJson, 'EX', 1);
redisClient.publish(publishKey, publishContent);
}
});
} }
subscribe(); 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