This commit is contained in:
Josh Harvey 2014-11-18 18:27:38 -05:00
parent 408e4b59d5
commit c38372a173
3 changed files with 19 additions and 29 deletions

View file

@ -1,10 +1,8 @@
'use strict'; 'use strict';
var async = require('async'); var async = require('async');
var logger = require('./logger'); var logger = require('./logger');
var SATOSHI_FACTOR = 1e8; var SATOSHI_FACTOR = 1e8;
var POLLING_RATE = 60 * 1000; // poll each minute var POLLING_RATE = 60 * 1000; // poll each minute
var REAP_RATE = 5 * 1000; var REAP_RATE = 5 * 1000;
@ -15,7 +13,6 @@ var MIN_CONFIDENCE = 0.7;
var db = null; var db = null;
var tickerPlugin = null; var tickerPlugin = null;
var traderPlugin = null; var traderPlugin = null;
var walletPlugin = null; var walletPlugin = null;
@ -242,14 +239,10 @@ function reapTxs() {
}); });
} }
// TODO: Run these in parallel and return success
exports.trade = function trade(deviceFingerprint, rawTrade, cb) { exports.trade = function trade(deviceFingerprint, rawTrade, cb) {
var tx = {
txId: rawTrade.txId,
toAddress: rawTrade.toAddress,
currencyCode: rawTrade.currency
};
db.addPendingTx(deviceFingerprint, tx);
// TODO: move this to DB, too
// add bill to trader queue (if trader is enabled) // add bill to trader queue (if trader is enabled)
if (traderPlugin) { if (traderPlugin) {
tradesQueue.push({ tradesQueue.push({
@ -258,8 +251,16 @@ exports.trade = function trade(deviceFingerprint, rawTrade, cb) {
}); });
} }
// record/log inserted bill var tx = {
db.recordBill(deviceFingerprint, rawTrade, cb); txId: rawTrade.txId,
toAddress: rawTrade.toAddress,
currencyCode: rawTrade.currency
};
async.parallel([
async.apply(db.addPendingTx, deviceFingerprint, tx),
async.apply(db.recordBill, deviceFingerprint, rawTrade)
], cb);
}; };
exports.sendBitcoins = function sendBitcoins(deviceFingerprint, rawTx, cb) { exports.sendBitcoins = function sendBitcoins(deviceFingerprint, rawTx, cb) {

View file

@ -75,25 +75,14 @@ exports.recordBill = function recordBill(deviceFingerprint, rec, cb) {
rec.fiat rec.fiat
]; ];
if (rec.partialTx) {
values.push(rec.partialTx.satoshis, rec.partialTx.fiat);
fields.push('total_satoshis', 'total_fiat');
}
// NOTE: if is here to maintain compatibility with older machines
if (rec.uuid) {
values.push(rec.uuid);
fields.push('uuid');
}
connect(function(err, client, done) { connect(function(err, client, done) {
if (err) return cb(err); if (err) return cb(err);
query(client, getInsertQuery('bills', fields), values, function(err) { query(client, getInsertQuery('bills', fields), values, function(err) {
done(); done();
if (err && PG_ERRORS[err.code] === 'uniqueViolation') // TODO: Handle unique violations more cleanly for idempotency
return cb(null, {code: 204}); // Now, it just returns an error, which should be fine, but a 204 status
// would be nicer.
cb(); // 201 => Accepted / saved cb(err);
}); });
}); });
}; };

View file

@ -81,9 +81,9 @@ function poll(req, res) {
} }
function trade(req, res) { function trade(req, res) {
plugins.trade(getFingerprint(req), req.body, function(err, data) { plugins.trade(getFingerprint(req), req.body, function(err) {
var statusCode = data && data.code !== null ? data.code : 201; var statusCode = err ? 500 : 201;
res.json(statusCode, {err: null}); res.json(statusCode, {err: err});
}); });
} }