format for latest standard

This commit is contained in:
Josh Harvey 2018-03-10 18:59:40 +00:00
parent 4108efd9c7
commit c2af183911
77 changed files with 1697 additions and 1693 deletions

View file

@ -17,21 +17,21 @@ function atomic (machineTx, pi) {
const sql2 = 'select * from bills where cash_in_txs_id=$1'
return t.oneOrNone(sql, [machineTx.id])
.then(row => {
if (row && row.tx_version >= machineTx.txVersion) throw new E.StaleTxError('Stale tx')
.then(row => {
if (row && row.tx_version >= machineTx.txVersion) throw new E.StaleTxError('Stale tx')
return t.any(sql2, [machineTx.id])
.then(billRows => {
const dbTx = cashInLow.toObj(row)
return t.any(sql2, [machineTx.id])
.then(billRows => {
const dbTx = cashInLow.toObj(row)
return preProcess(dbTx, machineTx, pi)
.then(preProcessedTx => cashInLow.upsert(t, dbTx, preProcessedTx))
.then(r => {
return insertNewBills(t, billRows, machineTx)
.then(newBills => _.set('newBills', newBills, r))
})
return preProcess(dbTx, machineTx, pi)
.then(preProcessedTx => cashInLow.upsert(t, dbTx, preProcessedTx))
.then(r => {
return insertNewBills(t, billRows, machineTx)
.then(newBills => _.set('newBills', newBills, r))
})
})
})
})
}
transaction.txMode = tmSRD
@ -48,7 +48,7 @@ function insertNewBills (t, billRows, machineTx) {
const sql = pgp.helpers.insert(dbBills, columns, 'bills')
return t.none(sql)
.then(() => bills)
.then(() => bills)
}
function pullNewBills (billRows, machineTx) {

View file

@ -15,8 +15,8 @@ module.exports = {toObj, upsert, insert, update, massage, isClearToSend}
function convertBigNumFields (obj) {
const convert = value => value && value.isBigNumber
? value.toString()
: value
? value.toString()
: value
return _.mapValues(convert, obj)
}
@ -45,18 +45,18 @@ function toObj (row) {
function upsert (t, dbTx, preProcessedTx) {
if (!dbTx) {
return insert(t, preProcessedTx)
.then(tx => ({dbTx, tx}))
.then(tx => ({dbTx, tx}))
}
return update(t, dbTx, diff(dbTx, preProcessedTx))
.then(tx => ({dbTx, tx}))
.then(tx => ({dbTx, tx}))
}
function insert (t, tx) {
const dbTx = massage(tx)
const sql = pgp.helpers.insert(dbTx, null, 'cash_in_txs') + ' returning *'
return t.one(sql)
.then(toObj)
.then(toObj)
}
function update (t, tx, changes) {
@ -67,7 +67,7 @@ function update (t, tx, changes) {
pgp.as.format(' where id=$1', [tx.id]) + ' returning *'
return t.one(sql)
.then(toObj)
.then(toObj)
}
function diff (oldTx, newTx) {

View file

@ -16,13 +16,13 @@ module.exports = {post, monitorPending, cancel, PENDING_INTERVAL}
function post (machineTx, pi) {
return db.tx(cashInAtomic.atomic(machineTx, pi))
.then(r => {
const updatedTx = r.tx
.then(r => {
const updatedTx = r.tx
return postProcess(r, pi)
.then(changes => cashInLow.update(db, updatedTx, changes))
.then(tx => _.set('bills', machineTx.bills, tx))
})
return postProcess(r, pi)
.then(changes => cashInLow.update(db, updatedTx, changes))
.then(tx => _.set('bills', machineTx.bills, tx))
})
}
function registerTrades (pi, newBills) {
@ -41,7 +41,7 @@ function logAction (rec, tx) {
const sql = pgp.helpers.insert(action, null, 'cash_in_actions')
return db.none(sql)
.then(_.constant(rec))
.then(_.constant(rec))
}
function logActionById (action, _rec, txId) {
@ -57,30 +57,30 @@ function postProcess (r, pi) {
if (!cashInLow.isClearToSend(r.dbTx, r.tx)) return Promise.resolve({})
return pi.sendCoins(r.tx)
.then(txHash => ({
txHash,
sendConfirmed: true,
sendTime: 'now()^',
sendPending: false,
error: null,
errorCode: null
}))
.catch(err => {
.then(txHash => ({
txHash,
sendConfirmed: true,
sendTime: 'now()^',
sendPending: false,
error: null,
errorCode: null
}))
.catch(err => {
// Important: We don't know what kind of error this is
// so not safe to assume that funds weren't sent.
// Therefore, don't set sendPending to false except for
// errors (like InsufficientFundsError) that are guaranteed
// not to send.
const sendPending = err.name !== 'InsufficientFundsError'
const sendPending = err.name !== 'InsufficientFundsError'
return {
sendTime: 'now()^',
error: err.message,
errorCode: err.name,
sendPending
}
})
.then(sendRec => logAction(sendRec, r.tx))
return {
sendTime: 'now()^',
error: err.message,
errorCode: err.name,
sendPending
}
})
.then(sendRec => logAction(sendRec, r.tx))
}
function monitorPending (settings) {
@ -98,12 +98,12 @@ function monitorPending (settings) {
const pi = plugins(settings, tx.deviceId)
return post(tx, pi)
.catch(logger.error)
.catch(logger.error)
}
return db.any(sql, [PENDING_INTERVAL, MAX_PENDING])
.then(rows => pEachSeries(rows, row => processPending(row)))
.catch(logger.error)
.then(rows => pEachSeries(rows, row => processPending(row)))
.catch(logger.error)
}
function cancel (txId) {
@ -114,13 +114,13 @@ function cancel (txId) {
}
return Promise.resolve()
.then(() => {
return pgp.helpers.update(updateRec, null, 'cash_in_txs') +
.then(() => {
return pgp.helpers.update(updateRec, null, 'cash_in_txs') +
pgp.as.format(' where id=$1', [txId])
})
.then(sql => db.result(sql, false))
.then(res => {
if (res.rowCount !== 1) throw new Error('No such tx-id')
})
.then(() => logActionById('operatorCompleted', {}, txId))
})
.then(sql => db.result(sql, false))
.then(res => {
if (res.rowCount !== 1) throw new Error('No such tx-id')
})
.then(() => logActionById('operatorCompleted', {}, txId))
}