feat(txs): WIP rebuild of offline txs, next part
This commit is contained in:
parent
67c5caeeff
commit
bb4336b78f
2 changed files with 148 additions and 82 deletions
|
|
@ -182,8 +182,11 @@ exports.trade = function trade(rawTrade, deviceFingerprint) {
|
|||
sessions[deviceFingerprint] = {
|
||||
timestamp: Date.now(),
|
||||
reaper: setTimeout(function() {
|
||||
db.getTransaction(rawTrade.txId, function(err, tx) {
|
||||
_sendBitcoins(tx, function() { });
|
||||
// NOTE: at this point we either have bills ONLY *or a partial tx*
|
||||
// TODO: deal with #1 from ^
|
||||
db.getPendingTransactions(rawTrade.txId, function(err, txs) {
|
||||
// NOTE: returns ARRAY of txs
|
||||
_sendBitcoins(txs[0], function() { });
|
||||
delete sessions[deviceFingerprint];
|
||||
});
|
||||
}, SESSION_TIMEOUT)
|
||||
|
|
@ -193,21 +196,6 @@ exports.trade = function trade(rawTrade, deviceFingerprint) {
|
|||
// record (vel log) inserted bill
|
||||
db.recordBill(deviceFingerprint, rawTrade);
|
||||
|
||||
// cache partial Transaction (if supported by machine)
|
||||
if (rawTrade.partialTx) {
|
||||
var tx = {
|
||||
txId: rawTrade.txId,
|
||||
status: 'partial',
|
||||
toAddress: rawTrade.toAddress,
|
||||
currencyCode: rawTrade.currency,
|
||||
satoshis: rawTrade.partialTx.satoshis,
|
||||
fiat: rawTrade.partialTx.fiat
|
||||
};
|
||||
db.summonTransaction(deviceFingerprint, tx, function() {
|
||||
logger.debug('partial tx: %j', tx);
|
||||
});
|
||||
}
|
||||
|
||||
// add bill to trader queue (if trader is enabled)
|
||||
if (traderPlugin) {
|
||||
tradesQueue.push({
|
||||
|
|
@ -258,11 +246,14 @@ function _sendBitcoins(tx, callback) {
|
|||
'insufficientFunds' :
|
||||
'failed';
|
||||
|
||||
db.reportTransactionError(tx, err.message, status);
|
||||
// report insufficient funds error
|
||||
db.changeTxStatus(tx.txId, status, {error: err.message});
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
db.completeTransaction(tx, txHash);
|
||||
if (txHash) db.changeTxStatus(tx.txId, 'completed', {hash: txHash});
|
||||
else db.changeTxStatus(tx.txId, 'failed', {error: 'No txHash received'});
|
||||
|
||||
pollBalance();
|
||||
callback(null, txHash);
|
||||
}
|
||||
|
|
@ -295,6 +286,31 @@ exports.sendBitcoins = function sendBitcoins(deviceFingerprint, tx, callback) {
|
|||
});
|
||||
};
|
||||
|
||||
// NOTE: temporarily here
|
||||
exports.sendBitcoinians = function(deviceFingerprint, tx, callback) {
|
||||
db.summonTransaction(deviceFingerprint, tx, function(err, txInfo) {
|
||||
if (err) return callback(err);
|
||||
|
||||
if (txInfo) {
|
||||
if (txInfo.status === 'insufficientFunds') {
|
||||
|
||||
}
|
||||
|
||||
if (txInfo.status === 'executing') {
|
||||
|
||||
}
|
||||
|
||||
// TODO: check `part` and what has already been sent
|
||||
}
|
||||
|
||||
// no error & no tx record exists
|
||||
// TODO: should it be confirmed with bills?
|
||||
|
||||
|
||||
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
/*
|
||||
* Polling livecycle
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue