This commit is contained in:
Josh Harvey 2016-07-28 18:12:04 +03:00
parent 9a9c927801
commit b45e9ef148

View file

@ -48,9 +48,9 @@ exports.recordBill = function recordBill (deviceId, rec) {
'currency_code', 'currency_code',
'crypto_code', 'crypto_code',
'to_address', 'to_address',
'tx_id', 'cash_in_txs_id',
'device_time', 'device_time',
'satoshis', 'crypto_atoms',
'denomination' 'denomination'
] ]
@ -84,7 +84,7 @@ 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 txId. // a unique cash_in_txs record in the table already keyed by txId.
exports.addOutgoingTx = function addOutgoingTx (deviceId, tx) { exports.addOutgoingTx = function addOutgoingTx (deviceId, tx) {
const fields = ['tx_id', 'device_id', 'to_address', const fields = ['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'
] ]
@ -107,12 +107,12 @@ exports.addOutgoingTx = function addOutgoingTx (deviceId, 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 tx_id=$3` const sql = `update cash_in_txs set tx_hash=$1, error=$2 where id=$3`
return db.none(sql, [txHash, error, tx.id]) return db.none(sql, [txHash, error, tx.id])
} }
exports.addInitialIncoming = function addInitialIncoming (deviceId, tx) { exports.addInitialIncoming = function addInitialIncoming (deviceId, tx) {
const fields = ['tx_id', 'device_id', 'to_address', const fields = ['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'
] ]
@ -135,7 +135,7 @@ exports.addInitialIncoming = function addInitialIncoming (deviceId, tx) {
function insertDispense (deviceId, tx, cartridges) { function insertDispense (deviceId, tx, cartridges) {
const fields = [ const fields = [
'device_id', 'tx_id', 'device_id', 'cash_out_txs_id',
'dispense1', 'reject1', 'count1', 'dispense1', 'reject1', 'count1',
'dispense2', 'reject2', 'count2', 'dispense2', 'reject2', 'count2',
'refill', 'error' 'refill', 'error'
@ -160,14 +160,14 @@ function insertDispense (deviceId, tx, cartridges) {
exports.addIncomingPhone = function addIncomingPhone (tx, notified) { exports.addIncomingPhone = function addIncomingPhone (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 tx_id=$3 WHERE id=$3
AND phone IS NULL` AND phone IS NULL`
const values = [tx.phone, notified, tx.id] const values = [tx.phone, notified, tx.id]
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 (tx_id, action) values ($1, $2)' const sql2 = 'insert into cash_out_actions (cash_out_txs_id, action) values ($1, $2)'
if (noPhone) return {noPhone: noPhone} if (noPhone) return {noPhone: noPhone}
@ -182,14 +182,11 @@ 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.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.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)
@ -213,14 +210,14 @@ exports.fetchPhoneTxs = function fetchPhoneTxs (phone, dispenseTimeout) {
} }
exports.fetchTx = function fetchTx (txId) { exports.fetchTx = function fetchTx (txId) {
const sql = 'SELECT * FROM cash_out_txs WHERE tx_id=$1' const sql = 'SELECT * FROM cash_out_txs WHERE id=$1'
return db.one(sql, [txId]) 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 tx_id=$2 and dispensed=$3' const sql = 'update cash_out_txs set dispensed=$1 where id=$2 and dispensed=$3'
const values = [true, tx.id, false] const values = [true, tx.id, false]
return db.result(sql, values) return db.result(sql, values)
@ -228,7 +225,7 @@ exports.addDispenseRequest = function addDispenseRequest (tx) {
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 (tx_id, action) values ($1, $2)' const sql2 = 'insert into cash_out_actions (cash_out_txs_id, action) values ($1, $2)'
return db.none(sql2, [tx.id, 'dispenseRequested']) return db.none(sql2, [tx.id, 'dispenseRequested'])
.then(() => ({dispense: true})) .then(() => ({dispense: true}))
@ -238,7 +235,7 @@ exports.addDispenseRequest = function addDispenseRequest (tx) {
exports.addDispense = function addDispense (deviceId, tx, cartridges) { exports.addDispense = function addDispense (deviceId, tx, cartridges) {
return insertDispense(deviceId, tx, cartridges) return insertDispense(deviceId, tx, cartridges)
.then(() => { .then(() => {
const sql2 = 'insert into cash_out_actions (tx_id, action) values ($1, $2)' const sql2 = 'insert into cash_out_actions (cash_out_txs_id, action) values ($1, $2)'
return db.none(sql2, [tx.id, 'dispensed']) return db.none(sql2, [tx.id, 'dispensed'])
}) })
@ -321,7 +318,7 @@ 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 tx_id=$1' const sql = 'select status, confirmation_time from cash_out_txs where id=$1'
return t.one(sql, [tx.id]) return t.one(sql, [tx.id])
.then(row => { .then(row => {
const newStatus = ratchetStatus(row.status, status) const newStatus = ratchetStatus(row.status, status)
@ -331,8 +328,8 @@ 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 tx_id=$2' ? 'UPDATE cash_out_txs SET status=$1, confirmation_time=now() WHERE id=$2'
: 'UPDATE cash_out_txs SET status=$1 WHERE tx_id=$2' : 'UPDATE cash_out_txs SET status=$1 WHERE id=$2'
const values2 = [newStatus, tx.id] const values2 = [newStatus, tx.id]
@ -349,11 +346,11 @@ exports.updateTxStatus = function updateTxStatus (tx, status) {
.then(r => { .then(r => {
if (!r) return if (!r) return
const sql3 = 'insert into cash_out_actions (tx_id, action) values ($1, $2)' const sql3 = 'insert into cash_out_actions (cash_out_txs_id, action) values ($1, $2)'
return db.none(sql3, [tx.id, 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 tx_id=$1` const sql4 = `update cash_out_hds set confirmed=true where id=$1`
return db.none(sql4, [tx.id]) return db.none(sql4, [tx.id])
} }
}) })
@ -366,18 +363,18 @@ exports.updateRedeem = function updateRedeem (txId) {
return db.none(sql, values) return db.none(sql, values)
.then(() => { .then(() => {
const sql2 = 'insert into cash_out_actions (tx_id, action) values ($1, $2)' const sql2 = 'insert into cash_out_actions (cash_out_txs_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 tx_id=$2' const sql = 'UPDATE cash_out_txs SET notified=$1 WHERE id=$2'
const values = [true, tx.id] const values = [true, tx.id]
return db.none(sql, values) return db.none(sql, values)
.then(() => { .then(() => {
const sql2 = 'insert into cash_out_actions (tx_id, action) values ($1, $2)' const sql2 = 'insert into cash_out_actions (cash_out_txs_id, action) values ($1, $2)'
return db.none(sql2, [tx.id, 'notified']) return db.none(sql2, [tx.id, 'notified'])
}) })
} }
@ -441,7 +438,7 @@ exports.nextCashOutSerialHD = function nextCashOutSerialHD (txId, cryptoCode) {
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 = ['tx_id', 'crypto_code', 'hd_serial'] const fields2 = ['id', 'crypto_code', 'hd_serial']
const sql2 = getInsertQuery('cash_out_hds', fields2) const sql2 = getInsertQuery('cash_out_hds', fields2)
const values2 = [txId, cryptoCode, serialNumber] const values2 = [txId, cryptoCode, serialNumber]
return db.none(sql2, values2) return db.none(sql2, values2)
@ -453,7 +450,7 @@ exports.nextCashOutSerialHD = function nextCashOutSerialHD (txId, cryptoCode) {
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.tx_id=cash_out_hds.tx_id where cash_out_txs.id=cash_out_hds.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`
@ -472,6 +469,6 @@ exports.fetchOldHD = function fetchLiveHD () {
} }
exports.markSwept = function markSwept (txId) { exports.markSwept = function markSwept (txId) {
const sql = `update cash_out_hds set swept=$1 where tx_id=$2` const sql = `update cash_out_hds set swept=$1 where id=$2`
return db.none(sql, [true, txId]) return db.none(sql, [true, txId])
} }