This commit is contained in:
Josh Harvey 2016-06-01 02:35:29 +03:00
parent 35cbb46688
commit 0140e01c9f
5 changed files with 38 additions and 39 deletions

View file

@ -3,6 +3,7 @@
const BigNumber = require('bignumber.js')
const pgp = require('pg-promise')()
const backoff = require('u-promised').backoff
const logger = require('./logger')
@ -113,7 +114,7 @@ exports.sentCoins = function sentCoins (session, tx, toSend, fee, error, txHash)
return db.none(sql, [txHash, error, session.id])
}
exports.addInitialIncoming = function addInitialIncoming (session, tx, addressRec) {
exports.addInitialIncoming = function addInitialIncoming (session, tx) {
const fields = ['session_id', 'device_fingerprint', 'to_address',
'crypto_atoms', 'crypto_code', 'currency_code', 'fiat', 'tx_hash',
'phone', 'error'
@ -133,15 +134,6 @@ exports.addInitialIncoming = function addInitialIncoming (session, tx, addressRe
]
return db.none(getInsertQuery('cash_out_txs', fields), values)
.then(() => {
const hd = addressRec.hd
if (hd) {
const fields2 = ['session_id', 'to_address', 'crypto_code',
'hd_path_prefix', 'hd_serial']
const values2 = [tx.sessionId, tx.toAddress, tx.cryptoCode, hd.pathPrefix, hd.serial]
return db.none(getInsertQuery('cash_out_hds', fields2), values2)
}
})
}
function insertDispense (session, tx, cartridges) {
@ -434,9 +426,18 @@ exports.cacheResponse = function (session, path, method, body) {
return db.none(sql, values)
}
exports.nextCashOutSerialHD = function nextCashOutSerialHD (cryptoCode) {
exports.nextCashOutSerialHD = function nextCashOutSerialHD (sessionId, cryptoCode) {
const sql = `select hd_serial from cash_out_hds
where crypto_code=$1 order by hd_serial desc limit 1`
return db.oneOrNone(sql, [cryptoCode])
.then(row => row ? row.hd_serial + 1 : 0)
const attempt = db.oneOrNone(sql, [cryptoCode])
.then(row => {
const serialNumber = row ? row.hd_serial + 1 : 0
const fields2 = ['session_id', 'crypto_code', 'hd_serial']
const sql2 = getInsertQuery('cash_out_hds', fields2)
const values2 = [sessionId, cryptoCode, serialNumber]
return db.none(sql2, values2)
.then(() => serialNumber)
})
return backoff(100, 0, 5, attempt)
}