refactor(psql): fns made a little bit more atomic
This commit is contained in:
parent
d5ce8d8f1e
commit
bc4bd456da
2 changed files with 45 additions and 30 deletions
|
|
@ -242,14 +242,9 @@ exports.fiatBalance = function fiatBalance() {
|
|||
return fiatTransferBalance;
|
||||
};
|
||||
|
||||
exports.sendBitcoins = function sendBitcoins(deviceFingerprint, tx, callback) {
|
||||
db.summonTransaction(deviceFingerprint, tx, function(err, txInfo) {
|
||||
if (err) return callback(err);
|
||||
|
||||
if (!txInfo || txInfo.status === 'partial') {
|
||||
clearSession(deviceFingerprint);
|
||||
|
||||
return walletPlugin.sendBitcoins(
|
||||
function _sendBitcoins(tx, callback) {
|
||||
logger.debug('executing tx: %j', tx);
|
||||
walletPlugin.sendBitcoins(
|
||||
tx.toAddress,
|
||||
tx.satoshis,
|
||||
cachedConfig.exchanges.settings.transactionFee,
|
||||
|
|
@ -269,6 +264,16 @@ exports.sendBitcoins = function sendBitcoins(deviceFingerprint, tx, callback) {
|
|||
callback(null, txHash);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
exports.sendBitcoins = function sendBitcoins(deviceFingerprint, tx, callback) {
|
||||
db.summonTransaction(deviceFingerprint, tx, function(err, txInfo) {
|
||||
if (err) return callback(err);
|
||||
|
||||
if (!txInfo || txInfo.status === 'partial') {
|
||||
// TODO: make sure session exists, to prevent sending coins twice
|
||||
clearSession(deviceFingerprint);
|
||||
return _sendBitcoins(tx, callback);
|
||||
}
|
||||
|
||||
// Out of bitcoins: special case
|
||||
|
|
|
|||
|
|
@ -54,17 +54,27 @@ function updatePartialTransaction(values, cb) {
|
|||
values2,
|
||||
cb);
|
||||
}
|
||||
function fetchTransaction(txId, cb) {
|
||||
client.query('SELECT status, tx_hash, error FROM transactions WHERE id=$1',
|
||||
exports.getTransaction = function getTransaction(txId, cb) {
|
||||
client.query('SELECT * FROM transactions WHERE id=$1',
|
||||
[txId],
|
||||
function (err, results) {
|
||||
function(err, results) {
|
||||
if (err) return cb(err);
|
||||
|
||||
// This should never happen, since we already checked for existence
|
||||
if (results.rows.length === 0) return cb(new Error('Couldn\'t find transaction.'));
|
||||
cb(null, results.rows.length >= 0 && results.rows[0]);
|
||||
});
|
||||
};
|
||||
exports.fetchTransaction = function fetchTransaction(txId, cb) {
|
||||
exports.getTransaction(txId, function(err, tx) {
|
||||
if (err) return cb(err);
|
||||
|
||||
var result = results.rows[0];
|
||||
cb(null, {txHash: result.tx_hash, err: result.error, status: result.status});
|
||||
if (!tx)
|
||||
return cb(new Error('Couldn\'t find transaction.'));
|
||||
|
||||
cb(null, {
|
||||
txHash: tx.tx_hash,
|
||||
err: tx.error,
|
||||
status: tx.status
|
||||
});
|
||||
});
|
||||
}
|
||||
exports.summonTransaction = function summonTransaction(deviceFingerprint, tx, cb) {
|
||||
|
|
@ -95,7 +105,7 @@ exports.summonTransaction = function summonTransaction(deviceFingerprint, tx, cb
|
|||
if (status === 'partial')
|
||||
return updatePartialTransaction(values, cb);
|
||||
|
||||
return fetchTransaction(tx.txId, cb);
|
||||
return exports.fetchTransaction(tx.txId, cb);
|
||||
}
|
||||
|
||||
return cb(err);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue