refactor(psql): fns made a little bit more atomic

This commit is contained in:
Damian Mee 2014-09-09 08:00:41 +02:00
parent d5ce8d8f1e
commit bc4bd456da
2 changed files with 45 additions and 30 deletions

View file

@ -242,33 +242,38 @@ exports.fiatBalance = function fiatBalance() {
return fiatTransferBalance;
};
function _sendBitcoins(tx, callback) {
logger.debug('executing tx: %j', tx);
walletPlugin.sendBitcoins(
tx.toAddress,
tx.satoshis,
cachedConfig.exchanges.settings.transactionFee,
function(err, txHash) {
if (err) {
var status = err.name === 'InsufficientFunds' ?
'insufficientFunds' :
'failed';
db.reportTransactionError(tx, err.message, status);
return callback(err);
}
db.completeTransaction(tx, txHash);
pollBalance();
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 walletPlugin.sendBitcoins(
tx.toAddress,
tx.satoshis,
cachedConfig.exchanges.settings.transactionFee,
function(err, txHash) {
if (err) {
var status = err.name === 'InsufficientFunds' ?
'insufficientFunds' :
'failed';
db.reportTransactionError(tx, err.message, status);
return callback(err);
}
db.completeTransaction(tx, txHash);
pollBalance();
callback(null, txHash);
}
);
return _sendBitcoins(tx, callback);
}
// Out of bitcoins: special case