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'
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) {