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

@ -63,10 +63,10 @@ module.exports = function(logger){
var oldPool = pools[oldCoin]; var oldPool = pools[oldCoin];
var proxyPort = proxySwitch[algo].port; var proxyPort = proxySwitch[algo].port;
if (newCoin == oldCoin) { if (newCoin == oldCoin) {
logger.debug(logSystem, logComponent, logSubCat, 'Switch message would have no effect - ignoring ' + newCoin); logger.debug(logSystem, logComponent, logSubCat, 'Switch message would have no effect - ignoring ' + newCoin);
break; break;
} }
logger.debug(logSystem, logComponent, logSubCat, 'Proxy message for ' + algo + ' from ' + oldCoin + ' to ' + newCoin); logger.debug(logSystem, logComponent, logSubCat, 'Proxy message for ' + algo + ' from ' + oldCoin + ' to ' + newCoin);
@ -91,8 +91,8 @@ module.exports = function(logger){
else { else {
logger.debug(logSystem, logComponent, logSubCat, 'Last proxy state saved to redis for ' + algo); logger.debug(logSystem, logComponent, logSubCat, 'Last proxy state saved to redis for ' + algo);
} }
}); });
}); });
} }
break; break;
} }
@ -230,9 +230,9 @@ module.exports = function(logger){
// Setup proxySwitch object to control proxy operations from configuration and any restored // Setup proxySwitch object to control proxy operations from configuration and any restored
// state. Each algorithm has a listening port, current coin name, and an active pool to // state. Each algorithm has a listening port, current coin name, and an active pool to
// which traffic is directed when activated in the config. // which traffic is directed when activated in the config.
// //
// In addition, the proxy config also takes diff and varDiff parmeters the override the // In addition, the proxy config also takes diff and varDiff parmeters the override the
// defaults for the standard config of the coin. // defaults for the standard config of the coin.
// //
Object.keys(portalConfig.proxy).forEach(function(algorithm) { Object.keys(portalConfig.proxy).forEach(function(algorithm) {
@ -245,21 +245,21 @@ module.exports = function(logger){
}; };
// Copy diff and vardiff configuation into pools that match our algorithm so the stratum server can pick them up // Copy diff and vardiff configuation into pools that match our algorithm so the stratum server can pick them up
// //
// Note: This seems a bit wonky and brittle - better if proxy just used the diff config of the port it was // Note: This seems a bit wonky and brittle - better if proxy just used the diff config of the port it was
// routed into instead. // routed into instead.
// //
if (portalConfig.proxy[algorithm].hasOwnProperty('varDiff')) { if (portalConfig.proxy[algorithm].hasOwnProperty('varDiff')) {
proxySwitch[algorithm].varDiff = new Stratum.varDiff(proxySwitch[algorithm].port, portalConfig.proxy[algorithm].varDiff); proxySwitch[algorithm].varDiff = new Stratum.varDiff(proxySwitch[algorithm].port, portalConfig.proxy[algorithm].varDiff);
proxySwitch[algorithm].diff = portalConfig.proxy[algorithm].diff; proxySwitch[algorithm].diff = portalConfig.proxy[algorithm].diff;
} }
Object.keys(pools).forEach(function (coinName) { Object.keys(pools).forEach(function (coinName) {
var a = poolConfigs[coinName].coin.algorithm; var a = poolConfigs[coinName].coin.algorithm;
var p = pools[coinName]; var p = pools[coinName];
if (a === algorithm) { if (a === algorithm) {
p.setVarDiff(proxySwitch[algorithm].port, proxySwitch[algorithm].varDiff); p.setVarDiff(proxySwitch[algorithm].port, proxySwitch[algorithm].varDiff);
} }
}); });
proxySwitch[algorithm].proxy = net.createServer(function(socket) { proxySwitch[algorithm].proxy = net.createServer(function(socket) {
@ -267,12 +267,12 @@ module.exports = function(logger){
var logSubCat = 'Thread ' + (parseInt(forkId) + 1); var logSubCat = 'Thread ' + (parseInt(forkId) + 1);
logger.debug(logSystem, 'Connect', logSubCat, 'Proxy connect from ' + socket.remoteAddress + ' on ' + proxySwitch[algorithm].port logger.debug(logSystem, 'Connect', logSubCat, 'Proxy connect from ' + socket.remoteAddress + ' on ' + proxySwitch[algorithm].port
+ ' routing to ' + currentPool); + ' routing to ' + currentPool);
pools[currentPool].getStratumServer().handleNewClient(socket); pools[currentPool].getStratumServer().handleNewClient(socket);
}).listen(parseInt(proxySwitch[algorithm].port), function() { }).listen(parseInt(proxySwitch[algorithm].port), function() {
logger.debug(logSystem, logComponent, logSubCat, 'Proxy listening for ' + algorithm + ' on port ' + proxySwitch[algorithm].port logger.debug(logSystem, logComponent, logSubCat, 'Proxy listening for ' + algorithm + ' on port ' + proxySwitch[algorithm].port
+ ' into ' + proxySwitch[algorithm].currentPool); + ' into ' + proxySwitch[algorithm].currentPool);
}); });
} }
else { else {

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){
depthTasks.push(function(callback){
if (coinVolumes.hasOwnProperty('Poloniex') && coinPrices.hasOwnProperty('Poloniex')){ _this.getMarketDepthFromPoloniex('BTC', symbol, marketData['BTC'].bid, callback)
var btcDepth = new Number(0); });
var ltcDepth = new Number(0); }
if (marketData.hasOwnProperty('LTC') && marketData['LTC'].bid > 0){
if (coinVolumes['Poloniex']['BTC'] > 0 && coinPrices['Poloniex']['BTC'] > 0){ depthTasks.push(function(callback){
var coinPrice = new Number(coinPrices['Poloniex']['BTC']); _this.getMarketDepthFromPoloniex('LTC', symbol, marketData['LTC'].bid, callback)
depthTasks.push(function(callback){ });
_this.getMarketDepthFromPoloniex('BTC', symbol, coinPrice, callback)
});
}
if (coinVolumes['Poloniex']['LTC'] > 0 && coinPrices['Poloniex']['LTC'] > 0){
var coinPrice = new Number(coinPrices['Poloniex']['LTC']);
depthTasks.push(function(callback){
_this.getMarketDepthFromPoloniex('LTC', symbol, coinPrice, 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,37 +156,110 @@ 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){
callback(null); 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);
});
}; };
this.getCoindDaemonInfo = function(callback){ this.getCoindDaemonInfo = function(callback){
var daemonTasks = []; var daemonTasks = [];
Object.keys(profitStatus).forEach(function(algo){ Object.keys(profitStatus).forEach(function(algo){
Object.keys(profitStatus[algo]).forEach(function(symbol){ Object.keys(profitStatus[algo]).forEach(function(symbol){
var coinName = profitStatus[algo][symbol].name; var coinName = profitStatus[algo][symbol].name;
var poolConfig = poolConfigs[coinName]; var poolConfig = poolConfigs[coinName];
var daemonConfig = poolConfig.shareProcessing.internal.daemon; var daemonConfig = poolConfig.shareProcessing.internal.daemon;
daemonTasks.push(function(callback){ daemonTasks.push(function(callback){
_this.getDaemonInfoForCoin(symbol, daemonConfig, callback) _this.getDaemonInfoForCoin(symbol, daemonConfig, callback)
}); });
@ -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;
@ -232,26 +280,36 @@ 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(){
daemon.cmd('getdifficulty', null, function(result){ async.parallel([
if (result[0].error != null){ function(taskCallback){
callback(result[0].error); daemon.cmd('getdifficulty', null, function(result){
return; if (result[0].error != null){
} taskCallback(result[0].error);
profitStatus[profitSymbols[symbol]][symbol].difficulty = result[0].response; return;
}
daemon.cmd('getblocktemplate', profitStatus[symbolToAlgorithmMap[symbol]][symbol].difficulty = result[0].response;
[{"capabilities": [ "coinbasetxn", "workid", "coinbase/append" ]}], taskCallback(null);
function(result){ });
if (result[0].error != null){ },
callback(result[0].error); function(taskCallback){
return; daemon.cmd('getblocktemplate', [{"capabilities": [ "coinbasetxn", "workid", "coinbase/append" ]}], function(result){
} if (result[0].error != null){
profitStatus[profitSymbols[symbol]][symbol].reward = new Number(result[0].response.coinbasevalue / 100000000); taskCallback(result[0].error);
}); return;
callback(null) }
}); profitStatus[symbolToAlgorithmMap[symbol]][symbol].reward = new Number(result[0].response.coinbasevalue / 100000000);
}).once('connectionFailed', function(error){ taskCallback(null);
});
}
], function(err){
if (err){
callback(err);
return;
}
callback(null);
});
}).once('connectionFailed', function(error){
callback(error); callback(error);
}).on('error', function(error){ }).on('error', function(error){
callback(error); callback(error);