fix partial_send reporting for timeout
This commit is contained in:
parent
ad65869229
commit
cb11552c11
2 changed files with 35 additions and 31 deletions
|
|
@ -194,17 +194,18 @@ function _sendBitcoins(toAddress, satoshis, cb) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function executeTx(session, tx, authority, cb) {
|
function executeTx(session, tx, authority, cb) {
|
||||||
db.addOutgoingTx(session, tx, function(err, satoshisToSend) {
|
db.addOutgoingTx(session, tx, function(err, toSend) {
|
||||||
if (err) return cb(err);
|
if (err) return cb(err);
|
||||||
|
var satoshisToSend = toSend.satoshis;
|
||||||
if (satoshisToSend === 0)
|
if (satoshisToSend === 0)
|
||||||
return cb(null, {statusCode: 204, txId: tx.txId, txHash: null});
|
return cb(null, {statusCode: 204, txId: tx.txId, txHash: null});
|
||||||
|
|
||||||
_sendBitcoins(tx.toAddress, satoshisToSend, function(_err, txHash) {
|
_sendBitcoins(tx.toAddress, satoshisToSend, function(_err, txHash) {
|
||||||
var fee = null; // Need to fill this out in plugins
|
var fee = null; // Need to fill this out in plugins
|
||||||
db.sentCoins(session, tx, authority, satoshisToSend, fee, _err, txHash);
|
if (_err) toSend = {satoshis: 0, fiat: 0};
|
||||||
|
db.sentCoins(session, tx, authority, toSend, fee, _err, txHash);
|
||||||
|
|
||||||
if (_err) return cb(err);
|
if (_err) return cb(_err);
|
||||||
|
|
||||||
pollBalance();
|
pollBalance();
|
||||||
cb(null, {
|
cb(null, {
|
||||||
|
|
@ -238,6 +239,7 @@ function reapIncomingTx(session, tx) {
|
||||||
function reapTx(row) {
|
function reapTx(row) {
|
||||||
var session = {fingerprint: row.device_fingerprint, id: row.session_id};
|
var session = {fingerprint: row.device_fingerprint, id: row.session_id};
|
||||||
var tx = {
|
var tx = {
|
||||||
|
fiat: 0,
|
||||||
satoshis: row.satoshis,
|
satoshis: row.satoshis,
|
||||||
toAddress: row.to_address,
|
toAddress: row.to_address,
|
||||||
currencyCode: row.currency_code,
|
currencyCode: row.currency_code,
|
||||||
|
|
|
||||||
|
|
@ -95,8 +95,8 @@ exports.recordBill = function recordBill(session, rec, cb) {
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.recordDeviceEvent = function recordDeviceEvent(session, event) {
|
exports.recordDeviceEvent = function recordDeviceEvent(session, event) {
|
||||||
connect(function(err, client, done) {
|
connect(function(cerr, client, done) {
|
||||||
if (err) return;
|
if (cerr) return;
|
||||||
var sql = 'INSERT INTO device_events (device_fingerprint, event_type, ' +
|
var sql = 'INSERT INTO device_events (device_fingerprint, event_type, ' +
|
||||||
'note, device_time) VALUES ($1, $2, $3, $4)';
|
'note, device_time) VALUES ($1, $2, $3, $4)';
|
||||||
var values = [session.fingerprint, event.eventType, event.note,
|
var values = [session.fingerprint, event.eventType, event.note,
|
||||||
|
|
@ -185,7 +185,8 @@ function computeSendAmount(tx, totals) {
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.removeOldPending = function removeOldPending(timeoutMS) {
|
exports.removeOldPending = function removeOldPending(timeoutMS) {
|
||||||
connect(function(err, client, done) {
|
connect(function(cerr, client, done) {
|
||||||
|
if (cerr) return;
|
||||||
var sql = 'DELETE FROM pending_transactions ' +
|
var sql = 'DELETE FROM pending_transactions ' +
|
||||||
'WHERE incoming AND extract(EPOCH FROM now() - created) > $1';
|
'WHERE incoming AND extract(EPOCH FROM now() - created) > $1';
|
||||||
var timeoutS = timeoutMS / 1000;
|
var timeoutS = timeoutMS / 1000;
|
||||||
|
|
@ -198,7 +199,8 @@ exports.removeOldPending = function removeOldPending(timeoutMS) {
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.pendingTxs = function pendingTxs(timeoutMS, cb) {
|
exports.pendingTxs = function pendingTxs(timeoutMS, cb) {
|
||||||
connect(function(err, client, done) {
|
connect(function(cerr, client, done) {
|
||||||
|
if (cerr) return cb(cerr);
|
||||||
var sql = 'SELECT * ' +
|
var sql = 'SELECT * ' +
|
||||||
'FROM pending_transactions ' +
|
'FROM pending_transactions ' +
|
||||||
'WHERE (incoming OR extract(EPOCH FROM now() - created) > $1) ' +
|
'WHERE (incoming OR extract(EPOCH FROM now() - created) > $1) ' +
|
||||||
|
|
@ -228,7 +230,7 @@ function insertOutgoingTx(client, session, tx, totals, cb) {
|
||||||
function(err) {
|
function(err) {
|
||||||
|
|
||||||
if (err) return cb(err);
|
if (err) return cb(err);
|
||||||
cb(null, satoshis);
|
cb(null, {satoshis: satoshis, fiat: fiat});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -317,8 +319,8 @@ function buildOutgoingTx(client, session, tx, cb) {
|
||||||
|
|
||||||
// Calling function should only send bitcoins if result.satoshisToSend > 0
|
// Calling function should only send bitcoins if result.satoshisToSend > 0
|
||||||
exports.addOutgoingTx = function addOutgoingTx(session, tx, cb) {
|
exports.addOutgoingTx = function addOutgoingTx(session, tx, cb) {
|
||||||
connect(function(err, client, done) {
|
connect(function(cerr, client, done) {
|
||||||
if (err) return cb(err);
|
if (cerr) return cb(cerr);
|
||||||
async.series([
|
async.series([
|
||||||
async.apply(silentQuery, client, 'BEGIN'),
|
async.apply(silentQuery, client, 'BEGIN'),
|
||||||
async.apply(insertOutgoingCompleteTx, client, session, tx),
|
async.apply(insertOutgoingCompleteTx, client, session, tx),
|
||||||
|
|
@ -331,25 +333,25 @@ exports.addOutgoingTx = function addOutgoingTx(session, tx, cb) {
|
||||||
}
|
}
|
||||||
silentQuery(client, 'COMMIT', function() {
|
silentQuery(client, 'COMMIT', function() {
|
||||||
done();
|
done();
|
||||||
var satoshisToSend = results[3];
|
var toSend = results[3];
|
||||||
cb(null, satoshisToSend);
|
cb(null, toSend);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.sentCoins = function sentCoins(session, tx, authority, satoshis, fee,
|
exports.sentCoins = function sentCoins(session, tx, authority, toSend, fee,
|
||||||
error, txHash) {
|
error, txHash) {
|
||||||
connect(function(err, client, done) {
|
connect(function(cerr, client, done) {
|
||||||
if (err) return logger.error(err);
|
if (cerr) return logger.error(cerr);
|
||||||
|
|
||||||
var newTx = _.clone(tx);
|
var newTx = _.clone(tx);
|
||||||
newTx.txHash = txHash;
|
newTx.txHash = txHash;
|
||||||
newTx.error = error;
|
newTx.error = error;
|
||||||
insertOutgoing(client, session, newTx, satoshis, newTx.fiat, 'partial_send',
|
insertOutgoing(client, session, newTx, toSend.satoshis, toSend.fiat,
|
||||||
authority, function(_err) {
|
'partial_send', authority, function(err) {
|
||||||
done();
|
done();
|
||||||
if (err) logger.error(_err);
|
if (err) logger.error(err);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
@ -362,8 +364,8 @@ function maybeRemovePending(client, session, authority, cb) {
|
||||||
exports.addIncomingTx = function addIncomingTx(session, tx, authority,
|
exports.addIncomingTx = function addIncomingTx(session, tx, authority,
|
||||||
satoshisReceived, cb) {
|
satoshisReceived, cb) {
|
||||||
|
|
||||||
connect(function(err, client, done) {
|
connect(function(cerr, client, done) {
|
||||||
if (err) return cb(err);
|
if (cerr) return cb(cerr);
|
||||||
async.waterfall([
|
async.waterfall([
|
||||||
async.apply(silentQuery, client, 'BEGIN', null),
|
async.apply(silentQuery, client, 'BEGIN', null),
|
||||||
async.apply(maybeRemovePending, client, session, authority),
|
async.apply(maybeRemovePending, client, session, authority),
|
||||||
|
|
@ -396,8 +398,8 @@ exports.addOutgoingPending = function addOutgoingPending(session, currencyCode,
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.addInitialIncoming = function addInitialIncoming(session, tx, cb) {
|
exports.addInitialIncoming = function addInitialIncoming(session, tx, cb) {
|
||||||
connect(function(err, client, done) {
|
connect(function(cerr, client, done) {
|
||||||
if (err) return cb(err);
|
if (cerr) return cb(cerr);
|
||||||
async.waterfall([
|
async.waterfall([
|
||||||
async.apply(silentQuery, client, 'BEGIN', null),
|
async.apply(silentQuery, client, 'BEGIN', null),
|
||||||
async.apply(addPendingTx, client, session, true, tx.currencyCode,
|
async.apply(addPendingTx, client, session, true, tx.currencyCode,
|
||||||
|
|
@ -436,15 +438,15 @@ function initialRequest(client, session, cb) {
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.dispenseStatus = function dispenseStatus(session, cb) {
|
exports.dispenseStatus = function dispenseStatus(session, cb) {
|
||||||
connect(function(err, client, done) {
|
connect(function(cerr, client, done) {
|
||||||
if (err) return cb(err);
|
if (cerr) return cb(cerr);
|
||||||
|
|
||||||
async.parallel([
|
async.parallel([
|
||||||
async.apply(initialRequest, client, session),
|
async.apply(initialRequest, client, session),
|
||||||
async.apply(lastTxStatus, client, session)
|
async.apply(lastTxStatus, client, session)
|
||||||
], function(_err, results) {
|
], function(err, results) {
|
||||||
done();
|
done();
|
||||||
if (_err) return cb(_err);
|
if (err) return cb(err);
|
||||||
|
|
||||||
var pending = (results[0].rows.length === 1) &&
|
var pending = (results[0].rows.length === 1) &&
|
||||||
(results[1].rows.length === 1) &&
|
(results[1].rows.length === 1) &&
|
||||||
|
|
@ -500,17 +502,17 @@ function insertDispense(client, session, tx, transactionId, counts, cb) {
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.addDispense = function addDispense(session, tx) {
|
exports.addDispense = function addDispense(session, tx) {
|
||||||
connect(function(err, client, done) {
|
connect(function(cerr, client, done) {
|
||||||
if (err) return logger.error(err);
|
if (cerr) return logger.error(cerr);
|
||||||
|
|
||||||
async.waterfall([
|
async.waterfall([
|
||||||
async.apply(insertIncoming, client, session, tx, 0, tx.fiat,
|
async.apply(insertIncoming, client, session, tx, 0, tx.fiat,
|
||||||
'dispense', 'authorized'),
|
'dispense', 'authorized'),
|
||||||
async.apply(lastDispenseCount, client, session),
|
async.apply(lastDispenseCount, client, session),
|
||||||
async.apply(insertDispense, client, session, tx)
|
async.apply(insertDispense, client, session, tx)
|
||||||
], function(_err) {
|
], function(err) {
|
||||||
done();
|
done();
|
||||||
if (_err) logger.error(_err);
|
if (err) logger.error(err);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue