WIP
This commit is contained in:
parent
0cec3670a9
commit
e12238c4fe
4 changed files with 163 additions and 73 deletions
|
|
@ -35,7 +35,6 @@ function getInsertQuery(tableName, fields, hasId) {
|
|||
return query;
|
||||
}
|
||||
|
||||
|
||||
exports.init = function init(_conString) {
|
||||
conString = _conString;
|
||||
if (!conString) {
|
||||
|
|
@ -86,13 +85,15 @@ exports.recordBill = function recordBill(deviceFingerprint, rec, cb) {
|
|||
});
|
||||
};
|
||||
|
||||
exports.recordDeviceEvent = function recordDeviceEvent(deviceFingerprint, event) {
|
||||
exports.recordDeviceEvent = function recordDeviceEvent(deviceFingerprint,
|
||||
event) {
|
||||
connect(function(err, client, done) {
|
||||
if (err) return;
|
||||
client.query('INSERT INTO device_events (device_fingerprint, event_type, note, device_time)' +
|
||||
'VALUES ($1, $2, $3, $4)',
|
||||
[deviceFingerprint, event.eventType, event.note, event.deviceTime],
|
||||
done);
|
||||
var sql = 'INSERT INTO device_events (device_fingerprint, event_type, ' +
|
||||
'note, device_time) VALUES ($1, $2, $3, $4)';
|
||||
var values = [deviceFingerprint, event.eventType, event.note,
|
||||
event.deviceTime];
|
||||
client.query(sql, values, done);
|
||||
});
|
||||
};
|
||||
|
||||
|
|
@ -144,7 +145,8 @@ 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. Maybe timeout arrived after machineSend.');
|
||||
'computeSendAmount result < 0, this shouldn\'t happen. ' +
|
||||
'Maybe timeout arrived after machineSend.');
|
||||
result.fiat = 0;
|
||||
result.satoshis = 0;
|
||||
}
|
||||
|
|
@ -165,9 +167,9 @@ exports.pendingTxs = function pendingTxs(timeoutMS, cb) {
|
|||
});
|
||||
};
|
||||
|
||||
function removePendingTx(client, tx, cb) {
|
||||
function removePendingTx(client, sessionId, cb) {
|
||||
var sql = 'DELETE FROM pending_transactions WHERE session_id=$1';
|
||||
silentQuery(client, sql, [tx.txId], cb);
|
||||
silentQuery(client, sql, [sessionId], cb);
|
||||
}
|
||||
|
||||
function insertOutgoingTx(client, deviceFingerprint, tx, totals, cb) {
|
||||
|
|
@ -225,13 +227,14 @@ function insertTx(client, deviceFingerprint, tx, satoshis, fiat, stage,
|
|||
query(client, getInsertQuery('transactions', fields, true), values, cb);
|
||||
}
|
||||
|
||||
exports.addPendingTx = function addPendingTx(deviceFingerprint, sessionId,
|
||||
incoming, cb) {
|
||||
function addPendingTx(deviceFingerprint, sessionId,
|
||||
incoming, currencyCode, toAddress, cb) {
|
||||
connect(function(err, client, done) {
|
||||
if (err) return cb(err);
|
||||
var fields = ['session_id', 'incoming'];
|
||||
var fields = ['session_id', 'incoming', 'currency_code', 'to_address'];
|
||||
var sql = getInsertQuery('pending_transactions', fields);
|
||||
query(client, sql, [sessionId, incoming], function(_err) {
|
||||
var values = [sessionId, incoming, currencyCode, toAddress];
|
||||
query(client, sql, values, function(_err) {
|
||||
done();
|
||||
|
||||
// If pending tx already exists, do nothing
|
||||
|
|
@ -241,7 +244,7 @@ exports.addPendingTx = function addPendingTx(deviceFingerprint, sessionId,
|
|||
cb(_err);
|
||||
});
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
// Calling function should only send bitcoins if result.satoshisToSend > 0
|
||||
exports.addOutgoingTx = function addOutgoingTx(deviceFingerprint, tx, cb) {
|
||||
|
|
@ -249,9 +252,10 @@ exports.addOutgoingTx = function addOutgoingTx(deviceFingerprint, tx, cb) {
|
|||
if (err) return cb(err);
|
||||
async.waterfall([
|
||||
async.apply(silentQuery, client, 'BEGIN', null),
|
||||
async.apply(insertOutgoingCompleteTx, client, deviceFingerprint, tx)
|
||||
async.apply(removePendingTx, client, tx),
|
||||
async.apply(billsAndTxs, client, tx.txId, tx.currencyCode, deviceFingerprint),
|
||||
async.apply(insertOutgoingCompleteTx, client, deviceFingerprint, tx),
|
||||
async.apply(removePendingTx, client, tx.sessionId),
|
||||
async.apply(billsAndTxs, client, tx.txId, tx.currencyCode,
|
||||
deviceFingerprint),
|
||||
async.apply(insertOutgoingTx, client, deviceFingerprint, tx),
|
||||
], function(err, satoshisToSend) {
|
||||
if (err) {
|
||||
|
|
@ -266,32 +270,56 @@ exports.addOutgoingTx = function addOutgoingTx(deviceFingerprint, tx, cb) {
|
|||
});
|
||||
};
|
||||
|
||||
function removeIncomingPendingTx(client, tx, status, cb) {
|
||||
if (status !== 'published') return removePendingTx(client, tx, cb);
|
||||
cb();
|
||||
}
|
||||
|
||||
exports.addIncomingTx = function addIncomingTx(deviceFingerprint, tx, status,
|
||||
exports.addIncomingTx = function addIncomingTx(deviceFingerprint, tx, source,
|
||||
satoshisReceived, cb) {
|
||||
connect(function(err, client, done) {
|
||||
function maybeRemovePending(client, sessionId, source, cb) {
|
||||
if (source === 'published') return cb();
|
||||
removePendingTx(client, sessionId, cb);
|
||||
}
|
||||
|
||||
if (err) return cb(err);
|
||||
async.waterfall([
|
||||
async.apply(silentQuery, client, 'BEGIN', null),
|
||||
async.apply(removeOutgoingPendingTx, client, tx, status),
|
||||
async.apply(insertTx, client, tx, satoshisReceived, 0, 'deposit')
|
||||
], function(err, result) {
|
||||
async.apply(maybeRemovePending, client, tx.sessionId, source),
|
||||
async.apply(insertTx, client, tx, satoshisReceived, 0, 'deposit', source)
|
||||
], function(err) {
|
||||
if (err) {
|
||||
rollback(client, done);
|
||||
return cb(err);
|
||||
}
|
||||
silentQuery(client, 'COMMIT', null, function() {
|
||||
done();
|
||||
cb(null, result);
|
||||
cb();
|
||||
});
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
exports.addInitialIncoming = function addInitialIncoming(deviceFingerprint, tx,
|
||||
address, cb) {
|
||||
connect(function(err, client, done) {
|
||||
if (err) return cb(err);
|
||||
async.waterfall([
|
||||
async.apply(silentQuery, client, 'BEGIN', null),
|
||||
async.apply(addPendingTx, client, deviceFingerprint, tx.sessionId,
|
||||
tx.incoming, tx.currencyCode, tx.toAddress),
|
||||
async.apply(insertTx, client, tx, tx.satoshis, tx.fiat,
|
||||
'initial_request', 'pending')
|
||||
], function(err) {
|
||||
if (err) {
|
||||
rollback(client, done);
|
||||
return cb(err);
|
||||
}
|
||||
silentQuery(client, 'COMMIT', null, function() {
|
||||
done();
|
||||
cb();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
};
|
||||
|
||||
/*
|
||||
exports.decrementCartridges =
|
||||
function decrementCartridges(fingerprint, cartridge1, cartridge2, cb) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue