Permalink
Browse files

markets refactor; modular, easier to add additional markets

  • Loading branch information...
Luke Williams
Luke Williams committed Apr 19, 2015
1 parent 6f8cfc9 commit 87ee2ea48d9643c3f3526436441c4328d22c16a6
View
@@ -25,25 +25,17 @@ db.connect(dbString, function() {
});
}
});
db.check_market('poloniex', function(exists) {
if(exists == false) {
console.log('no poloniex entry found, creating now..');
db.create_market(settings.markets.coin, settings.markets.exchange, 'poloniex', function(){
});
}
});
db.check_market('bittrex', function(exists) {
if(exists == false) {
console.log('no bittrex entry found, creating now..');
db.create_market(settings.markets.coin, settings.markets.exchange, 'bittrex', function(){
//console.log('bittrex entry created successfully.');
});
}
});
// check markets
var markets = settings.markets.enabled;
for (var i = 0; i < markets.length; i++) {
db.check_market(markets[i], function(market, exists) {
if(exists == false) {
console.log('no %s entry found, creating now..', market);
db.create_market(settings.markets.coin, settings.markets.exchange, market, function(){
});
}
});
}
db.check_richlist(settings.coin, function(exists){
if (exists == false) {
@@ -53,15 +45,16 @@ db.connect(dbString, function() {
});
}
});
db.check_heavy(settings.coin, function(exists){
if (exists == false) {
console.log('no heavy entry found, creating now..');
db.create_heavy(settings.coin, function() {
});
}
});
if ( settings.heavy == true ) {
db.check_heavy(settings.coin, function(exists){
if (exists == false) {
console.log('no heavy entry found, creating now..');
db.create_heavy(settings.coin, function() {
});
}
});
}
var server = app.listen(app.get('port'), function() {
debug('Express server listening on port ' + server.address().port);
View

This file was deleted.

Oops, something went wrong.
View
@@ -7,8 +7,8 @@ var mongoose = require('mongoose')
, Heavy = require('../models/heavy')
, lib = require('./explorer')
, settings = require('./settings')
, poloniex = require('./poloniex')
, bittrex = require('./bittrex');
, poloniex = require('./markets/poloniex')
, bittrex = require('./markets/bittrex');
function find_address(hash, cb) {
Address.findOne({a_id: hash}, function(err, address) {
@@ -183,106 +183,21 @@ function save_tx(txid, cb) {
});
}
function poloniex_get_orders(cb) {
poloniex.get_orders(settings.markets.coin, settings.markets.exchange, function (error, orders){
if (error) {
return cb();
} else {
return cb(orders.bids, orders.asks);
}
});
}
function poloniex_get_trades(cb) {
poloniex.get_trades(settings.markets.coin, settings.markets.exchange, function (error, trades){
if (error) {
return cb();
} else {
return cb(trades);
}
});
}
function poloniex_get_summary(cb) {
poloniex.get_summary(settings.markets.coin, settings.markets.exchange, function (error, stats){
if (error) {
return cb();
} else {
return cb(stats);
}
});
}
function poloniex_get_chartdata(cb) {
poloniex.get_chartdata(settings.markets.coin, settings.markets.exchange, '1DD', function (error, chartdata){
if (error) {
console.log(error);
return cb();
} else {
if (chartdata.error == null) {
var processed = [];
lib.syncLoop(chartdata.length, function (loop) {
var i = loop.iteration();
processed.push([chartdata[i].date * 1000, parseFloat(chartdata[i].open), parseFloat(chartdata[i].high), parseFloat(chartdata[i].low), parseFloat(chartdata[i].close)]);
loop.next();
}, function(){
return cb(processed);
});
}
}
});
}
function bittrex_get_summary(cb) {
bittrex.get_summary(settings.markets.coin, settings.markets.exchange, function(err, result) {
if (err) {
console.log(err);
return cb();
} else {
return cb(result);
}
});
}
function bittrex_get_history(cb) {
bittrex.get_history(settings.markets.coin, settings.markets.exchange, function(err, result) {
if (err) {
console.log(err);
return cb();
} else {
return cb(result);
}
});
}
function bittrex_get_orders(cb) {
bittrex.get_orders(settings.markets.coin, settings.markets.exchange, function(err, orders) {
var buys = [];
var sells = [];
lib.syncLoop(orders.buy.length, function (loop) {
var i = loop.iteration();
var order = {
amount: parseFloat(orders.buy[i].Quantity),
price: parseFloat(orders.buy[i].Rate),
total: (parseFloat(orders.buy[i].Quantity) * parseFloat(orders.buy[i].Rate)).toFixed(8)
}
buys.push(order);
loop.next();
}, function(){
lib.syncLoop(orders.sell.length, function (loop) {
var i = loop.iteration();
var order = {
amount: parseFloat(orders.sell[i].Quantity),
price: parseFloat(orders.sell[i].Rate),
total: (parseFloat(orders.sell[i].Quantity) * parseFloat(orders.sell[i].Rate)).toFixed(8)
}
sells.push(order);
loop.next();
}, function(){
return cb(buys,sells);
function get_market_data(market, cb) {
switch(market) {
case 'bittrex':
bittrex.get_data(settings.markets.coin, settings.markets.exchange, function(obj){
return cb(obj);
});
});
});
break;
case 'poloniex':
poloniex.get_data(settings.markets.coin, settings.markets.exchange, function(obj){
return cb(obj);
});
break;
default:
return cb('market not supported');
}
}
module.exports = {
@@ -451,9 +366,9 @@ module.exports = {
check_market: function(market, cb) {
Markets.findOne({market: market}, function(err, exists) {
if(exists) {
return cb(true);
return cb(market, true);
} else {
return cb(false);
return cb(market, false);
}
});
},
@@ -624,48 +539,21 @@ module.exports = {
// updates market data for given market; called by sync.js
update_markets_db: function(market, cb) {
if(market == 'poloniex') {
poloniex_get_chartdata(function(chartdata){
poloniex_get_orders(function(buys, sells){
poloniex_get_trades(function(trades){
poloniex_get_summary(function(stats){
if (buys && sells && chartdata && trades && stats) {
Markets.update({market:market}, {
chartdata: chartdata,
buys: buys,
sells: sells,
history: trades,
summary: stats,
}, function() {
return cb(true);
});
}
});
});
get_market_data(market, function (obj) {
if (obj.buys && obj.sells && obj.chartdata && obj.trades && obj.stats) {
Markets.update({market:market}, {
chartdata: JSON.stringify(obj.chartdata),
buys: obj.buys,
sells: obj.sells,
history: obj.trades,
summary: obj.stats,
}, function() {
return cb(true);
});
});
} else if (market == 'bittrex') {
bittrex_get_history(function(history) {
bittrex_get_summary(function(summary) {
bittrex_get_orders(function(buys, sells) {
if (history && summary && buys && sells) {
Markets.update({market:market}, {
history: history,
summary: summary,
buys: buys,
sells: sells,
}, function() {
return cb(true);
})
}
});
});
});
}
else {
console.log("unknown market: %s", market);
return cb(false);
}
} else {
return cb(false);
}
});
},
// updates stats data for given coin; called by sync.js
Oops, something went wrong.

0 comments on commit 87ee2ea

Please sign in to comment.