fix up crypto services

This commit is contained in:
Josh Harvey 2016-12-09 00:10:18 +02:00
parent e24da06a1b
commit 1b89ed5c76
9 changed files with 82 additions and 46 deletions

View file

@ -87,6 +87,15 @@ function plugins (settings) {
}
}
function fetchCurrentConfigVersion () {
const sql = `select id from config_users
where type=$1
order by id desc
limit 1`
return db.one(sql, ['config'])
}
function pollQueries (deviceTime, deviceId, deviceRec) {
const config = configManager.machineScoped(deviceId, settings.config)
const fiatCode = config.fiatCurrency
@ -95,22 +104,29 @@ function plugins (settings) {
config.bottomCashOutDenomination ]
const virtualCartridges = [config.virtualCashOutDenomination]
const tickerPromises = cryptoCodes.map(c => ticker.getRates(fiatCode, c))
const balancePromises = cryptoCodes.map(wallet.balance)
const tickerPromises = cryptoCodes.map(c => ticker.getRates(settings, fiatCode, c))
const balancePromises = cryptoCodes.map(c => wallet.balance(settings, c))
const pingPromise = recordPing(deviceId, deviceTime, deviceRec)
const currentConfigVersionPromise = fetchCurrentConfigVersion()
const promises = [dbm.cartridgeCounts(deviceId), pingPromise].concat(tickerPromises, balancePromises)
const promises = [
dbm.cartridgeCounts(deviceId),
pingPromise,
currentConfigVersionPromise
].concat(tickerPromises, balancePromises)
return Promise.all(promises)
.then(arr => {
const cartridgeCounts = arr[0]
const tickers = arr.slice(2, cryptoCodes.length + 2)
const balances = arr.slice(cryptoCodes.length + 2)
const currentConfigVersion = arr[2]
const tickers = arr.slice(3, cryptoCodes.length + 3)
const balances = arr.slice(cryptoCodes.length + 3)
return {
cartridges: buildCartridges(cartridges, virtualCartridges, cartridgeCounts),
rates: buildRates(deviceId, tickers),
balances: buildBalances(deviceId, balances)
balances: buildBalances(deviceId, balances),
currentConfigVersion
}
})
}
@ -119,7 +135,7 @@ function plugins (settings) {
// a dbm unique dbm record in the table already.
function executeTx (deviceId, tx) {
return dbm.addOutgoingTx(deviceId, tx)
.then(() => wallet.sendCoins(tx.toAddress, tx.cryptoAtoms, tx.cryptoCode))
.then(() => wallet.sendCoins(settings, tx.toAddress, tx.cryptoAtoms, tx.cryptoCode))
.then(txHash => {
const fee = null // Need to fill this out in plugins
const toSend = {cryptoAtoms: tx.cryptoAtoms, fiat: tx.fiat}
@ -144,7 +160,7 @@ function plugins (settings) {
.then(() => {
const market = [fiatCode, cryptoCode].join('')
if (!exchange.active(cryptoCode)) return
if (!exchange.active(settings, cryptoCode)) return
logger.debug('[%s] Pushing trade: %d', market, cryptoAtoms)
if (!tradesQueues[market]) tradesQueues[market] = []
@ -187,7 +203,7 @@ function plugins (settings) {
serialNumber
}
return wallet.newAddress(cryptoCode, info)
return wallet.newAddress(settings, cryptoCode, info)
.then(address => {
const newTx = R.assoc('toAddress', address, tx)
@ -208,7 +224,10 @@ function plugins (settings) {
function fiatBalance (fiatCode, cryptoCode, deviceId) {
const config = configManager.scoped(cryptoCode, deviceId, settings.config)
return Promise.all([ticker.getRates(fiatCode, cryptoCode), wallet.balance(cryptoCode)])
return Promise.all([
ticker.getRates(settings, fiatCode, cryptoCode),
wallet.balance(settings, cryptoCode)
])
.then(([rates, balanceRec]) => {
const rawRate = rates.rates.ask
const commission = (new BigNumber(config.cashInCommission).div(100)).plus(1)
@ -231,7 +250,7 @@ function plugins (settings) {
}
function processTxStatus (tx) {
return wallet.getStatus(tx.toAddress, tx.cryptoAtoms, tx.cryptoCode)
return wallet.getStatus(settings, tx.toAddress, tx.cryptoAtoms, tx.cryptoCode)
.then(res => dbm.updateTxStatus(tx, res.status))
}
@ -355,7 +374,7 @@ function plugins (settings) {
}
function executeTradesForMarket (settings, fiatCode, cryptoCode) {
if (!exchange.active(cryptoCode)) return
if (!exchange.active(settings, cryptoCode)) return
const market = [fiatCode, cryptoCode].join('')
logger.debug('[%s] checking for trades', market)
@ -370,7 +389,7 @@ function plugins (settings) {
logger.debug('[%s] making a trade: %d', market, tradeEntry.cryptoAtoms.toString())
return exchange.buy(tradeEntry.cryptoAtoms, tradeEntry.fiatCode, tradeEntry.cryptoCode)
return exchange.buy(settings, tradeEntry.cryptoAtoms, tradeEntry.fiatCode, tradeEntry.cryptoCode)
.then(() => logger.debug('[%s] Successful trade.', market))
.catch(err => {
tradesQueues[market].push(tradeEntry)
@ -444,7 +463,7 @@ function plugins (settings) {
function sweepHD (row) {
const cryptoCode = row.crypto_code
return wallet.sweep(row.hd_serial)
return wallet.sweep(settings, row.hd_serial)
.then(txHash => {
if (txHash) {
logger.debug('[%s] Swept address with tx: %s', cryptoCode, txHash)