add time.js; fix up times, cash-out

This commit is contained in:
Josh Harvey 2016-06-05 14:11:14 +03:00
parent 01d46f1fe7
commit 4f07af096c
3 changed files with 59 additions and 20 deletions

View file

@ -1,6 +1,7 @@
'use strict' 'use strict'
const fs = require('fs') const fs = require('fs')
const uuid = require('node-uuid')
const R = require('ramda') const R = require('ramda')
const async = require('async') const async = require('async')
const HKDF = require('node-hkdf-sync') const HKDF = require('node-hkdf-sync')
@ -9,24 +10,25 @@ BigNumber.config({CRYPTO: true})
const db = require('./postgresql_interface') const db = require('./postgresql_interface')
const logger = require('./logger') const logger = require('./logger')
const notifier = require('./notifier') const notifier = require('./notifier')
const T = require('./time')
const uuid = require('node-uuid')
const tradeIntervals = {} const tradeIntervals = {}
const CHECK_NOTIFICATION_INTERVAL = 60 * 1000 const CHECK_NOTIFICATION_INTERVAL = T.minute
const ALERT_SEND_INTERVAL = 60 * 60 * 1000 const ALERT_SEND_INTERVAL = T.hour
const POLLING_RATE = 60 * 1000 // poll each minute const POLLING_RATE = T.minute
const INCOMING_TX_INTERVAL = 5 * 1000 const INCOMING_TX_INTERVAL = 30 * T.seconds
const LIVE_INCOMING_TX_INTERVAL = 30 * 1000 const LIVE_INCOMING_TX_INTERVAL = 5 * T.seconds
const STALE_INCOMING_TX_AGE = 7 * 24 * 60 * 60 * 1000 const STALE_INCOMING_TX_AGE = T.week
const UNNOTIFIED_INTERVAL = 60 * 1000 const STALE_LIVE_INCOMING_TX_AGE = 10 * T.minutes
const MAX_NOTIFY_AGE = 48 * 60 * 60 * 1000 const UNNOTIFIED_INTERVAL = T.minute
const MIN_NOTIFY_AGE = 5 * 60 * 1000 const MAX_NOTIFY_AGE = 2 * T.days
const TRANSACTION_EXPIRATION = 48 * 60 * 60 * 1000 const MIN_NOTIFY_AGE = 5 * T.minutes
const SWEEP_LIVE_HD_INTERVAL = 60 * 1000 const TRANSACTION_EXPIRATION = 2 * T.days
const SWEEP_OLD_HD_INTERVAL = 2 * 60 * 1000 const SWEEP_LIVE_HD_INTERVAL = T.minute
const TRADE_INTERVAL = 60 * 1000 const SWEEP_OLD_HD_INTERVAL = 2 * T.minutes
const TRADE_TTL = 5 * 60 * 1000 const TRADE_INTERVAL = T.minute
const TRADE_TTL = 5 * T.minutes
let cryptoCodes = null let cryptoCodes = null
@ -357,7 +359,11 @@ exports.cashOut = function cashOut (session, tx) {
const cryptoCode = tx.cryptoCode || 'BTC' const cryptoCode = tx.cryptoCode || 'BTC'
const walletPlugin = walletPlugins[cryptoCode] 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) => { .then(serialNumber => new Promise((resolve, reject) => {
const tmpInfo = { const tmpInfo = {
label: 'TX ' + Date.now(), label: 'TX ' + Date.now(),
@ -407,11 +413,14 @@ function processTxStatus (tx) {
const cryptoCode = tx.cryptoCode const cryptoCode = tx.cryptoCode
const walletPlugin = walletPlugins[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) return walletPlugin.getStatus(tx.toAddress, tx.cryptoAtoms)
.then(res => db.updateTxStatus(tx, res.status)) .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) { function notifyConfirmation (tx) {
@ -431,7 +440,7 @@ function notifyConfirmation (tx) {
function monitorLiveIncoming () { function monitorLiveIncoming () {
const statuses = ['notSeen', 'published', 'insufficientFunds'] 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))) .then(txs => Promise.all(txs.map(processTxStatus)))
.catch(err => logger.error(err)) .catch(err => logger.error(err))
} }
@ -790,6 +799,11 @@ exports.cacheResponse = (session, path, method, body) => db.cacheResponse(sessio
function sweepHD (row) { function sweepHD (row) {
const cryptoCode = row.crypto_code const cryptoCode = row.crypto_code
const walletPlugin = walletPlugins[cryptoCode] 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) return walletPlugin.sweep(row.hd_serial)
.then(txHash => { .then(txHash => {
if (txHash) { if (txHash) {

23
lib/time.js Normal file
View file

@ -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
}

View file

@ -1,4 +1,6 @@
- l-m shouldn't keep polling l-s when not on pending screen (low priority) - l-m shouldn't keep polling l-s when not on pending screen (low priority)
- scrutinize hkdf, maybe use own simplified version - 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