diff --git a/bin/ssu b/bin/ssu index 057a4e1e..dfe761b2 100755 --- a/bin/ssu +++ b/bin/ssu @@ -19,7 +19,7 @@ function bail () { console.log('Command line utility for lamassu-server') console.log('\nssu reboot ') console.log('This will remotely reboot your lamassu-machine.') - console.log('\nssu crypto ') + console.log('\nssu crypto []') console.log('This will configure a new cryptocurrency.') process.exit(1) } @@ -115,9 +115,10 @@ function crypto () { var code = argv[1] var tickerPlugin = argv[2] var walletPlugin = argv[3] + var traderPlugin = argv[4] if (!code || !tickerPlugin || !walletPlugin) { - console.log('\nssu crypto ') + console.log('\nssu crypto []') console.log('This will configure a new cryptocurrency.') process.exit(1) } @@ -137,7 +138,8 @@ function crypto () { var config = data.data config.exchanges.plugins.current[code] = { ticker: tickerPlugin, - transfer: walletPlugin + transfer: walletPlugin, + trader: traderPlugin } config.exchanges.settings.coins = ['BTC', code] return db.none('update user_config set data=$1 where type=$2', [config, 'exchanges']) diff --git a/lib/plugins.js b/lib/plugins.js index 64980173..fb7c7808 100644 --- a/lib/plugins.js +++ b/lib/plugins.js @@ -9,7 +9,7 @@ BigNumber.config({DECIMAL_PLACES: 40}) var logger = require('./logger') var argv = require('minimist')(process.argv.slice(2)) -var tradeInterval = null +var tradeIntervals = {} var POLLING_RATE = 60 * 1000 // poll each minute var REAP_RATE = 2 * 1000 @@ -183,11 +183,11 @@ exports.configure = function configure (config) { } ) - tradesQueues[cryptoCode] = [] + tradesQueues[cryptoCode] = tradesQueues[cryptoCode] || [] loadOrConfigPlugin( traderPlugins[cryptoCode], - 'trade', + 'trader', cryptoCode, null, function onTraderChange (newTrader) { @@ -334,13 +334,13 @@ exports.trade = function trade (session, rawTrade, cb) { // add bill to trader queue (if trader is enabled) var cryptoCode = rawTrade.cryptoCode || 'BTC' var traderPlugin = traderPlugins[cryptoCode] - var tradesQueue = tradesQueues[cryptoCode] if (traderPlugin) { - tradesQueue.push({ + logger.debug('[%s] Pushing trade: %d', cryptoCode, rawTrade.cryptoAtoms) + tradesQueues[cryptoCode].push({ currency: rawTrade.currency, cryptoAtoms: rawTrade.cryptoAtoms, - cryptoCode: rawTrade.cryptoCode + cryptoCode: cryptoCode }) } @@ -417,11 +417,10 @@ exports.startPolling = function startPolling () { cryptoCodes.forEach(function (cryptoCode) { setInterval(async.apply(pollBalance, cryptoCode), POLLING_RATE) setInterval(async.apply(pollRate, cryptoCode), POLLING_RATE) + startTrader(cryptoCode) }) setInterval(reapTxs, REAP_RATE) - - startTrader() } function startTrader (cryptoCode) { @@ -430,21 +429,23 @@ function startTrader (cryptoCode) { // `Trader#executeTrades` returns early if we don't have a trade exchange // configured at the moment. var traderPlugin = traderPlugins[cryptoCode] + if (!traderPlugin || tradeIntervals[cryptoCode]) return - if (traderPlugin && !tradeInterval) { - tradeInterval = setInterval( - function () { executeTrades(cryptoCode) }, - cachedConfig.exchanges.settings.tradeInterval - ) - } + logger.debug('[%s] startTrader', cryptoCode) + + tradeIntervals[cryptoCode] = setInterval( + function () { executeTrades(cryptoCode) }, + cachedConfig.exchanges.settings.tradeInterval + ) } function stopTrader (cryptoCode) { - if (tradeInterval) { - clearInterval(tradeInterval) - tradeInterval = null - tradesQueues[cryptoCode] = [] - } + if (!tradeIntervals[cryptoCode]) return + + logger.debug('[%s] stopTrader', cryptoCode) + clearInterval(tradeIntervals[cryptoCode]) + tradeIntervals[cryptoCode] = null + tradesQueues[cryptoCode] = [] } function pollBalance (cryptoCode, cb) { @@ -536,8 +537,11 @@ function purchase (trade, cb) { function consolidateTrades (cryptoCode) { // NOTE: value in cryptoAtoms stays the same no matter the currency + + logger.debug('tradesQueues size: %d', tradesQueues[cryptoCode].length) + logger.debug('tradesQueues head: %j', tradesQueues[cryptoCode][0]) var cryptoAtoms = tradesQueues[cryptoCode].reduce(function (prev, current) { - return current.cryptoAtoms.plus(prev) + return prev.plus(current.cryptoAtoms) }, new BigNumber(0)) var consolidatedTrade = { @@ -548,7 +552,7 @@ function consolidateTrades (cryptoCode) { tradesQueues[cryptoCode] = [] - logger.debug('consolidated: ', JSON.stringify(consolidatedTrade)) + logger.debug('[%s] consolidated: %j', cryptoCode, consolidatedTrade) return consolidatedTrade } @@ -556,21 +560,23 @@ function executeTrades (cryptoCode) { var traderPlugin = traderPlugins[cryptoCode] if (!traderPlugin) return - logger.debug('checking for trades') + logger.debug('[%s] checking for trades', cryptoCode) var trade = consolidateTrades(cryptoCode) if (trade.cryptoAtoms.eq(0)) { - logger.debug('rejecting 0 trade') + logger.debug('[%s] rejecting 0 trade', cryptoCode) return } logger.debug('making a trade: %d', trade.cryptoAtoms.toString()) purchase(trade, function (err) { if (err) { + logger.debug(err) tradesQueues[cryptoCode].push(trade) if (err.name !== 'orderTooSmall') logger.error(err) } + logger.debug('Successful trade.') }) } diff --git a/todo.txt b/todo.txt index db724706..3f235870 100644 --- a/todo.txt +++ b/todo.txt @@ -30,5 +30,17 @@ alter table pending_transactions alter satoshis TYPE bigint; alter table bills add crypto_code text default 'BTC'; alter table bills alter satoshis TYPE bigint; +- handle geth send failure better +- unlock geth account - remove debug -- test trading +- implement coin selection screen +- ask neal to work on config scripts + +TypeError: Cannot read property 'rates' of null + at /Users/josh/projects/lamassu-server/lib/routes.js:36:49 + at Array.forEach (native) + at buildRates (/Users/josh/projects/lamassu-server/lib/routes.js:35:15) + at poll (/Users/josh/projects/lamassu-server/lib/routes.js:66:15) + at callbacks (/Users/josh/projects/lamassu-server/node_modules/express/lib/router/index.js:164:37) + at /Users/josh/projects/lamassu-server/lib/app.js:56:7 + at /Users/josh/projects/lamassu-server/node_modules/lamassu-config/lib/main.js:142:5