normalize db records to server records

This commit is contained in:
Josh Harvey 2016-05-08 20:37:52 +03:00
parent c4e557e86d
commit 59e93445ec
4 changed files with 59 additions and 47 deletions

View file

@ -286,7 +286,7 @@ function executeTx (session, tx, authority, cb) {
logger.error(err) logger.error(err)
return cb(err) return cb(err)
} }
var cryptoAtomsToSend = toSend.satoshis var cryptoAtomsToSend = toSend.cryptoAtoms
if (cryptoAtomsToSend === 0) { if (cryptoAtomsToSend === 0) {
logger.debug('No cryptoAtoms to send') logger.debug('No cryptoAtoms to send')
return cb(null, {statusCode: 204, txId: tx.txId, txHash: null}) return cb(null, {statusCode: 204, txId: tx.txId, txHash: null})
@ -324,10 +324,10 @@ function reapTx (row) {
var session = {fingerprint: row.device_fingerprint, id: row.session_id} var session = {fingerprint: row.device_fingerprint, id: row.session_id}
var tx = { var tx = {
fiat: 0, fiat: 0,
satoshis: new BigNumber(row.satoshis), cryptoAtoms: new BigNumber(row.cryptoAtoms),
toAddress: row.to_address, toAddress: row.toAddress,
currencyCode: row.currency_code, currencyCode: row.currencyCode,
cryptoCode: row.crypto_code, cryptoCode: row.cryptoCode,
incoming: row.incoming incoming: row.incoming
} }
if (!row.incoming) reapOutgoingTx(session, tx) if (!row.incoming) reapOutgoingTx(session, tx)

View file

@ -83,7 +83,7 @@ exports.recordBill = function recordBill (session, rec, cb) {
rec.toAddress, rec.toAddress,
session.id, session.id,
rec.deviceTime, rec.deviceTime,
rec.satoshis, rec.cryptoAtoms,
rec.fiat rec.fiat
] ]
@ -149,7 +149,7 @@ function silentQuery (client, queryStr, values, cb) {
}) })
} }
// OPTIMIZE: No need to query bills if tx.fiat and tx.satoshis are set // OPTIMIZE: No need to query bills if tx.fiat and tx.cryptoAtoms are set
function billsAndTxs (client, session, cb) { function billsAndTxs (client, session, cb) {
var sessionId = session.id var sessionId = session.id
var fingerprint = session.fingerprint var fingerprint = session.fingerprint
@ -174,9 +174,9 @@ function billsAndTxs (client, session, cb) {
// we need to parse, since we know these won't be huge numbers. // we need to parse, since we know these won't be huge numbers.
cb(null, { cb(null, {
billsFiat: parseInt(results[0].rows[0].fiat, 10), billsFiat: parseInt(results[0].rows[0].fiat, 10),
billsSatoshis: new BigNumber(results[0].rows[0].satoshis), billsCryptoAtoms: new BigNumber(results[0].rows[0].satoshis),
txFiat: parseInt(results[1].rows[0].fiat, 10), txFiat: parseInt(results[1].rows[0].fiat, 10),
txSatoshis: new BigNumber(results[1].rows[0].satoshis) txCryptoAtoms: new BigNumber(results[1].rows[0].satoshis)
}) })
}) })
} }
@ -184,20 +184,20 @@ function billsAndTxs (client, session, cb) {
function computeSendAmount (tx, totals) { function computeSendAmount (tx, totals) {
var fiatRemaining = (tx.fiat || totals.billsFiat) - totals.txFiat var fiatRemaining = (tx.fiat || totals.billsFiat) - totals.txFiat
var satoshisRemaining = tx.satoshis.eq(0) var cryptoAtomsRemaining = tx.cryptoAtoms.eq(0)
? totals.billsSatoshis.minus(totals.txSatoshis) ? totals.billsCryptoAtoms.minus(totals.txCryptoAtoms)
: tx.satoshis.minus(totals.txSatoshis) : tx.cryptoAtoms.minus(totals.txCryptoAtoms)
var result = { var result = {
fiat: fiatRemaining, fiat: fiatRemaining,
satoshis: satoshisRemaining cryptoAtoms: cryptoAtomsRemaining
} }
if (result.fiat < 0 || result.satoshis.lt(0)) { if (result.fiat < 0 || result.cryptoAtoms.lt(0)) {
logger.warn({tx: tx, totals: totals, result: result}, logger.warn({tx: tx, totals: totals, result: result},
"computeSendAmount result < 0, this shouldn't happen. " + "computeSendAmount result < 0, this shouldn't happen. " +
'Maybe timeout arrived after machineSend.') 'Maybe timeout arrived after machineSend.')
result.fiat = 0 result.fiat = 0
result.satoshis = new BigNumber(0) result.cryptoAtoms = new BigNumber(0)
} }
return result return result
} }
@ -227,7 +227,7 @@ exports.pendingTxs = function pendingTxs (timeoutMS, cb) {
var values = [timeoutS] var values = [timeoutS]
query(client, sql, values, function (err, results) { query(client, sql, values, function (err, results) {
done() done()
cb(err, results) cb(err, normalizeTxs(results))
}) })
}) })
} }
@ -242,14 +242,14 @@ function insertOutgoingTx (client, session, tx, totals, cb) {
var sendAmount = computeSendAmount(tx, totals) var sendAmount = computeSendAmount(tx, totals)
var stage = 'partial_request' var stage = 'partial_request'
var authority = tx.fiat ? 'machine' : 'timeout' var authority = tx.fiat ? 'machine' : 'timeout'
var satoshis = sendAmount.satoshis var cryptoAtoms = sendAmount.cryptoAtoms
var fiat = sendAmount.fiat var fiat = sendAmount.fiat
if (satoshis.eq(0)) return cb(null, {satoshis: new BigNumber(0), fiat: 0}) if (cryptoAtoms.eq(0)) return cb(null, {cryptoAtoms: new BigNumber(0), fiat: 0})
insertOutgoing(client, session, tx, satoshis, fiat, stage, authority, insertOutgoing(client, session, tx, cryptoAtoms, fiat, stage, authority,
function (err) { function (err) {
if (err) return cb(err) if (err) return cb(err)
cb(null, {satoshis: satoshis, fiat: fiat}) cb(null, {cryptoAtoms: cryptoAtoms, fiat: fiat})
}) })
} }
@ -259,22 +259,22 @@ function insertOutgoingCompleteTx (client, session, tx, cb) {
var stage = 'final_request' var stage = 'final_request'
var authority = 'machine' var authority = 'machine'
var satoshis = tx.satoshis var cryptoAtoms = tx.cryptoAtoms
var fiat = tx.fiat var fiat = tx.fiat
insertOutgoing(client, session, tx, satoshis, fiat, stage, authority, cb) insertOutgoing(client, session, tx, cryptoAtoms, fiat, stage, authority, cb)
} }
function insertIncoming (client, session, tx, satoshis, fiat, stage, authority, cb) { function insertIncoming (client, session, tx, cryptoAtoms, fiat, stage, authority, cb) {
var realSatoshis = satoshis || new BigNumber(0) var realCryptoAtoms = cryptoAtoms || new BigNumber(0)
insertTx(client, session, true, tx, realSatoshis, fiat, stage, authority, cb) insertTx(client, session, true, tx, realCryptoAtoms, fiat, stage, authority, cb)
} }
function insertOutgoing (client, session, tx, satoshis, fiat, stage, authority, function insertOutgoing (client, session, tx, cryptoAtoms, fiat, stage, authority,
cb) { cb) {
insertTx(client, session, false, tx, satoshis, fiat, stage, authority, cb) insertTx(client, session, false, tx, cryptoAtoms, fiat, stage, authority, cb)
} }
function insertTx (client, session, incoming, tx, satoshis, fiat, stage, function insertTx (client, session, incoming, tx, cryptoAtoms, fiat, stage,
authority, cb) { authority, cb) {
var fields = [ var fields = [
'session_id', 'session_id',
@ -299,7 +299,7 @@ function insertTx (client, session, incoming, tx, satoshis, fiat, stage,
incoming, incoming,
session.fingerprint, session.fingerprint,
tx.toAddress, tx.toAddress,
satoshis.toString(), cryptoAtoms,
tx.currencyCode, tx.currencyCode,
tx.cryptoCode, tx.cryptoCode,
fiat, fiat,
@ -328,12 +328,12 @@ function refreshPendingTx (client, session, cb) {
} }
function addPendingTx (client, session, incoming, currencyCode, cryptoCode, toAddress, function addPendingTx (client, session, incoming, currencyCode, cryptoCode, toAddress,
satoshis, cb) { cryptoAtoms, cb) {
var fields = ['device_fingerprint', 'session_id', 'incoming', var fields = ['device_fingerprint', 'session_id', 'incoming',
'currency_code', 'crypto_code', 'to_address', 'satoshis'] 'currency_code', 'crypto_code', 'to_address', 'satoshis']
var sql = getInsertQuery('pending_transactions', fields, false) var sql = getInsertQuery('pending_transactions', fields, false)
var values = [session.fingerprint, session.id, incoming, currencyCode, var values = [session.fingerprint, session.id, incoming, currencyCode,
cryptoCode, toAddress, satoshis.toString()] cryptoCode, toAddress, cryptoAtoms.toString()]
query(client, sql, values, function (err) { query(client, sql, values, function (err) {
cb(err) cb(err)
}) })
@ -346,7 +346,7 @@ function buildOutgoingTx (client, session, tx, cb) {
], cb) ], cb)
} }
// Calling function should only send bitcoins if result.satoshisToSend > 0 // Calling function should only send bitcoins if result.cryptoAtomsToSend > 0
exports.addOutgoingTx = function addOutgoingTx (session, tx, cb) { exports.addOutgoingTx = function addOutgoingTx (session, tx, cb) {
connect(function (cerr, client, done) { connect(function (cerr, client, done) {
if (cerr) return cb(cerr) if (cerr) return cb(cerr)
@ -378,7 +378,7 @@ exports.sentCoins = function sentCoins (session, tx, authority, toSend, fee,
var newTx = _.clone(tx) var newTx = _.clone(tx)
newTx.txHash = txHash newTx.txHash = txHash
newTx.error = error newTx.error = error
insertOutgoing(client, session, newTx, toSend.satoshis, toSend.fiat, insertOutgoing(client, session, newTx, toSend.cryptoAtoms, toSend.fiat,
'partial_send', authority, function (err) { 'partial_send', authority, function (err) {
done() done()
if (err) logger.error(err) if (err) logger.error(err)
@ -447,8 +447,8 @@ exports.addInitialIncoming = function addInitialIncoming (session, tx, cb) {
async.series([ async.series([
async.apply(silentQuery, client, 'BEGIN', null), async.apply(silentQuery, client, 'BEGIN', null),
async.apply(addPendingTx, client, session, true, tx.currencyCode, async.apply(addPendingTx, client, session, true, tx.currencyCode,
tx.cryptoCode, tx.toAddress, tx.satoshis), tx.cryptoCode, tx.toAddress, tx.cryptoAtoms),
async.apply(insertIncoming, client, session, tx, tx.satoshis, tx.fiat, async.apply(insertIncoming, client, session, tx, tx.cryptoAtoms, tx.fiat,
'initial_request', 'pending') 'initial_request', 'pending')
], function (err) { ], function (err) {
if (err) { if (err) {
@ -500,6 +500,24 @@ exports.addIncomingPhone = function addIncomingPhone (session, tx, cb) {
}) })
} }
function normalizeTxs (txs) {
return txs.map(function (tx) {
tx.toAddress = tx.to_address
tx.currencyCode = tx.currency_code
tx.txHash = tx.tx_hash
tx.cryptoCode = tx.crypto_code
tx.cryptoAtoms = new BigNumber(tx.satoshis)
tx.to_address = undefined
tx.currency_code = undefined
tx.tx_hash = undefined
tx.crypto_code = undefined
tx.satoshis = undefined
return tx
})
}
exports.fetchPhoneTxs = function fetchPhoneTxs (phone, dispenseTimeout, cb) { exports.fetchPhoneTxs = function fetchPhoneTxs (phone, dispenseTimeout, cb) {
var sql = 'SELECT * FROM transactions ' + var sql = 'SELECT * FROM transactions ' +
'WHERE phone=$1 AND dispensed=$2 ' + 'WHERE phone=$1 AND dispensed=$2 ' +
@ -510,7 +528,7 @@ exports.fetchPhoneTxs = function fetchPhoneTxs (phone, dispenseTimeout, cb) {
query(client, sql, [phone, false, dispenseTimeout], function (err, results) { query(client, sql, [phone, false, dispenseTimeout], function (err, results) {
done() done()
if (err) return cb(err) if (err) return cb(err)
cb(null, results.rows) cb(null, normalizeTxs(results.rows))
}) })
}) })
} }
@ -525,7 +543,7 @@ exports.fetchTx = function fetchTx (session, cb) {
query(client, sql, [session.fingerprint, session.id], function (err, results) { query(client, sql, [session.fingerprint, session.id], function (err, results) {
done() done()
if (err) return cb(err) if (err) return cb(err)
cb(null, results.rows) cb(null, normalizeTxs(results.rows))
}) })
}) })
} }
@ -645,7 +663,7 @@ exports.fetchOpenTxs = function fetchOpenTxs (statuses, age, cb) {
query(client, sql, [true, age, statuses], function (err, results) { query(client, sql, [true, age, statuses], function (err, results) {
done() done()
if (err) return cb(err) if (err) return cb(err)
cb(null, results) cb(null, normalizeTxs(results))
}) })
}) })
} }

View file

@ -129,7 +129,6 @@ function stateChange (req, res) {
function send (req, res) { function send (req, res) {
var tx = req.body var tx = req.body
tx.cryptoAtoms = new BigNumber(tx.cryptoAtoms) tx.cryptoAtoms = new BigNumber(tx.cryptoAtoms)
tx.satoshis = tx.cryptoAtoms
plugins.sendCoins(session(req), tx, function (err, status) { plugins.sendCoins(session(req), tx, function (err, status) {
// TODO: use status.statusCode here after confirming machine compatibility // TODO: use status.statusCode here after confirming machine compatibility
@ -148,7 +147,6 @@ function cashOut (req, res) {
logger.info({tx: req.body, cmd: 'cashOut'}) logger.info({tx: req.body, cmd: 'cashOut'})
var tx = req.body var tx = req.body
tx.cryptoAtoms = new BigNumber(tx.cryptoAtoms) tx.cryptoAtoms = new BigNumber(tx.cryptoAtoms)
tx.satoshis = tx.cryptoAtoms
plugins.cashOut(session(req), req.body, function (err, bitcoinAddress) { plugins.cashOut(session(req), req.body, function (err, bitcoinAddress) {
if (err) logger.error(err) if (err) logger.error(err)

View file

@ -1,7 +1,3 @@
- update tx statuses in background - change satoshis to crypto_atoms in db (ask neal about this)
- pull all unpublished and unauthorized txs in last x seconds - test bitgo
- pull all unconfirmed txs - start testing
- for each tx: poll wallet plugin to check status
- update db
- translate back and forth between db fields and json fields of tx