Make tradeQueueBalance private

Move `consolidateTrades` to private section
This commit is contained in:
Maciej Małecki 2014-04-21 00:41:00 +02:00
parent 49f77c50b8
commit 7464b93d7b

View file

@ -51,6 +51,34 @@ Trader.prototype._findWallet = function (name) {
return exchange.wallet || exchange; return exchange.wallet || exchange;
}; };
Trader.prototype._tradeQueueFiatBalance = function (exchangeRate) {
var satoshis = this._tradeQueue.reduce(function (memo, rec) {
return memo + rec.satoshis;
}, 0);
return (satoshis / SATOSHI_FACTOR) * exchangeRate;
};
Trader.prototype._consolidateTrades = function () {
var queue = this._tradeQueue;
var tradeRec = {
fiat: 0,
satoshis: 0,
currency: this.config.exchanges.settings.currency
};
while (true) {
var lastRec = queue.shift();
if (!lastRec) {
break;
}
tradeRec.fiat += lastRec.fiat;
tradeRec.satoshis += lastRec.satoshis;
tradeRec.currency = lastRec.currency;
}
return tradeRec;
};
Trader.prototype.configure = function (config) { Trader.prototype.configure = function (config) {
if (config.exchanges.settings.lowBalanceMargin < 1) { if (config.exchanges.settings.lowBalanceMargin < 1) {
throw new Error('`settings.lowBalanceMargin` has to be >= 1'); throw new Error('`settings.lowBalanceMargin` has to be >= 1');
@ -123,7 +151,7 @@ Trader.prototype.fiatBalance = function (transferSatoshis, tradeFiat) {
// We need to secure `tradeFiat` (amount of fiat in this transaction) and // We need to secure `tradeFiat` (amount of fiat in this transaction) and
// enough fiat to cover our trading queue (trades aren't executed immediately). // enough fiat to cover our trading queue (trades aren't executed immediately).
var adjustedFiat = tradeFiat + this.tradeQueueFiatBalance(rate); var adjustedFiat = tradeFiat + this._tradeQueueFiatBalance(rate);
// So we subtract `adjustedFiat` from `tradeBalance` and again, apply // So we subtract `adjustedFiat` from `tradeBalance` and again, apply
// `lowBalanceMargin`. // `lowBalanceMargin`.
@ -170,34 +198,6 @@ Trader.prototype.trade = function (fiat, satoshis, currency, callback) {
callback(null); callback(null);
}; };
Trader.prototype.tradeQueueFiatBalance = function (exchangeRate) {
var satoshis = this._tradeQueue.reduce(function (memo, rec) {
return memo + rec.satoshis;
}, 0);
return (satoshis / SATOSHI_FACTOR) * exchangeRate;
};
Trader.prototype._consolidateTrades = function () {
var queue = this._tradeQueue;
var tradeRec = {
fiat: 0,
satoshis: 0,
currency: this.config.exchanges.settings.currency
};
while (true) {
var lastRec = queue.shift();
if (!lastRec) {
break;
}
tradeRec.fiat += lastRec.fiat;
tradeRec.satoshis += lastRec.satoshis;
tradeRec.currency = lastRec.currency;
}
return tradeRec;
};
Trader.prototype.startPolling = function () { Trader.prototype.startPolling = function () {
this.pollBalance(); this.pollBalance();
this.pollRate(); this.pollRate();