WIP most partial tx functionality working
This commit is contained in:
parent
2fb8ed3a69
commit
8cf5f2a453
3 changed files with 61 additions and 21 deletions
|
|
@ -88,7 +88,7 @@ exports.recordBill = function recordBill(deviceFingerprint, rec, cb) {
|
|||
|
||||
connect(function(err, client, done) {
|
||||
if (err) return cb(err);
|
||||
client.query(client, getInsertQuery('bills', fields), values, function(err) {
|
||||
query(client, getInsertQuery('bills', fields), values, function(err) {
|
||||
done();
|
||||
if (err && PG_ERRORS[err.code] === 'uniqueViolation')
|
||||
return cb(null, {code: 204});
|
||||
|
|
@ -109,15 +109,18 @@ exports.recordDeviceEvent = function recordDeviceEvent(deviceFingerprint, event)
|
|||
};
|
||||
|
||||
function query(client, queryStr, values, cb) {
|
||||
console.dir([queryStr, values]);
|
||||
client.query(queryStr, values, cb);
|
||||
}
|
||||
|
||||
function silentQuery(client, queryStr, values, cb) {
|
||||
console.dir([queryStr, values]);
|
||||
client.query(queryStr, values, function(err) {
|
||||
cb(err);
|
||||
});
|
||||
}
|
||||
|
||||
// OPTIMIZE: No need to query bills if tx.fiat and tx.satoshis are set
|
||||
function billsAndTxs(client, txid, currencyCode, deviceFingerprint, cb) {
|
||||
var billsQuery = 'SELECT COALESCE(SUM(denomination), 0) as fiat, ' +
|
||||
'COALESCE(SUM(satoshis), 0) AS satoshis ' +
|
||||
|
|
@ -154,27 +157,40 @@ function computeSendAmount(tx, totals) {
|
|||
};
|
||||
if (result.fiat < 0 || result.satoshis < 0) {
|
||||
logger.warn({tx: tx, totals: totals, result: result},
|
||||
'computeSendAmount result < 0, this shouldn\'t happen');
|
||||
'computeSendAmount result < 0, this shouldn\'t happen. Maybe timeout arrived after machineSend.');
|
||||
result.fiat = 0;
|
||||
result.satoshis = 0;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
exports.pendingTxs = function pendingTxs(timeoutMS, cb) {
|
||||
connect(function(err, client, done) {
|
||||
var sql = 'SELECT * FROM transactions ' +
|
||||
'WHERE status=$1 AND ' +
|
||||
'EXTRACT(EPOCH FROM now() - created) > $2 ' +
|
||||
'ORDER BY created ASC';
|
||||
var timeoutS = timeoutMS / 1000;
|
||||
var values = ['pending', timeoutS];
|
||||
query(client, sql, values, function(err, results) {
|
||||
done();
|
||||
cb(err, results);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
function removePendingTx(client, tx, cb) {
|
||||
silentQuery(client, 'DELETE FROM TRANSACTIONS WHERE txid=$1 AND status=$2',
|
||||
[tx.txid, 'pending'], cb);
|
||||
silentQuery(client, 'DELETE FROM transactions WHERE txid=$1 AND status=$2',
|
||||
[tx.txId, 'pending'], cb);
|
||||
}
|
||||
|
||||
function maybeInsertTx(client, deviceFingerprint, tx, totals, cb) {
|
||||
var sendAmount = computeSendAmount(tx, totals);
|
||||
if (sendAmount.satoshis === 0) return cb();
|
||||
|
||||
var status = _.isNumber(tx.fiat) ? 'machineSend' : 'timeout';
|
||||
var satoshis = sendAmount.satoshis;
|
||||
var fiat = sendAmount.fiat;
|
||||
insertTx(client, deviceFingerprint, tx, satoshis, fiat, status, function(err, results) {
|
||||
// unique violation shouldn't happen, since then sendAmount would be 0
|
||||
// TODO: Don't worry about unique violation
|
||||
if (err) return cb(err);
|
||||
cb(null, {id: results.rows[0].id, satoshisToSend: sendAmount.satoshis});
|
||||
});
|
||||
|
|
@ -220,21 +236,21 @@ exports.addPendingTx = function addPendingTx(deviceFingerprint, tx) {
|
|||
});
|
||||
};
|
||||
|
||||
// Calling function should only send bitcoins if result !== null
|
||||
// Calling function should only send bitcoins if result.satoshisToSend > 0
|
||||
exports.addTx = function addTx(deviceFingerprint, tx, cb) {
|
||||
connect(function(err, client, done) {
|
||||
if (err) return cb(err);
|
||||
async.waterfall([
|
||||
async.apply(silentQuery, client, 'BEGIN'),
|
||||
async.apply(silentQuery, client, 'BEGIN', null),
|
||||
async.apply(removePendingTx, client, tx),
|
||||
async.apply(billsAndTxs, client, tx.currencyCode, deviceFingerprint),
|
||||
async.apply(billsAndTxs, client, tx.txId, tx.currencyCode, deviceFingerprint),
|
||||
async.apply(maybeInsertTx, client, deviceFingerprint, tx)
|
||||
], function(err, result) {
|
||||
if (err) {
|
||||
rollback(client, done);
|
||||
return cb(err);
|
||||
}
|
||||
silentQuery(client, 'COMMIT', function() {
|
||||
silentQuery(client, 'COMMIT', null, function() {
|
||||
done();
|
||||
cb(null, result);
|
||||
});
|
||||
|
|
@ -242,6 +258,11 @@ exports.addTx = function addTx(deviceFingerprint, tx, cb) {
|
|||
});
|
||||
};
|
||||
|
||||
exports.addDigitalTx = function addDigitalTx(dbTxId, err, txHash, fee) {
|
||||
console.log('DEBUG: adding digitaltx');
|
||||
console.dir([dbTxId, err, txHash, fee]);
|
||||
};
|
||||
|
||||
/*
|
||||
exports.decrementCartridges =
|
||||
function decrementCartridges(fingerprint, cartridge1, cartridge2, cb) {
|
||||
|
|
@ -259,3 +280,12 @@ exports.fillCartridges =
|
|||
client.query(query, [cartridge1, cartridge2, fingerprint], cb);
|
||||
};
|
||||
*/
|
||||
|
||||
/* DEBUG
|
||||
exports.init('psql://lamassu:lamassu@localhost/lamassu');
|
||||
|
||||
var fp = 'AB:9C:09:AA:7B:48:51:9A:0E:13:59:4E:5E:69:D0:74:E5:0F:4A:66';
|
||||
var txId = '5ef9c631-d948-4f0f-bf22-d2a563f5cd26';
|
||||
var tx = {txId: txId, currencyCode: 'USD', toAddress: '1xxx'};
|
||||
exports.addTx(fp, tx, function(err, result) { pg.end(); console.dir(result); });
|
||||
*/
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue