WIPP
This commit is contained in:
parent
bd95a90097
commit
b4d8f3cd4c
4 changed files with 194 additions and 6 deletions
4
.vscode/settings.json
vendored
4
.vscode/settings.json
vendored
|
|
@ -1 +1,3 @@
|
||||||
{}
|
{
|
||||||
|
"vsicons.presets.angular": false
|
||||||
|
}
|
||||||
|
|
|
||||||
130
lib/cash-out-tx.js
Normal file
130
lib/cash-out-tx.js
Normal file
|
|
@ -0,0 +1,130 @@
|
||||||
|
const _ = require('lodash/fp')
|
||||||
|
const pgp = require('pg-promise')()
|
||||||
|
const db = require('./db')
|
||||||
|
const BN = require('./bn')
|
||||||
|
|
||||||
|
module.exports = {post}
|
||||||
|
|
||||||
|
// id | uuid | not null
|
||||||
|
// device_id | text | not null
|
||||||
|
// to_address | text | not null
|
||||||
|
// crypto_atoms | bigint | not null
|
||||||
|
// crypto_code | text | not null
|
||||||
|
// fiat | numeric(14,5) | not null
|
||||||
|
// currency_code | text | not null
|
||||||
|
// tx_hash | text |
|
||||||
|
// status | status_stage | not null default 'notSeen'::status_stage
|
||||||
|
// dispensed | boolean | not null default false
|
||||||
|
// notified | boolean | not null default false
|
||||||
|
// redeem | boolean | not null default false
|
||||||
|
// phone | text |
|
||||||
|
// error | text |
|
||||||
|
// created | timestamp with time zone | not null default now()
|
||||||
|
// confirmation_time | timestamp with time zone |
|
||||||
|
|
||||||
|
const UPDATEABLE_FIELDS = ['txHash', 'status', 'dispensed', 'notified', 'redeem',
|
||||||
|
'phone', 'error', 'confirmationTime']
|
||||||
|
|
||||||
|
function post (tx, pi) {
|
||||||
|
const TransactionMode = pgp.txMode.TransactionMode
|
||||||
|
const isolationLevel = pgp.txMode.isolationLevel
|
||||||
|
const tmSRD = new TransactionMode({tiLevel: isolationLevel.serializable})
|
||||||
|
|
||||||
|
function transaction (t) {
|
||||||
|
const sql = 'select * from cash_out_txs where id=$1'
|
||||||
|
|
||||||
|
console.log('DEBUG888: %j', tx)
|
||||||
|
return t.oneOrNone(sql, [tx.id])
|
||||||
|
.then(row => upsert(row, tx))
|
||||||
|
}
|
||||||
|
|
||||||
|
transaction.txMode = tmSRD
|
||||||
|
|
||||||
|
return db.tx(transaction)
|
||||||
|
.then(txVector => {
|
||||||
|
const [, newTx] = txVector
|
||||||
|
return postProcess(txVector, pi)
|
||||||
|
.then(changes => update(newTx, changes))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
function nilEqual (a, b) {
|
||||||
|
if (_.isNil(a) && _.isNil(b)) return true
|
||||||
|
|
||||||
|
return undefined
|
||||||
|
}
|
||||||
|
|
||||||
|
function diff (oldTx, newTx) {
|
||||||
|
let updatedTx = {}
|
||||||
|
|
||||||
|
UPDATEABLE_FIELDS.forEach(fieldKey => {
|
||||||
|
console.log('DEBUG80: %j', [oldTx[fieldKey], newTx[fieldKey]])
|
||||||
|
if (oldTx && _.isEqualWith(nilEqual, oldTx[fieldKey], newTx[fieldKey])) return
|
||||||
|
|
||||||
|
// We never null out an existing field
|
||||||
|
if (oldTx && _.isNil(newTx[fieldKey])) return
|
||||||
|
|
||||||
|
updatedTx[fieldKey] = newTx[fieldKey]
|
||||||
|
})
|
||||||
|
|
||||||
|
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]
|
||||||
|
})
|
||||||
|
|
||||||
|
return newObj
|
||||||
|
}
|
||||||
|
|
||||||
|
function upsert (row, tx) {
|
||||||
|
const oldTx = toObj(row)
|
||||||
|
|
||||||
|
// insert bills
|
||||||
|
|
||||||
|
if (!oldTx) {
|
||||||
|
return insert(tx)
|
||||||
|
.then(newTx => [oldTx, newTx])
|
||||||
|
}
|
||||||
|
|
||||||
|
return update(tx, diff(oldTx, tx))
|
||||||
|
.then(newTx => [oldTx, newTx])
|
||||||
|
}
|
||||||
|
|
||||||
|
function insert (tx) {
|
||||||
|
const dbTx = _.mapKeys(_.snakeCase, _.omit(['direction', 'bills'], tx))
|
||||||
|
|
||||||
|
const sql = pgp.helpers.insert(dbTx, null, 'cash_out_txs') + ' returning *'
|
||||||
|
return db.one(sql)
|
||||||
|
.then(toObj)
|
||||||
|
}
|
||||||
|
|
||||||
|
function update (tx, changes) {
|
||||||
|
if (_.isEmpty(changes)) return Promise.resolve(tx)
|
||||||
|
|
||||||
|
const dbChanges = _.mapKeys(_.snakeCase, _.omit(['direction', 'bills'], changes))
|
||||||
|
console.log('DEBUG893: %j', dbChanges)
|
||||||
|
const sql = pgp.helpers.update(dbChanges, null, 'cash_out_txs') +
|
||||||
|
pgp.as.format(' where id=$1', [tx.id]) + ' returning *'
|
||||||
|
|
||||||
|
return db.one(sql)
|
||||||
|
.then(toObj)
|
||||||
|
}
|
||||||
|
|
||||||
|
function postProcess (txVector, pi) {
|
||||||
|
const [oldTx, newTx] = txVector
|
||||||
|
|
||||||
|
return Promise.resolve({})
|
||||||
|
}
|
||||||
|
|
@ -1,10 +1,21 @@
|
||||||
|
const _ = require('lodash/fp')
|
||||||
const R = require('ramda')
|
const R = require('ramda')
|
||||||
|
|
||||||
const db = require('./db')
|
const db = require('./db')
|
||||||
const dbm = require('./postgresql_interface')
|
const dbm = require('./postgresql_interface')
|
||||||
const T = require('./time')
|
const T = require('./time')
|
||||||
|
const BN = require('./bn')
|
||||||
|
|
||||||
const TRANSACTION_EXPIRATION = 2 * T.days
|
const TRANSACTION_EXPIRATION = 2 * T.days
|
||||||
|
|
||||||
|
function httpError (msg, code) {
|
||||||
|
const err = new Error(msg)
|
||||||
|
err.name = 'HTTPError'
|
||||||
|
err.code = code || 500
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
function stateChange (deviceId, deviceTime, rec) {
|
function stateChange (deviceId, deviceTime, rec) {
|
||||||
const event = {
|
const event = {
|
||||||
id: rec.uuid,
|
id: rec.uuid,
|
||||||
|
|
@ -16,8 +27,34 @@ function stateChange (deviceId, deviceTime, rec) {
|
||||||
return dbm.machineEvent(event)
|
return dbm.machineEvent(event)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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]
|
||||||
|
})
|
||||||
|
|
||||||
|
return newObj
|
||||||
|
}
|
||||||
|
|
||||||
function fetchPhoneTx (phone) {
|
function fetchPhoneTx (phone) {
|
||||||
return dbm.fetchPhoneTxs(phone, TRANSACTION_EXPIRATION)
|
const sql = `select * from cash_out_txs
|
||||||
|
where phone=$1 and dispensed=$2
|
||||||
|
and (extract(epoch from (coalesce(confirmation_time, now()) - created))) * 1000 < $3`
|
||||||
|
|
||||||
|
const values = [phone, false, TRANSACTION_EXPIRATION]
|
||||||
|
|
||||||
|
return db.any(sql, values)
|
||||||
|
.then(_.map(toObj))
|
||||||
.then(txs => {
|
.then(txs => {
|
||||||
const confirmedTxs = txs.filter(tx => R.contains(tx.status, ['instant', 'confirmed']))
|
const confirmedTxs = txs.filter(tx => R.contains(tx.status, ['instant', 'confirmed']))
|
||||||
if (confirmedTxs.length > 0) {
|
if (confirmedTxs.length > 0) {
|
||||||
|
|
@ -25,11 +62,23 @@ function fetchPhoneTx (phone) {
|
||||||
return !acc || val.cryptoAtoms.gt(acc.cryptoAtoms) ? val : acc
|
return !acc || val.cryptoAtoms.gt(acc.cryptoAtoms) ? val : acc
|
||||||
}, null, confirmedTxs)
|
}, null, confirmedTxs)
|
||||||
|
|
||||||
return {tx: maxTx}
|
return maxTx
|
||||||
}
|
}
|
||||||
|
|
||||||
if (txs.length > 0) return {pending: true}
|
if (txs.length > 0) throw httpError('Pending transactions', 412)
|
||||||
return {}
|
throw httpError('No transactions', 404)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
function fetchStatusTx (txId, status) {
|
||||||
|
const sql = 'select * from cash_out_txs where id=$1'
|
||||||
|
|
||||||
|
return db.oneOrNone(sql, [txId, status])
|
||||||
|
.then(toObj)
|
||||||
|
.then(tx => {
|
||||||
|
if (!tx) throw httpError('No transaction', 404)
|
||||||
|
if (tx.status === status) throw httpError('Not Modified', 304)
|
||||||
|
return tx
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -37,4 +86,4 @@ function updateDeviceConfigVersion (versionId) {
|
||||||
return db.none('update devices set user_config_id=$1', [versionId])
|
return db.none('update devices set user_config_id=$1', [versionId])
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {stateChange, fetchPhoneTx, updateDeviceConfigVersion}
|
module.exports = {stateChange, fetchPhoneTx, fetchStatusTx, updateDeviceConfigVersion}
|
||||||
|
|
|
||||||
|
|
@ -78,6 +78,12 @@ function poll (req, res, next) {
|
||||||
.catch(next)
|
.catch(next)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getTx (req, res, next) {
|
||||||
|
if (req.query.phone) return helpers.fetchPhoneTx(req.query.phone)
|
||||||
|
if (req.query.status) return helpers.fetchStatusTx(req.query.status)
|
||||||
|
throw httpError('Not Found', 404)
|
||||||
|
}
|
||||||
|
|
||||||
function postTx (req, res, next) {
|
function postTx (req, res, next) {
|
||||||
const pi = plugins(req.settings, req.deviceId)
|
const pi = plugins(req.settings, req.deviceId)
|
||||||
|
|
||||||
|
|
@ -238,6 +244,7 @@ app.post('/verify_transaction', verifyTx)
|
||||||
|
|
||||||
app.post('/phone_code', phoneCode)
|
app.post('/phone_code', phoneCode)
|
||||||
app.post('/tx', postTx)
|
app.post('/tx', postTx)
|
||||||
|
app.get('/tx', getTx)
|
||||||
|
|
||||||
app.use(errorHandler)
|
app.use(errorHandler)
|
||||||
app.use((req, res) => res.status(404).json({err: 'No such route'}))
|
app.use((req, res) => res.status(404).json({err: 'No such route'}))
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue