WIPP
This commit is contained in:
parent
4184e99273
commit
493e669e6d
5 changed files with 198 additions and 196 deletions
|
|
@ -1,9 +1,8 @@
|
||||||
const settingsLoader = require('./settings-loader')
|
|
||||||
const configManager = require('./config-manager')
|
const configManager = require('./config-manager')
|
||||||
|
|
||||||
function fetchExchange (cryptoCode) {
|
function fetchExchange (settings, cryptoCode) {
|
||||||
return settingsLoader.settings()
|
return Promise.resolve()
|
||||||
.then(settings => {
|
.then(() => {
|
||||||
const plugin = configManager.cryptoScoped(cryptoCode, settings.config).cryptoServices.wallet
|
const plugin = configManager.cryptoScoped(cryptoCode, settings.config).cryptoServices.wallet
|
||||||
if (!plugin) throw new Error('No exchange plugin defined for: ' + cryptoCode)
|
if (!plugin) throw new Error('No exchange plugin defined for: ' + cryptoCode)
|
||||||
const account = settings.accounts.plugin
|
const account = settings.accounts.plugin
|
||||||
|
|
@ -13,18 +12,18 @@ function fetchExchange (cryptoCode) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
function buy (cryptoAtoms, fiatCode, cryptoCode) {
|
function buy (settings, cryptoAtoms, fiatCode, cryptoCode) {
|
||||||
return fetchExchange(cryptoCode)
|
return fetchExchange(settings, cryptoCode)
|
||||||
.then(r => r.exchange.buy(r.account, cryptoAtoms, fiatCode, cryptoCode))
|
.then(r => r.exchange.buy(r.account, cryptoAtoms, fiatCode, cryptoCode))
|
||||||
}
|
}
|
||||||
|
|
||||||
function sell (cryptoAtoms, fiatCode, cryptoCode) {
|
function sell (settings, cryptoAtoms, fiatCode, cryptoCode) {
|
||||||
return fetchExchange(cryptoCode)
|
return fetchExchange(settings, cryptoCode)
|
||||||
.then(r => r.exchange.sell(r.account, cryptoAtoms, fiatCode, cryptoCode))
|
.then(r => r.exchange.sell(r.account, cryptoAtoms, fiatCode, cryptoCode))
|
||||||
}
|
}
|
||||||
|
|
||||||
function active (cryptoCode) {
|
function active (settings, cryptoCode) {
|
||||||
return fetchExchange(cryptoCode)
|
return fetchExchange(settings, cryptoCode)
|
||||||
.then(() => true)
|
.then(() => true)
|
||||||
.catch(() => false)
|
.catch(() => false)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
197
lib/plugins.js
197
lib/plugins.js
|
|
@ -1,5 +1,3 @@
|
||||||
'use strict'
|
|
||||||
|
|
||||||
const uuid = require('uuid')
|
const uuid = require('uuid')
|
||||||
const R = require('ramda')
|
const R = require('ramda')
|
||||||
const BigNumber = require('bignumber.js')
|
const BigNumber = require('bignumber.js')
|
||||||
|
|
@ -12,16 +10,14 @@ const db = require('./postgresql_interface')
|
||||||
const logger = require('./logger')
|
const logger = require('./logger')
|
||||||
const notifier = require('./notifier')
|
const notifier = require('./notifier')
|
||||||
const T = require('./time')
|
const T = require('./time')
|
||||||
const settingsLoader = require('./settings')
|
|
||||||
const configManager = require('./config-manager')
|
const configManager = require('./config-manager')
|
||||||
|
const settingsLoader = require('./settings-loader')
|
||||||
const ticker = require('./ticker')
|
const ticker = require('./ticker')
|
||||||
const wallet = require('./wallet')
|
const wallet = require('./wallet')
|
||||||
const exchange = require('./exchange')
|
const exchange = require('./exchange')
|
||||||
const sms = require('./sms')
|
const sms = require('./sms')
|
||||||
const email = require('./email')
|
const email = require('./email')
|
||||||
|
|
||||||
const tradeIntervals = {}
|
|
||||||
|
|
||||||
const CHECK_NOTIFICATION_INTERVAL = T.minute
|
const CHECK_NOTIFICATION_INTERVAL = T.minute
|
||||||
const ALERT_SEND_INTERVAL = T.hour
|
const ALERT_SEND_INTERVAL = T.hour
|
||||||
const INCOMING_TX_INTERVAL = 30 * T.seconds
|
const INCOMING_TX_INTERVAL = 30 * T.seconds
|
||||||
|
|
@ -36,6 +32,8 @@ const SWEEP_LIVE_HD_INTERVAL = T.minute
|
||||||
const SWEEP_OLD_HD_INTERVAL = 2 * T.minutes
|
const SWEEP_OLD_HD_INTERVAL = 2 * T.minutes
|
||||||
const TRADE_INTERVAL = T.minute
|
const TRADE_INTERVAL = T.minute
|
||||||
const TRADE_TTL = 5 * T.minutes
|
const TRADE_TTL = 5 * T.minutes
|
||||||
|
const STALE_TICKER = 3 * 60 * 1000
|
||||||
|
const STALE_BALANCE = 3 * 60 * 1000
|
||||||
|
|
||||||
const tradesQueues = {}
|
const tradesQueues = {}
|
||||||
|
|
||||||
|
|
@ -47,13 +45,46 @@ const coins = {
|
||||||
let alertFingerprint = null
|
let alertFingerprint = null
|
||||||
let lastAlertTime = null
|
let lastAlertTime = null
|
||||||
|
|
||||||
function getConfig (machineId) {
|
exports.logEvent = db.recordDeviceEvent
|
||||||
const config = settingsLoader.settings().config
|
|
||||||
return configManager.machineScoped(machineId, config)
|
function buildRates (settings, deviceId, tickers) {
|
||||||
|
const config = configManager.machineScoped(deviceId, settings.config)
|
||||||
|
const cryptoCodes = config.currencies.cryptoCurrencies
|
||||||
|
|
||||||
|
const cashInCommission = new BigNumber(config.commissions.cashInCommission).div(100).plus(1)
|
||||||
|
const cashOutCommission = new BigNumber(config.commissions.cashOutCommission).div(100).plus(1)
|
||||||
|
|
||||||
|
const rates = {}
|
||||||
|
|
||||||
|
cryptoCodes.forEach((cryptoCode, i) => {
|
||||||
|
const rateRec = tickers[i]
|
||||||
|
if (Date.now() - rateRec.timestamp > STALE_TICKER) return logger.warn('Stale rate for ' + cryptoCode)
|
||||||
|
const rate = rateRec.rates
|
||||||
|
rates[cryptoCode] = {
|
||||||
|
cashIn: rate.ask.times(cashInCommission),
|
||||||
|
cashOut: rate.bid.div(cashOutCommission)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
return rates
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.getConfig = getConfig
|
function buildBalances (settings, deviceId, balanceRecs) {
|
||||||
exports.logEvent = db.recordDeviceEvent
|
const config = configManager.machineScoped(deviceId, settings.config)
|
||||||
|
const cryptoCodes = config.currencies.cryptoCurrencies
|
||||||
|
|
||||||
|
const balances = {}
|
||||||
|
|
||||||
|
cryptoCodes.forEach((cryptoCode, i) => {
|
||||||
|
const balanceRec = balanceRecs[i]
|
||||||
|
if (!balanceRec) return logger.warn('No balance for ' + cryptoCode + ' yet')
|
||||||
|
if (Date.now() - balanceRec.timestamp > STALE_BALANCE) return logger.warn('Stale balance for ' + cryptoCode)
|
||||||
|
|
||||||
|
balances[cryptoCode] = balanceRec.balance
|
||||||
|
})
|
||||||
|
|
||||||
|
return balances
|
||||||
|
}
|
||||||
|
|
||||||
function buildCartridges (cartridges, virtualCartridges, rec) {
|
function buildCartridges (cartridges, virtualCartridges, rec) {
|
||||||
return {
|
return {
|
||||||
|
|
@ -71,16 +102,32 @@ function buildCartridges (cartridges, virtualCartridges, rec) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.pollQueries = function pollQueries (deviceId) {
|
exports.pollQueries = function pollQueries (settings, deviceTime, deviceId, deviceRec) {
|
||||||
const config = getConfig(deviceId)
|
const config = configManager.machineScoped(deviceId, settings.config)
|
||||||
|
const fiatCode = config.currencies.fiatCurrency
|
||||||
|
const cryptoCodes = config.currencies.cryptoCurrencies
|
||||||
const cartridges = [ config.currencies.topCashOutDenomination,
|
const cartridges = [ config.currencies.topCashOutDenomination,
|
||||||
config.currencies.bottomCashOutDenomination ]
|
config.currencies.bottomCashOutDenomination ]
|
||||||
const virtualCartridges = [config.currencies.virtualCashOutDenomination]
|
const virtualCartridges = [config.currencies.virtualCashOutDenomination]
|
||||||
|
|
||||||
return db.cartridgeCounts(deviceId)
|
const tickerPromises = cryptoCodes.map(c => ticker.getRates(settings, fiatCode, c))
|
||||||
.then(result => ({
|
const balancePromises = cryptoCodes.map(c => wallet.balance(settings, c))
|
||||||
cartridges: buildCartridges(cartridges, virtualCartridges, result)
|
const pingPromise = recordPing(deviceId, deviceTime, deviceRec)
|
||||||
}))
|
|
||||||
|
const promises = [db.cartridgeCounts(deviceId), pingPromise].concat(tickerPromises, balancePromises)
|
||||||
|
|
||||||
|
return Promise.all(promises)
|
||||||
|
.then(arr => {
|
||||||
|
const cartridgeCounts = arr[0]
|
||||||
|
const tickers = arr.slice(2, cryptoCodes.length + 2)
|
||||||
|
const balances = arr.slice(cryptoCodes.length + 2)
|
||||||
|
|
||||||
|
return {
|
||||||
|
cartridges: buildCartridges(cartridges, virtualCartridges, cartridgeCounts),
|
||||||
|
rates: buildRates(settings, deviceId, tickers),
|
||||||
|
balances: buildBalances(settings, deviceId, balances)
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// NOTE: This will fail if we have already sent coins because there will be
|
// NOTE: This will fail if we have already sent coins because there will be
|
||||||
|
|
@ -137,7 +184,7 @@ exports.stateChange = function stateChange (deviceId, deviceTime, rec, cb) {
|
||||||
return db.machineEvent(event)
|
return db.machineEvent(event)
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.recordPing = function recordPing (deviceId, deviceTime, rec, cb) {
|
function recordPing (deviceId, deviceTime, rec) {
|
||||||
const event = {
|
const event = {
|
||||||
id: uuid.v4(),
|
id: uuid.v4(),
|
||||||
deviceId: deviceId,
|
deviceId: deviceId,
|
||||||
|
|
@ -177,17 +224,16 @@ exports.cashOut = function cashOut (deviceId, tx) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.dispenseAck = function (deviceId, tx) {
|
exports.dispenseAck = function (settings, deviceId, tx) {
|
||||||
const config = getConfig(deviceId)
|
const config = configManager.machineScoped(deviceId, settings.config)
|
||||||
const cartridges = [ config.currencies.topCashOutDenomination,
|
const cartridges = [ config.currencies.topCashOutDenomination,
|
||||||
config.currencies.bottomCashOutDenomination ]
|
config.currencies.bottomCashOutDenomination ]
|
||||||
|
|
||||||
return db.addDispense(deviceId, tx, cartridges)
|
return db.addDispense(deviceId, tx, cartridges)
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.fiatBalance = function fiatBalance (fiatCode, cryptoCode, deviceId) {
|
function fiatBalance (settings, fiatCode, cryptoCode, deviceId) {
|
||||||
const _config = settingsLoader.settings().config
|
const config = configManager.scoped(cryptoCode, deviceId, settings.config)
|
||||||
const config = configManager.scoped(cryptoCode, deviceId, _config)
|
|
||||||
|
|
||||||
return Promise.all([ticker.ticker(cryptoCode), wallet.balance(cryptoCode)])
|
return Promise.all([ticker.ticker(cryptoCode), wallet.balance(cryptoCode)])
|
||||||
.then(([rates, balanceRec]) => {
|
.then(([rates, balanceRec]) => {
|
||||||
|
|
@ -257,11 +303,7 @@ function monitorUnnotified () {
|
||||||
exports.startPolling = function startPolling () {
|
exports.startPolling = function startPolling () {
|
||||||
executeTrades()
|
executeTrades()
|
||||||
|
|
||||||
const cryptoCodes = getAllCryptoCodes()
|
setInterval(executeTrades, TRADE_INTERVAL)
|
||||||
cryptoCodes.forEach(cryptoCode => {
|
|
||||||
startTrader(cryptoCode)
|
|
||||||
})
|
|
||||||
|
|
||||||
setInterval(monitorLiveIncoming, LIVE_INCOMING_TX_INTERVAL)
|
setInterval(monitorLiveIncoming, LIVE_INCOMING_TX_INTERVAL)
|
||||||
setInterval(monitorIncoming, INCOMING_TX_INTERVAL)
|
setInterval(monitorIncoming, INCOMING_TX_INTERVAL)
|
||||||
setInterval(monitorUnnotified, UNNOTIFIED_INTERVAL)
|
setInterval(monitorUnnotified, UNNOTIFIED_INTERVAL)
|
||||||
|
|
@ -275,14 +317,6 @@ exports.startPolling = function startPolling () {
|
||||||
sweepOldHD()
|
sweepOldHD()
|
||||||
}
|
}
|
||||||
|
|
||||||
function startTrader (cryptoCode) {
|
|
||||||
if (tradeIntervals[cryptoCode]) return
|
|
||||||
|
|
||||||
logger.debug('[%s] startTrader', cryptoCode)
|
|
||||||
|
|
||||||
tradeIntervals[cryptoCode] = setInterval(() => executeTrades(cryptoCode), TRADE_INTERVAL)
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Trader functions
|
* Trader functions
|
||||||
*/
|
*/
|
||||||
|
|
@ -325,7 +359,30 @@ function consolidateTrades (cryptoCode, fiatCode) {
|
||||||
return consolidatedTrade
|
return consolidatedTrade
|
||||||
}
|
}
|
||||||
|
|
||||||
function executeTrades (cryptoCode, fiatCode) {
|
function executeTrades () {
|
||||||
|
return settingsLoader()
|
||||||
|
.then(settings => {
|
||||||
|
const config = settings.config
|
||||||
|
return db.devices()
|
||||||
|
.then(devices => {
|
||||||
|
const deviceIds = devices.map(device => device.device_id)
|
||||||
|
const lists = deviceIds.map(deviceId => {
|
||||||
|
const currencies = configManager.machineScoped(deviceId, config).currencies
|
||||||
|
const fiatCode = currencies.fiatCurrency
|
||||||
|
const cryptoCodes = currencies.cryptoCurrencies
|
||||||
|
return cryptoCodes.map(cryptoCode => ({fiatCode, cryptoCode}))
|
||||||
|
})
|
||||||
|
|
||||||
|
const tradesPromises = R.uniq(R.flatten(lists))
|
||||||
|
.map(r => executeTradesForMarket(settings, r.fiatCode, r.cryptoCode))
|
||||||
|
|
||||||
|
return Promise.all(tradesPromises)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.catch(logger.error)
|
||||||
|
}
|
||||||
|
|
||||||
|
function executeTradesForMarket (settings, fiatCode, cryptoCode) {
|
||||||
const market = [fiatCode, cryptoCode].join('')
|
const market = [fiatCode, cryptoCode].join('')
|
||||||
logger.debug('[%s] checking for trades', market)
|
logger.debug('[%s] checking for trades', market)
|
||||||
|
|
||||||
|
|
@ -405,59 +462,35 @@ function checkNotification () {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
function getCryptoCodes (deviceId) {
|
function checkDeviceBalances (settings, deviceId) {
|
||||||
return settingsLoader.settings()
|
const config = configManager.machineScoped(deviceId, settings.config)
|
||||||
.then(settings => {
|
const cryptoCodes = config.currencies.cryptoCurrencies
|
||||||
return configManager.machineScoped(deviceId, settings.config).currencies.cryptoCurrencies
|
const fiatCode = config.currencies.fiatCurrency
|
||||||
})
|
const fiatBalancePromises = cryptoCodes.map(c => fiatBalance(settings, fiatCode, c, deviceId))
|
||||||
}
|
|
||||||
exports.getCryptoCodes = getCryptoCodes
|
|
||||||
|
|
||||||
// Get union of all cryptoCodes from all machines
|
return Promise.all(fiatBalancePromises)
|
||||||
function getAllCryptoCodes () {
|
.then(arr => {
|
||||||
return Promise.all([db.devices(), settingsLoader.settings()])
|
return arr.map((balance, i) => ({
|
||||||
.then(([rows, settings]) => {
|
fiatBalance: balance,
|
||||||
return rows.reduce((acc, r) => {
|
cryptoCode: cryptoCodes[i],
|
||||||
const cryptoCodes = configManager.machineScoped(r.device_id, settings.config).currencies.cryptoCurrencies
|
fiatCode,
|
||||||
cryptoCodes.forEach(c => acc.add(c))
|
deviceId
|
||||||
return acc
|
}))
|
||||||
}, new Set())
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
function getAllMarkets () {
|
function checkBalances (settings) {
|
||||||
return Promise.all([db.devices(), settingsLoader.settings()])
|
return db.devices()
|
||||||
.then(([rows, settings]) => {
|
.then(devices => {
|
||||||
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([getAllMarkets(), db.devices()])
|
|
||||||
.then(([markets, devices]) => {
|
|
||||||
const deviceIds = devices.map(r => r.device_id)
|
const deviceIds = devices.map(r => r.device_id)
|
||||||
const balances = []
|
const deviceBalancePromises = deviceIds.map(deviceId => checkDeviceBalances(settings, deviceId))
|
||||||
|
|
||||||
markets.forEach(market => {
|
return Promise.all(deviceBalancePromises)
|
||||||
const fiatCode = market.fiatCode
|
.then(arr => {
|
||||||
const cryptoCode = market.cryptoCode
|
const toMarket = r => r.fiatBalance + r.cryptoCode
|
||||||
const minBalance = deviceIds.map(deviceId => {
|
const min = R.minBy(r => r.fiatBalance)
|
||||||
const fiatBalanceRec = exports.fiatBalance(fiatCode, cryptoCode, deviceId)
|
return R.reduceBy(min, Infinity, toMarket, R.flatten(arr))
|
||||||
return fiatBalanceRec ? fiatBalanceRec.balance : Infinity
|
|
||||||
})
|
|
||||||
.reduce((min, cur) => Math.min(min, cur), Infinity)
|
|
||||||
|
|
||||||
const rec = {fiatBalance: minBalance, cryptoCode, fiatCode}
|
|
||||||
balances.push(rec)
|
|
||||||
})
|
})
|
||||||
|
|
||||||
return balances
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
139
lib/routes.js
139
lib/routes.js
|
|
@ -8,6 +8,7 @@ const logger = require('./logger')
|
||||||
const configManager = require('./config-manager')
|
const configManager = require('./config-manager')
|
||||||
const db = require('./db')
|
const db = require('./db')
|
||||||
const pairing = require('./pairing')
|
const pairing = require('./pairing')
|
||||||
|
const settingsLoader = require('./settings')
|
||||||
|
|
||||||
let plugins
|
let plugins
|
||||||
|
|
||||||
|
|
@ -15,68 +16,31 @@ module.exports = {
|
||||||
init
|
init
|
||||||
}
|
}
|
||||||
|
|
||||||
const STALE_TICKER = 3 * 60 * 1000
|
|
||||||
const STALE_BALANCE = 3 * 60 * 1000
|
|
||||||
const CLOCK_SKEW = 60 * 1000
|
const CLOCK_SKEW = 60 * 1000
|
||||||
const REQUEST_TTL = 3 * 60 * 1000
|
const REQUEST_TTL = 3 * 60 * 1000
|
||||||
|
|
||||||
const pids = {}
|
const pids = {}
|
||||||
const reboots = {}
|
const reboots = {}
|
||||||
|
|
||||||
function buildRates (deviceId) {
|
function loadSettings (req, res, next) {
|
||||||
const cryptoCodes = plugins.getCryptoCodes()
|
settingsLoader.settings()
|
||||||
const config = plugins.getConfig(deviceId)
|
.then(settings => {
|
||||||
|
req.settings = settings
|
||||||
const cashInCommission = new BigNumber(config.commissions.cashInCommission).div(100).plus(1)
|
next()
|
||||||
const cashOutCommission = new BigNumber(config.commissions.cashOutCommission).div(100).plus(1)
|
|
||||||
|
|
||||||
const rates = {}
|
|
||||||
cryptoCodes.forEach(cryptoCode => {
|
|
||||||
const _rate = plugins.getDeviceRate(cryptoCode)
|
|
||||||
if (!_rate) return logger.warn('No rate for ' + cryptoCode + ' yet')
|
|
||||||
if (Date.now() - _rate.timestamp > STALE_TICKER) return logger.warn('Stale rate for ' + cryptoCode)
|
|
||||||
const rate = _rate.rates
|
|
||||||
rates[cryptoCode] = {
|
|
||||||
cashIn: rate.ask.times(cashInCommission),
|
|
||||||
cashOut: rate.bid.div(cashOutCommission)
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
.catch(next)
|
||||||
return rates
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function buildBalances (deviceId) {
|
function poll (req, res, next) {
|
||||||
const cryptoCodes = plugins.getCryptoCodes(deviceId)
|
|
||||||
const fiatCode = plugins.getFiatCode(deviceId)
|
|
||||||
const _balances = {}
|
|
||||||
|
|
||||||
cryptoCodes.forEach(cryptoCode => {
|
|
||||||
const balanceRec = plugins.fiatBalance(fiatCode, cryptoCode, deviceId)
|
|
||||||
if (!balanceRec) return logger.warn('No balance for ' + cryptoCode + ' yet')
|
|
||||||
if (Date.now() - balanceRec.timestamp > STALE_BALANCE) return logger.warn('Stale balance for ' + cryptoCode)
|
|
||||||
|
|
||||||
_balances[cryptoCode] = balanceRec.balance
|
|
||||||
})
|
|
||||||
|
|
||||||
return _balances
|
|
||||||
}
|
|
||||||
|
|
||||||
function poll (req, res) {
|
|
||||||
const deviceId = req.deviceId
|
const deviceId = req.deviceId
|
||||||
const deviceTime = req.deviceTime
|
const deviceTime = req.deviceTime
|
||||||
const pid = req.query.pid
|
const pid = req.query.pid
|
||||||
|
const settings = req.settings
|
||||||
|
const config = configManager.machineScoped(deviceId, settings.config)
|
||||||
|
|
||||||
pids[deviceId] = {pid, ts: Date.now()}
|
pids[deviceId] = {pid, ts: Date.now()}
|
||||||
|
|
||||||
let rates = {}
|
plugins.pollQueries(settings, deviceTime, deviceId, req.query)
|
||||||
let balances = {}
|
|
||||||
|
|
||||||
rates = buildRates(deviceId)
|
|
||||||
balances = buildBalances(deviceId)
|
|
||||||
|
|
||||||
const config = plugins.getConfig(deviceId)
|
|
||||||
|
|
||||||
plugins.pollQueries(deviceId)
|
|
||||||
.then(results => {
|
.then(results => {
|
||||||
const cartridges = results.cartridges
|
const cartridges = results.cartridges
|
||||||
|
|
||||||
|
|
@ -102,8 +66,8 @@ function poll (req, res) {
|
||||||
zeroConfLimit: config.commissions.zeroConfLimit,
|
zeroConfLimit: config.commissions.zeroConfLimit,
|
||||||
fiatTxLimit: config.limits.cashOutTransactionLimit,
|
fiatTxLimit: config.limits.cashOutTransactionLimit,
|
||||||
reboot,
|
reboot,
|
||||||
rates,
|
rates: results.rates,
|
||||||
balances,
|
balances: results.balances,
|
||||||
coins: config.currencies.cryptoCurrencies
|
coins: config.currencies.cryptoCurrencies
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -111,32 +75,31 @@ function poll (req, res) {
|
||||||
response.idVerificationLimit = config.compliance.idVerificationLimit
|
response.idVerificationLimit = config.compliance.idVerificationLimit
|
||||||
}
|
}
|
||||||
|
|
||||||
res.json(response)
|
return res.json(response)
|
||||||
})
|
})
|
||||||
.catch(e => { console.log(e); logger.error(e) })
|
.catch(next)
|
||||||
|
|
||||||
plugins.recordPing(deviceId, deviceTime, req.query)
|
|
||||||
.catch(logger.error)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function trade (req, res, next) {
|
function trade (req, res, next) {
|
||||||
const tx = req.body
|
const tx = req.body
|
||||||
tx.cryptoAtoms = new BigNumber(tx.cryptoAtoms)
|
tx.cryptoAtoms = new BigNumber(tx.cryptoAtoms)
|
||||||
|
|
||||||
plugins.trade(req.deviceId, tx)
|
plugins.trade(req.settings, req.deviceId, tx)
|
||||||
.then(() => cacheAndRespond(req, res))
|
.then(() => cacheAndRespond(req, res))
|
||||||
|
.catch(next)
|
||||||
}
|
}
|
||||||
|
|
||||||
function stateChange (req, res) {
|
function stateChange (req, res, next) {
|
||||||
plugins.stateChange(req.deviceId, req.deviceTime, req.body)
|
plugins.stateChange(req.settings, req.deviceId, req.deviceTime, req.body)
|
||||||
.then(() => cacheAndRespond(req, res))
|
.then(() => cacheAndRespond(req, res))
|
||||||
|
.catch(next)
|
||||||
}
|
}
|
||||||
|
|
||||||
function send (req, res, next) {
|
function send (req, res, next) {
|
||||||
const tx = req.body
|
const tx = req.body
|
||||||
tx.cryptoAtoms = new BigNumber(tx.cryptoAtoms)
|
tx.cryptoAtoms = new BigNumber(tx.cryptoAtoms)
|
||||||
|
|
||||||
return plugins.sendCoins(req.deviceId, tx)
|
return plugins.sendCoins(req.settings, req.deviceId, tx)
|
||||||
.then(status => {
|
.then(status => {
|
||||||
const body = {txId: status && status.txId}
|
const body = {txId: status && status.txId}
|
||||||
return cacheAndRespond(req, res, body)
|
return cacheAndRespond(req, res, body)
|
||||||
|
|
@ -144,33 +107,38 @@ function send (req, res, next) {
|
||||||
.catch(next)
|
.catch(next)
|
||||||
}
|
}
|
||||||
|
|
||||||
function cashOut (req, res) {
|
function cashOut (req, res, next) {
|
||||||
logger.info({tx: req.body, cmd: 'cashOut'})
|
logger.info({tx: req.body, cmd: 'cashOut'})
|
||||||
const tx = req.body
|
const tx = req.body
|
||||||
tx.cryptoAtoms = new BigNumber(tx.cryptoAtoms)
|
tx.cryptoAtoms = new BigNumber(tx.cryptoAtoms)
|
||||||
|
|
||||||
return plugins.cashOut(req.deviceId, tx)
|
return plugins.cashOut(req.settings, req.deviceId, tx)
|
||||||
.then(cryptoAddress => cacheAndRespond(req, res, {toAddress: cryptoAddress}))
|
.then(cryptoAddress => cacheAndRespond(req, res, {toAddress: cryptoAddress}))
|
||||||
|
.catch(next)
|
||||||
}
|
}
|
||||||
|
|
||||||
function dispenseAck (req, res) {
|
function dispenseAck (req, res, next) {
|
||||||
plugins.dispenseAck(req.deviceId, req.body.tx)
|
plugins.dispenseAck(req.settings, req.deviceId, req.body.tx)
|
||||||
.then(() => cacheAndRespond(req, res))
|
.then(() => cacheAndRespond(req, res))
|
||||||
|
.catch(next)
|
||||||
}
|
}
|
||||||
|
|
||||||
function deviceEvent (req, res) {
|
function deviceEvent (req, res, next) {
|
||||||
plugins.logEvent(req.deviceId, req.body)
|
plugins.logEvent(req.deviceId, req.body)
|
||||||
.then(() => cacheAndRespond(req, res))
|
.then(() => cacheAndRespond(req, res))
|
||||||
|
.catch(next)
|
||||||
}
|
}
|
||||||
|
|
||||||
function verifyUser (req, res) {
|
function verifyUser (req, res, next) {
|
||||||
plugins.verifyUser(req.body)
|
plugins.verifyUser(req.settings, req.body)
|
||||||
.then(idResult => cacheAndRespond(req, res, idResult))
|
.then(idResult => cacheAndRespond(req, res, idResult))
|
||||||
|
.catch(next)
|
||||||
}
|
}
|
||||||
|
|
||||||
function verifyTx (req, res) {
|
function verifyTx (req, res, next) {
|
||||||
plugins.verifyTransaction(req.body)
|
plugins.verifyTransaction(req.settings, req.body)
|
||||||
.then(idResult => cacheAndRespond(req, res, idResult))
|
.then(idResult => cacheAndRespond(req, res, idResult))
|
||||||
|
.catch(next)
|
||||||
}
|
}
|
||||||
|
|
||||||
function ca (req, res) {
|
function ca (req, res) {
|
||||||
|
|
@ -193,36 +161,38 @@ function pair (req, res, next) {
|
||||||
.catch(next)
|
.catch(next)
|
||||||
}
|
}
|
||||||
|
|
||||||
function phoneCode (req, res) {
|
function phoneCode (req, res, next) {
|
||||||
const phone = req.body.phone
|
const phone = req.body.phone
|
||||||
|
|
||||||
logger.debug('Phone code requested for: ' + phone)
|
return plugins.getPhoneCode(req.settings, phone)
|
||||||
|
|
||||||
return plugins.getPhoneCode(phone)
|
|
||||||
.then(code => cacheAndRespond(req, res, {code}))
|
.then(code => cacheAndRespond(req, res, {code}))
|
||||||
.catch(err => {
|
.catch(err => {
|
||||||
if (err.name === 'BadNumberError') throw httpError('Bad number', 410)
|
if (err.name === 'BadNumberError') throw httpError('Bad number', 410)
|
||||||
throw err
|
throw err
|
||||||
})
|
})
|
||||||
|
.catch(next)
|
||||||
}
|
}
|
||||||
|
|
||||||
function updatePhone (req, res) {
|
function updatePhone (req, res, next) {
|
||||||
const notified = req.query.notified === 'true'
|
const notified = req.query.notified === 'true'
|
||||||
const tx = req.body
|
const tx = req.body
|
||||||
|
|
||||||
return plugins.updatePhone(tx, notified)
|
return plugins.updatePhone(tx, notified)
|
||||||
.then(r => cacheAndRespond(req, res, r))
|
.then(r => cacheAndRespond(req, res, r))
|
||||||
|
.catch(next)
|
||||||
}
|
}
|
||||||
|
|
||||||
function fetchPhoneTx (req, res) {
|
function fetchPhoneTx (req, res, next) {
|
||||||
return plugins.fetchPhoneTx(req.query.phone)
|
return plugins.fetchPhoneTx(req.query.phone)
|
||||||
.then(r => res.json(r))
|
.then(r => res.json(r))
|
||||||
|
.catch(next)
|
||||||
}
|
}
|
||||||
|
|
||||||
function registerRedeem (req, res) {
|
function registerRedeem (req, res, next) {
|
||||||
const txId = req.params.txId
|
const txId = req.params.txId
|
||||||
return plugins.registerRedeem(txId)
|
return plugins.registerRedeem(txId)
|
||||||
.then(() => cacheAndRespond(req, res))
|
.then(() => cacheAndRespond(req, res))
|
||||||
|
.catch(next)
|
||||||
}
|
}
|
||||||
|
|
||||||
function waitForDispense (req, res, next) {
|
function waitForDispense (req, res, next) {
|
||||||
|
|
@ -238,11 +208,12 @@ function waitForDispense (req, res, next) {
|
||||||
.catch(next)
|
.catch(next)
|
||||||
}
|
}
|
||||||
|
|
||||||
function dispense (req, res) {
|
function dispense (req, res, next) {
|
||||||
const tx = req.body.tx
|
const tx = req.body.tx
|
||||||
|
|
||||||
return plugins.requestDispense(tx)
|
return plugins.requestDispense(tx)
|
||||||
.then(dispenseRec => cacheAndRespond(req, res, dispenseRec))
|
.then(dispenseRec => cacheAndRespond(req, res, dispenseRec))
|
||||||
|
.catch(next)
|
||||||
}
|
}
|
||||||
|
|
||||||
function isUniqueViolation (err) {
|
function isUniqueViolation (err) {
|
||||||
|
|
@ -363,19 +334,19 @@ function init (opts) {
|
||||||
app.post('/pair', pair)
|
app.post('/pair', pair)
|
||||||
app.get('/ca', ca)
|
app.get('/ca', ca)
|
||||||
|
|
||||||
app.get('/poll', authMiddleware, poll)
|
app.get('/poll', authMiddleware, loadSettings, poll)
|
||||||
|
|
||||||
app.post('/trade', authMiddleware, trade)
|
app.post('/trade', authMiddleware, loadSettings, trade)
|
||||||
app.post('/send', authMiddleware, send)
|
app.post('/send', authMiddleware, loadSettings, send)
|
||||||
app.post('/state', authMiddleware, stateChange)
|
app.post('/state', authMiddleware, loadSettings, stateChange)
|
||||||
app.post('/cash_out', authMiddleware, cashOut)
|
app.post('/cash_out', authMiddleware, loadSettings, cashOut)
|
||||||
app.post('/dispense_ack', authMiddleware, dispenseAck)
|
app.post('/dispense_ack', authMiddleware, loadSettings, dispenseAck)
|
||||||
|
|
||||||
app.post('/event', authMiddleware, deviceEvent)
|
app.post('/event', authMiddleware, deviceEvent)
|
||||||
app.post('/verify_user', authMiddleware, verifyUser)
|
app.post('/verify_user', authMiddleware, loadSettings, verifyUser)
|
||||||
app.post('/verify_transaction', authMiddleware, verifyTx)
|
app.post('/verify_transaction', authMiddleware, loadSettings, verifyTx)
|
||||||
|
|
||||||
app.post('/phone_code', authMiddleware, phoneCode)
|
app.post('/phone_code', authMiddleware, loadSettings, phoneCode)
|
||||||
app.post('/update_phone', authMiddleware, updatePhone)
|
app.post('/update_phone', authMiddleware, updatePhone)
|
||||||
app.get('/phone_tx', authMiddleware, fetchPhoneTx)
|
app.get('/phone_tx', authMiddleware, fetchPhoneTx)
|
||||||
app.post('/register_redeem/:txId', authMiddleware, registerRedeem)
|
app.post('/register_redeem/:txId', authMiddleware, registerRedeem)
|
||||||
|
|
|
||||||
|
|
@ -1,20 +1,20 @@
|
||||||
const mem = require('mem')
|
const mem = require('mem')
|
||||||
const settingsLoader = require('./settings-loader')
|
|
||||||
const configManager = require('./config-manager')
|
const configManager = require('./config-manager')
|
||||||
|
|
||||||
const FETCH_INTERVAL = 10000
|
const FETCH_INTERVAL = 10000
|
||||||
function getRates (fiatCode, cryptoCode) {
|
function getRates (settings, fiatCode, cryptoCode) {
|
||||||
return Promise.resolve()
|
return Promise.resolve()
|
||||||
.then(() => {
|
.then(() => {
|
||||||
return settingsLoader.settings()
|
const config = settings.config
|
||||||
.then(settings => {
|
const plugin = configManager.cryptoScoped(cryptoCode, config).cryptoServices.ticker
|
||||||
const config = settings.config
|
const account = settings.accounts.plugin
|
||||||
const plugin = configManager.cryptoScoped(cryptoCode, config).cryptoServices.ticker
|
const ticker = require('lamassu-' + plugin)
|
||||||
const account = settings.accounts.plugin
|
|
||||||
const ticker = require('lamassu-' + plugin)
|
|
||||||
|
|
||||||
return ticker.ticker(account, fiatCode, cryptoCode)
|
return ticker.ticker(account, fiatCode, cryptoCode)
|
||||||
})
|
.then(r => ({
|
||||||
|
rates: r.rates,
|
||||||
|
timestamp: Date.now()
|
||||||
|
}))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,11 @@
|
||||||
const mem = require('mem')
|
const mem = require('mem')
|
||||||
const settingsLoader = require('./settings-loader')
|
|
||||||
const configManager = require('./config-manager')
|
const configManager = require('./config-manager')
|
||||||
|
|
||||||
const FETCH_INTERVAL = 5000
|
const FETCH_INTERVAL = 5000
|
||||||
|
|
||||||
function fetchWallet (cryptoCode) {
|
function fetchWallet (settings, cryptoCode) {
|
||||||
return settingsLoader.settings()
|
return Promise.resolve()
|
||||||
.then(settings => {
|
.then(() => {
|
||||||
const plugin = configManager.cryptoScoped(cryptoCode, settings.config).cryptoServices.wallet
|
const plugin = configManager.cryptoScoped(cryptoCode, settings.config).cryptoServices.wallet
|
||||||
const account = settings.accounts.plugin
|
const account = settings.accounts.plugin
|
||||||
const wallet = require('lamassu-' + plugin)
|
const wallet = require('lamassu-' + plugin)
|
||||||
|
|
@ -15,8 +14,8 @@ function fetchWallet (cryptoCode) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
function balance (cryptoCode) {
|
function balance (settings, cryptoCode) {
|
||||||
return fetchWallet(cryptoCode)
|
return fetchWallet(settings, cryptoCode)
|
||||||
.then(r => r.wallet.balance(r.account))
|
.then(r => r.wallet.balance(r.account))
|
||||||
.then(balance => ({balance, timestamp: Date.now()}))
|
.then(balance => ({balance, timestamp: Date.now()}))
|
||||||
}
|
}
|
||||||
|
|
@ -32,13 +31,13 @@ function sendCoins (toAddress, cryptoAtoms, cryptoCode) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
function newAddress (cryptoCode, info) {
|
function newAddress (settings, cryptoCode, info) {
|
||||||
return fetchWallet(cryptoCode)
|
return fetchWallet(settings, cryptoCode)
|
||||||
.then(r => r.wallet.newAddress(r.account, cryptoCode, info))
|
.then(r => r.wallet.newAddress(r.account, cryptoCode, info))
|
||||||
}
|
}
|
||||||
|
|
||||||
function getStatus (toAdress, cryptoAtoms, cryptoCode) {
|
function getStatus (settings, toAdress, cryptoAtoms, cryptoCode) {
|
||||||
return fetchWallet(cryptoCode)
|
return fetchWallet(settings, cryptoCode)
|
||||||
.then(r => r.wallet.getStatus(r.account, toAdress, cryptoAtoms, cryptoCode))
|
.then(r => r.wallet.getStatus(r.account, toAdress, cryptoAtoms, cryptoCode))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue