Merge pull request #35 from chester1000/refactor-fixes
Post-refactor fixes
This commit is contained in:
commit
291bf21bc3
4 changed files with 34 additions and 23 deletions
14
lib/app.js
14
lib/app.js
|
|
@ -13,18 +13,18 @@ module.exports = function (options) {
|
||||||
var app = express();
|
var app = express();
|
||||||
var connectionString;
|
var connectionString;
|
||||||
var server;
|
var server;
|
||||||
var config;
|
var lamassuConfig;
|
||||||
var db;
|
var db;
|
||||||
|
|
||||||
connectionString = options.postgres ||
|
connectionString = options.postgres ||
|
||||||
'postgres://lamassu:lamassu@localhost/lamassu';
|
'postgres://lamassu:lamassu@localhost/lamassu';
|
||||||
|
|
||||||
config = new LamassuConfig(connectionString);
|
lamassuConfig = new LamassuConfig(connectionString);
|
||||||
db = new PostgresqlInterface(connectionString);
|
db = new PostgresqlInterface(connectionString);
|
||||||
plugins.init(db);
|
plugins.init(db);
|
||||||
|
|
||||||
|
|
||||||
config.load(function (err, config) {
|
lamassuConfig.load(function (err, config) {
|
||||||
if (err) {
|
if (err) {
|
||||||
logger.error('Loading config failed');
|
logger.error('Loading config failed');
|
||||||
throw err;
|
throw err;
|
||||||
|
|
@ -34,8 +34,8 @@ module.exports = function (options) {
|
||||||
plugins.startPolling();
|
plugins.startPolling();
|
||||||
});
|
});
|
||||||
|
|
||||||
config.on('configUpdate', function () {
|
lamassuConfig.on('configUpdate', function () {
|
||||||
config.load(function (err, config) {
|
lamassuConfig.load(function (err, config) {
|
||||||
if (err) {
|
if (err) {
|
||||||
return logger.error('Error while reloading config');
|
return logger.error('Error while reloading config');
|
||||||
}
|
}
|
||||||
|
|
@ -62,7 +62,7 @@ module.exports = function (options) {
|
||||||
server = https.createServer(serverOptions, app);
|
server = https.createServer(serverOptions, app);
|
||||||
|
|
||||||
authMiddleware = function(req, res, next) {
|
authMiddleware = function(req, res, next) {
|
||||||
config.isAuthorized(routes.getFingerprint(req), function (err, device) {
|
lamassuConfig.isAuthorized(routes.getFingerprint(req), function (err, device) {
|
||||||
if (err) {
|
if (err) {
|
||||||
res.json({err: 'Internal Server Error'});
|
res.json({err: 'Internal Server Error'});
|
||||||
return next(err);
|
return next(err);
|
||||||
|
|
@ -88,7 +88,7 @@ module.exports = function (options) {
|
||||||
|
|
||||||
routes.init({
|
routes.init({
|
||||||
app: app,
|
app: app,
|
||||||
lamassuConfig: config,
|
lamassuConfig: lamassuConfig,
|
||||||
plugins: plugins,
|
plugins: plugins,
|
||||||
authMiddleware: authMiddleware,
|
authMiddleware: authMiddleware,
|
||||||
mock: options.mock
|
mock: options.mock
|
||||||
|
|
|
||||||
|
|
@ -144,6 +144,9 @@ exports.configure = function configure(config) {
|
||||||
pollBalance();
|
pollBalance();
|
||||||
pollRate();
|
pollRate();
|
||||||
};
|
};
|
||||||
|
exports.getCachedConfig = function getCachedConfig() {
|
||||||
|
return cachedConfig;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
// This is where we record starting trade balance at the beginning
|
// This is where we record starting trade balance at the beginning
|
||||||
|
|
@ -168,7 +171,7 @@ exports.trade = function trade(rawTrade, deviceFingerprint) {
|
||||||
|
|
||||||
exports.fiatBalance = function fiatBalance(deviceFingerprint) {
|
exports.fiatBalance = function fiatBalance(deviceFingerprint) {
|
||||||
var rawRate = exports.getDeviceRate().rates.ask;
|
var rawRate = exports.getDeviceRate().rates.ask;
|
||||||
var commision = cachedConfig.exchanges.settings.commision;
|
var commission = cachedConfig.exchanges.settings.commission;
|
||||||
|
|
||||||
if (!rawRate || !lastBalances) return null;
|
if (!rawRate || !lastBalances) return null;
|
||||||
|
|
||||||
|
|
@ -181,7 +184,7 @@ exports.fiatBalance = function fiatBalance(deviceFingerprint) {
|
||||||
|
|
||||||
// `balance.transferBalance` is the balance of our transfer account (the one
|
// `balance.transferBalance` is the balance of our transfer account (the one
|
||||||
// we use to send Bitcoins to clients) in satoshis.
|
// we use to send Bitcoins to clients) in satoshis.
|
||||||
var transferBalance = balance.transferBalance;
|
var transferBalance = lastBalances.transferBalance.BTC;
|
||||||
|
|
||||||
// Since `transferBalance` is in satoshis, we need to turn it into
|
// Since `transferBalance` is in satoshis, we need to turn it into
|
||||||
// bitcoins and then fiat to learn how much fiat currency we can exchange.
|
// bitcoins and then fiat to learn how much fiat currency we can exchange.
|
||||||
|
|
@ -367,6 +370,8 @@ function consolidateTrades() {
|
||||||
}, 0)
|
}, 0)
|
||||||
};
|
};
|
||||||
|
|
||||||
|
tradesQueue = [];
|
||||||
|
|
||||||
logger.debug('consolidated: ', JSON.stringify(consolidatedTrade));
|
logger.debug('consolidated: ', JSON.stringify(consolidatedTrade));
|
||||||
return consolidatedTrade;
|
return consolidatedTrade;
|
||||||
};
|
};
|
||||||
|
|
@ -383,9 +388,12 @@ function executeTrades() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.trade.debug('making a trade: %d', trade.satoshis / SATOSHI_FACTOR);
|
logger.debug('making a trade: %d', trade.satoshis / SATOSHI_FACTOR);
|
||||||
purchase(trade, function(err) {
|
purchase(trade, function(err) {
|
||||||
if (err) logger.error(err);
|
if (err) {
|
||||||
|
tradesQueue.push(trade);
|
||||||
|
logger.error(err);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ var logger = require('./logger');
|
||||||
var mock = false;
|
var mock = false;
|
||||||
|
|
||||||
var plugins;
|
var plugins;
|
||||||
|
var lamassuConfig;
|
||||||
var config;
|
var config;
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
|
@ -44,19 +45,22 @@ function poll(req, res) {
|
||||||
var fiatBalance = plugins.fiatBalance(fingerprint);
|
var fiatBalance = plugins.fiatBalance(fingerprint);
|
||||||
if (fiatBalance === null) return res.json({err: 'No balance available'});
|
if (fiatBalance === null) return res.json({err: 'No balance available'});
|
||||||
|
|
||||||
var idVerificationLimit = _trader.config.exchanges.settings.
|
var config = plugins.getCachedConfig();
|
||||||
compliance.hasOwnProperty('idVerificationLimit') ?
|
var complianceSettings = config.exchanges.settings.compliance;
|
||||||
_trader.config.exchanges.settings.compliance.idVerificationLimit :
|
|
||||||
null;
|
|
||||||
|
|
||||||
res.json({
|
var response = {
|
||||||
err: null,
|
err: null,
|
||||||
rate: rate * config.exchanges.settings.commission,
|
rate: rate * config.exchanges.settings.commission,
|
||||||
fiat: fiatBalance,
|
fiat: fiatBalance,
|
||||||
locale: config.brain.locale,
|
locale: config.brain.locale,
|
||||||
txLimit: parseInt(config.exchanges.settings.compliance.maximum.limit, 10),
|
txLimit: parseInt(complianceSettings.maximum.limit, 10),
|
||||||
idVerificationLimit: idVerificationLimit
|
idVerificationEnabled: complianceSettings.idVerificationEnabled
|
||||||
});
|
};
|
||||||
|
|
||||||
|
if (response.idVerificationEnabled)
|
||||||
|
response.idVerificationLimit = complianceSettings.idVerificationLimit
|
||||||
|
|
||||||
|
res.json(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
function trade(req, res) {
|
function trade(req, res) {
|
||||||
|
|
@ -67,7 +71,7 @@ function trade(req, res) {
|
||||||
|
|
||||||
function deviceEvent(req, res) {
|
function deviceEvent(req, res) {
|
||||||
var fingerprint = req.connection.getPeerCertificate().fingerprint;
|
var fingerprint = req.connection.getPeerCertificate().fingerprint;
|
||||||
_trader.event(req.body, fingerprint);
|
plugins.event(req.body, fingerprint);
|
||||||
res.json({err: null});
|
res.json({err: null});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -112,7 +116,7 @@ function pair(req, res) {
|
||||||
var token = req.body.token;
|
var token = req.body.token;
|
||||||
var name = req.body.name;
|
var name = req.body.name;
|
||||||
|
|
||||||
config.pair(
|
lamassuConfig.pair(
|
||||||
token,
|
token,
|
||||||
getFingerprint(req),
|
getFingerprint(req),
|
||||||
name,
|
name,
|
||||||
|
|
@ -127,7 +131,7 @@ function pair(req, res) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function init(localConfig) {
|
function init(localConfig) {
|
||||||
config = localConfig.lamassuConfig;
|
lamassuConfig = localConfig.lamassuConfig;
|
||||||
plugins = localConfig.plugins;
|
plugins = localConfig.plugins;
|
||||||
mock = localConfig.mock;
|
mock = localConfig.mock;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -131,6 +131,5 @@ describe('Plugins', function() {
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue