WIP tweaks
This commit is contained in:
parent
5c82d2ad95
commit
9546b71a6e
3 changed files with 22 additions and 13 deletions
|
|
@ -12,7 +12,7 @@ var REAP_RATE = 2 * 1000;
|
||||||
var PENDING_TIMEOUT = 70 * 1000;
|
var PENDING_TIMEOUT = 70 * 1000;
|
||||||
|
|
||||||
// TODO: might have to update this if user is allowed to extend monitoring time
|
// TODO: might have to update this if user is allowed to extend monitoring time
|
||||||
var DEPOSIT_TIMEOUT = 120 * 1000;
|
var DEPOSIT_TIMEOUT = 130 * 1000;
|
||||||
|
|
||||||
|
|
||||||
var db = null;
|
var db = null;
|
||||||
|
|
@ -325,8 +325,7 @@ exports.trade = function trade(session, rawTrade, cb) {
|
||||||
};
|
};
|
||||||
|
|
||||||
async.parallel([
|
async.parallel([
|
||||||
async.apply(db.addOutgoingPending, session, tx.currencyCode, tx.toAddress,
|
async.apply(db.addOutgoingPending, session, tx.currencyCode, tx.toAddress),
|
||||||
tx.satoshis),
|
|
||||||
async.apply(db.recordBill, session, rawTrade)
|
async.apply(db.recordBill, session, rawTrade)
|
||||||
], cb);
|
], cb);
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -5,14 +5,13 @@
|
||||||
|
|
||||||
var pg = require('pg');
|
var pg = require('pg');
|
||||||
var async = require('async');
|
var async = require('async');
|
||||||
var util = require('util');
|
|
||||||
var _ = require('lodash');
|
var _ = require('lodash');
|
||||||
|
|
||||||
var logger = require('./logger');
|
var logger = require('./logger');
|
||||||
|
|
||||||
/*
|
/*
|
||||||
function inspect(rec) {
|
function inspect(rec) {
|
||||||
console.log(util.inspect(rec, {depth: null, colors: true}));
|
console.log(require('util').inspect(rec, {depth: null, colors: true}));
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
@ -190,7 +189,7 @@ exports.removeOldPending = function removeOldPending(timeoutMS) {
|
||||||
connect(function(cerr, client, done) {
|
connect(function(cerr, client, done) {
|
||||||
if (cerr) return;
|
if (cerr) return;
|
||||||
var sql = 'DELETE FROM pending_transactions ' +
|
var sql = 'DELETE FROM pending_transactions ' +
|
||||||
'WHERE incoming AND extract(EPOCH FROM now() - created) > $1';
|
'WHERE incoming AND extract(EPOCH FROM now() - updated) > $1';
|
||||||
var timeoutS = timeoutMS / 1000;
|
var timeoutS = timeoutMS / 1000;
|
||||||
var values = [timeoutS];
|
var values = [timeoutS];
|
||||||
query(client, sql, values, function(err) {
|
query(client, sql, values, function(err) {
|
||||||
|
|
@ -205,8 +204,8 @@ exports.pendingTxs = function pendingTxs(timeoutMS, cb) {
|
||||||
if (cerr) return cb(cerr);
|
if (cerr) return cb(cerr);
|
||||||
var sql = 'SELECT * ' +
|
var sql = 'SELECT * ' +
|
||||||
'FROM pending_transactions ' +
|
'FROM pending_transactions ' +
|
||||||
'WHERE (incoming OR extract(EPOCH FROM now() - created) > $1) ' +
|
'WHERE (incoming OR extract(EPOCH FROM now() - updated) > $1) ' +
|
||||||
'ORDER BY created ASC';
|
'ORDER BY updated ASC';
|
||||||
var timeoutS = timeoutMS / 1000;
|
var timeoutS = timeoutMS / 1000;
|
||||||
var values = [timeoutS];
|
var values = [timeoutS];
|
||||||
query(client, sql, values, function(err, results) {
|
query(client, sql, values, function(err, results) {
|
||||||
|
|
@ -228,6 +227,8 @@ function insertOutgoingTx(client, session, tx, totals, cb) {
|
||||||
var authority = tx.fiat ? 'machine' : 'timeout';
|
var authority = tx.fiat ? 'machine' : 'timeout';
|
||||||
var satoshis = sendAmount.satoshis;
|
var satoshis = sendAmount.satoshis;
|
||||||
var fiat = sendAmount.fiat;
|
var fiat = sendAmount.fiat;
|
||||||
|
if (satoshis === 0) return cb(null, {satoshis: 0, fiat: 0});
|
||||||
|
|
||||||
insertOutgoing(client, session, tx, satoshis, fiat, stage, authority,
|
insertOutgoing(client, session, tx, satoshis, fiat, stage, authority,
|
||||||
function(err) {
|
function(err) {
|
||||||
|
|
||||||
|
|
@ -296,6 +297,12 @@ function insertTx(client, session, incoming, tx, satoshis, fiat, stage,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function refreshPendingTx(client, session, cb) {
|
||||||
|
var sql = 'UPDATE pending_transactions SET updated=now() ' +
|
||||||
|
'WHERE device_fingerprint=$1 AND session_id=$2';
|
||||||
|
query(client, sql, [session.fingerprint, session.id], cb);
|
||||||
|
}
|
||||||
|
|
||||||
function addPendingTx(client, session, incoming, currencyCode, toAddress,
|
function addPendingTx(client, session, incoming, currencyCode, toAddress,
|
||||||
satoshis, cb) {
|
satoshis, cb) {
|
||||||
var fields = ['device_fingerprint', 'session_id', 'incoming',
|
var fields = ['device_fingerprint', 'session_id', 'incoming',
|
||||||
|
|
@ -306,6 +313,9 @@ function addPendingTx(client, session, incoming, currencyCode, toAddress,
|
||||||
query(client, sql, values, function(err) {
|
query(client, sql, values, function(err) {
|
||||||
|
|
||||||
// If pending tx already exists, do nothing
|
// If pending tx already exists, do nothing
|
||||||
|
if (err && isUniqueViolation(err))
|
||||||
|
return refreshPendingTx(client, session, cb);
|
||||||
|
|
||||||
if (err && !isUniqueViolation(err)) return cb(err);
|
if (err && !isUniqueViolation(err)) return cb(err);
|
||||||
|
|
||||||
cb();
|
cb();
|
||||||
|
|
@ -373,7 +383,7 @@ exports.addIncomingTx = function addIncomingTx(session, tx, authority,
|
||||||
if (cerr) return cb(cerr);
|
if (cerr) return cb(cerr);
|
||||||
var satoshisRequested = tx.satoshis;
|
var satoshisRequested = tx.satoshis;
|
||||||
var insufficientFunds = satoshisReceived < satoshisRequested;
|
var insufficientFunds = satoshisReceived < satoshisRequested;
|
||||||
async.waterfall([
|
async.series([
|
||||||
async.apply(silentQuery, client, 'BEGIN', null),
|
async.apply(silentQuery, client, 'BEGIN', null),
|
||||||
async.apply(maybeRemovePending, client, session, insufficientFunds,
|
async.apply(maybeRemovePending, client, session, insufficientFunds,
|
||||||
authority),
|
authority),
|
||||||
|
|
@ -393,11 +403,11 @@ exports.addIncomingTx = function addIncomingTx(session, tx, authority,
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.addOutgoingPending = function addOutgoingPending(session, currencyCode,
|
exports.addOutgoingPending = function addOutgoingPending(session, currencyCode,
|
||||||
toAddress, satoshis, cb) {
|
toAddress, cb) {
|
||||||
connect(function(cerr, client, done) {
|
connect(function(cerr, client, done) {
|
||||||
if (cerr) return cb(cerr);
|
if (cerr) return cb(cerr);
|
||||||
|
|
||||||
addPendingTx(client, session, false, currencyCode, toAddress, satoshis,
|
addPendingTx(client, session, false, currencyCode, toAddress, 0,
|
||||||
function(err) {
|
function(err) {
|
||||||
done();
|
done();
|
||||||
cb(err);
|
cb(err);
|
||||||
|
|
@ -408,7 +418,7 @@ exports.addOutgoingPending = function addOutgoingPending(session, currencyCode,
|
||||||
exports.addInitialIncoming = function addInitialIncoming(session, tx, cb) {
|
exports.addInitialIncoming = function addInitialIncoming(session, tx, cb) {
|
||||||
connect(function(cerr, client, done) {
|
connect(function(cerr, client, done) {
|
||||||
if (cerr) return cb(cerr);
|
if (cerr) return cb(cerr);
|
||||||
async.waterfall([
|
async.series([
|
||||||
async.apply(silentQuery, client, 'BEGIN', null),
|
async.apply(silentQuery, client, 'BEGIN', null),
|
||||||
async.apply(addPendingTx, client, session, true, tx.currencyCode,
|
async.apply(addPendingTx, client, session, true, tx.currencyCode,
|
||||||
tx.toAddress, tx.satoshis),
|
tx.toAddress, tx.satoshis),
|
||||||
|
|
|
||||||
|
|
@ -41,7 +41,7 @@ exports.up = function(next) {
|
||||||
'currency_code text NOT NULL, ' +
|
'currency_code text NOT NULL, ' +
|
||||||
'to_address text NOT NULL, ' +
|
'to_address text NOT NULL, ' +
|
||||||
'satoshis integer NOT NULL, ' +
|
'satoshis integer NOT NULL, ' +
|
||||||
'created timestamp NOT NULL DEFAULT now() ' +
|
'updated timestamp NOT NULL DEFAULT now() ' +
|
||||||
')',
|
')',
|
||||||
|
|
||||||
'CREATE TABLE dispenses ( ' +
|
'CREATE TABLE dispenses ( ' +
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue