This commit is contained in:
Josh Harvey 2016-11-27 03:09:51 +02:00
parent 7e5515a873
commit b16d11045c
10 changed files with 368 additions and 550 deletions

View file

@ -1,9 +1,7 @@
'use strict'
const fs = require('fs')
const uuid = require('uuid')
const R = require('ramda')
const HKDF = require('node-hkdf-sync')
const BigNumber = require('bignumber.js')
// Needed for BigNumber for now
@ -14,13 +12,18 @@ const db = require('./postgresql_interface')
const logger = require('./logger')
const notifier = require('./notifier')
const T = require('./time')
const settingsLoader = require('./settings')
const configManager = require('./config-manager')
const ticker = require('./ticker')
const wallet = require('./wallet')
const exchange = require('./exchange')
const sms = require('./sms')
const email = require('./email')
const tradeIntervals = {}
const CHECK_NOTIFICATION_INTERVAL = T.minute
const ALERT_SEND_INTERVAL = T.hour
const POLLING_RATE = T.minute
const INCOMING_TX_INTERVAL = 30 * T.seconds
const LIVE_INCOMING_TX_INTERVAL = 5 * T.seconds
const STALE_INCOMING_TX_AGE = T.week
@ -34,22 +37,6 @@ const SWEEP_OLD_HD_INTERVAL = 2 * T.minutes
const TRADE_INTERVAL = T.minute
const TRADE_TTL = 5 * T.minutes
const tickerPlugins = {}
const traderPlugins = {}
const walletPlugins = {}
let idVerifierPlugin = null
let emailPlugin = null
let smsPlugin = null
let hkdf = null
const currentlyUsedPlugins = {}
let cachedConfig = null
let deviceCurrency = 'USD'
const lastBalances = {}
const lastRates = {}
const tradesQueues = {}
const coins = {
@ -60,201 +47,9 @@ const coins = {
let alertFingerprint = null
let lastAlertTime = null
exports.init = function init (seedPath) {
const masterSeed = new Buffer(fs.readFileSync(seedPath, 'utf8').trim(), 'hex')
hkdf = new HKDF('sha256', 'lamassu-server-salt', masterSeed)
}
function loadPlugin (name, config) {
// plugins definitions
const moduleMethods = {
ticker: ['ticker'],
trader: ['purchase', 'sell'],
wallet: ['balance', 'sendCoins', 'newAddress'],
idVerifier: ['verifyUser', 'verifyTransaction'],
info: ['checkAddress'],
email: ['sendMessage']
}
let plugin = null
// each used plugin MUST be installed
try {
plugin = require('lamassu-' + name)
} catch (err) {
logger.debug(err.stack)
if (err.code === 'MODULE_NOT_FOUND') {
throw new Error(name + ' module is not installed. ' +
'Try running \'npm install --save lamassu-' + name + '\' first')
}
logger.error('Error in %s plugin', name)
logger.error(err)
throw err
}
// each plugin MUST implement those
if (typeof plugin.SUPPORTED_MODULES !== 'undefined') {
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')
}
plugin.SUPPORTED_MODULES.forEach(moduleName => {
moduleMethods[moduleName].forEach(methodName => {
if (typeof plugin[methodName] !== 'function') {
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'))
}
if (typeof plugin.config !== 'function') {
logger.warn(new Error('\'' + name +
'\' fails to implement *recommended* \'config\' method'))
plugin.config = () => {}
} else if (config !== null) {
plugin.config(config, logger) // only when plugin supports it, and config is passed
}
return plugin
}
function loadOrConfigPlugin (pluginHandle, pluginType, cryptoCode, config, accounts, options,
onChangeCallback) {
const currentName = config.cryptoServices[pluginType] ||
config.extraServices[pluginType] ||
config.compliance[pluginType]
currentlyUsedPlugins[cryptoCode] = currentlyUsedPlugins[cryptoCode] || {}
const pluginChanged = currentlyUsedPlugins[cryptoCode][pluginType] !== currentName
if (!currentName) pluginHandle = null
else { // some plugins may be disabled
const pluginConfig = accounts[currentName]
const mergedConfig = R.merge(pluginConfig, options)
if (pluginHandle && !pluginChanged) pluginHandle.config(mergedConfig)
else {
pluginHandle = loadPlugin(currentName, mergedConfig)
currentlyUsedPlugins[cryptoCode] = currentlyUsedPlugins[cryptoCode] || {}
currentlyUsedPlugins[cryptoCode][pluginType] = currentName
const pluginName = pluginHandle.NAME || currentName
cryptoCode
? logger.debug('[%s] plugin(%s) loaded: %s', cryptoCode, pluginType, pluginName)
: logger.debug('plugin(%s) loaded: %s', pluginType, pluginName)
}
}
if (typeof onChangeCallback === 'function') onChangeCallback(pluginHandle)
return pluginHandle
}
exports.loadOrConfigPlugin = loadOrConfigPlugin
// Note: this whole function gets called every time there's a config update
exports.configure = function configure (config) {
cachedConfig = config
const cryptoCodes = getAllCryptoCodes()
return configManager.loadAccounts()
.then(accounts => {
cryptoCodes.forEach(cryptoCode => {
const cryptoScopedConfig = configManager.cryptoScoped(cryptoCode, cachedConfig)
// TICKER [required] configure (or load)
loadOrConfigPlugin(
tickerPlugins[cryptoCode],
'ticker',
cryptoCode,
cryptoScopedConfig,
accounts,
{currency: deviceCurrency},
function onTickerChange (newTicker) {
tickerPlugins[cryptoCode] = newTicker
cache.clearRate()
}
)
// Give each crypto a different derived seed so as not to allow any
// plugin to spend another plugin's funds
const cryptoSeed = hkdf.derive(cryptoCode, 32)
loadOrConfigPlugin(
walletPlugins[cryptoCode],
'wallet',
cryptoCode,
cryptoScopedConfig,
accounts,
{masterSeed: cryptoSeed},
function onWalletChange (newWallet) {
walletPlugins[cryptoCode] = newWallet
pollBalance(cryptoCode)
}
)
tradesQueues[cryptoCode] = tradesQueues[cryptoCode] || []
loadOrConfigPlugin(
traderPlugins[cryptoCode],
'trader',
cryptoCode,
cryptoScopedConfig,
accounts,
null,
function onTraderChange (newTrader) {
traderPlugins[cryptoCode] = newTrader
if (newTrader === null) stopTrader(cryptoCode)
else startTrader(cryptoCode)
}
)
})
const unscopedCfg = configManager.unscoped(cachedConfig)
// ID VERIFIER [optional] configure (or load)
idVerifierPlugin = loadOrConfigPlugin(
idVerifierPlugin,
'idVerifier',
null,
unscopedCfg,
accounts
)
emailPlugin = loadOrConfigPlugin(
emailPlugin,
'email',
null,
unscopedCfg,
accounts
)
smsPlugin = loadOrConfigPlugin(
smsPlugin,
'sms',
null,
unscopedCfg,
accounts
)
})
}
function getConfig (machineId) {
return configManager.machineScoped(machineId, cachedConfig)
const config = settingsLoader.settings().config
return configManager.machineScoped(machineId, config)
}
exports.getConfig = getConfig
@ -288,22 +83,16 @@ exports.pollQueries = function pollQueries (deviceId) {
}))
}
function _sendCoins (toAddress, cryptoAtoms, cryptoCode) {
const walletPlugin = walletPlugins[cryptoCode]
return walletPlugin.sendCoins(toAddress, cryptoAtoms.toNumber(), cryptoCode)
}
// NOTE: This will fail if we have already sent coins because there will be
// a db unique db record in the table already.
function executeTx (deviceId, tx) {
return db.addOutgoingTx(deviceId, tx)
.then(() => _sendCoins(tx.toAddress, tx.cryptoAtoms, tx.cryptoCode))
.then(() => wallet.sendCoins(tx.toAddress, tx.cryptoAtoms, tx.cryptoCode))
.then(txHash => {
const fee = null // Need to fill this out in plugins
const toSend = {cryptoAtoms: tx.cryptoAtoms, fiat: tx.fiat}
return db.sentCoins(tx, toSend, fee, null, txHash)
.then(() => pollBalance(tx.cryptoCode))
.then(() => ({
statusCode: 201, // Created
txHash,
@ -316,10 +105,13 @@ function executeTx (deviceId, tx) {
exports.trade = function trade (deviceId, rawTrade) {
// TODO: move this to DB, too
// add bill to trader queue (if trader is enabled)
const cryptoCode = rawTrade.cryptoCode || 'BTC'
const traderPlugin = traderPlugins[cryptoCode]
const cryptoCode = rawTrade.cryptoCode
return db.recordBill(deviceId, rawTrade)
.then(() => exchange.active(cryptoCode))
.then(active => {
if (!active) return
if (traderPlugin) {
logger.debug('[%s] Pushing trade: %d', cryptoCode, rawTrade.cryptoAtoms)
tradesQueues[cryptoCode].push({
currency: rawTrade.currency,
@ -327,9 +119,7 @@ exports.trade = function trade (deviceId, rawTrade) {
cryptoCode,
timestamp: Date.now()
})
}
return db.recordBill(deviceId, rawTrade)
})
}
exports.stateChange = function stateChange (deviceId, deviceTime, rec, cb) {
@ -359,22 +149,21 @@ exports.sendCoins = function sendCoins (deviceId, rawTx) {
}
exports.cashOut = function cashOut (deviceId, tx) {
const cryptoCode = tx.cryptoCode || 'BTC'
const walletPlugin = walletPlugins[cryptoCode]
const cryptoCode = tx.cryptoCode
const serialPromise = walletPlugin.supportsHD
const serialPromise = wallet.supportsHD
? db.nextCashOutSerialHD(tx.id, cryptoCode)
: Promise.resolve()
return serialPromise
.then(serialNumber => {
const tmpInfo = {
const info = {
label: 'TX ' + Date.now(),
account: 'deposit',
serialNumber
}
return walletPlugin.newAddress(tmpInfo)
return wallet.newAddress(cryptoCode, info)
.then(address => {
const newTx = R.assoc('toAddress', address, tx)
@ -392,45 +181,35 @@ exports.dispenseAck = function (deviceId, tx) {
return db.addDispense(deviceId, tx, cartridges)
}
exports.fiatBalance = function fiatBalance (cryptoCode, deviceId) {
const config = configManager.scoped(cryptoCode, deviceId, cachedConfig)
const deviceRate = exports.getDeviceRate(cryptoCode)
exports.fiatBalance = function fiatBalance (fiatCode, cryptoCode, deviceId) {
const _config = settingsLoader.settings().config
const config = configManager.scoped(cryptoCode, deviceId, _config)
if (!deviceRate) return null
const rawRate = deviceRate.rates.ask
const commission = (new BigNumber(config.commissions.cashInCommission).div(100)).plus(1)
const lastBalanceRec = lastBalances[cryptoCode]
if (!lastBalanceRec) return null
return Promise.all([ticker.ticker(cryptoCode), wallet.balance(cryptoCode)])
.then(([rates, balanceRec]) => {
const rawRate = rates[cryptoCode].rates.ask
const commission = (new BigNumber(config.commissions.cashInCommission).div(100)).plus(1)
const balance = balanceRec.balance
const lastBalance = lastBalanceRec.balance
if (!rawRate || !balance) return null
if (!rawRate || !lastBalance) return null
// The rate is actually our commission times real rate.
const rate = rawRate.times(commission)
// The rate is actually our commission times real rate.
const rate = rawRate.times(commission)
// `lowBalanceMargin` is our safety net. It's a number > 1, and we divide
// all our balances by it to provide a safety margin.
const lowBalanceMargin = (new BigNumber(config.commissions.lowBalanceMargin).div(100)).plus(1)
// `lowBalanceMargin` is our safety net. It's a number > 1, and we divide
// all our balances by it to provide a safety margin.
const lowBalanceMargin = (new BigNumber(config.commissions.lowBalanceMargin).div(100)).plus(1)
const unitScale = new BigNumber(10).pow(coins[cryptoCode].unitScale)
const fiatTransferBalance = balance.div(unitScale).times(rate).div(lowBalanceMargin)
const unitScale = new BigNumber(10).pow(coins[cryptoCode].unitScale)
const fiatTransferBalance = lastBalance.div(unitScale).times(rate).div(lowBalanceMargin)
return {timestamp: lastBalanceRec.timestamp, balance: fiatTransferBalance.round(3).toNumber()}
return {timestamp: balanceRec.timestamp, balance: fiatTransferBalance.round(3).toNumber()}
})
}
function processTxStatus (tx) {
const cryptoCode = tx.cryptoCode
const walletPlugin = walletPlugins[cryptoCode]
if (!walletPlugin) return logger.warn('Trying to check tx status but no wallet plugins for: ' + cryptoCode)
return walletPlugin.getStatus(tx.toAddress, tx.cryptoAtoms)
return wallet.getStatus(tx.toAddress, tx.cryptoAtoms, tx.cryptoCode)
.then(res => db.updateTxStatus(tx, res.status))
.catch(err => {
console.log(err.stack)
logger.error('[%s] Tx status processing error: %s', cryptoCode, err.stack)
})
}
function notifyConfirmation (tx) {
@ -444,7 +223,7 @@ function notifyConfirmation (tx) {
}
}
return smsPlugin.sendMessage(rec)
return sms.sendMessage(rec)
.then(() => db.updateNotify(tx))
}
@ -493,172 +272,80 @@ exports.startPolling = function startPolling () {
}
function startTrader (cryptoCode) {
// Always start trading, even if we don't have a trade exchange configured,
// since configuration can always change in `Trader#configure`.
// `Trader#executeTrades` returns early if we don't have a trade exchange
// configured at the moment.
const traderPlugin = traderPlugins[cryptoCode]
if (!traderPlugin || tradeIntervals[cryptoCode]) return
if (tradeIntervals[cryptoCode]) return
logger.debug('[%s] startTrader', cryptoCode)
tradeIntervals[cryptoCode] = setInterval(
() => { executeTrades(cryptoCode) },
TRADE_INTERVAL
)
}
function stopTrader (cryptoCode) {
if (!tradeIntervals[cryptoCode]) return
logger.debug('[%s] stopTrader', cryptoCode)
clearInterval(tradeIntervals[cryptoCode])
tradeIntervals[cryptoCode] = null
tradesQueues[cryptoCode] = []
}
function pollBalance (cryptoCode) {
logger.debug('[%s] collecting balance', cryptoCode)
const walletPlugin = walletPlugins[cryptoCode]
return walletPlugin.balance()
.then(balance => {
const unitScale = new BigNumber(10).pow(coins[cryptoCode].unitScale)
const coinBalance = new BigNumber(balance[cryptoCode])
const scaledCoinBalance = coinBalance.div(unitScale)
logger.debug('[%s] Balance update: %s', cryptoCode, scaledCoinBalance.toString())
lastBalances[cryptoCode] = {timestamp: Date.now(), balance: coinBalance}
return lastBalances
})
.catch(err => {
logger.error('[%s] Error loading balance: %s', cryptoCode, err.message)
return lastBalances
})
}
/*
* Getters | Helpers
*/
exports.getDeviceRate = function getDeviceRate (cryptoCode) {
const lastRate = lastRates[cryptoCode]
if (!lastRate) return null
return lastRate[deviceCurrency]
tradeIntervals[cryptoCode] = setInterval(() => executeTrades(cryptoCode), TRADE_INTERVAL)
}
/*
* Trader functions
*/
function purchase (trade, cb) {
const cryptoCode = trade.cryptoCode
const traderPlugin = traderPlugins[cryptoCode]
traderPlugin.purchase(trade.cryptoAtoms, deviceCurrency, cryptoCode, err => {
if (err) return cb(err)
pollBalance(cryptoCode)
if (typeof cb === 'function') cb()
})
function buy (trade) {
return exchange.buy(trade.cryptoAtoms, trade.fiatCode, trade.cryptoCode)
}
function consolidateTrades (cryptoCode) {
// NOTE: value in cryptoAtoms stays the same no matter the currency
function consolidateTrades (cryptoCode, fiatCode) {
const market = [fiatCode, cryptoCode].join('')
if (tradesQueues[cryptoCode].length === 0) return null
if (tradesQueues[market].length === 0) return null
logger.debug('tradesQueues size: %d', tradesQueues[cryptoCode].length)
logger.debug('tradesQueues head: %j', tradesQueues[cryptoCode][0])
logger.debug('[%s] tradesQueues size: %d', market, tradesQueues[market].length)
logger.debug('[%s] tradesQueues head: %j', market, tradesQueues[market][0])
const t0 = Date.now()
const filtered = tradesQueues[cryptoCode]
const filtered = tradesQueues[market]
.filter(trade => t0 - trade.timestamp < TRADE_TTL)
const filteredCount = tradesQueues[cryptoCode].length - filtered.length
const filteredCount = tradesQueues[market].length - filtered.length
if (filteredCount > 0) {
tradesQueues[cryptoCode] = filtered
logger.debug('[%s] expired %d trades', cryptoCode, filteredCount)
tradesQueues[market] = filtered
logger.debug('[%s] expired %d trades', market, filteredCount)
}
const cryptoAtoms = filtered
.reduce((prev, current) => prev.plus(current.cryptoAtoms), new BigNumber(0))
const consolidatedTrade = {
currency: deviceCurrency,
fiatCode,
cryptoAtoms,
cryptoCode
}
tradesQueues[cryptoCode] = []
tradesQueues[market] = []
logger.debug('[%s] consolidated: %j', cryptoCode, consolidatedTrade)
logger.debug('[%s] consolidated: %j', market, consolidatedTrade)
return consolidatedTrade
}
function executeTrades (cryptoCode) {
const traderPlugin = traderPlugins[cryptoCode]
if (!traderPlugin) return
function executeTrades (cryptoCode, fiatCode) {
const market = [fiatCode, cryptoCode].join('')
logger.debug('[%s] checking for trades', market)
logger.debug('[%s] checking for trades', cryptoCode)
const trade = consolidateTrades(cryptoCode)
if (trade === null) return logger.debug('[%s] no trades', cryptoCode)
const trade = consolidateTrades(cryptoCode, fiatCode)
if (trade === null) return logger.debug('[%s] no trades', market)
if (trade.cryptoAtoms.eq(0)) {
logger.debug('[%s] rejecting 0 trade', cryptoCode)
logger.debug('[%s] rejecting 0 trade', market)
return
}
logger.debug('making a trade: %d', trade.cryptoAtoms.toString())
purchase(trade, err => {
if (err) {
tradesQueues[cryptoCode].push(trade)
if (err.name !== 'orderTooSmall') return logger.error(err)
else return logger.debug(err)
}
logger.debug('Successful trade.')
logger.debug('[%s] making a trade: %d', market, trade.cryptoAtoms.toString())
return buy(trade)
.catch(err => {
tradesQueues[market].push(trade)
logger.error(err)
})
}
/*
* ID Verifier functions
*/
exports.verifyUser = function verifyUser (data, cb) {
return new Promise((resolve, reject) => {
idVerifierPlugin.verifyUser(data, (err, res) => {
if (err) return reject(err)
return resolve(res)
})
})
}
exports.verifyTx = function verifyTx (data, cb) {
return new Promise((resolve, reject) => {
idVerifierPlugin.verifyTransaction(data, (err, res) => {
if (err) return reject(err)
return resolve(res)
})
.then(() => {
logger.debug('[%s] Successful trade.', market)
})
}
function sendMessage (rec) {
const pluginPromises = []
const config = configManager.unscoped(cachedConfig)
if (!config.notifications.notificationsEnabled) return Promise.all([])
if (config.notifications.notificationsEmailEnabled) {
pluginPromises.push(emailPlugin.sendMessage(rec))
}
if (config.notifications.notificationsSMSEnabled) {
pluginPromises.push(smsPlugin.sendMessage(rec))
}
return Promise.all(pluginPromises)
return Promise.all([sms.sendMessage(rec), email.sendMessage(rec)])
}
exports.sendMessage = sendMessage
@ -715,36 +402,54 @@ function checkNotification () {
}
function getCryptoCodes (deviceId) {
return configManager.machineScoped(deviceId, cachedConfig).currencies.cryptoCurrencies
return settingsLoader.settings()
.then(settings => {
return configManager.machineScoped(deviceId, settings.config).currencies.cryptoCurrencies
})
}
exports.getCryptoCodes = getCryptoCodes
// Get union of all cryptoCodes from all machines
function getAllCryptoCodes () {
return db.devices()
.then(rows => {
return Promise.all([db.devices(), settingsLoader.settings()])
.then(([rows, settings]) => {
return rows.reduce((acc, r) => {
getCryptoCodes(r.device_id).forEach(c => acc.add(c))
const cryptoCodes = configManager.machineScoped(r.device_id, settings.config).currencies.cryptoCurrencies
cryptoCodes.forEach(c => acc.add(c))
return acc
}, new Set())
})
}
function getAllMarkets () {
return Promise.all([db.devices(), settingsLoader.settings()])
.then(([rows, settings]) => {
return rows.reduce((acc, r) => {
const currencies = configManager.machineScoped(r.device_id, settings.config).currencies
const cryptoCodes = currencies.cryptoCurrencies
const fiatCodes = currencies.fiatCodes
fiatCodes.forEach(fiatCode => cryptoCodes.forEach(cryptoCode => acc.add(fiatCode + cryptoCode)))
return acc
}, new Set())
})
}
function checkBalances () {
return Promise.all([getAllCryptoCodes(), db.devices()])
.then(arr => {
const cryptoCodes = arr[0]
const deviceIds = arr[1].map(r => r.device_id)
return Promise.all([getAllMarkets(), db.devices()])
.then(([markets, devices]) => {
const deviceIds = devices.map(r => r.device_id)
const balances = []
cryptoCodes.forEach(cryptoCode => {
markets.forEach(market => {
const fiatCode = market.fiatCode
const cryptoCode = market.cryptoCode
const minBalance = deviceIds.map(deviceId => {
const fiatBalanceRec = exports.fiatBalance(cryptoCode, deviceId)
const fiatBalanceRec = exports.fiatBalance(fiatCode, cryptoCode, deviceId)
return fiatBalanceRec ? fiatBalanceRec.balance : Infinity
})
.reduce((min, cur) => Math.min(min, cur), Infinity)
const rec = {fiatBalance: minBalance, cryptoCode, fiatCode: deviceCurrency}
const rec = {fiatBalance: minBalance, cryptoCode, fiatCode}
balances.push(rec)
})
@ -752,27 +457,29 @@ function checkBalances () {
})
}
exports.startCheckingNotification = function startCheckingNotification () {
const config = configManager.unscoped(cachedConfig)
exports.startCheckingNotification = function startCheckingNotification (config) {
notifier.init(db, checkBalances, config.notifications)
checkNotification()
setInterval(checkNotification, CHECK_NOTIFICATION_INTERVAL)
}
exports.getPhoneCode = function getPhoneCode (phone) {
const code = smsPlugin.NAME === 'MockSMS'
? '123'
: BigNumber.random().toFixed(6).slice(2)
return sms.name()
.then(name => {
const code = name === 'MockSMS'
? '123'
: BigNumber.random().toFixed(6).slice(2)
const rec = {
sms: {
toNumber: phone,
body: 'Your cryptomat code: ' + code
const rec = {
sms: {
toNumber: phone,
body: 'Your cryptomat code: ' + code
}
}
}
return smsPlugin.sendMessage(rec)
.then(() => code)
return sms.sendMessage(rec)
.then(() => code)
})
}
exports.updatePhone = db.addIncomingPhone
@ -803,13 +510,8 @@ exports.fetchTx = db.fetchTx
function sweepHD (row) {
const cryptoCode = row.crypto_code
const walletPlugin = walletPlugins[cryptoCode]
if (!walletPlugin) {
return logger.warn('Trying to sweep but no plugin set up for: ' + cryptoCode)
}
return walletPlugin.sweep(row.hd_serial)
return wallet.sweep(row.hd_serial)
.then(txHash => {
if (txHash) {
logger.debug('[%s] Swept address with tx: %s', cryptoCode, txHash)