This commit is contained in:
Josh Harvey 2016-07-27 19:25:33 +03:00
parent 5312086622
commit dbcfa6a7e7
3 changed files with 56 additions and 58 deletions

View file

@ -359,7 +359,7 @@ exports.sendCoins = function sendCoins (rawTx) {
return executeTx(rawTx) return executeTx(rawTx)
} }
exports.cashOut = function cashOut (tx) { exports.cashOut = function cashOut (deviceId, tx) {
const cryptoCode = tx.cryptoCode || 'BTC' const cryptoCode = tx.cryptoCode || 'BTC'
const walletPlugin = walletPlugins[cryptoCode] const walletPlugin = walletPlugins[cryptoCode]
@ -379,7 +379,7 @@ exports.cashOut = function cashOut (tx) {
if (err) return reject(err) if (err) return reject(err)
const newTx = R.assoc('toAddress', address, tx) const newTx = R.assoc('toAddress', address, tx)
return db.addInitialIncoming(newTx, address) return db.addInitialIncoming(deviceId, newTx, address)
.then(() => resolve(address)) .then(() => resolve(address))
}) })
})) }))
@ -781,9 +781,7 @@ exports.fetchPhoneTx = function fetchPhoneTx (phone) {
}) })
} }
exports.fetchTx = function fetchTx (session) { exports.fetchTx = db.fetchTx
return db.fetchTx(session)
}
exports.requestDispense = function requestDispense (tx) { exports.requestDispense = function requestDispense (tx) {
return db.addDispenseRequest(tx) return db.addDispenseRequest(tx)
@ -805,7 +803,7 @@ function sweepHD (row) {
.then(txHash => { .then(txHash => {
if (txHash) { if (txHash) {
logger.debug('[%s] Swept address with tx: %s', cryptoCode, txHash) logger.debug('[%s] Swept address with tx: %s', cryptoCode, txHash)
return db.markSwept(row.session_id) return db.markSwept(row.tx_id)
} }
}) })
.catch(err => logger.error('[%s] Sweep error: %s', cryptoCode, err.message)) .catch(err => logger.error('[%s] Sweep error: %s', cryptoCode, err.message))

View file

@ -82,16 +82,16 @@ exports.recordDeviceEvent = function recordDeviceEvent (deviceId, event) {
} }
// NOTE: This will fail if we have already sent coins because there will be // NOTE: This will fail if we have already sent coins because there will be
// a unique cash_in_txs record in the table already keyed by sessionId. // a unique cash_in_txs record in the table already keyed by txId.
exports.addOutgoingTx = function addOutgoingTx (fingerprint, tx) { exports.addOutgoingTx = function addOutgoingTx (deviceId, tx) {
const fields = ['session_id', 'device_fingerprint', 'to_address', const fields = ['tx_id', 'device_id', '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'
] ]
const values = [ const values = [
tx.sessionId, tx.id,
fingerprint, deviceId,
tx.toAddress, tx.toAddress,
tx.cryptoAtoms.toString(), tx.cryptoAtoms.toString(),
tx.cryptoCode, tx.cryptoCode,
@ -107,19 +107,19 @@ exports.addOutgoingTx = function addOutgoingTx (fingerprint, tx) {
} }
exports.sentCoins = function sentCoins (tx, toSend, fee, error, txHash) { exports.sentCoins = function sentCoins (tx, toSend, fee, error, txHash) {
const sql = `update cash_in_txs set tx_hash=$1, error=$2 where session_id=$3` const sql = `update cash_in_txs set tx_hash=$1, error=$2 where tx_id=$3`
return db.none(sql, [txHash, error, tx.sessionId]) return db.none(sql, [txHash, error, tx.id])
} }
exports.addInitialIncoming = function addInitialIncoming (session, tx) { exports.addInitialIncoming = function addInitialIncoming (deviceId, tx) {
const fields = ['session_id', 'device_fingerprint', 'to_address', const fields = ['tx_id', 'device_id', 'to_address',
'crypto_atoms', 'crypto_code', 'currency_code', 'fiat', 'tx_hash', 'crypto_atoms', 'crypto_code', 'currency_code', 'fiat', 'tx_hash',
'phone', 'error' 'phone', 'error'
] ]
const values = [ const values = [
session.id, tx.id,
session.fingerprint, deviceId,
tx.toAddress, tx.toAddress,
tx.cryptoAtoms.toString(), tx.cryptoAtoms.toString(),
tx.cryptoCode, tx.cryptoCode,
@ -133,9 +133,9 @@ exports.addInitialIncoming = function addInitialIncoming (session, tx) {
return db.none(getInsertQuery('cash_out_txs', fields), values) return db.none(getInsertQuery('cash_out_txs', fields), values)
} }
function insertDispense (session, tx, cartridges) { function insertDispense (deviceId, tx, cartridges) {
const fields = [ const fields = [
'device_fingerprint', 'session_id', 'device_id', 'tx_id',
'dispense1', 'reject1', 'count1', 'dispense1', 'reject1', 'count1',
'dispense2', 'reject2', 'count2', 'dispense2', 'reject2', 'count2',
'refill', 'error' 'refill', 'error'
@ -150,7 +150,7 @@ function insertDispense (session, tx, cartridges) {
const count1 = cartridges[0].count const count1 = cartridges[0].count
const count2 = cartridges[1].count const count2 = cartridges[1].count
const values = [ const values = [
session.fingerprint, tx.sessionId, deviceId, tx.id,
dispense1, reject1, count1, dispense2, reject2, count2, dispense1, reject1, count1, dispense2, reject2, count2,
false, tx.error false, tx.error
] ]
@ -167,11 +167,11 @@ exports.addIncomingPhone = function addIncomingPhone (tx, notified) {
return db.result(sql, values) return db.result(sql, values)
.then(results => { .then(results => {
const noPhone = results.rowCount === 0 const noPhone = results.rowCount === 0
const sql2 = 'insert into cash_out_actions (session_id, action) values ($1, $2)' const sql2 = 'insert into cash_out_actions (tx_id, action) values ($1, $2)'
if (noPhone) return {noPhone: noPhone} if (noPhone) return {noPhone: noPhone}
return db.none(sql2, [tx.txId, 'addedPhone']) return db.none(sql2, [tx.id, 'addedPhone'])
.then(() => ({noPhone: noPhone})) .then(() => ({noPhone: noPhone}))
}) })
} }
@ -182,14 +182,14 @@ function normalizeTx (tx) {
tx.txHash = tx.tx_hash tx.txHash = tx.tx_hash
tx.cryptoCode = tx.crypto_code tx.cryptoCode = tx.crypto_code
tx.cryptoAtoms = new BigNumber(tx.crypto_atoms) tx.cryptoAtoms = new BigNumber(tx.crypto_atoms)
tx.sessionId = tx.session_id tx.id = tx.tx_id
tx.to_address = undefined tx.to_address = undefined
tx.currency_code = undefined tx.currency_code = undefined
tx.tx_hash = undefined tx.tx_hash = undefined
tx.crypto_code = undefined tx.crypto_code = undefined
tx.satoshis = undefined tx.satoshis = undefined
tx.session_id = undefined tx.tx_id = undefined
// Eventually turn this into BigDecimal, for now, integer // Eventually turn this into BigDecimal, for now, integer
tx.fiat = parseInt(tx.fiat, 10) tx.fiat = parseInt(tx.fiat, 10)
@ -212,35 +212,35 @@ exports.fetchPhoneTxs = function fetchPhoneTxs (phone, dispenseTimeout) {
.then(rows => normalizeTxs(rows)) .then(rows => normalizeTxs(rows))
} }
exports.fetchTx = function fetchTx (session) { exports.fetchTx = function fetchTx (txId) {
const sql = 'SELECT * FROM cash_out_txs WHERE session_id=$1' const sql = 'SELECT * FROM cash_out_txs WHERE tx_id=$1'
return db.one(sql, [session.id]) return db.one(sql, [txId])
.then(row => normalizeTx(row)) .then(row => normalizeTx(row))
} }
exports.addDispenseRequest = function addDispenseRequest (tx) { exports.addDispenseRequest = function addDispenseRequest (tx) {
const sql = 'update cash_out_txs set dispensed=$1 where session_id=$2 and dispensed=$3' const sql = 'update cash_out_txs set dispensed=$1 where tx_id=$2 and dispensed=$3'
const values = [true, tx.sessionId, false] const values = [true, tx.id, false]
return db.result(sql, values) return db.result(sql, values)
.then(results => { .then(results => {
const alreadyDispensed = results.rowCount === 0 const alreadyDispensed = results.rowCount === 0
if (alreadyDispensed) return {dispense: false, reason: 'alreadyDispensed'} if (alreadyDispensed) return {dispense: false, reason: 'alreadyDispensed'}
const sql2 = 'insert into cash_out_actions (session_id, action) values ($1, $2)' const sql2 = 'insert into cash_out_actions (tx_id, action) values ($1, $2)'
return db.none(sql2, [tx.sessionId, 'dispenseRequested']) return db.none(sql2, [tx.id, 'dispenseRequested'])
.then(() => ({dispense: true})) .then(() => ({dispense: true}))
}) })
} }
exports.addDispense = function addDispense (session, tx, cartridges) { exports.addDispense = function addDispense (deviceId, tx, cartridges) {
return insertDispense(session, tx, cartridges) return insertDispense(deviceId, tx, cartridges)
.then(() => { .then(() => {
const sql2 = 'insert into cash_out_actions (session_id, action) values ($1, $2)' const sql2 = 'insert into cash_out_actions (tx_id, action) values ($1, $2)'
return db.none(sql2, [tx.sessionId, 'dispensed']) return db.none(sql2, [tx.id, 'dispensed'])
}) })
} }
@ -321,8 +321,8 @@ exports.updateTxStatus = function updateTxStatus (tx, status) {
const tmSRD = new TransactionMode({tiLevel: isolationLevel.serializable}) const tmSRD = new TransactionMode({tiLevel: isolationLevel.serializable})
function transaction (t) { function transaction (t) {
const sql = 'select status, confirmation_time from cash_out_txs where session_id=$1' const sql = 'select status, confirmation_time from cash_out_txs where tx_id=$1'
return t.one(sql, [tx.sessionId]) return t.one(sql, [tx.id])
.then(row => { .then(row => {
const newStatus = ratchetStatus(row.status, status) const newStatus = ratchetStatus(row.status, status)
if (row.status === newStatus) return if (row.status === newStatus) return
@ -331,10 +331,10 @@ exports.updateTxStatus = function updateTxStatus (tx, status) {
(newStatus === 'instant' || newStatus === 'confirmed') (newStatus === 'instant' || newStatus === 'confirmed')
const sql2 = setConfirmationTime const sql2 = setConfirmationTime
? 'UPDATE cash_out_txs SET status=$1, confirmation_time=now() WHERE session_id=$2' ? 'UPDATE cash_out_txs SET status=$1, confirmation_time=now() WHERE tx_id=$2'
: 'UPDATE cash_out_txs SET status=$1 WHERE session_id=$2' : 'UPDATE cash_out_txs SET status=$1 WHERE tx_id=$2'
const values2 = [newStatus, tx.sessionId] const values2 = [newStatus, tx.id]
return t.none(sql2, values2) return t.none(sql2, values2)
.then(() => ({status: newStatus})) .then(() => ({status: newStatus}))
@ -349,12 +349,12 @@ exports.updateTxStatus = function updateTxStatus (tx, status) {
.then(r => { .then(r => {
if (!r) return if (!r) return
const sql3 = 'insert into cash_out_actions (session_id, action) values ($1, $2)' const sql3 = 'insert into cash_out_actions (tx_id, action) values ($1, $2)'
return db.none(sql3, [tx.sessionId, r.status]) return db.none(sql3, [tx.id, r.status])
.then(() => { .then(() => {
if (r.status === 'confirmed') { if (r.status === 'confirmed') {
const sql4 = `update cash_out_hds set confirmed=true where session_id=$1` const sql4 = `update cash_out_hds set confirmed=true where tx_id=$1`
return db.none(sql4, [tx.sessionId]) return db.none(sql4, [tx.id])
} }
}) })
}) })
@ -366,19 +366,19 @@ exports.updateRedeem = function updateRedeem (txId) {
return db.none(sql, values) return db.none(sql, values)
.then(() => { .then(() => {
const sql2 = 'insert into cash_out_actions (session_id, action) values ($1, $2)' const sql2 = 'insert into cash_out_actions (tx_id, action) values ($1, $2)'
return db.none(sql2, [txId, 'redeem']) return db.none(sql2, [txId, 'redeem'])
}) })
} }
exports.updateNotify = function updateNotify (tx) { exports.updateNotify = function updateNotify (tx) {
const sql = 'UPDATE cash_out_txs SET notified=$1 WHERE session_id=$2' const sql = 'UPDATE cash_out_txs SET notified=$1 WHERE tx_id=$2'
const values = [true, tx.sessionId] const values = [true, tx.id]
return db.none(sql, values) return db.none(sql, values)
.then(() => { .then(() => {
const sql2 = 'insert into cash_out_actions (session_id, action) values ($1, $2)' const sql2 = 'insert into cash_out_actions (tx_id, action) values ($1, $2)'
return db.none(sql2, [tx.sessionId, 'notified']) return db.none(sql2, [tx.id, 'notified'])
}) })
} }
@ -435,15 +435,15 @@ exports.cacheResponse = function (deviceId, txId, path, method, body) {
return db.none(sql, values) return db.none(sql, values)
} }
exports.nextCashOutSerialHD = function nextCashOutSerialHD (sessionId, cryptoCode) { exports.nextCashOutSerialHD = function nextCashOutSerialHD (txId, cryptoCode) {
const sql = `select hd_serial from cash_out_hds const sql = `select hd_serial from cash_out_hds
where crypto_code=$1 order by hd_serial desc limit 1` where crypto_code=$1 order by hd_serial desc limit 1`
const attempt = () => db.oneOrNone(sql, [cryptoCode]) const attempt = () => db.oneOrNone(sql, [cryptoCode])
.then(row => { .then(row => {
const serialNumber = row ? row.hd_serial + 1 : 0 const serialNumber = row ? row.hd_serial + 1 : 0
const fields2 = ['session_id', 'crypto_code', 'hd_serial'] const fields2 = ['tx_id', 'crypto_code', 'hd_serial']
const sql2 = getInsertQuery('cash_out_hds', fields2) const sql2 = getInsertQuery('cash_out_hds', fields2)
const values2 = [sessionId, cryptoCode, serialNumber] const values2 = [txId, cryptoCode, serialNumber]
return db.none(sql2, values2) return db.none(sql2, values2)
.then(() => serialNumber) .then(() => serialNumber)
}) })
@ -453,7 +453,7 @@ exports.nextCashOutSerialHD = function nextCashOutSerialHD (sessionId, cryptoCod
exports.fetchLiveHD = function fetchLiveHD () { exports.fetchLiveHD = function fetchLiveHD () {
const sql = `select * from cash_out_txs, cash_out_hds const sql = `select * from cash_out_txs, cash_out_hds
where cash_out_txs.session_id=cash_out_hds.session_id where cash_out_txs.tx_id=cash_out_hds.tx_id
and status=$1 and swept=$2 and and status=$1 and swept=$2 and
((extract(epoch from (now() - cash_out_txs.created))) * 1000)<$3` ((extract(epoch from (now() - cash_out_txs.created))) * 1000)<$3`
@ -471,7 +471,7 @@ exports.fetchOldHD = function fetchLiveHD () {
return db.any(sql) return db.any(sql)
} }
exports.markSwept = function markSwept (sessionId) { exports.markSwept = function markSwept (txId) {
const sql = `update cash_out_hds set swept=$1 where session_id=$2` const sql = `update cash_out_hds set swept=$1 where tx_id=$2`
return db.none(sql, [true, sessionId]) return db.none(sql, [true, txId])
} }

View file

@ -155,7 +155,7 @@ function cashOut (req, res) {
const tx = req.body const tx = req.body
tx.cryptoAtoms = new BigNumber(tx.cryptoAtoms) tx.cryptoAtoms = new BigNumber(tx.cryptoAtoms)
return plugins.cashOut(tx) return plugins.cashOut(getDeviceId(req), tx)
.then(cryptoAddress => res.json({toAddress: cryptoAddress})) .then(cryptoAddress => res.json({toAddress: cryptoAddress}))
.catch(err => { .catch(err => {
res.json({ res.json({
@ -167,7 +167,7 @@ function cashOut (req, res) {
} }
function dispenseAck (req, res) { function dispenseAck (req, res) {
plugins.dispenseAck(req.body) plugins.dispenseAck(getDeviceId(req), req.body)
res.json({success: true}) res.json({success: true})
} }