fixed out of bitcoins; cleanup

This commit is contained in:
Josh Harvey 2014-04-26 23:13:58 -04:00
parent 96f480a73c
commit 30084617c3
3 changed files with 27 additions and 21 deletions

View file

@ -55,13 +55,6 @@ Trader.prototype._findWallet = function (name) {
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;
@ -176,20 +169,23 @@ Trader.prototype._clearSession = function (deviceFingerprint) {
Trader.prototype.sendBitcoins = function (deviceFingerprint, tx, cb) {
var self = this;
self.db.summonTransaction(deviceFingerprint, tx, function (err, isNew, txHash) {
self.db.summonTransaction(deviceFingerprint, tx, function (err, txRec) {
if (err) {
return cb(err);
}
if (isNew) {
this._clearSession(deviceFingerprint);
if (!txRec) {
self._clearSession(deviceFingerprint);
return self.transferExchange.sendBitcoins(
tx.toAddress,
tx.satoshis,
self.config.exchanges.settings.transactionFee,
function(err, txHash) {
if (err) {
self.db.reportTransactionError(tx, err);
var status = err.name === 'InsufficientFunds' ?
'insufficientFunds' :
'failed';
self.db.reportTransactionError(tx, err.message, status);
return cb(err);
}
@ -200,9 +196,17 @@ Trader.prototype.sendBitcoins = function (deviceFingerprint, tx, cb) {
);
}
// Out of bitcoins: special case
var txErr = null;
if (txRec.err) {
txErr = new Error(txRec.err);
if (txRec.status === 'insufficientFunds') txErr.name = 'InsufficientFunds';
}
// transaction exists, but txHash might be null,
// in which case ATM should continue polling
cb(null, txHash);
// in which case ATM should continue polling
self.pollBalance();
cb(txErr, txRec.txHash);
});
};
@ -251,8 +255,6 @@ Trader.prototype.executeTrades = function () {
};
Trader.prototype.startPolling = function () {
this.pollBalance();
this.pollRate();
this.executeTrades();
this.balanceInterval = setInterval(this.pollBalance.bind(this), 60 * 1000);