diff --git a/lib/plugins.js b/lib/plugins.js index 9cc982b3..98e9500c 100644 --- a/lib/plugins.js +++ b/lib/plugins.js @@ -1,6 +1,7 @@ 'use strict' const fs = require('fs') +const uuid = require('node-uuid') const R = require('ramda') const async = require('async') const HKDF = require('node-hkdf-sync') @@ -9,24 +10,25 @@ BigNumber.config({CRYPTO: true}) const db = require('./postgresql_interface') const logger = require('./logger') const notifier = require('./notifier') +const T = require('./time') -const uuid = require('node-uuid') const tradeIntervals = {} -const CHECK_NOTIFICATION_INTERVAL = 60 * 1000 -const ALERT_SEND_INTERVAL = 60 * 60 * 1000 -const POLLING_RATE = 60 * 1000 // poll each minute -const INCOMING_TX_INTERVAL = 5 * 1000 -const LIVE_INCOMING_TX_INTERVAL = 30 * 1000 -const STALE_INCOMING_TX_AGE = 7 * 24 * 60 * 60 * 1000 -const UNNOTIFIED_INTERVAL = 60 * 1000 -const MAX_NOTIFY_AGE = 48 * 60 * 60 * 1000 -const MIN_NOTIFY_AGE = 5 * 60 * 1000 -const TRANSACTION_EXPIRATION = 48 * 60 * 60 * 1000 -const SWEEP_LIVE_HD_INTERVAL = 60 * 1000 -const SWEEP_OLD_HD_INTERVAL = 2 * 60 * 1000 -const TRADE_INTERVAL = 60 * 1000 -const TRADE_TTL = 5 * 60 * 1000 +const CHECK_NOTIFICATION_INTERVAL = T.minute +const ALERT_SEND_INTERVAL = T.hour +const POLLING_RATE = T.minute +const INCOMING_TX_INTERVAL = 30 * T.seconds +const LIVE_INCOMING_TX_INTERVAL = 5 * T.seconds +const STALE_INCOMING_TX_AGE = T.week +const STALE_LIVE_INCOMING_TX_AGE = 10 * T.minutes +const UNNOTIFIED_INTERVAL = T.minute +const MAX_NOTIFY_AGE = 2 * T.days +const MIN_NOTIFY_AGE = 5 * T.minutes +const TRANSACTION_EXPIRATION = 2 * T.days +const SWEEP_LIVE_HD_INTERVAL = T.minute +const SWEEP_OLD_HD_INTERVAL = 2 * T.minutes +const TRADE_INTERVAL = T.minute +const TRADE_TTL = 5 * T.minutes let cryptoCodes = null @@ -357,7 +359,11 @@ exports.cashOut = function cashOut (session, tx) { const cryptoCode = tx.cryptoCode || 'BTC' const walletPlugin = walletPlugins[cryptoCode] - return db.nextCashOutSerialHD(tx.sessionId, cryptoCode) + const serialPromise = walletPlugin.supportsHD + ? db.nextCashOutSerialHD(tx.sessionId, cryptoCode) + : Promise.resolve() + + return serialPromise .then(serialNumber => new Promise((resolve, reject) => { const tmpInfo = { label: 'TX ' + Date.now(), @@ -407,11 +413,14 @@ function processTxStatus (tx) { const cryptoCode = tx.cryptoCode const walletPlugin = walletPlugins[cryptoCode] - if (!walletPlugin) return console.error('No wallet plugins for: ' + cryptoCode) + if (!walletPlugin) return logger.warn('Trying to check tx status but no wallet plugins for: ' + cryptoCode) return walletPlugin.getStatus(tx.toAddress, tx.cryptoAtoms) .then(res => db.updateTxStatus(tx, res.status)) - .catch(err => logger.error('[%s] Tx status processing error: %s', cryptoCode, err.message)) + .catch(err => { + console.log(err.stack) + logger.error('[%s] Tx status processing error: %s', cryptoCode, err.message) + }) } function notifyConfirmation (tx) { @@ -431,7 +440,7 @@ function notifyConfirmation (tx) { function monitorLiveIncoming () { const statuses = ['notSeen', 'published', 'insufficientFunds'] - db.fetchOpenTxs(statuses, STALE_INCOMING_TX_AGE) + db.fetchOpenTxs(statuses, STALE_LIVE_INCOMING_TX_AGE) .then(txs => Promise.all(txs.map(processTxStatus))) .catch(err => logger.error(err)) } @@ -790,6 +799,11 @@ exports.cacheResponse = (session, path, method, body) => db.cacheResponse(sessio function sweepHD (row) { const cryptoCode = row.crypto_code const walletPlugin = walletPlugins[cryptoCode] + + if (!walletPlugin) { + return logger.warn('Trying to sweep but no plugin set up for: ' + cryptoCode) + } + return walletPlugin.sweep(row.hd_serial) .then(txHash => { if (txHash) { diff --git a/lib/time.js b/lib/time.js new file mode 100644 index 00000000..49d49653 --- /dev/null +++ b/lib/time.js @@ -0,0 +1,23 @@ +const seconds = 1000 +const second = seconds +const minutes = 60 * seconds +const minute = minutes +const hours = 60 * minutes +const hour = hours +const days = 24 * hours +const day = days +const weeks = 7 * days +const week = weeks + +module.exports = { + seconds, + second, + minutes, + minute, + hours, + hour, + days, + day, + weeks, + week +} diff --git a/todo.txt b/todo.txt index a33a692f..02fe6200 100644 --- a/todo.txt +++ b/todo.txt @@ -1,4 +1,6 @@ - l-m shouldn't keep polling l-s when not on pending screen (low priority) - scrutinize hkdf, maybe use own simplified version -- test eth cash-out confirmation record in cash_out_hds +- test bitcoind cash-out +- test eth cash out, check that HD is still working +- throttle status check for 3rd services like bitgo