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;
|
return fiatTransferBalance;
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.sendBitcoins = function sendBitcoins(deviceFingerprint, tx, callback) {
|
function _sendBitcoins(tx, callback) {
|
||||||
db.summonTransaction(deviceFingerprint, tx, function(err, txInfo) {
|
logger.debug('executing tx: %j', tx);
|
||||||
if (err) return callback(err);
|
walletPlugin.sendBitcoins(
|
||||||
|
|
||||||
if (!txInfo || txInfo.status === 'partial') {
|
|
||||||
clearSession(deviceFingerprint);
|
|
||||||
|
|
||||||
return walletPlugin.sendBitcoins(
|
|
||||||
tx.toAddress,
|
tx.toAddress,
|
||||||
tx.satoshis,
|
tx.satoshis,
|
||||||
cachedConfig.exchanges.settings.transactionFee,
|
cachedConfig.exchanges.settings.transactionFee,
|
||||||
|
|
@ -269,6 +264,16 @@ exports.sendBitcoins = function sendBitcoins(deviceFingerprint, tx, callback) {
|
||||||
callback(null, txHash);
|
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
|
// Out of bitcoins: special case
|
||||||
|
|
|
||||||
|
|
@ -54,17 +54,27 @@ function updatePartialTransaction(values, cb) {
|
||||||
values2,
|
values2,
|
||||||
cb);
|
cb);
|
||||||
}
|
}
|
||||||
function fetchTransaction(txId, cb) {
|
exports.getTransaction = function getTransaction(txId, cb) {
|
||||||
client.query('SELECT status, tx_hash, error FROM transactions WHERE id=$1',
|
client.query('SELECT * FROM transactions WHERE id=$1',
|
||||||
[txId],
|
[txId],
|
||||||
function (err, results) {
|
function(err, results) {
|
||||||
if (err) return cb(err);
|
if (err) return cb(err);
|
||||||
|
|
||||||
// This should never happen, since we already checked for existence
|
cb(null, results.rows.length >= 0 && results.rows[0]);
|
||||||
if (results.rows.length === 0) return cb(new Error('Couldn\'t find transaction.'));
|
});
|
||||||
|
};
|
||||||
|
exports.fetchTransaction = function fetchTransaction(txId, cb) {
|
||||||
|
exports.getTransaction(txId, function(err, tx) {
|
||||||
|
if (err) return cb(err);
|
||||||
|
|
||||||
var result = results.rows[0];
|
if (!tx)
|
||||||
cb(null, {txHash: result.tx_hash, err: result.error, status: result.status});
|
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) {
|
exports.summonTransaction = function summonTransaction(deviceFingerprint, tx, cb) {
|
||||||
|
|
@ -95,7 +105,7 @@ exports.summonTransaction = function summonTransaction(deviceFingerprint, tx, cb
|
||||||
if (status === 'partial')
|
if (status === 'partial')
|
||||||
return updatePartialTransaction(values, cb);
|
return updatePartialTransaction(values, cb);
|
||||||
|
|
||||||
return fetchTransaction(tx.txId, cb);
|
return exports.fetchTransaction(tx.txId, cb);
|
||||||
}
|
}
|
||||||
|
|
||||||
return cb(err);
|
return cb(err);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue