checkpoint

This commit is contained in:
Jerry Brady 2014-04-17 01:53:40 +00:00
parent 384a10d825
commit 9ad11516d2
3 changed files with 180 additions and 122 deletions

View File

@ -8,7 +8,7 @@ module.exports = function() {
// Constants // Constants
var version = '0.1.0', var version = '0.1.0',
PUBLIC_API_URL = 'http://poloniex.com/public', PUBLIC_API_URL = 'https://poloniex.com/public',
PRIVATE_API_URL = 'https://poloniex.com/tradingApi', PRIVATE_API_URL = 'https://poloniex.com/tradingApi',
USER_AGENT = 'npm-crypto-apis/' + version USER_AGENT = 'npm-crypto-apis/' + version

View File

@ -1,5 +1,6 @@
var async = require('async'); var async = require('async');
var Cryptsy = require('./apiCryptsy.js');
var Poloniex = require('./apiPoloniex.js'); var Poloniex = require('./apiPoloniex.js');
var Stratum = require('stratum-pool'); var Stratum = require('stratum-pool');
@ -16,7 +17,7 @@ module.exports = function(logger){
// build status tracker for collecting coin market information // build status tracker for collecting coin market information
// //
var profitStatus = {}; var profitStatus = {};
var profitSymbols = {}; var symbolToAlgorithmMap = {};
Object.keys(poolConfigs).forEach(function(coin){ Object.keys(poolConfigs).forEach(function(coin){
var poolConfig = poolConfigs[coin]; var poolConfig = poolConfigs[coin];
@ -30,12 +31,10 @@ module.exports = function(logger){
symbol: poolConfig.coin.symbol, symbol: poolConfig.coin.symbol,
difficulty: 0, difficulty: 0,
reward: 0, reward: 0,
prices: {}, exchangeInfo: {}
depths: {},
volumes: {},
}; };
profitStatus[algo][poolConfig.coin.symbol] = coinStatus; profitStatus[algo][poolConfig.coin.symbol] = coinStatus;
profitSymbols[poolConfig.coin.symbol] = algo; symbolToAlgorithmMap[poolConfig.coin.symbol] = algo;
}); });
@ -52,8 +51,6 @@ module.exports = function(logger){
logger.debug(logSystem, 'Config', 'No alternative coins to switch to in current config, switching disabled.'); logger.debug(logSystem, 'Config', 'No alternative coins to switch to in current config, switching disabled.');
return; return;
} }
logger.debug(logSystem, 'profitStatus', JSON.stringify(profitStatus));
logger.debug(logSystem, 'profitStatus', JSON.stringify(profitSymbols));
// //
@ -63,6 +60,10 @@ module.exports = function(logger){
// 'API_KEY', // 'API_KEY',
// 'API_SECRET' // 'API_SECRET'
); );
var cryptsyApi = new Cryptsy(
// 'API_KEY',
// 'API_SECRET'
);
// //
// market data collection from Poloniex // market data collection from Poloniex
@ -75,51 +76,34 @@ module.exports = function(logger){
taskCallback(err); taskCallback(err);
return; return;
} }
Object.keys(profitSymbols).forEach(function(symbol){
var btcPrice = new Number(0); Object.keys(symbolToAlgorithmMap).forEach(function(symbol){
var ltcPrice = new Number(0); var exchangeInfo = profitStatus[symbolToAlgorithmMap[symbol]][symbol].exchangeInfo;
if (!exchangeInfo.hasOwnProperty('Poloniex'))
exchangeInfo['Poloniex'] = {};
var marketData = exchangeInfo['Poloniex'];
if (data.hasOwnProperty('BTC_' + symbol)) { if (data.hasOwnProperty('BTC_' + symbol)) {
btcPrice = new Number(data['BTC_' + symbol]); if (!marketData.hasOwnProperty('BTC'))
marketData['BTC'] = {};
var btcData = data['BTC_' + symbol];
marketData['BTC'].ask = new Number(btcData.lowestAsk);
marketData['BTC'].bid = new Number(btcData.highestBid);
marketData['BTC'].last = new Number(btcData.last);
marketData['BTC'].baseVolume = new Number(btcData.baseVolume);
marketData['BTC'].quoteVolume = new Number(btcData.quoteVolume);
} }
if (data.hasOwnProperty('LTC_' + symbol)) { if (data.hasOwnProperty('LTC_' + symbol)) {
ltcPrice = new Number(data['LTC_' + symbol]); if (!marketData.hasOwnProperty('LTC'))
} marketData['LTC'] = {};
if (btcPrice > 0 || ltcPrice > 0) { var ltcData = data['LTC_' + symbol];
var prices = { marketData['LTC'].ask = new Number(ltcData.lowestAsk);
BTC: btcPrice, marketData['LTC'].bid = new Number(ltcData.highestBid);
LTC: ltcPrice marketData['LTC'].last = new Number(ltcData.last);
}; marketData['LTC'].baseVolume = new Number(ltcData.baseVolume);
profitStatus[profitSymbols[symbol]][symbol].prices['Poloniex'] = prices; marketData['LTC'].quoteVolume = new Number(ltcData.quoteVolume);
}
});
taskCallback();
});
},
function(taskCallback){
poloApi.get24hVolume(function(err, data){
if (err){
taskCallback(err);
return;
}
Object.keys(profitSymbols).forEach(function(symbol){
var btcVolume = new Number(0);
var ltcVolume = new Number(0);
if (data.hasOwnProperty('BTC_' + symbol)) {
btcVolume = new Number(data['BTC_' + symbol].BTC);
}
if (data.hasOwnProperty('LTC_' + symbol)) {
ltcVolume = new Number(data['LTC_' + symbol].LTC);
}
if (btcVolume > 0 || ltcVolume > 0) {
var volumes = {
BTC: btcVolume,
LTC: ltcVolume
};
profitStatus[profitSymbols[symbol]][symbol].volumes['Poloniex'] = volumes;
} }
}); });
taskCallback(); taskCallback();
@ -127,34 +111,25 @@ module.exports = function(logger){
}, },
function(taskCallback){ function(taskCallback){
var depthTasks = []; var depthTasks = [];
Object.keys(profitSymbols).forEach(function(symbol){ Object.keys(symbolToAlgorithmMap).forEach(function(symbol){
var coinVolumes = profitStatus[profitSymbols[symbol]][symbol].volumes; var marketData = profitStatus[symbolToAlgorithmMap[symbol]][symbol].exchangeInfo['Poloniex'];
var coinPrices = profitStatus[profitSymbols[symbol]][symbol].prices; if (marketData.hasOwnProperty('BTC') && marketData['BTC'].bid > 0){
if (coinVolumes.hasOwnProperty('Poloniex') && coinPrices.hasOwnProperty('Poloniex')){
var btcDepth = new Number(0);
var ltcDepth = new Number(0);
if (coinVolumes['Poloniex']['BTC'] > 0 && coinPrices['Poloniex']['BTC'] > 0){
var coinPrice = new Number(coinPrices['Poloniex']['BTC']);
depthTasks.push(function(callback){ depthTasks.push(function(callback){
_this.getMarketDepthFromPoloniex('BTC', symbol, coinPrice, callback) _this.getMarketDepthFromPoloniex('BTC', symbol, marketData['BTC'].bid, callback)
}); });
} }
if (coinVolumes['Poloniex']['LTC'] > 0 && coinPrices['Poloniex']['LTC'] > 0){ if (marketData.hasOwnProperty('LTC') && marketData['LTC'].bid > 0){
var coinPrice = new Number(coinPrices['Poloniex']['LTC']);
depthTasks.push(function(callback){ depthTasks.push(function(callback){
_this.getMarketDepthFromPoloniex('LTC', symbol, coinPrice, callback) _this.getMarketDepthFromPoloniex('LTC', symbol, marketData['LTC'].bid, callback)
}); });
} }
}
}); });
if (depthTasks.length == 0){ if (!depthTasks.length){
taskCallback; taskCallback();
return; return;
} }
async.parallel(depthTasks, function(err){ async.series(depthTasks, function(err){
if (err){ if (err){
taskCallback(err); taskCallback(err);
return; return;
@ -181,30 +156,103 @@ module.exports = function(logger){
if (data.hasOwnProperty('bids')){ if (data.hasOwnProperty('bids')){
data['bids'].forEach(function(order){ data['bids'].forEach(function(order){
var price = new Number(order[0]); var price = new Number(order[0]);
var limit = new Number(coinPrice * portalConfig.profitSwitch.depth);
var qty = new Number(order[1]); var qty = new Number(order[1]);
// only measure the depth down to configured depth // only measure the depth down to configured depth
if (price >= coinPrice * portalConfig.profitSwitch.depth){ if (price >= limit){
depth += (qty * price); depth += (qty * price);
} }
}); });
} }
if (!profitStatus[profitSymbols[symbolB]][symbolB].depths.hasOwnProperty('Poloniex')){ var marketData = profitStatus[symbolToAlgorithmMap[symbolB]][symbolB].exchangeInfo['Poloniex'];
profitStatus[profitSymbols[symbolB]][symbolB].depths['Poloniex'] = { marketData[symbolA].depth = depth;
BTC: 0,
LTC: 0
};
}
profitStatus[profitSymbols[symbolB]][symbolB].depths['Poloniex'][symbolA] = depth;
callback(); callback();
}); });
}; };
// TODO
this.getProfitDataCryptsy = function(callback){ this.getProfitDataCryptsy = function(callback){
async.series([
function(taskCallback){
cryptsyApi.getTicker(function(err, data){
if (err || data.success != 1){
taskCallback(err);
return;
}
Object.keys(symbolToAlgorithmMap).forEach(function(symbol){
var exchangeInfo = profitStatus[symbolToAlgorithmMap[symbol]][symbol].exchangeInfo;
if (!exchangeInfo.hasOwnProperty('Cryptsy'))
exchangeInfo['Cryptsy'] = {};
var marketData = exchangeInfo['Cryptsy'];
var results = data.return.markets;
if (results.hasOwnProperty(symbol + '/BTC')) {
if (!marketData.hasOwnProperty('BTC'))
marketData['BTC'] = {};
var btcData = results[symbol + '/BTC'];
marketData['BTC'].last = new Number(btcData.lasttradeprice);
marketData['BTC'].baseVolume = new Number(marketData['BTC'].last / btcData.volume);
marketData['BTC'].quoteVolume = new Number(btcData.volume);
if (btcData.sellorders != null)
marketData['BTC'].ask = new Number(btcData.sellorders[0].price);
if (btcData.buyorders != null) {
marketData['BTC'].bid = new Number(btcData.buyorders[0].price);
var limit = new Number(marketData['BTC'].bid * portalConfig.profitSwitch.depth);
var depth = new Number(0);
btcData['buyorders'].forEach(function(order){
var price = new Number(order.price);
var qty = new Number(order.quantity);
if (price >= limit){
depth += (qty * price);
}
});
marketData['BTC'].depth = depth;
}
}
if (data.hasOwnProperty(symbol + '/LTC')) {
if (!marketData.hasOwnProperty('LTC'))
marketData['LTC'] = {};
var ltcData = results[symbol + '/LTC'];
marketData['LTC'].last = new Number(ltcData.lasttradeprice);
marketData['LTC'].baseVolume = new Number(marketData['LTC'].last / ltcData.volume);
marketData['LTC'].quoteVolume = new Number(ltcData.volume);
if (ltcData.sellorders != null)
marketData['LTC'].ask = new Number(ltcData.sellorders[0].price);
if (ltcData.buyorders != null) {
marketData['LTC'].bid = new Number(ltcData.buyorders[0].price);
var limit = new Number(marketData['LTC'].bid * portalConfig.profitSwitch.depth);
var depth = new Number(0);
ltcData['buyorders'].forEach(function(order){
var price = new Number(order.price);
var qty = new Number(order.quantity);
if (price >= limit){
depth += (qty * price);
}
});
marketData['LTC'].depth = depth;
}
}
});
taskCallback();
});
}
], function(err){
if (err){
callback(err);
return;
}
callback(null); callback(null);
});
}; };
this.getCoindDaemonInfo = function(callback){ this.getCoindDaemonInfo = function(callback){
var daemonTasks = []; var daemonTasks = [];
Object.keys(profitStatus).forEach(function(algo){ Object.keys(profitStatus).forEach(function(algo){
@ -222,7 +270,7 @@ module.exports = function(logger){
callback(); callback();
return; return;
} }
async.parallel(daemonTasks, function(err){ async.series(daemonTasks, function(err){
if (err){ if (err){
callback(err); callback(err);
return; return;
@ -233,23 +281,33 @@ module.exports = function(logger){
this.getDaemonInfoForCoin = function(symbol, cfg, callback){ this.getDaemonInfoForCoin = function(symbol, cfg, callback){
var daemon = new Stratum.daemon.interface([cfg]); var daemon = new Stratum.daemon.interface([cfg]);
daemon.once('online', function(){ daemon.once('online', function(){
async.parallel([
function(taskCallback){
daemon.cmd('getdifficulty', null, function(result){ daemon.cmd('getdifficulty', null, function(result){
if (result[0].error != null){ if (result[0].error != null){
callback(result[0].error); taskCallback(result[0].error);
return; return;
} }
profitStatus[profitSymbols[symbol]][symbol].difficulty = result[0].response; profitStatus[symbolToAlgorithmMap[symbol]][symbol].difficulty = result[0].response;
taskCallback(null);
daemon.cmd('getblocktemplate',
[{"capabilities": [ "coinbasetxn", "workid", "coinbase/append" ]}],
function(result){
if (result[0].error != null){
callback(result[0].error);
return;
}
profitStatus[profitSymbols[symbol]][symbol].reward = new Number(result[0].response.coinbasevalue / 100000000);
}); });
callback(null) },
function(taskCallback){
daemon.cmd('getblocktemplate', [{"capabilities": [ "coinbasetxn", "workid", "coinbase/append" ]}], function(result){
if (result[0].error != null){
taskCallback(result[0].error);
return;
}
profitStatus[symbolToAlgorithmMap[symbol]][symbol].reward = new Number(result[0].response.coinbasevalue / 100000000);
taskCallback(null);
});
}
], function(err){
if (err){
callback(err);
return;
}
callback(null);
}); });
}).once('connectionFailed', function(error){ }).once('connectionFailed', function(error){
callback(error); callback(error);