fix promise stuff; fix db crypto/fiat bug

This commit is contained in:
Josh Harvey 2016-05-31 15:41:56 +03:00
parent a0961ce2e2
commit f17cfdfaa9
3 changed files with 30 additions and 24 deletions

View file

@ -398,6 +398,7 @@ function processTxStatus (tx) {
const cryptoCode = tx.cryptoCode
const walletPlugin = walletPlugins[cryptoCode]
if (!walletPlugin) throw new Error('No wallet plugins for: ' + cryptoCode)
walletPlugin.getStatus(tx.toAddress, tx.cryptoAtoms, function (err, res) {
if (err) {
logger.error(err)
@ -406,6 +407,7 @@ function processTxStatus (tx) {
var status = res.status
if (tx.status === status) return resolve()
const confirm = (status === 'instant' && tx.status !== 'confirmed') ||
(status === 'confirmed' && tx.status !== 'instant')
db.updateTxStatus(tx, status, confirm).then(resolve).catch(reject)
@ -431,20 +433,20 @@ function notifyConfirmation (tx) {
function monitorLiveIncoming () {
const statuses = ['notSeen', 'published', 'insufficientFunds']
db.fetchOpenTxs(statuses, STALE_INCOMING_TX_AGE)
.then(txs => txs.forEach(processTxStatus))
.then(txs => Promise.all(txs.map(processTxStatus)))
.catch(err => logger.error(err))
}
function monitorIncoming () {
const statuses = ['notSeen', 'published', 'authorized', 'instant', 'rejected', 'insufficientFunds']
db.fetchOpenTxs(statuses, STALE_INCOMING_TX_AGE)
.then(txs => txs.forEach(processTxStatus))
.then(txs => Promise.all(txs.map(processTxStatus)))
.catch(err => logger.error(err))
}
function monitorUnnotified () {
db.fetchUnnotifiedTxs(MAX_NOTIFY_AGE, MIN_NOTIFY_AGE)
.then(txs => txs.forEach(notifyConfirmation))
.then(txs => Promise.all(txs.map(notifyConfirmation)))
.catch(err => logger.error(err))
}

View file

@ -44,7 +44,7 @@ exports.init = function init (conString) {
}
// logs inputted bill and overall tx status (if available)
exports.recordBill = function recordBill (session, rec, cb) {
exports.recordBill = function recordBill (session, rec) {
const fields = [
'id',
'device_fingerprint',
@ -84,7 +84,7 @@ exports.recordDeviceEvent = function recordDeviceEvent (session, event) {
return db.none(sql, values)
}
exports.addOutgoingTx = function addOutgoingTx (session, tx, cb) {
exports.addOutgoingTx = function addOutgoingTx (session, tx) {
const fields = ['session_id', 'device_fingerprint', 'to_address',
'crypto_atoms', 'crypto_code', 'currency_code', 'fiat', 'tx_hash',
'fee', 'phone', 'error'
@ -112,7 +112,7 @@ exports.sentCoins = function sentCoins (session, tx, toSend, fee, error, txHash)
return db.none(sql, [txHash, error, session.id])
}
exports.addInitialIncoming = function addInitialIncoming (session, tx, cb) {
exports.addInitialIncoming = function addInitialIncoming (session, tx) {
const fields = ['session_id', 'device_fingerprint', 'to_address',
'crypto_atoms', 'crypto_code', 'currency_code', 'fiat', 'tx_hash',
'phone', 'error'
@ -123,8 +123,8 @@ exports.addInitialIncoming = function addInitialIncoming (session, tx, cb) {
session.fingerprint,
tx.toAddress,
tx.cryptoAtoms.toString(),
tx.currencyCode,
tx.cryptoCode,
tx.currencyCode,
tx.fiat,
tx.txHash,
tx.phone,
@ -134,7 +134,7 @@ exports.addInitialIncoming = function addInitialIncoming (session, tx, cb) {
return db.none(getInsertQuery('cash_out_txs', fields), values)
}
function insertDispense (session, tx, cartridges, cb) {
function insertDispense (session, tx, cartridges) {
const fields = [
'device_fingerprint', 'session_id',
'dispense1', 'reject1', 'count1',
@ -159,7 +159,7 @@ function insertDispense (session, tx, cartridges, cb) {
return db.none(sql, values)
}
exports.addIncomingPhone = function addIncomingPhone (session, tx, notified, cb) {
exports.addIncomingPhone = function addIncomingPhone (session, tx, notified) {
const sql = `UPDATE cash_out_txs SET phone=$1, notified=$2
WHERE session_id=$3
AND phone IS NULL`
@ -242,7 +242,7 @@ exports.addDispense = function addDispense (session, tx, cartridges) {
})
}
exports.cartridgeCounts = function cartridgeCounts (session, cb) {
exports.cartridgeCounts = function cartridgeCounts (session) {
const sql = 'SELECT id, count1, count2 FROM dispenses ' +
'WHERE device_fingerprint=$1 AND refill=$2 ' +
'ORDER BY id DESC LIMIT 1'
@ -253,7 +253,7 @@ exports.cartridgeCounts = function cartridgeCounts (session, cb) {
})
}
exports.machineEvent = function machineEvent (rec, cb) {
exports.machineEvent = function machineEvent (rec) {
const TTL = 2 * 60 * 60 * 1000
const fields = ['id', 'device_fingerprint', 'event_type', 'note', 'device_time']
const sql = getInsertQuery('machine_events', fields)
@ -265,19 +265,19 @@ exports.machineEvent = function machineEvent (rec, cb) {
.then(() => db.none(deleteSql, deleteValues))
}
exports.devices = function devices (cb) {
exports.devices = function devices () {
const sql = 'SELECT fingerprint, name FROM devices WHERE authorized=$1'
return db.manyOrNone(sql, [true])
}
exports.machineEvents = function machineEvents (cb) {
exports.machineEvents = function machineEvents () {
const sql = 'SELECT *, (EXTRACT(EPOCH FROM (now() - created))) * 1000 AS age FROM machine_events'
return db.manyOrNone(sql, [])
}
function singleQuotify (item) { return '\'' + item + '\'' }
exports.fetchOpenTxs = function fetchOpenTxs (statuses, age, cb) {
exports.fetchOpenTxs = function fetchOpenTxs (statuses, age) {
const _statuses = '(' + statuses.map(singleQuotify).join(',') + ')'
const sql = 'SELECT * ' +
@ -289,7 +289,7 @@ exports.fetchOpenTxs = function fetchOpenTxs (statuses, age, cb) {
.then(rows => normalizeTxs(rows))
}
exports.fetchUnnotifiedTxs = function fetchUnnotifiedTxs (age, waitPeriod, cb) {
exports.fetchUnnotifiedTxs = function fetchUnnotifiedTxs (age, waitPeriod) {
const sql = `SELECT *
FROM cash_out_txs
WHERE ((EXTRACT(EPOCH FROM (now() - created))) * 1000)<$1
@ -316,7 +316,7 @@ exports.updateTxStatus = function updateTxStatus (tx, status, confirm) {
})
}
exports.updateRedeem = function updateRedeem (session, cb) {
exports.updateRedeem = function updateRedeem (session) {
const sql = 'UPDATE cash_out_txs SET redeem=$1 WHERE session_id=$2'
const values = [true, session.id]

View file

@ -132,15 +132,19 @@ function send (req, res) {
var tx = req.body
tx.cryptoAtoms = new BigNumber(tx.cryptoAtoms)
plugins.sendCoins(session(req), tx, function (err, status) {
// TODO: use status.statusCode here after confirming machine compatibility
// FIX: (joshm) set txHash to status.txId instead of previous status.txHash which wasn't being set
// Need to clean up txHash vs txId
res.json({
errType: err && err.name,
err: err && err.message,
return plugins.sendCoins(session(req), tx)
.then(status => res.json({
txHash: status && status.txHash,
txId: status && status.txId
}))
.catch(err => {
logger.error(err)
res.json({
err: err.message,
errType: err.name
})
})
}