From 59e93445ec6c1bc76a3394281b5a6e22d2b4b110 Mon Sep 17 00:00:00 2001 From: Josh Harvey Date: Sun, 8 May 2016 20:37:52 +0300 Subject: [PATCH] normalize db records to server records --- lib/plugins.js | 10 ++--- lib/postgresql_interface.js | 84 ++++++++++++++++++++++--------------- lib/routes.js | 2 - todo.txt | 10 ++--- 4 files changed, 59 insertions(+), 47 deletions(-) diff --git a/lib/plugins.js b/lib/plugins.js index 61b6b4dc..6fad64a8 100644 --- a/lib/plugins.js +++ b/lib/plugins.js @@ -286,7 +286,7 @@ function executeTx (session, tx, authority, cb) { logger.error(err) return cb(err) } - var cryptoAtomsToSend = toSend.satoshis + var cryptoAtomsToSend = toSend.cryptoAtoms if (cryptoAtomsToSend === 0) { logger.debug('No cryptoAtoms to send') 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 tx = { fiat: 0, - satoshis: new BigNumber(row.satoshis), - toAddress: row.to_address, - currencyCode: row.currency_code, - cryptoCode: row.crypto_code, + cryptoAtoms: new BigNumber(row.cryptoAtoms), + toAddress: row.toAddress, + currencyCode: row.currencyCode, + cryptoCode: row.cryptoCode, incoming: row.incoming } if (!row.incoming) reapOutgoingTx(session, tx) diff --git a/lib/postgresql_interface.js b/lib/postgresql_interface.js index 0701b781..6558790c 100644 --- a/lib/postgresql_interface.js +++ b/lib/postgresql_interface.js @@ -83,7 +83,7 @@ exports.recordBill = function recordBill (session, rec, cb) { rec.toAddress, session.id, rec.deviceTime, - rec.satoshis, + rec.cryptoAtoms, 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) { var sessionId = session.id 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. cb(null, { 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), - 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) { var fiatRemaining = (tx.fiat || totals.billsFiat) - totals.txFiat - var satoshisRemaining = tx.satoshis.eq(0) - ? totals.billsSatoshis.minus(totals.txSatoshis) - : tx.satoshis.minus(totals.txSatoshis) + var cryptoAtomsRemaining = tx.cryptoAtoms.eq(0) + ? totals.billsCryptoAtoms.minus(totals.txCryptoAtoms) + : tx.cryptoAtoms.minus(totals.txCryptoAtoms) var result = { 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}, "computeSendAmount result < 0, this shouldn't happen. " + 'Maybe timeout arrived after machineSend.') result.fiat = 0 - result.satoshis = new BigNumber(0) + result.cryptoAtoms = new BigNumber(0) } return result } @@ -227,7 +227,7 @@ exports.pendingTxs = function pendingTxs (timeoutMS, cb) { var values = [timeoutS] query(client, sql, values, function (err, results) { 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 stage = 'partial_request' var authority = tx.fiat ? 'machine' : 'timeout' - var satoshis = sendAmount.satoshis + var cryptoAtoms = sendAmount.cryptoAtoms 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) { 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 authority = 'machine' - var satoshis = tx.satoshis + var cryptoAtoms = tx.cryptoAtoms 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) { - var realSatoshis = satoshis || new BigNumber(0) - insertTx(client, session, true, tx, realSatoshis, fiat, stage, authority, cb) +function insertIncoming (client, session, tx, cryptoAtoms, fiat, stage, authority, cb) { + var realCryptoAtoms = cryptoAtoms || new BigNumber(0) + 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) { - 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) { var fields = [ 'session_id', @@ -299,7 +299,7 @@ function insertTx (client, session, incoming, tx, satoshis, fiat, stage, incoming, session.fingerprint, tx.toAddress, - satoshis.toString(), + cryptoAtoms, tx.currencyCode, tx.cryptoCode, fiat, @@ -328,12 +328,12 @@ function refreshPendingTx (client, session, cb) { } function addPendingTx (client, session, incoming, currencyCode, cryptoCode, toAddress, - satoshis, cb) { + cryptoAtoms, cb) { var fields = ['device_fingerprint', 'session_id', 'incoming', 'currency_code', 'crypto_code', 'to_address', 'satoshis'] var sql = getInsertQuery('pending_transactions', fields, false) var values = [session.fingerprint, session.id, incoming, currencyCode, - cryptoCode, toAddress, satoshis.toString()] + cryptoCode, toAddress, cryptoAtoms.toString()] query(client, sql, values, function (err) { cb(err) }) @@ -346,7 +346,7 @@ function buildOutgoingTx (client, session, tx, 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) { connect(function (cerr, client, done) { if (cerr) return cb(cerr) @@ -378,7 +378,7 @@ exports.sentCoins = function sentCoins (session, tx, authority, toSend, fee, var newTx = _.clone(tx) newTx.txHash = txHash newTx.error = error - insertOutgoing(client, session, newTx, toSend.satoshis, toSend.fiat, + insertOutgoing(client, session, newTx, toSend.cryptoAtoms, toSend.fiat, 'partial_send', authority, function (err) { done() if (err) logger.error(err) @@ -447,8 +447,8 @@ exports.addInitialIncoming = function addInitialIncoming (session, tx, cb) { async.series([ async.apply(silentQuery, client, 'BEGIN', null), async.apply(addPendingTx, client, session, true, tx.currencyCode, - tx.cryptoCode, tx.toAddress, tx.satoshis), - async.apply(insertIncoming, client, session, tx, tx.satoshis, tx.fiat, + tx.cryptoCode, tx.toAddress, tx.cryptoAtoms), + async.apply(insertIncoming, client, session, tx, tx.cryptoAtoms, tx.fiat, 'initial_request', 'pending') ], function (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) { var sql = 'SELECT * FROM transactions ' + '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) { done() 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) { done() 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) { done() if (err) return cb(err) - cb(null, results) + cb(null, normalizeTxs(results)) }) }) } diff --git a/lib/routes.js b/lib/routes.js index 5033218e..0a854bc3 100644 --- a/lib/routes.js +++ b/lib/routes.js @@ -129,7 +129,6 @@ function stateChange (req, res) { function send (req, res) { var tx = req.body tx.cryptoAtoms = new BigNumber(tx.cryptoAtoms) - tx.satoshis = tx.cryptoAtoms plugins.sendCoins(session(req), tx, function (err, status) { // TODO: use status.statusCode here after confirming machine compatibility @@ -148,7 +147,6 @@ function cashOut (req, res) { logger.info({tx: req.body, cmd: 'cashOut'}) var tx = req.body tx.cryptoAtoms = new BigNumber(tx.cryptoAtoms) - tx.satoshis = tx.cryptoAtoms plugins.cashOut(session(req), req.body, function (err, bitcoinAddress) { if (err) logger.error(err) diff --git a/todo.txt b/todo.txt index c4f0436d..b7baf2b2 100644 --- a/todo.txt +++ b/todo.txt @@ -1,7 +1,3 @@ -- update tx statuses in background - - pull all unpublished and unauthorized txs in last x seconds - - pull all unconfirmed txs - - for each tx: poll wallet plugin to check status - - update db - -- translate back and forth between db fields and json fields of tx +- change satoshis to crypto_atoms in db (ask neal about this) +- test bitgo +- start testing