WIP
This commit is contained in:
parent
258a175dc6
commit
316a240130
3 changed files with 59 additions and 70 deletions
|
|
@ -81,17 +81,8 @@ function checkStuckScreen (deviceEvents) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function devicesAndEvents () {
|
function devicesAndEvents () {
|
||||||
return new Promise(function (resolve, reject) {
|
return Promise.all(db.devices, db.machineEvents)
|
||||||
db.devices(function (err, devices) {
|
.then(arr => ({devices: arr[0], events: arr[1]}))
|
||||||
if (err) return reject(err)
|
|
||||||
|
|
||||||
db.machineEvents(function (err, events) {
|
|
||||||
if (err) return reject(err)
|
|
||||||
|
|
||||||
return resolve({devices: devices, events: events})
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function checkStatus () {
|
function checkStatus () {
|
||||||
|
|
@ -103,10 +94,10 @@ function checkStatus () {
|
||||||
var devices = rec.devices
|
var devices = rec.devices
|
||||||
var events = rec.events
|
var events = rec.events
|
||||||
|
|
||||||
devices.rows.forEach(function (deviceRow) {
|
devices.forEach(function (deviceRow) {
|
||||||
var deviceFingerprint = deviceRow.fingerprint
|
var deviceFingerprint = deviceRow.fingerprint
|
||||||
var deviceName = deviceRow.name || deviceFingerprint
|
var deviceName = deviceRow.name || deviceFingerprint
|
||||||
var deviceEvents = events.rows.filter(function (eventRow) {
|
var deviceEvents = events.filter(function (eventRow) {
|
||||||
return eventRow.device_fingerprint === deviceFingerprint
|
return eventRow.device_fingerprint === deviceFingerprint
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -232,7 +232,7 @@ exports.getConfig = function getConfig () {
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.logEvent = function event (session, rawEvent) {
|
exports.logEvent = function event (session, rawEvent) {
|
||||||
db.recordDeviceEvent(session, rawEvent)
|
return db.recordDeviceEvent(session, rawEvent)
|
||||||
}
|
}
|
||||||
|
|
||||||
function buildCartridges (cartridges, virtualCartridges, rec) {
|
function buildCartridges (cartridges, virtualCartridges, rec) {
|
||||||
|
|
@ -257,18 +257,26 @@ exports.pollQueries = function pollQueries (session, cb) {
|
||||||
if (!cartridges) return cb(null, {})
|
if (!cartridges) return cb(null, {})
|
||||||
var virtualCartridges = cachedConfig.exchanges.settings.virtualCartridges
|
var virtualCartridges = cachedConfig.exchanges.settings.virtualCartridges
|
||||||
|
|
||||||
db.cartridgeCounts(session, function (err, result) {
|
return db.cartridgeCounts(session)
|
||||||
if (err) return cb(err)
|
.then(result => ({
|
||||||
return cb(null, {
|
|
||||||
cartridges: buildCartridges(cartridges, virtualCartridges, 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 walletPlugin = walletPlugins[cryptoCode]
|
||||||
var transactionFee = cachedConfig.exchanges.settings.transactionFee
|
var transactionFee = cachedConfig.exchanges.settings.transactionFee
|
||||||
logger.debug('Sending coins [%s] to: %s', cryptoCode, toAddress)
|
logger.debug('Sending coins [%s] to: %s', cryptoCode, toAddress)
|
||||||
|
|
||||||
if (cryptoCode === 'BTC') {
|
if (cryptoCode === 'BTC') {
|
||||||
walletPlugin.sendBitcoins(toAddress, cryptoAtoms.truncated().toNumber(), transactionFee, cb)
|
walletPlugin.sendBitcoins(toAddress, cryptoAtoms.truncated().toNumber(), transactionFee, cb)
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -276,39 +284,25 @@ function _sendCoins (toAddress, cryptoAtoms, cryptoCode, cb) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function executeTx (session, tx, cb) {
|
function executeTx (session, tx) {
|
||||||
db.addOutgoingTx(session, tx, function (err) {
|
return db.addOutgoingTx(session, tx)
|
||||||
if (err) {
|
.then(() => _sendCoins(tx.toAddress, tx.cryptoAtoms, tx.cryptoCode))
|
||||||
logger.error(err)
|
.then(txHash => {
|
||||||
return cb(err)
|
const fee = null // Need to fill this out in plugins
|
||||||
}
|
const toSend = {cryptoAtoms: tx.cryptoAtoms, fiat: tx.fiat}
|
||||||
|
|
||||||
var cryptoCode = tx.cryptoCode
|
return db.sentCoins(session, tx, toSend, fee, null, txHash)
|
||||||
_sendCoins(tx.toAddress, tx.cryptoAtoms, cryptoCode, function (_err, txHash) {
|
.then(() => pollBalance(tx.cryptoCode))
|
||||||
var fee = null // Need to fill this out in plugins
|
.then(() => ({
|
||||||
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
|
statusCode: 201, // Created
|
||||||
txHash: txHash,
|
txHash: txHash,
|
||||||
txId: tx.txId
|
txId: tx.txId
|
||||||
})
|
}))
|
||||||
})
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Run these in parallel and return success
|
// 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
|
// TODO: move this to DB, too
|
||||||
// add bill to trader queue (if trader is enabled)
|
// add bill to trader queue (if trader is enabled)
|
||||||
var cryptoCode = rawTrade.cryptoCode || 'BTC'
|
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) {
|
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}),
|
note: JSON.stringify({state: rec.state, isIdle: rec.isIdle, sessionId: session.id}),
|
||||||
deviceTime: session.deviceTime
|
deviceTime: session.deviceTime
|
||||||
}
|
}
|
||||||
db.machineEvent(event, cb)
|
return db.machineEvent(event)
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.recordPing = function recordPing (session, rec, cb) {
|
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}),
|
note: JSON.stringify({state: rec.state, isIdle: rec.idle === 'true', sessionId: session.id}),
|
||||||
deviceTime: session.deviceTime
|
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}
|
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 = {
|
var tmpInfo = {
|
||||||
label: 'TX ' + Date.now(),
|
label: 'TX ' + Date.now(),
|
||||||
account: 'deposit'
|
account: 'deposit'
|
||||||
|
|
@ -362,12 +356,13 @@ exports.cashOut = function cashOut (session, tx, cb) {
|
||||||
var cryptoCode = tx.cryptoCode || 'BTC'
|
var cryptoCode = tx.cryptoCode || 'BTC'
|
||||||
var walletPlugin = walletPlugins[cryptoCode]
|
var walletPlugin = walletPlugins[cryptoCode]
|
||||||
|
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
walletPlugin.newAddress(tmpInfo, function (err, address) {
|
walletPlugin.newAddress(tmpInfo, function (err, address) {
|
||||||
if (err) return cb(err)
|
if (err) return reject(err)
|
||||||
|
|
||||||
const newTx = R.assoc('toAddress', address, tx)
|
const newTx = R.assoc('toAddress', address, tx)
|
||||||
db.addInitialIncoming(session, newTx, function (_err) {
|
return db.addInitialIncoming(session, newTx)
|
||||||
cb(_err, address)
|
.then(() => resolve(address))
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -74,8 +74,8 @@ function poll (req, res) {
|
||||||
var settings = config.exchanges.settings
|
var settings = config.exchanges.settings
|
||||||
var complianceSettings = settings.compliance
|
var complianceSettings = settings.compliance
|
||||||
|
|
||||||
plugins.pollQueries(session(req), function (err, results) {
|
plugins.pollQueries(session(req))
|
||||||
if (err) return logger.error(err)
|
.then(results => {
|
||||||
var cartridges = results.cartridges
|
var cartridges = results.cartridges
|
||||||
|
|
||||||
var reboot = reboots[fingerprint] === pid
|
var reboot = reboots[fingerprint] === pid
|
||||||
|
|
@ -101,27 +101,30 @@ function poll (req, res) {
|
||||||
|
|
||||||
res.json(response)
|
res.json(response)
|
||||||
})
|
})
|
||||||
|
.catch(logger.error)
|
||||||
|
|
||||||
plugins.recordPing(session(req), req.query, function (err) {
|
plugins.recordPing(session(req), req.query)
|
||||||
if (err) console.error(err)
|
.catch(logger.error)
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function trade (req, res) {
|
function trade (req, res) {
|
||||||
var tx = req.body
|
var tx = req.body
|
||||||
tx.cryptoAtoms = new BigNumber(tx.cryptoAtoms)
|
tx.cryptoAtoms = new BigNumber(tx.cryptoAtoms)
|
||||||
|
|
||||||
plugins.trade(session(req), tx, function (err) {
|
plugins.trade(session(req), tx)
|
||||||
if (err) logger.error(err)
|
.then(() => res.status(201).json({}))
|
||||||
var statusCode = err ? 500 : 201
|
.catch(err => {
|
||||||
res.status(statusCode).json({err: err})
|
logger.error(err)
|
||||||
|
res.status(500).json({err: err})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
function stateChange (req, res) {
|
function stateChange (req, res) {
|
||||||
plugins.stateChange(session(req), req.body, function (err) {
|
plugins.stateChange(session(req), req.body)
|
||||||
if (err) console.error(err)
|
.then(() => res.json({success: true}))
|
||||||
res.json({success: true})
|
.catch(err => {
|
||||||
|
console.error(err)
|
||||||
|
res.json({success: false})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue