From 7bb071fc9525a18425a571719a349cfb25028627 Mon Sep 17 00:00:00 2001 From: Josh Harvey Date: Sun, 27 Nov 2016 20:14:36 +0200 Subject: [PATCH] WIPP --- lib/plugins.js | 46 +++++++++++++++++++++++----------------------- lib/routes.js | 9 +++++---- lib/wallet.js | 2 +- 3 files changed, 29 insertions(+), 28 deletions(-) diff --git a/lib/plugins.js b/lib/plugins.js index 9833420f..a45c1a03 100644 --- a/lib/plugins.js +++ b/lib/plugins.js @@ -45,8 +45,6 @@ const coins = { let alertFingerprint = null let lastAlertTime = null -exports.logEvent = db.recordDeviceEvent - function buildRates (deviceId, tickers) { const settings = settingsLoader.settings const config = configManager.machineScoped(deviceId, settings.config) @@ -104,7 +102,7 @@ function buildCartridges (cartridges, virtualCartridges, rec) { } } -exports.pollQueries = function pollQueries (deviceTime, deviceId, deviceRec) { +function pollQueries (deviceTime, deviceId, deviceRec) { const settings = settingsLoader.settings const config = configManager.machineScoped(deviceId, settings.config) const fiatCode = config.currencies.fiatCurrency @@ -151,8 +149,7 @@ function executeTx (deviceId, tx) { }) } -// TODO: Run these in parallel and return success -exports.trade = function trade (deviceId, rawTrade) { +function trade (deviceId, rawTrade) { // TODO: move this to DB, too // add bill to trader queue (if trader is enabled) const cryptoCode = rawTrade.cryptoCode @@ -176,7 +173,7 @@ exports.trade = function trade (deviceId, rawTrade) { }) } -exports.stateChange = function stateChange (deviceId, deviceTime, rec, cb) { +function stateChange (deviceId, deviceTime, rec, cb) { const event = { id: rec.uuid, deviceId: deviceId, @@ -198,11 +195,11 @@ function recordPing (deviceId, deviceTime, rec) { return db.machineEvent(event) } -exports.sendCoins = function sendCoins (deviceId, rawTx) { +function sendCoins (deviceId, rawTx) { return executeTx(deviceId, rawTx) } -exports.cashOut = function cashOut (deviceId, tx) { +function cashOut (deviceId, tx) { const cryptoCode = tx.cryptoCode const serialPromise = wallet.supportsHD @@ -227,7 +224,7 @@ exports.cashOut = function cashOut (deviceId, tx) { }) } -exports.dispenseAck = function (deviceId, tx) { +function dispenseAck (deviceId, tx) { const settings = settingsLoader.settings const config = configManager.machineScoped(deviceId, settings.config) const cartridges = [ config.currencies.topCashOutDenomination, @@ -307,7 +304,7 @@ function monitorUnnotified () { /* * Polling livecycle */ -exports.startPolling = function startPolling () { +function startPolling () { executeTrades() setInterval(executeTrades, TRADE_INTERVAL) @@ -413,7 +410,6 @@ function executeTradesForMarket (settings, fiatCode, cryptoCode) { function sendMessage (rec) { return Promise.all([sms.sendMessage(rec), email.sendMessage(rec)]) } -exports.sendMessage = sendMessage function sendNoAlerts () { const subject = '[Lamassu] All clear' @@ -500,13 +496,13 @@ function checkBalances () { }) } -exports.startCheckingNotification = function startCheckingNotification (config) { +function startCheckingNotification (config) { notifier.init(checkBalances) checkNotification() setInterval(checkNotification, CHECK_NOTIFICATION_INTERVAL) } -exports.getPhoneCode = function getPhoneCode (phone) { +function getPhoneCode (phone) { return sms.name() .then(name => { const code = name === 'MockSMS' @@ -525,10 +521,7 @@ exports.getPhoneCode = function getPhoneCode (phone) { }) } -exports.updatePhone = db.addIncomingPhone -exports.registerRedeem = db.updateRedeem - -exports.fetchPhoneTx = function fetchPhoneTx (phone) { +function fetchPhoneTx (phone) { return db.fetchPhoneTxs(phone, TRANSACTION_EXPIRATION) .then(txs => { const confirmedTxs = txs.filter(tx => R.contains(tx.status, ['instant', 'confirmed'])) @@ -545,12 +538,6 @@ exports.fetchPhoneTx = function fetchPhoneTx (phone) { }) } -exports.requestDispense = function requestDispense (tx) { - return db.addDispenseRequest(tx) -} - -exports.fetchTx = db.fetchTx - function sweepHD (row) { const cryptoCode = row.crypto_code @@ -575,3 +562,16 @@ function sweepOldHD () { .then(rows => Promise.all(rows.map(sweepHD))) .catch(err => logger.error(err)) } + +module.exports = { + pollQueries, + trade, + stateChange, + sendCoins, + cashOut, + dispenseAck, + startPolling, + startCheckingNotification, + getPhoneCode, + fetchPhoneTx +} diff --git a/lib/routes.js b/lib/routes.js index fe423435..87052f00 100644 --- a/lib/routes.js +++ b/lib/routes.js @@ -7,6 +7,7 @@ const BigNumber = require('bignumber.js') const logger = require('./logger') const configManager = require('./config-manager') const db = require('./db') +const dbm = require('./postgresql_interface') const pairing = require('./pairing') const settingsLoader = require('./settings') @@ -168,7 +169,7 @@ function updatePhone (req, res, next) { const notified = req.query.notified === 'true' const tx = req.body - return plugins.updatePhone(tx, notified) + return dbm.updatePhone(tx, notified) .then(r => cacheAndRespond(req, res, r)) .catch(next) } @@ -181,14 +182,14 @@ function fetchPhoneTx (req, res, next) { function registerRedeem (req, res, next) { const txId = req.params.txId - return plugins.registerRedeem(txId) + return dbm.registerRedeem(txId) .then(() => cacheAndRespond(req, res)) .catch(next) } function waitForDispense (req, res, next) { logger.debug('waitForDispense') - return plugins.fetchTx(req.params.txId) + return dbm.fetchTx(req.params.txId) .then(tx => { logger.debug('tx fetched') logger.debug(tx) @@ -202,7 +203,7 @@ function waitForDispense (req, res, next) { function dispense (req, res, next) { const tx = req.body.tx - return plugins.requestDispense(tx) + return dbm.addDispenseRequest(tx) .then(dispenseRec => cacheAndRespond(req, res, dispenseRec)) .catch(next) } diff --git a/lib/wallet.js b/lib/wallet.js index a1512dd9..0bfe2d18 100644 --- a/lib/wallet.js +++ b/lib/wallet.js @@ -18,7 +18,7 @@ function fetchWallet (cryptoCode) { function balance (cryptoCode) { return fetchWallet(cryptoCode) - .then(r => r.wallet.balance(r.account)) + .then(r => r.wallet.balance(r.account, cryptoCode)) .then(balance => ({balance, timestamp: Date.now()})) }