Reserve notes for redeem

This commit is contained in:
Josh Harvey 2017-05-04 21:27:12 +03:00
parent 1488a60a60
commit 67e12b19cb
6 changed files with 235 additions and 93 deletions

View file

@ -2,11 +2,11 @@ const _ = require('lodash/fp')
const pgp = require('pg-promise')()
const db = require('./db')
const BN = require('./bn')
const billMath = require('./bill-math')
const T = require('./time')
const logger = require('./logger')
const plugins = require('./plugins')
const helper = require('./cash-out-helper')
module.exports = {
post,
@ -16,10 +16,8 @@ module.exports = {
cancel
}
const mapValuesWithKey = _.mapValues.convert({cap: false})
const UPDATEABLE_FIELDS = ['txHash', 'status', 'dispense', 'notified', 'redeem',
'phone', 'error', 'swept']
const UPDATEABLE_FIELDS = ['txHash', 'status', 'dispense', 'dispenseConfirmed',
'notified', 'redeem', 'phone', 'error', 'swept']
const STALE_INCOMING_TX_AGE = T.week
const STALE_LIVE_INCOMING_TX_AGE = 10 * T.minutes
@ -27,6 +25,9 @@ const MAX_NOTIFY_AGE = 2 * T.days
const MIN_NOTIFY_AGE = 5 * T.minutes
const INSUFFICIENT_FUNDS_CODE = 570
const toObj = helper.toObj
const toDb = helper.toDb
function httpError (msg, code) {
const err = new Error(msg)
err.name = 'HTTPError'
@ -128,27 +129,6 @@ function diff (oldTx, newTx) {
return updatedTx
}
function toObj (row) {
if (!row) return null
const keys = _.keys(row)
let newObj = {}
keys.forEach(key => {
const objKey = _.camelCase(key)
if (key === 'crypto_atoms' || key === 'fiat') {
newObj[objKey] = BN(row[key])
return
}
newObj[objKey] = row[key]
})
newObj.direction = 'cashOut'
return newObj
}
function upsert (oldTx, tx) {
if (!oldTx) {
return insert(tx)
@ -159,27 +139,6 @@ function upsert (oldTx, tx) {
.then(newTx => [oldTx, newTx])
}
function convertBigNumFields (obj) {
const convert = (value, key) => _.includes(key, ['cryptoAtoms', 'fiat'])
? value.toString()
: value
const convertKey = key => _.includes(key, ['cryptoAtoms', 'fiat'])
? key + '#'
: key
return _.mapKeys(convertKey, mapValuesWithKey(convert, obj))
}
function convertField (key) {
return _.snakeCase(key)
}
function toDb (tx) {
const massager = _.flow(convertBigNumFields, _.omit(['direction', 'bills']), _.mapKeys(convertField))
return massager(tx)
}
function insert (tx) {
const dbTx = toDb(tx)
@ -191,7 +150,7 @@ function insert (tx) {
function update (tx, changes) {
if (_.isEmpty(changes)) return Promise.resolve(tx)
const dbChanges = toDb(tx)
const dbChanges = toDb(changes)
const sql = pgp.helpers.update(dbChanges, null, 'cash_out_txs') +
pgp.as.format(' where id=$1', [tx.id])
@ -223,6 +182,16 @@ function updateCassettes (tx) {
return db.none(sql, values)
}
function wasJustAuthorized (oldTx, newTx, isZeroConf) {
const isAuthorized = () => _.includes(oldTx.status, ['notSeen', 'published']) &&
_.includes(newTx.status, ['authorized', 'instant', 'confirmed'])
const isConfirmed = () => _.includes(oldTx.status, ['notSeen', 'published', 'authorized']) &&
_.includes(newTx.status, ['instant', 'confirmed'])
return isZeroConf ? isAuthorized() : isConfirmed()
}
function preProcess (oldTx, newTx, pi) {
if (!oldTx) {
return pi.isHd(newTx)
@ -246,10 +215,14 @@ function preProcess (oldTx, newTx, pi) {
if (!oldTx) return updatedTx
if (updatedTx.status !== oldTx.status) {
const isZeroConf = pi.isZeroConf(updatedTx)
if (wasJustAuthorized(oldTx, updatedTx, isZeroConf)) pi.sell(updatedTx)
const rec = {
to_address: updatedTx.toAddress,
tx_hash: updatedTx.txHash
}
return logAction(updatedTx.status, rec, updatedTx)
}
@ -273,23 +246,29 @@ function preProcess (oldTx, newTx, pi) {
function postProcess (txVector, pi) {
const [oldTx, newTx] = txVector
if (newTx.dispense && !oldTx.dispense) {
return pi.buildCassettes()
if ((newTx.dispense && !oldTx.dispense) || (newTx.redeem && !oldTx.redeem)) {
return pi.buildAvailableCassettes(newTx.id)
.then(cassettes => {
pi.sell(newTx)
const bills = billMath.makeChange(cassettes.cassettes, newTx.fiat)
console.log('DEBUG130: %j', cassettes.cassettes)
if (!bills) throw httpError('Out of bills', INSUFFICIENT_FUNDS_CODE)
return _.set('bills', bills, newTx)
return bills
})
.then(tx => {
.then(bills => {
const provisioned1 = bills[0].provisioned
const provisioned2 = bills[1].provisioned
const denomination1 = bills[0].denomination
const denomination2 = bills[1].denomination
const rec = {
provisioned_1: tx.bills[0].provisioned,
provisioned_2: tx.bills[1].provisioned,
denomination_1: tx.bills[0].denomination,
denomination_2: tx.bills[1].denomination
provisioned_1: provisioned1,
provisioned_2: provisioned2,
denomination_1: denomination1,
denomination_2: denomination2
}
return logAction('provisionNotes', rec, tx)
return logAction('provisionNotes', rec, newTx)
.then(_.constant({bills}))
})
.catch(err => {
return logError('provisionNotesError', err, newTx)
@ -297,7 +276,7 @@ function postProcess (txVector, pi) {
})
}
return Promise.resolve(newTx)
return Promise.resolve({})
}
function updateStatus (oldTx, newTx) {