updated to new ticker api; fixed fiatBalance for trade exchange with different currency
This commit is contained in:
parent
f1533479f4
commit
e29290646e
3 changed files with 135 additions and 28 deletions
|
|
@ -17,6 +17,7 @@ var Trader = module.exports = function (db) {
|
|||
this.rates = {};
|
||||
this._tradeQueue = [];
|
||||
this._sessionInfo = {};
|
||||
this.rateInfo = null;
|
||||
};
|
||||
|
||||
Trader.prototype._findExchange = function (name) {
|
||||
|
|
@ -258,20 +259,51 @@ Trader.prototype.stopPolling = function () {
|
|||
clearInterval(this.rateInterval);
|
||||
};
|
||||
|
||||
// Trade exchange could be in a different currency than Bitcoin Machine.
|
||||
// For instance, trade exchange could be Bitstamp, denominated in USD,
|
||||
// while Bitcoin Machine is set to ILS.
|
||||
//
|
||||
// We need this function to convert the trade exchange balance into the
|
||||
// Bitcoin Machine denomination, in the example case: ILS.
|
||||
//
|
||||
// The best way to do that with available data is to take the ratio between
|
||||
// the exchange rates for the Bitcoin Machine and the trade exchange.
|
||||
Trader.prototype._tradeForexMultiplier = function _tradeForexMultiplier() {
|
||||
var deviceCurrency = this.config.exchanges.settings.currency;
|
||||
var tradeCurrency = this.tradeExchange.currency();
|
||||
var deviceRate = this._deviceRate();
|
||||
var tradeRate = this._tradeRate();
|
||||
|
||||
var forexMultiplier = deviceRate && tradeRate ?
|
||||
deviceRate / tradeRate :
|
||||
null;
|
||||
|
||||
return deviceCurrency === tradeCurrency ?
|
||||
1 :
|
||||
forexMultiplier;
|
||||
};
|
||||
|
||||
Trader.prototype._tradeBalanceFunc = function _tradeBalanceFunc(callback) {
|
||||
if (!this.tradeExchange) return callback(null, null);
|
||||
var forexMultiplier = this._tradeForexMultiplier();
|
||||
if (!forexMultiplier) return callback(new Error('Can\'t compute balance, no tickers yet.'));
|
||||
this.tradeExchange.balance(function (err, localBalance) {
|
||||
if (err) return callback(err);
|
||||
callback(null, localBalance * forexMultiplier);
|
||||
});
|
||||
};
|
||||
|
||||
Trader.prototype.pollBalance = function (callback) {
|
||||
var self = this;
|
||||
|
||||
logger.debug('collecting balance');
|
||||
|
||||
async.parallel({
|
||||
transferBalance: self.transferExchange.balance.bind(self.transferExchange),
|
||||
tradeBalance: function (next) {
|
||||
if (!self.tradeExchange) {
|
||||
return next(null, null);
|
||||
}
|
||||
var transferBalanceFunc = this.transferExchange.balance.bind(this.transferExchange);
|
||||
var tradeBalanceFunc = this._tradeBalanceFunc.bind(this);
|
||||
|
||||
self.tradeExchange.balance(next);
|
||||
}
|
||||
async.parallel({
|
||||
transferBalance: transferBalanceFunc,
|
||||
tradeBalance: tradeBalanceFunc
|
||||
}, function (err, balance) {
|
||||
if (err) {
|
||||
return callback && callback(err);
|
||||
|
|
@ -281,7 +313,7 @@ Trader.prototype.pollBalance = function (callback) {
|
|||
logger.debug('Balance update:', balance);
|
||||
self.balance = balance;
|
||||
|
||||
return callback && callback(null, balance);
|
||||
return callback && callback();
|
||||
});
|
||||
};
|
||||
|
||||
|
|
@ -289,7 +321,14 @@ Trader.prototype.pollRate = function (callback) {
|
|||
var self = this;
|
||||
|
||||
logger.debug('polling for rates...');
|
||||
self.tickerExchange.ticker(function(err, resRates) {
|
||||
var deviceCurrency = this.config.exchanges.settings.currency;
|
||||
var currencies = [deviceCurrency];
|
||||
if (this.tradeExchange) {
|
||||
var tradeCurrency = this.tradeExchange.currency();
|
||||
if (tradeCurrency !== deviceCurrency) currencies.push(tradeCurrency);
|
||||
}
|
||||
|
||||
self.tickerExchange.ticker(currencies, function(err, resRates) {
|
||||
if (err) {
|
||||
logger.error(err);
|
||||
return callback && callback(err);
|
||||
|
|
@ -297,13 +336,25 @@ Trader.prototype.pollRate = function (callback) {
|
|||
|
||||
logger.debug('got rates: %j', resRates);
|
||||
self.rateInfo = {rates: resRates, timestamp: new Date()};
|
||||
callback && callback();
|
||||
});
|
||||
};
|
||||
|
||||
// This is the rate in local currency quote to the user
|
||||
Trader.prototype._deviceRate = function _deviceRate() {
|
||||
if (!this.rateInfo) return null;
|
||||
return this.rateInfo.rates[this.config.exchanges.settings.currency].rate;
|
||||
};
|
||||
|
||||
Trader.prototype._tradeRate = function _tradeRate() {
|
||||
if (!this.tradeExchange || !this.rateInfo) return null;
|
||||
return this.rateInfo.rates[this.tradeExchange.currency()].rate;
|
||||
};
|
||||
|
||||
// This is the rate in local currency to quote to the user
|
||||
Trader.prototype.rate = function () {
|
||||
if (!this.rateInfo) return null;
|
||||
return {
|
||||
rate: this.rateInfo.rates[this.config.exchanges.settings.currency],
|
||||
rate: this._deviceRate(),
|
||||
timestamp: this.rateInfo.timestamp
|
||||
};
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue