From 316a24013083a22875066cf87da1dc19c063948d Mon Sep 17 00:00:00 2001 From: Josh Harvey Date: Sat, 28 May 2016 22:04:44 +0300 Subject: [PATCH] WIP --- lib/notifier.js | 17 +++------- lib/plugins.js | 85 +++++++++++++++++++++++-------------------------- lib/routes.js | 27 +++++++++------- 3 files changed, 59 insertions(+), 70 deletions(-) diff --git a/lib/notifier.js b/lib/notifier.js index 58457639..7e87335a 100644 --- a/lib/notifier.js +++ b/lib/notifier.js @@ -81,17 +81,8 @@ function checkStuckScreen (deviceEvents) { } function devicesAndEvents () { - return new Promise(function (resolve, reject) { - db.devices(function (err, devices) { - if (err) return reject(err) - - db.machineEvents(function (err, events) { - if (err) return reject(err) - - return resolve({devices: devices, events: events}) - }) - }) - }) + return Promise.all(db.devices, db.machineEvents) + .then(arr => ({devices: arr[0], events: arr[1]})) } function checkStatus () { @@ -103,10 +94,10 @@ function checkStatus () { var devices = rec.devices var events = rec.events - devices.rows.forEach(function (deviceRow) { + devices.forEach(function (deviceRow) { var deviceFingerprint = deviceRow.fingerprint var deviceName = deviceRow.name || deviceFingerprint - var deviceEvents = events.rows.filter(function (eventRow) { + var deviceEvents = events.filter(function (eventRow) { return eventRow.device_fingerprint === deviceFingerprint }) diff --git a/lib/plugins.js b/lib/plugins.js index 1e4c31be..12047b6f 100644 --- a/lib/plugins.js +++ b/lib/plugins.js @@ -232,7 +232,7 @@ exports.getConfig = function getConfig () { } exports.logEvent = function event (session, rawEvent) { - db.recordDeviceEvent(session, rawEvent) + return db.recordDeviceEvent(session, rawEvent) } function buildCartridges (cartridges, virtualCartridges, rec) { @@ -257,18 +257,26 @@ exports.pollQueries = function pollQueries (session, cb) { if (!cartridges) return cb(null, {}) var virtualCartridges = cachedConfig.exchanges.settings.virtualCartridges - db.cartridgeCounts(session, function (err, result) { - if (err) return cb(err) - return cb(null, { - cartridges: buildCartridges(cartridges, virtualCartridges, result) + return db.cartridgeCounts(session) + .then(result => ({ + cartridges: buildCartridges(cartridges, virtualCartridges, result) + })) +} + +function _sendCoins (toAddress, cryptoAtoms, cryptoCode) { + return new Promise((resolve, reject) => { + _sendCoinsCb(toAddress, cryptoAtoms, cryptoCode, (err, txHash) => { + if (err) return reject(err) + return resolve(txHash) }) }) } -function _sendCoins (toAddress, cryptoAtoms, cryptoCode, cb) { +function _sendCoinsCb (toAddress, cryptoAtoms, cryptoCode, cb) { var walletPlugin = walletPlugins[cryptoCode] var transactionFee = cachedConfig.exchanges.settings.transactionFee logger.debug('Sending coins [%s] to: %s', cryptoCode, toAddress) + if (cryptoCode === 'BTC') { walletPlugin.sendBitcoins(toAddress, cryptoAtoms.truncated().toNumber(), transactionFee, cb) } else { @@ -276,39 +284,25 @@ function _sendCoins (toAddress, cryptoAtoms, cryptoCode, cb) { } } -function executeTx (session, tx, cb) { - db.addOutgoingTx(session, tx, function (err) { - if (err) { - logger.error(err) - return cb(err) - } +function executeTx (session, tx) { + return db.addOutgoingTx(session, tx) + .then(() => _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} - var cryptoCode = tx.cryptoCode - _sendCoins(tx.toAddress, tx.cryptoAtoms, cryptoCode, function (_err, txHash) { - var fee = null // Need to fill this out in plugins - var toSend = {cryptoAtoms: tx.cryptoAtoms, fiat: tx.fiat} - - if (_err) { - logger.error(_err) - toSend = {cryptoAtoms: new BigNumber(0), fiat: 0} - } - db.sentCoins(session, tx, toSend, fee, _err, txHash) - - if (_err) return cb(_err) - - pollBalance(cryptoCode) - - cb(null, { - statusCode: 201, // Created - txHash: txHash, - txId: tx.txId - }) - }) + return db.sentCoins(session, tx, toSend, fee, null, txHash) + .then(() => pollBalance(tx.cryptoCode)) + .then(() => ({ + statusCode: 201, // Created + txHash: txHash, + txId: tx.txId + })) }) } // TODO: Run these in parallel and return success -exports.trade = function trade (session, rawTrade, cb) { +exports.trade = function trade (session, rawTrade) { // TODO: move this to DB, too // add bill to trader queue (if trader is enabled) var cryptoCode = rawTrade.cryptoCode || 'BTC' @@ -323,7 +317,7 @@ exports.trade = function trade (session, rawTrade, cb) { }) } - db.recordBill(session, rawTrade, cb) + return db.recordBill(session, rawTrade) } exports.stateChange = function stateChange (session, rec, cb) { @@ -334,7 +328,7 @@ exports.stateChange = function stateChange (session, rec, cb) { note: JSON.stringify({state: rec.state, isIdle: rec.isIdle, sessionId: session.id}), deviceTime: session.deviceTime } - db.machineEvent(event, cb) + return db.machineEvent(event) } exports.recordPing = function recordPing (session, rec, cb) { @@ -345,15 +339,15 @@ exports.recordPing = function recordPing (session, rec, cb) { note: JSON.stringify({state: rec.state, isIdle: rec.idle === 'true', sessionId: session.id}), deviceTime: session.deviceTime } - db.machineEvent(event, cb) + return db.machineEvent(event) } -exports.sendCoins = function sendCoins (session, rawTx, cb) { +exports.sendCoins = function sendCoins (session, rawTx) { var _session = {id: rawTx.sessionId || session.id, fingerprint: session.fingerprint} - executeTx(_session, rawTx, cb) + return executeTx(_session, rawTx) } -exports.cashOut = function cashOut (session, tx, cb) { +exports.cashOut = function cashOut (session, tx) { var tmpInfo = { label: 'TX ' + Date.now(), account: 'deposit' @@ -362,12 +356,13 @@ exports.cashOut = function cashOut (session, tx, cb) { var cryptoCode = tx.cryptoCode || 'BTC' var walletPlugin = walletPlugins[cryptoCode] - walletPlugin.newAddress(tmpInfo, function (err, address) { - if (err) return cb(err) + return new Promise((resolve, reject) => { + walletPlugin.newAddress(tmpInfo, function (err, address) { + if (err) return reject(err) - const newTx = R.assoc('toAddress', address, tx) - db.addInitialIncoming(session, newTx, function (_err) { - cb(_err, address) + const newTx = R.assoc('toAddress', address, tx) + return db.addInitialIncoming(session, newTx) + .then(() => resolve(address)) }) }) } diff --git a/lib/routes.js b/lib/routes.js index 6ac43ea1..ddd53909 100644 --- a/lib/routes.js +++ b/lib/routes.js @@ -74,8 +74,8 @@ function poll (req, res) { var settings = config.exchanges.settings var complianceSettings = settings.compliance - plugins.pollQueries(session(req), function (err, results) { - if (err) return logger.error(err) + plugins.pollQueries(session(req)) + .then(results => { var cartridges = results.cartridges var reboot = reboots[fingerprint] === pid @@ -101,27 +101,30 @@ function poll (req, res) { res.json(response) }) + .catch(logger.error) - plugins.recordPing(session(req), req.query, function (err) { - if (err) console.error(err) - }) + plugins.recordPing(session(req), req.query) + .catch(logger.error) } function trade (req, res) { var tx = req.body tx.cryptoAtoms = new BigNumber(tx.cryptoAtoms) - plugins.trade(session(req), tx, function (err) { - if (err) logger.error(err) - var statusCode = err ? 500 : 201 - res.status(statusCode).json({err: err}) + plugins.trade(session(req), tx) + .then(() => res.status(201).json({})) + .catch(err => { + logger.error(err) + res.status(500).json({err: err}) }) } function stateChange (req, res) { - plugins.stateChange(session(req), req.body, function (err) { - if (err) console.error(err) - res.json({success: true}) + plugins.stateChange(session(req), req.body) + .then(() => res.json({success: true})) + .catch(err => { + console.error(err) + res.json({success: false}) }) }