WIPP
This commit is contained in:
parent
df5d9cac89
commit
a375adb8b9
3 changed files with 516 additions and 496 deletions
158
lib/plugins.js
158
lib/plugins.js
|
|
@ -10,7 +10,6 @@ const logger = require('./logger')
|
|||
const notifier = require('./notifier')
|
||||
const T = require('./time')
|
||||
const configManager = require('./config-manager')
|
||||
const settingsLoader = require('./settings-loader')
|
||||
const ticker = require('./ticker')
|
||||
const wallet = require('./wallet')
|
||||
const exchange = require('./exchange')
|
||||
|
|
@ -38,8 +37,8 @@ const coins = {
|
|||
let alertFingerprint = null
|
||||
let lastAlertTime = null
|
||||
|
||||
function buildRates (deviceId, tickers) {
|
||||
const settings = settingsLoader.settings()
|
||||
function plugins (settings) {
|
||||
function buildRates (deviceId, tickers) {
|
||||
const config = configManager.machineScoped(deviceId, settings.config)
|
||||
const cryptoCodes = config.cryptoCurrencies
|
||||
const cashOut = config.cashOutEnabled
|
||||
|
|
@ -60,10 +59,9 @@ function buildRates (deviceId, tickers) {
|
|||
})
|
||||
|
||||
return rates
|
||||
}
|
||||
}
|
||||
|
||||
function buildBalances (deviceId, balanceRecs) {
|
||||
const settings = settingsLoader.settings()
|
||||
function buildBalances (deviceId, balanceRecs) {
|
||||
const config = configManager.machineScoped(deviceId, settings.config)
|
||||
const cryptoCodes = config.cryptoCurrencies
|
||||
|
||||
|
|
@ -78,9 +76,9 @@ function buildBalances (deviceId, balanceRecs) {
|
|||
})
|
||||
|
||||
return balances
|
||||
}
|
||||
}
|
||||
|
||||
function buildCartridges (cartridges, virtualCartridges, rec) {
|
||||
function buildCartridges (cartridges, virtualCartridges, rec) {
|
||||
return {
|
||||
cartridges: [
|
||||
{
|
||||
|
|
@ -94,10 +92,9 @@ function buildCartridges (cartridges, virtualCartridges, rec) {
|
|||
],
|
||||
virtualCartridges
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function pollQueries (deviceTime, deviceId, deviceRec) {
|
||||
const settings = settingsLoader.settings()
|
||||
function pollQueries (deviceTime, deviceId, deviceRec) {
|
||||
const config = configManager.machineScoped(deviceId, settings.config)
|
||||
const fiatCode = config.fiatCurrency
|
||||
const cryptoCodes = config.cryptoCurrencies
|
||||
|
|
@ -123,11 +120,11 @@ function pollQueries (deviceTime, deviceId, deviceRec) {
|
|||
balances: buildBalances(deviceId, balances)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// NOTE: This will fail if we have already sent coins because there will be
|
||||
// a dbm unique dbm record in the table already.
|
||||
function executeTx (deviceId, tx) {
|
||||
// NOTE: This will fail if we have already sent coins because there will be
|
||||
// a dbm unique dbm record in the table already.
|
||||
function executeTx (deviceId, tx) {
|
||||
return dbm.addOutgoingTx(deviceId, tx)
|
||||
.then(() => wallet.sendCoins(tx.toAddress, tx.cryptoAtoms, tx.cryptoCode))
|
||||
.then(txHash => {
|
||||
|
|
@ -141,9 +138,9 @@ function executeTx (deviceId, tx) {
|
|||
txId: tx.id
|
||||
}))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
function trade (deviceId, rawTrade) {
|
||||
function trade (deviceId, rawTrade) {
|
||||
// TODO: move this to dbm, too
|
||||
// add bill to trader queue (if trader is enabled)
|
||||
const cryptoCode = rawTrade.cryptoCode
|
||||
|
|
@ -165,9 +162,9 @@ function trade (deviceId, rawTrade) {
|
|||
timestamp: Date.now()
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
function stateChange (deviceId, deviceTime, rec) {
|
||||
function stateChange (deviceId, deviceTime, rec) {
|
||||
const event = {
|
||||
id: rec.uuid,
|
||||
deviceId: deviceId,
|
||||
|
|
@ -176,9 +173,9 @@ function stateChange (deviceId, deviceTime, rec) {
|
|||
deviceTime: deviceTime
|
||||
}
|
||||
return dbm.machineEvent(event)
|
||||
}
|
||||
}
|
||||
|
||||
function recordPing (deviceId, deviceTime, rec) {
|
||||
function recordPing (deviceId, deviceTime, rec) {
|
||||
const event = {
|
||||
id: uuid.v4(),
|
||||
deviceId: deviceId,
|
||||
|
|
@ -187,13 +184,13 @@ function recordPing (deviceId, deviceTime, rec) {
|
|||
deviceTime: deviceTime
|
||||
}
|
||||
return dbm.machineEvent(event)
|
||||
}
|
||||
}
|
||||
|
||||
function sendCoins (deviceId, rawTx) {
|
||||
function sendCoins (deviceId, rawTx) {
|
||||
return executeTx(deviceId, rawTx)
|
||||
}
|
||||
}
|
||||
|
||||
function cashOut (deviceId, tx) {
|
||||
function cashOut (deviceId, tx) {
|
||||
const cryptoCode = tx.cryptoCode
|
||||
|
||||
const serialPromise = wallet.supportsHD
|
||||
|
|
@ -216,19 +213,17 @@ function cashOut (deviceId, tx) {
|
|||
.then(() => address)
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
function dispenseAck (deviceId, tx) {
|
||||
const settings = settingsLoader.settings()
|
||||
function dispenseAck (deviceId, tx) {
|
||||
const config = configManager.machineScoped(deviceId, settings.config)
|
||||
const cartridges = [ config.topCashOutDenomination,
|
||||
config.bottomCashOutDenomination ]
|
||||
|
||||
return dbm.addDispense(deviceId, tx, cartridges)
|
||||
}
|
||||
}
|
||||
|
||||
function fiatBalance (fiatCode, cryptoCode, deviceId) {
|
||||
const settings = settingsLoader.settings()
|
||||
function fiatBalance (fiatCode, cryptoCode, deviceId) {
|
||||
const config = configManager.scoped(cryptoCode, deviceId, settings.config)
|
||||
|
||||
return Promise.all([ticker.getRates(fiatCode, cryptoCode), wallet.balance(cryptoCode)])
|
||||
|
|
@ -251,14 +246,14 @@ function fiatBalance (fiatCode, cryptoCode, deviceId) {
|
|||
|
||||
return {timestamp: balanceRec.timestamp, balance: fiatTransferBalance.round(3).toNumber()}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
function processTxStatus (tx) {
|
||||
function processTxStatus (tx) {
|
||||
return wallet.getStatus(tx.toAddress, tx.cryptoAtoms, tx.cryptoCode)
|
||||
.then(res => dbm.updateTxStatus(tx, res.status))
|
||||
}
|
||||
}
|
||||
|
||||
function notifyConfirmation (tx) {
|
||||
function notifyConfirmation (tx) {
|
||||
logger.debug('notifyConfirmation')
|
||||
|
||||
const phone = tx.phone
|
||||
|
|
@ -271,49 +266,49 @@ function notifyConfirmation (tx) {
|
|||
|
||||
return sms.sendMessage(rec)
|
||||
.then(() => dbm.updateNotify(tx))
|
||||
}
|
||||
}
|
||||
|
||||
function monitorLiveIncoming () {
|
||||
function monitorLiveIncoming () {
|
||||
const statuses = ['notSeen', 'published', 'insufficientFunds']
|
||||
|
||||
return dbm.fetchOpenTxs(statuses, STALE_LIVE_INCOMING_TX_AGE)
|
||||
.then(txs => Promise.all(txs.map(processTxStatus)))
|
||||
.catch(logger.error)
|
||||
}
|
||||
}
|
||||
|
||||
function monitorIncoming () {
|
||||
function monitorIncoming () {
|
||||
const statuses = ['notSeen', 'published', 'authorized', 'instant', 'rejected', 'insufficientFunds']
|
||||
|
||||
return dbm.fetchOpenTxs(statuses, STALE_INCOMING_TX_AGE)
|
||||
.then(txs => Promise.all(txs.map(processTxStatus)))
|
||||
.catch(logger.error)
|
||||
}
|
||||
}
|
||||
|
||||
function monitorUnnotified () {
|
||||
function monitorUnnotified () {
|
||||
dbm.fetchUnnotifiedTxs(MAX_NOTIFY_AGE, MIN_NOTIFY_AGE)
|
||||
.then(txs => Promise.all(txs.map(notifyConfirmation)))
|
||||
.catch(logger.error)
|
||||
}
|
||||
}
|
||||
|
||||
function pong () {
|
||||
function pong () {
|
||||
db.none('insert into server_events (event_type) values ($1)', ['ping'])
|
||||
.catch(logger.error)
|
||||
}
|
||||
}
|
||||
|
||||
function pongClear () {
|
||||
function pongClear () {
|
||||
const sql = `delete from server_events
|
||||
where event_type=$1
|
||||
and created < now() - interval $2`
|
||||
|
||||
db.none(sql, ['ping', PONG_TTL])
|
||||
.catch(logger.error)
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
/*
|
||||
* Trader functions
|
||||
*/
|
||||
|
||||
function consolidateTrades (cryptoCode, fiatCode) {
|
||||
function consolidateTrades (cryptoCode, fiatCode) {
|
||||
const market = [fiatCode, cryptoCode].join('')
|
||||
|
||||
const marketTradesQueues = tradesQueues[market]
|
||||
|
|
@ -355,11 +350,9 @@ function consolidateTrades (cryptoCode, fiatCode) {
|
|||
|
||||
logger.debug('[%s] consolidated: %j', market, consolidatedTrade)
|
||||
return consolidatedTrade
|
||||
}
|
||||
|
||||
function executeTrades () {
|
||||
const settings = settingsLoader.settings()
|
||||
}
|
||||
|
||||
function executeTrades () {
|
||||
return dbm.devices()
|
||||
.then(devices => {
|
||||
const deviceIds = devices.map(device => device.device_id)
|
||||
|
|
@ -377,9 +370,9 @@ function executeTrades () {
|
|||
return Promise.all(tradesPromises)
|
||||
})
|
||||
.catch(logger.error)
|
||||
}
|
||||
}
|
||||
|
||||
function executeTradesForMarket (settings, fiatCode, cryptoCode) {
|
||||
function executeTradesForMarket (settings, fiatCode, cryptoCode) {
|
||||
if (!exchange.active(cryptoCode)) return
|
||||
|
||||
const market = [fiatCode, cryptoCode].join('')
|
||||
|
|
@ -402,10 +395,9 @@ function executeTradesForMarket (settings, fiatCode, cryptoCode) {
|
|||
if (err.name === 'NoExchangeError') return logger.debug(err.message)
|
||||
logger.error(err)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
function sendMessage (rec) {
|
||||
const settings = settingsLoader.settings()
|
||||
function sendMessage (rec) {
|
||||
const config = configManager.unscoped(settings.config)
|
||||
|
||||
let promises = []
|
||||
|
|
@ -413,9 +405,9 @@ function sendMessage (rec) {
|
|||
if (config.notificationsSMSEnabled) promises.push(sms.sendMessage(rec))
|
||||
|
||||
return Promise.all(promises)
|
||||
}
|
||||
}
|
||||
|
||||
function sendNoAlerts () {
|
||||
function sendNoAlerts () {
|
||||
const subject = '[Lamassu] All clear'
|
||||
const rec = {
|
||||
sms: {
|
||||
|
|
@ -427,9 +419,9 @@ function sendNoAlerts () {
|
|||
}
|
||||
}
|
||||
return sendMessage(rec)
|
||||
}
|
||||
}
|
||||
|
||||
function checkNotification () {
|
||||
function checkNotification () {
|
||||
return notifier.checkStatus()
|
||||
.then(alertRec => {
|
||||
const currentAlertFingerprint = notifier.alertFingerprint(alertRec)
|
||||
|
|
@ -463,10 +455,9 @@ function checkNotification () {
|
|||
if (results && results.length > 0) logger.debug('Successfully sent alerts')
|
||||
})
|
||||
.catch(logger.error)
|
||||
}
|
||||
}
|
||||
|
||||
function checkDeviceBalances (deviceId) {
|
||||
const settings = settingsLoader.settings()
|
||||
function checkDeviceBalances (deviceId) {
|
||||
const config = configManager.machineScoped(deviceId, settings.config)
|
||||
const cryptoCodes = config.cryptoCurrencies
|
||||
const fiatCode = config.fiatCurrency
|
||||
|
|
@ -481,9 +472,9 @@ function checkDeviceBalances (deviceId) {
|
|||
deviceId
|
||||
}))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
function checkBalances () {
|
||||
function checkBalances () {
|
||||
return dbm.devices()
|
||||
.then(devices => {
|
||||
const deviceIds = devices.map(r => r.device_id)
|
||||
|
|
@ -496,19 +487,19 @@ function checkBalances () {
|
|||
return R.values(R.reduceBy(min, Infinity, toMarket, R.flatten(arr)))
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
function startCheckingNotification (config) {
|
||||
function startCheckingNotification (config) {
|
||||
notifier.init(checkBalances)
|
||||
checkNotification()
|
||||
setInterval(checkNotification, CHECK_NOTIFICATION_INTERVAL)
|
||||
}
|
||||
}
|
||||
|
||||
function randomCode () {
|
||||
function randomCode () {
|
||||
return new BigNumber(crypto.randomBytes(3).toString('hex'), 16).shift(-6).toFixed(6).slice(-6)
|
||||
}
|
||||
}
|
||||
|
||||
function getPhoneCode (phone) {
|
||||
function getPhoneCode (phone) {
|
||||
const code = argv.mockSms
|
||||
? '123'
|
||||
: randomCode()
|
||||
|
|
@ -522,9 +513,9 @@ function getPhoneCode (phone) {
|
|||
|
||||
return sms.sendMessage(rec)
|
||||
.then(() => code)
|
||||
}
|
||||
}
|
||||
|
||||
function fetchPhoneTx (phone) {
|
||||
function fetchPhoneTx (phone) {
|
||||
return dbm.fetchPhoneTxs(phone, TRANSACTION_EXPIRATION)
|
||||
.then(txs => {
|
||||
const confirmedTxs = txs.filter(tx => R.contains(tx.status, ['instant', 'confirmed']))
|
||||
|
|
@ -539,9 +530,9 @@ function fetchPhoneTx (phone) {
|
|||
if (txs.length > 0) return {pending: true}
|
||||
return {}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
function sweepHD (row) {
|
||||
function sweepHD (row) {
|
||||
const cryptoCode = row.crypto_code
|
||||
|
||||
return wallet.sweep(row.hd_serial)
|
||||
|
|
@ -552,21 +543,21 @@ function sweepHD (row) {
|
|||
}
|
||||
})
|
||||
.catch(err => logger.error('[%s] Sweep error: %s', cryptoCode, err.message))
|
||||
}
|
||||
}
|
||||
|
||||
function sweepLiveHD () {
|
||||
function sweepLiveHD () {
|
||||
return dbm.fetchLiveHD()
|
||||
.then(rows => Promise.all(rows.map(sweepHD)))
|
||||
.catch(err => logger.error(err))
|
||||
}
|
||||
}
|
||||
|
||||
function sweepOldHD () {
|
||||
function sweepOldHD () {
|
||||
return dbm.fetchOldHD()
|
||||
.then(rows => Promise.all(rows.map(sweepHD)))
|
||||
.catch(err => logger.error(err))
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
return {
|
||||
pollQueries,
|
||||
trade,
|
||||
stateChange,
|
||||
|
|
@ -584,4 +575,7 @@ module.exports = {
|
|||
monitorUnnotified,
|
||||
sweepLiveHD,
|
||||
sweepOldHD
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = plugins
|
||||
|
|
|
|||
|
|
@ -10,8 +10,14 @@ const TRADE_INTERVAL = 10 * T.seconds
|
|||
const PONG_INTERVAL = 10 * T.seconds
|
||||
const PONG_CLEAR_INTERVAL = 1 * T.day
|
||||
|
||||
function start () {
|
||||
let pi = plugins
|
||||
let pi
|
||||
|
||||
function reload (settings) {
|
||||
pi = plugins(settings)
|
||||
}
|
||||
|
||||
function start (settings) {
|
||||
reload(settings)
|
||||
|
||||
pi.executeTrades()
|
||||
pi.pong()
|
||||
|
|
@ -32,4 +38,4 @@ function start () {
|
|||
setInterval(() => pi.pongClear(), PONG_CLEAR_INTERVAL)
|
||||
}
|
||||
|
||||
module.exports = {start}
|
||||
module.exports = {start, reload}
|
||||
|
|
|
|||
|
|
@ -12,6 +12,14 @@ function load (versionId) {
|
|||
}))
|
||||
}
|
||||
|
||||
function loadLatest (versionId) {
|
||||
return Promise.all([loadLatestConfig(), loadAccounts()])
|
||||
.then(([config, accounts]) => ({
|
||||
config,
|
||||
accounts
|
||||
}))
|
||||
}
|
||||
|
||||
function loadConfig (versionId) {
|
||||
const sql = `select data
|
||||
from user_config
|
||||
|
|
@ -21,6 +29,17 @@ function loadConfig (versionId) {
|
|||
.then(row => row ? row.data.config : [])
|
||||
}
|
||||
|
||||
function loadLatestConfig () {
|
||||
const sql = `select data
|
||||
from user_config
|
||||
where type=$1
|
||||
order by versionId desc
|
||||
limit 1`
|
||||
|
||||
return db.oneOrNone(sql, ['config'])
|
||||
.then(row => row ? row.data.config : [])
|
||||
}
|
||||
|
||||
function loadAccounts () {
|
||||
const toFields = fieldArr => R.fromPairs(R.map(r => [r.code, r.value], fieldArr))
|
||||
const toPairs = r => [r.code, toFields(r.fields)]
|
||||
|
|
@ -45,5 +64,6 @@ module.exports = {
|
|||
settings,
|
||||
loadConfig,
|
||||
load,
|
||||
loadLatest,
|
||||
save
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue