implement confirmation sms, bug fixes

This commit is contained in:
Josh Harvey 2016-05-09 18:40:31 +03:00
parent 380f47082e
commit 014033b73e
5 changed files with 134 additions and 18 deletions

View file

@ -22,6 +22,9 @@ var PENDING_TIMEOUT = 70 * 1000
var INCOMING_TX_INTERVAL = 5 * 1000
var LIVE_INCOMING_TX_INTERVAL = 30 * 1000
var STALE_INCOMING_TX_AGE = 7 * 24 * 60 * 60 * 1000
var UNNOTIFIED_INTERVAL = 60 * 1000
var MAX_NOTIFY_AGE = 48 * 60 * 60 * 1000
var MIN_NOTIFY_AGE = 5 * 60 * 1000
if (argv.timeout) PENDING_TIMEOUT = argv.timeout / 1000
@ -442,21 +445,42 @@ exports.fiatBalance = function fiatBalance (cryptoCode) {
}
function processTxStatus (tx) {
const cryptoCode = tx.cryptoCode
const walletPlugin = walletPlugins[cryptoCode]
return new Promise((resolve, reject) => {
const cryptoCode = tx.cryptoCode
const walletPlugin = walletPlugins[cryptoCode]
walletPlugin.getStatus(tx.toAddress, tx.cryptoAtoms, function (err, res) {
if (err) return logger.error(err)
console.log('DEBUG5')
console.log(res)
var status = res.status
if (tx.status === status) return
db.updateTxStatus(tx, status, function (_err) {
if (_err) return logger.error(err)
walletPlugin.getStatus(tx.toAddress, tx.cryptoAtoms, function (err, res) {
if (err) {
logger.error(err)
return resolve() // Resolve on error because we ignore errors
}
var status = res.status
if (tx.status === status) return resolve()
db.updateTxStatus(tx, status, function (_err) {
if (_err) logger.error(err)
resolve()
})
})
})
}
function notifyConfirmation (tx) {
logger.debug('notifyConfirmation\n%j', tx)
const phone = tx.phone
const rec = {
sms: {
toNumber: phone,
body: 'Your cash is waiting! Go to the Cryptomat and press Redeem.'
}
}
return smsPlugin.sendMessage(rec)
.then(() => db.updateNotify(tx))
}
function monitorLiveIncoming () {
const statuses = ['notSeen', 'published']
db.fetchOpenTxs(statuses, STALE_INCOMING_TX_AGE, function (err, txs) {
@ -469,7 +493,17 @@ function monitorIncoming () {
const statuses = ['notSeen', 'published', 'authorized', 'instant', 'rejected']
db.fetchOpenTxs(statuses, STALE_INCOMING_TX_AGE, function (err, txs) {
if (err) return
txs.forEach(processTxStatus)
const promises = txs.map(tx => processTxStatus(tx))
return Promise.all(promises)
.then(monitorUnnotified)
})
}
function monitorUnnotified () {
db.fetchUnnotifiedTxs(MAX_NOTIFY_AGE, MIN_NOTIFY_AGE, function (err, txs) {
if (err) return
txs.forEach(notifyConfirmation)
})
}
@ -488,6 +522,11 @@ exports.startPolling = function startPolling () {
setInterval(reapTxs, REAP_RATE)
setInterval(monitorLiveIncoming, LIVE_INCOMING_TX_INTERVAL)
setInterval(monitorIncoming, INCOMING_TX_INTERVAL)
setInterval(monitorUnnotified, UNNOTIFIED_INTERVAL)
monitorLiveIncoming()
monitorIncoming()
monitorUnnotified()
}
function startTrader (cryptoCode) {
@ -763,6 +802,10 @@ exports.updatePhone = function updatePhone (session, tx) {
return db.addIncomingPhone(session, tx)
}
exports.registerRedeem = function registerRedeem (session) {
return db.updateRedeem(session)
}
exports.fetchPhoneTx = function fetchPhoneTx (phone) {
db.fetchPhoneTxs(phone)
.then(txs => {