WIP
This commit is contained in:
parent
0cec3670a9
commit
e12238c4fe
4 changed files with 163 additions and 73 deletions
|
|
@ -18,7 +18,6 @@ var infoPlugin = null;
|
|||
|
||||
var currentlyUsedPlugins = {};
|
||||
|
||||
|
||||
var cachedConfig = null;
|
||||
var deviceCurrency = 'USD';
|
||||
|
||||
|
|
@ -33,7 +32,6 @@ var reapTxInterval = null;
|
|||
var tradesQueue = [];
|
||||
var dispenseStatuses = {};
|
||||
|
||||
|
||||
// that's basically a constructor
|
||||
exports.init = function init(databaseHandle) {
|
||||
if (!databaseHandle) {
|
||||
|
|
@ -43,16 +41,15 @@ exports.init = function init(databaseHandle) {
|
|||
db = databaseHandle;
|
||||
};
|
||||
|
||||
|
||||
function loadPlugin(name, config) {
|
||||
|
||||
// plugins definitions
|
||||
var moduleMethods = {
|
||||
ticker: [ 'ticker' ],
|
||||
trader: [ 'balance', 'purchase', 'sell' ],
|
||||
wallet: [ 'balance', 'sendBitcoins', 'newAddress' ],
|
||||
idVerifier: [ 'verifyUser', 'verifyTransaction' ],
|
||||
info: [ 'getAddressLastTx', 'getTx' ]
|
||||
ticker: ['ticker'],
|
||||
trader: ['balance', 'purchase', 'sell'],
|
||||
wallet: ['balance', 'sendBitcoins', 'newAddress'],
|
||||
idVerifier: ['verifyUser', 'verifyTransaction'],
|
||||
info: ['getAddressLastTx', 'getTx']
|
||||
};
|
||||
|
||||
var plugin = null;
|
||||
|
|
@ -60,67 +57,72 @@ function loadPlugin(name, config) {
|
|||
// each used plugin MUST be installed
|
||||
try {
|
||||
plugin = require('lamassu-' + name);
|
||||
|
||||
} catch(_) {
|
||||
throw new Error(name + ' module is not installed. Try running \'npm install --save lamassu-' + name + '\' first');
|
||||
} catch (_) {
|
||||
throw new Error(name + ' module is not installed. ' +
|
||||
'Try running \'npm install --save lamassu-' + name + '\' first');
|
||||
}
|
||||
|
||||
|
||||
// each plugin MUST implement those
|
||||
if (typeof plugin.SUPPORTED_MODULES !== 'undefined') {
|
||||
if(plugin.SUPPORTED_MODULES === 'string')
|
||||
if (plugin.SUPPORTED_MODULES === 'string')
|
||||
plugin.SUPPORTED_MODULES = [plugin.SUPPORTED_MODULES];
|
||||
}
|
||||
|
||||
if(!(plugin.SUPPORTED_MODULES instanceof Array))
|
||||
throw new Error('\'' + name + '\' fails to implement *required* \'SUPPORTED_MODULES\' constant');
|
||||
if (!(plugin.SUPPORTED_MODULES instanceof Array))
|
||||
throw new Error('\'' + name + '\' fails to implement *required* ' +
|
||||
'\'SUPPORTED_MODULES\' constant');
|
||||
|
||||
plugin.SUPPORTED_MODULES.forEach(function(moduleName) {
|
||||
moduleMethods[moduleName].forEach(function(methodName) {
|
||||
if (typeof plugin[methodName] !== 'function') {
|
||||
throw new Error('\'' + name + '\' declares \'' + moduleName + '\', but fails to implement \'' + methodName + '\' method');
|
||||
throw new Error('\'' + name + '\' declares \'' + moduleName +
|
||||
'\', but fails to implement \'' + methodName + '\' method');
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
// each plugin SHOULD implement those
|
||||
if (typeof plugin.NAME === 'undefined')
|
||||
logger.warn(new Error('\'' + name + '\' fails to implement *recommended* \'NAME\' field'));
|
||||
logger.warn(new Error('\'' + name +
|
||||
'\' fails to implement *recommended* \'NAME\' field'));
|
||||
|
||||
if (typeof plugin.config !== 'function') {
|
||||
logger.warn(new Error('\'' + name + '\' fails to implement *recommended* \'config\' method'));
|
||||
logger.warn(new Error('\'' + name +
|
||||
'\' fails to implement *recommended* \'config\' method'));
|
||||
plugin.config = function() {};
|
||||
} else if (config !== null)
|
||||
} else if (config !== null) {
|
||||
plugin.config(config); // only when plugin supports it, and config is passed
|
||||
|
||||
}
|
||||
|
||||
return plugin;
|
||||
}
|
||||
|
||||
function loadOrConfigPlugin(pluginHandle, pluginType, currency, onChangeCallback) {
|
||||
function loadOrConfigPlugin(pluginHandle, pluginType, currency,
|
||||
onChangeCallback) {
|
||||
var currentName = cachedConfig.exchanges.plugins.current[pluginType];
|
||||
var pluginChanged = currentlyUsedPlugins[pluginType] !== currentName;
|
||||
|
||||
if (!currentName) pluginHandle = null;
|
||||
else { // some plugins may be disabled
|
||||
var pluginConfig = cachedConfig.exchanges.plugins.settings[currentName] || {};
|
||||
var pluginConfig = cachedConfig.exchanges.plugins.settings[currentName] ||
|
||||
{};
|
||||
|
||||
if (currency) pluginConfig.currency = currency;
|
||||
|
||||
if (pluginHandle && !pluginChanged) pluginHandle.config(pluginConfig);
|
||||
else {
|
||||
pluginHandle = loadPlugin(currentName, pluginConfig);
|
||||
logger.debug('plugin(%s) loaded: %s', pluginType, pluginHandle.NAME || currentName);
|
||||
logger.debug('plugin(%s) loaded: %s', pluginType, pluginHandle.NAME ||
|
||||
currentName);
|
||||
}
|
||||
}
|
||||
|
||||
if (typeof onChangeCallback === 'function') onChangeCallback(pluginHandle, currency);
|
||||
if (typeof onChangeCallback === 'function')
|
||||
onChangeCallback(pluginHandle, currency);
|
||||
|
||||
return pluginHandle;
|
||||
}
|
||||
|
||||
|
||||
exports.configure = function configure(config) {
|
||||
if (config.exchanges.settings.lowBalanceMargin < 1) {
|
||||
throw new Error('\'settings.lowBalanceMargin\' has to be >= 1');
|
||||
|
|
@ -217,9 +219,10 @@ function reapOutgoingTx(deviceFingerprint, tx) {
|
|||
}
|
||||
|
||||
function reapIncomingTx(deviceFingerprint, tx) {
|
||||
infoPlugin.checkAddress(tx.toAddress, function(err, status, satoshisReceived) {
|
||||
infoPlugin.checkAddress(tx.toAddress, function(err, status,
|
||||
satoshisReceived) {
|
||||
if (status === 'notSeen') return;
|
||||
db.addIngoingTx(deviceFingerprint, tx, status, satoshisReceived);
|
||||
db.addIncomingTx(deviceFingerprint, tx, status, satoshisReceived);
|
||||
});
|
||||
}
|
||||
|
||||
|
|
@ -277,7 +280,6 @@ exports.sendBitcoins = function sendBitcoins(deviceFingerprint, rawTx, cb) {
|
|||
executeTx(deviceFingerprint, rawTx, cb);
|
||||
};
|
||||
|
||||
|
||||
// sets given status both "locally" (dispenseStatuses) and saves to db
|
||||
function _setDispenseStatus(deviceFingerprint, tx, status, deposit) {
|
||||
tx.status = status;
|
||||
|
|
@ -313,14 +315,10 @@ exports.cashOut = function cashOut(deviceFingerprint, tx, cb) {
|
|||
account: 'deposit'
|
||||
};
|
||||
walletPlugin.newAddress(tmpInfo, function(err, address) {
|
||||
if (err)
|
||||
return cb(new Error(err));
|
||||
if (err) return cb(err);
|
||||
|
||||
tx.toAddress = address;
|
||||
tx.incoming = true;
|
||||
|
||||
db.addPendingTx(deviceFingerprint, tx, function(err) {
|
||||
cb(err, address);
|
||||
db.addInitialIncoming(deviceFingerprint, tx, address, function(_err) {
|
||||
cb(_err, address);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
|
@ -333,7 +331,6 @@ exports.dispenseStatus = function dispenseStatus(deviceFingerprint) {
|
|||
return dispenseStatuses[deviceFingerprint];
|
||||
};
|
||||
|
||||
|
||||
exports.fiatBalance = function fiatBalance() {
|
||||
var rawRate = exports.getDeviceRate().rates.ask;
|
||||
var commission = cachedConfig.exchanges.settings.commission;
|
||||
|
|
@ -357,12 +354,12 @@ exports.fiatBalance = function fiatBalance() {
|
|||
// Unit validity proof: [ $ ] = [ (B * 10^8) / 10^8 * $/B ]
|
||||
// [ $ ] = [ B * $/B ]
|
||||
// [ $ ] = [ $ ]
|
||||
var fiatTransferBalance = ((transferBalance / SATOSHI_FACTOR) * rate) / lowBalanceMargin;
|
||||
var fiatTransferBalance = ((transferBalance / SATOSHI_FACTOR) * rate) /
|
||||
lowBalanceMargin;
|
||||
|
||||
return fiatTransferBalance;
|
||||
};
|
||||
|
||||
|
||||
/*
|
||||
* Polling livecycle
|
||||
*/
|
||||
|
|
@ -401,7 +398,6 @@ function stopTrader() {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
function pollBalance(cb) {
|
||||
logger.debug('collecting balance');
|
||||
|
||||
|
|
@ -446,7 +442,6 @@ function pollRate(cb) {
|
|||
});
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Getters | Helpers
|
||||
*/
|
||||
|
|
@ -515,7 +510,6 @@ function executeTrades() {
|
|||
});
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* ID Verifier functions
|
||||
*/
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue