real trader works

This commit is contained in:
Josh Harvey 2016-04-10 02:39:56 +02:00
parent 1a10c3aba8
commit 92bed14927
3 changed files with 47 additions and 27 deletions

View file

@ -19,7 +19,7 @@ function bail () {
console.log('Command line utility for lamassu-server') console.log('Command line utility for lamassu-server')
console.log('\nssu reboot <machine-fingerprint>') console.log('\nssu reboot <machine-fingerprint>')
console.log('This will remotely reboot your lamassu-machine.') console.log('This will remotely reboot your lamassu-machine.')
console.log('\nssu crypto <code> <ticker-plugin> <wallet-plugin>') console.log('\nssu crypto <code> <ticker-plugin> <wallet-plugin> [<trader-plugin>]')
console.log('This will configure a new cryptocurrency.') console.log('This will configure a new cryptocurrency.')
process.exit(1) process.exit(1)
} }
@ -115,9 +115,10 @@ function crypto () {
var code = argv[1] var code = argv[1]
var tickerPlugin = argv[2] var tickerPlugin = argv[2]
var walletPlugin = argv[3] var walletPlugin = argv[3]
var traderPlugin = argv[4]
if (!code || !tickerPlugin || !walletPlugin) { if (!code || !tickerPlugin || !walletPlugin) {
console.log('\nssu crypto <code> <ticker-plugin> <wallet-plugin>') console.log('\nssu crypto <code> <ticker-plugin> <wallet-plugin> [<trader-plugin>]')
console.log('This will configure a new cryptocurrency.') console.log('This will configure a new cryptocurrency.')
process.exit(1) process.exit(1)
} }
@ -137,7 +138,8 @@ function crypto () {
var config = data.data var config = data.data
config.exchanges.plugins.current[code] = { config.exchanges.plugins.current[code] = {
ticker: tickerPlugin, ticker: tickerPlugin,
transfer: walletPlugin transfer: walletPlugin,
trader: traderPlugin
} }
config.exchanges.settings.coins = ['BTC', code] config.exchanges.settings.coins = ['BTC', code]
return db.none('update user_config set data=$1 where type=$2', [config, 'exchanges']) return db.none('update user_config set data=$1 where type=$2', [config, 'exchanges'])

View file

@ -9,7 +9,7 @@ BigNumber.config({DECIMAL_PLACES: 40})
var logger = require('./logger') var logger = require('./logger')
var argv = require('minimist')(process.argv.slice(2)) var argv = require('minimist')(process.argv.slice(2))
var tradeInterval = null var tradeIntervals = {}
var POLLING_RATE = 60 * 1000 // poll each minute var POLLING_RATE = 60 * 1000 // poll each minute
var REAP_RATE = 2 * 1000 var REAP_RATE = 2 * 1000
@ -183,11 +183,11 @@ exports.configure = function configure (config) {
} }
) )
tradesQueues[cryptoCode] = [] tradesQueues[cryptoCode] = tradesQueues[cryptoCode] || []
loadOrConfigPlugin( loadOrConfigPlugin(
traderPlugins[cryptoCode], traderPlugins[cryptoCode],
'trade', 'trader',
cryptoCode, cryptoCode,
null, null,
function onTraderChange (newTrader) { function onTraderChange (newTrader) {
@ -334,13 +334,13 @@ exports.trade = function trade (session, rawTrade, cb) {
// add bill to trader queue (if trader is enabled) // add bill to trader queue (if trader is enabled)
var cryptoCode = rawTrade.cryptoCode || 'BTC' var cryptoCode = rawTrade.cryptoCode || 'BTC'
var traderPlugin = traderPlugins[cryptoCode] var traderPlugin = traderPlugins[cryptoCode]
var tradesQueue = tradesQueues[cryptoCode]
if (traderPlugin) { if (traderPlugin) {
tradesQueue.push({ logger.debug('[%s] Pushing trade: %d', cryptoCode, rawTrade.cryptoAtoms)
tradesQueues[cryptoCode].push({
currency: rawTrade.currency, currency: rawTrade.currency,
cryptoAtoms: rawTrade.cryptoAtoms, cryptoAtoms: rawTrade.cryptoAtoms,
cryptoCode: rawTrade.cryptoCode cryptoCode: cryptoCode
}) })
} }
@ -417,11 +417,10 @@ exports.startPolling = function startPolling () {
cryptoCodes.forEach(function (cryptoCode) { cryptoCodes.forEach(function (cryptoCode) {
setInterval(async.apply(pollBalance, cryptoCode), POLLING_RATE) setInterval(async.apply(pollBalance, cryptoCode), POLLING_RATE)
setInterval(async.apply(pollRate, cryptoCode), POLLING_RATE) setInterval(async.apply(pollRate, cryptoCode), POLLING_RATE)
startTrader(cryptoCode)
}) })
setInterval(reapTxs, REAP_RATE) setInterval(reapTxs, REAP_RATE)
startTrader()
} }
function startTrader (cryptoCode) { function startTrader (cryptoCode) {
@ -430,21 +429,23 @@ function startTrader (cryptoCode) {
// `Trader#executeTrades` returns early if we don't have a trade exchange // `Trader#executeTrades` returns early if we don't have a trade exchange
// configured at the moment. // configured at the moment.
var traderPlugin = traderPlugins[cryptoCode] var traderPlugin = traderPlugins[cryptoCode]
if (!traderPlugin || tradeIntervals[cryptoCode]) return
if (traderPlugin && !tradeInterval) { logger.debug('[%s] startTrader', cryptoCode)
tradeInterval = setInterval(
function () { executeTrades(cryptoCode) }, tradeIntervals[cryptoCode] = setInterval(
cachedConfig.exchanges.settings.tradeInterval function () { executeTrades(cryptoCode) },
) cachedConfig.exchanges.settings.tradeInterval
} )
} }
function stopTrader (cryptoCode) { function stopTrader (cryptoCode) {
if (tradeInterval) { if (!tradeIntervals[cryptoCode]) return
clearInterval(tradeInterval)
tradeInterval = null logger.debug('[%s] stopTrader', cryptoCode)
tradesQueues[cryptoCode] = [] clearInterval(tradeIntervals[cryptoCode])
} tradeIntervals[cryptoCode] = null
tradesQueues[cryptoCode] = []
} }
function pollBalance (cryptoCode, cb) { function pollBalance (cryptoCode, cb) {
@ -536,8 +537,11 @@ function purchase (trade, cb) {
function consolidateTrades (cryptoCode) { function consolidateTrades (cryptoCode) {
// NOTE: value in cryptoAtoms stays the same no matter the currency // 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) { var cryptoAtoms = tradesQueues[cryptoCode].reduce(function (prev, current) {
return current.cryptoAtoms.plus(prev) return prev.plus(current.cryptoAtoms)
}, new BigNumber(0)) }, new BigNumber(0))
var consolidatedTrade = { var consolidatedTrade = {
@ -548,7 +552,7 @@ function consolidateTrades (cryptoCode) {
tradesQueues[cryptoCode] = [] tradesQueues[cryptoCode] = []
logger.debug('consolidated: ', JSON.stringify(consolidatedTrade)) logger.debug('[%s] consolidated: %j', cryptoCode, consolidatedTrade)
return consolidatedTrade return consolidatedTrade
} }
@ -556,21 +560,23 @@ function executeTrades (cryptoCode) {
var traderPlugin = traderPlugins[cryptoCode] var traderPlugin = traderPlugins[cryptoCode]
if (!traderPlugin) return if (!traderPlugin) return
logger.debug('checking for trades') logger.debug('[%s] checking for trades', cryptoCode)
var trade = consolidateTrades(cryptoCode) var trade = consolidateTrades(cryptoCode)
if (trade.cryptoAtoms.eq(0)) { if (trade.cryptoAtoms.eq(0)) {
logger.debug('rejecting 0 trade') logger.debug('[%s] rejecting 0 trade', cryptoCode)
return return
} }
logger.debug('making a trade: %d', trade.cryptoAtoms.toString()) logger.debug('making a trade: %d', trade.cryptoAtoms.toString())
purchase(trade, function (err) { purchase(trade, function (err) {
if (err) { if (err) {
logger.debug(err)
tradesQueues[cryptoCode].push(trade) tradesQueues[cryptoCode].push(trade)
if (err.name !== 'orderTooSmall') logger.error(err) if (err.name !== 'orderTooSmall') logger.error(err)
} }
logger.debug('Successful trade.')
}) })
} }

View file

@ -30,5 +30,17 @@ alter table pending_transactions alter satoshis TYPE bigint;
alter table bills add crypto_code text default 'BTC'; alter table bills add crypto_code text default 'BTC';
alter table bills alter satoshis TYPE bigint; alter table bills alter satoshis TYPE bigint;
- handle geth send failure better
- unlock geth account
- remove debug - 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