This commit is contained in:
Josh Harvey 2014-05-02 10:25:39 -04:00
parent 2d0938c1e5
commit d2cb7e8a07
7 changed files with 44 additions and 36 deletions

View file

@ -2,7 +2,7 @@
var path = require('path');
var async = require('async');
var winston = require('winston');
var logger = require('./logger');
var SATOSHI_FACTOR = Math.pow(10, 8);
@ -16,13 +16,6 @@ var Trader = module.exports = function (db) {
this.db = db;
this.rates = {};
var logLevel = process.env.LAMASSU_ENV === 'debug' ?
'debug' :
'info';
this.logger = new (winston.Logger)({
transports: [new (winston.transports.Console)({level: logLevel})]
});
this._tradeQueue = [];
this._sessionInfo = {};
};
@ -81,8 +74,12 @@ Trader.prototype._consolidateTrades = function () {
Trader.prototype._purchase = function (trade, cb) {
var self = this;
var rate = self.rate(trade.currency);
self.tradeExchange.purchase(trade.satoshis, rate.rate, function (err) {
var tradeCurrency = this.tradeExchange.currency();
var rate = this.rate(tradeCurrency).rate;
console.dir(this.rates);
console.log(rate);
console.log(tradeCurrency);
this.tradeExchange.purchase(trade.satoshis, rate, function (err) {
if (err) return cb(err);
self.pollBalance();
cb();
@ -233,27 +230,26 @@ Trader.prototype.trade = function (rec, deviceFingerprint) {
Trader.prototype.executeTrades = function () {
if (!this.tradeExchange) return;
this.logger.debug('checking for trades');
logger.debug('checking for trades');
var trade = this._consolidateTrades();
this.logger.debug('consolidated: ', JSON.stringify(trade));
logger.debug('consolidated: ', JSON.stringify(trade));
if (trade.fiat === 0) {
this.logger.debug('rejecting 0 trade');
logger.debug('rejecting 0 trade');
return;
}
if (trade.fiat < this.config.exchanges.settings.minimumTradeFiat) {
// throw it back in the water
this.logger.debug('reject fiat too small');
logger.debug('reject fiat too small');
this._tradeQueue.unshift(trade);
return;
}
this.logger.debug('making a trade: %d', trade.satoshis / Math.pow(10, 8));
var self = this;
logger.debug('making a trade: %d', trade.satoshis / Math.pow(10, 8));
this._purchase(trade, function (err) {
if (err) self.logger.error(err);
if (err) logger.error(err);
});
};
@ -281,7 +277,7 @@ Trader.prototype.stopPolling = function () {
Trader.prototype.pollBalance = function (callback) {
var self = this;
self.logger.debug('collecting balance');
logger.debug('collecting balance');
async.parallel({
transferBalance: self.transferExchange.balance.bind(self.transferExchange),
@ -298,7 +294,7 @@ Trader.prototype.pollBalance = function (callback) {
}
balance.timestamp = Date.now();
self.logger.debug('Balance update:', balance);
logger.debug('Balance update:', balance);
self.balance = balance;
return callback && callback(null, balance);
@ -308,19 +304,20 @@ Trader.prototype.pollBalance = function (callback) {
Trader.prototype.pollRate = function (callback) {
var self = this;
var currency = self.config.exchanges.settings.currency;
self.logger.debug('polling for rate...');
self.tickerExchange.ticker(currency, function(err, rate) {
logger.debug('polling for rates...');
self.tickerExchange.ticker(function(err, resRates) {
if (err) {
return callback && callback(err);
}
self.logger.debug('Rate update:', rate);
self.rates[currency] = {rate: rate, timestamp: new Date()};
return callback && callback(null, self.rates[currency]);
self.rateInfo = {rates: resRates, timestamp: new Date()};
});
};
// This is the rate in local currency quote to the user
Trader.prototype.rate = function () {
return this.rates[this.config.exchanges.settings.currency];
return {
rate: this.rateInfo.rates[this.config.exchanges.settings.currency],
timestamp: this.rateInfo.timestamp
};
};