Add everything back for trading

This commit is contained in:
Maciej Małecki 2014-04-25 16:34:24 +02:00
parent 75982f219d
commit 97deac7f20

View file

@ -79,6 +79,16 @@ Trader.prototype._consolidateTrades = function () {
return tradeRec;
};
Trader.prototype._purchase = function (trade) {
var self = this;
self.rate(trade.currency, function (err, rate) {
self.tradeExchange.purchase(trade.satoshis, rate, function (err) {
// XXX: don't ignore purchase errors
self.pollBalance();
});
});
};
Trader.prototype.configure = function (config) {
if (config.exchanges.settings.lowBalanceMargin < 1) {
throw new Error('`settings.lowBalanceMargin` has to be >= 1');
@ -201,12 +211,46 @@ Trader.prototype.trade = function (fiat, satoshis, currency, callback) {
callback(null);
};
Trader.prototype.executeTrades = function () {
if (!this.tradeExchange) return;
this.logger.info('checking for trades');
var trade = this._consolidateTrades();
this.logger.info('consolidated: ', JSON.stringify(trade));
if (trade.fiat === 0) {
this.logger.info('rejecting 0 trade');
return;
}
if (trade.fiat < this.config.exchanges.settings.minimumTradeFiat) {
// throw it back in the water
this.logger.info('reject fiat too small');
this._tradeQueue.unshift(trade);
return;
}
this.logger.info('making a trade: %d', trade.satoshis / Math.pow(10, 8));
this._purchase(trade);
};
Trader.prototype.startPolling = function () {
this.pollBalance();
this.pollRate();
this.executeTrades();
this.balanceInterval = setInterval(this.pollBalance.bind(this), 60 * 1000);
this.rateInterval = setInterval(this.pollRate.bind(this), 60 * 1000);
// Always start trading, even if we don't have a trade exchange configured,
// since configuration can always change in `Trader#configure`.
// `Trader#executeTrades` returns early if we don't have a trade exchange
// configured at the moment.
this.tradeInterval = setInterval(
this.executeTrades.bind(this),
this.config.exchanges.settings.tradeInterval
);
};
Trader.prototype.stopPolling = function () {