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 cryptoCode = tx.cryptoCode
const walletPlugin = walletPlugins[cryptoCode] const walletPlugin = walletPlugins[cryptoCode]
if (!walletPlugin) throw new Error('No wallet plugins for: ' + cryptoCode)
walletPlugin.getStatus(tx.toAddress, tx.cryptoAtoms, function (err, res) { walletPlugin.getStatus(tx.toAddress, tx.cryptoAtoms, function (err, res) {
if (err) { if (err) {
logger.error(err) logger.error(err)
@ -406,6 +407,7 @@ function processTxStatus (tx) {
var status = res.status var status = res.status
if (tx.status === status) return resolve() if (tx.status === status) return resolve()
const confirm = (status === 'instant' && tx.status !== 'confirmed') || const confirm = (status === 'instant' && tx.status !== 'confirmed') ||
(status === 'confirmed' && tx.status !== 'instant') (status === 'confirmed' && tx.status !== 'instant')
db.updateTxStatus(tx, status, confirm).then(resolve).catch(reject) db.updateTxStatus(tx, status, confirm).then(resolve).catch(reject)
@ -431,20 +433,20 @@ function notifyConfirmation (tx) {
function monitorLiveIncoming () { function monitorLiveIncoming () {
const statuses = ['notSeen', 'published', 'insufficientFunds'] const statuses = ['notSeen', 'published', 'insufficientFunds']
db.fetchOpenTxs(statuses, STALE_INCOMING_TX_AGE) db.fetchOpenTxs(statuses, STALE_INCOMING_TX_AGE)
.then(txs => txs.forEach(processTxStatus)) .then(txs => Promise.all(txs.map(processTxStatus)))
.catch(err => logger.error(err)) .catch(err => logger.error(err))
} }
function monitorIncoming () { function monitorIncoming () {
const statuses = ['notSeen', 'published', 'authorized', 'instant', 'rejected', 'insufficientFunds'] const statuses = ['notSeen', 'published', 'authorized', 'instant', 'rejected', 'insufficientFunds']
db.fetchOpenTxs(statuses, STALE_INCOMING_TX_AGE) db.fetchOpenTxs(statuses, STALE_INCOMING_TX_AGE)
.then(txs => txs.forEach(processTxStatus)) .then(txs => Promise.all(txs.map(processTxStatus)))
.catch(err => logger.error(err)) .catch(err => logger.error(err))
} }
function monitorUnnotified () { function monitorUnnotified () {
db.fetchUnnotifiedTxs(MAX_NOTIFY_AGE, MIN_NOTIFY_AGE) 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)) .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) // logs inputted bill and overall tx status (if available)
exports.recordBill = function recordBill (session, rec, cb) { exports.recordBill = function recordBill (session, rec) {
const fields = [ const fields = [
'id', 'id',
'device_fingerprint', 'device_fingerprint',
@ -84,7 +84,7 @@ exports.recordDeviceEvent = function recordDeviceEvent (session, event) {
return db.none(sql, values) 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', const fields = ['session_id', 'device_fingerprint', 'to_address',
'crypto_atoms', 'crypto_code', 'currency_code', 'fiat', 'tx_hash', 'crypto_atoms', 'crypto_code', 'currency_code', 'fiat', 'tx_hash',
'fee', 'phone', 'error' '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]) 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', const fields = ['session_id', 'device_fingerprint', 'to_address',
'crypto_atoms', 'crypto_code', 'currency_code', 'fiat', 'tx_hash', 'crypto_atoms', 'crypto_code', 'currency_code', 'fiat', 'tx_hash',
'phone', 'error' 'phone', 'error'
@ -123,8 +123,8 @@ exports.addInitialIncoming = function addInitialIncoming (session, tx, cb) {
session.fingerprint, session.fingerprint,
tx.toAddress, tx.toAddress,
tx.cryptoAtoms.toString(), tx.cryptoAtoms.toString(),
tx.currencyCode,
tx.cryptoCode, tx.cryptoCode,
tx.currencyCode,
tx.fiat, tx.fiat,
tx.txHash, tx.txHash,
tx.phone, tx.phone,
@ -134,7 +134,7 @@ exports.addInitialIncoming = function addInitialIncoming (session, tx, cb) {
return db.none(getInsertQuery('cash_out_txs', fields), values) return db.none(getInsertQuery('cash_out_txs', fields), values)
} }
function insertDispense (session, tx, cartridges, cb) { function insertDispense (session, tx, cartridges) {
const fields = [ const fields = [
'device_fingerprint', 'session_id', 'device_fingerprint', 'session_id',
'dispense1', 'reject1', 'count1', 'dispense1', 'reject1', 'count1',
@ -159,7 +159,7 @@ function insertDispense (session, tx, cartridges, cb) {
return db.none(sql, values) 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 const sql = `UPDATE cash_out_txs SET phone=$1, notified=$2
WHERE session_id=$3 WHERE session_id=$3
AND phone IS NULL` 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 ' + const sql = 'SELECT id, count1, count2 FROM dispenses ' +
'WHERE device_fingerprint=$1 AND refill=$2 ' + 'WHERE device_fingerprint=$1 AND refill=$2 ' +
'ORDER BY id DESC LIMIT 1' '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 TTL = 2 * 60 * 60 * 1000
const fields = ['id', 'device_fingerprint', 'event_type', 'note', 'device_time'] const fields = ['id', 'device_fingerprint', 'event_type', 'note', 'device_time']
const sql = getInsertQuery('machine_events', fields) const sql = getInsertQuery('machine_events', fields)
@ -265,19 +265,19 @@ exports.machineEvent = function machineEvent (rec, cb) {
.then(() => db.none(deleteSql, deleteValues)) .then(() => db.none(deleteSql, deleteValues))
} }
exports.devices = function devices (cb) { exports.devices = function devices () {
const sql = 'SELECT fingerprint, name FROM devices WHERE authorized=$1' const sql = 'SELECT fingerprint, name FROM devices WHERE authorized=$1'
return db.manyOrNone(sql, [true]) 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' const sql = 'SELECT *, (EXTRACT(EPOCH FROM (now() - created))) * 1000 AS age FROM machine_events'
return db.manyOrNone(sql, []) return db.manyOrNone(sql, [])
} }
function singleQuotify (item) { return '\'' + item + '\'' } 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 _statuses = '(' + statuses.map(singleQuotify).join(',') + ')'
const sql = 'SELECT * ' + const sql = 'SELECT * ' +
@ -289,7 +289,7 @@ exports.fetchOpenTxs = function fetchOpenTxs (statuses, age, cb) {
.then(rows => normalizeTxs(rows)) .then(rows => normalizeTxs(rows))
} }
exports.fetchUnnotifiedTxs = function fetchUnnotifiedTxs (age, waitPeriod, cb) { exports.fetchUnnotifiedTxs = function fetchUnnotifiedTxs (age, waitPeriod) {
const sql = `SELECT * const sql = `SELECT *
FROM cash_out_txs FROM cash_out_txs
WHERE ((EXTRACT(EPOCH FROM (now() - created))) * 1000)<$1 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 sql = 'UPDATE cash_out_txs SET redeem=$1 WHERE session_id=$2'
const values = [true, session.id] const values = [true, session.id]

View file

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