This commit is contained in:
Josh Harvey 2016-04-07 02:58:20 +01:00
parent d6ec60db55
commit 184d30209a
4 changed files with 74 additions and 29 deletions

View file

@ -40,6 +40,11 @@ var lastRates = {}
var tradesQueues = {}
var coins = {
BTC: {unitScale: 8},
ETH: {unitScale: 18}
}
// that's basically a constructor
exports.init = function init (databaseHandle) {
if (!databaseHandle) {
@ -53,7 +58,7 @@ function loadPlugin (name, config) {
// plugins definitions
var moduleMethods = {
ticker: ['ticker'],
trader: ['balance', 'purchase', 'sell'],
trader: ['purchase', 'sell'],
wallet: ['balance', 'sendBitcoins', 'newAddress'],
idVerifier: ['verifyUser', 'verifyTransaction'],
info: ['checkAddress']
@ -151,7 +156,7 @@ exports.configure = function configure (config) {
cachedConfig = config
deviceCurrency = config.exchanges.settings.currency
cryptoCodes = config.exchanges.settings.coins || ['BTC']
cryptoCodes = config.exchanges.settings.coins || ['BTC', 'ETH']
cryptoCodes.forEach(function (cryptoCode) {
// TICKER [required] configure (or load)
@ -248,21 +253,29 @@ function _sendCoins (toAddress, cryptoAtoms, cryptoCode, cb) {
if (cryptoCode === 'BTC') {
walletPlugin.sendBitcoins(toAddress, cryptoAtoms, transactionFee, cb)
} else {
walletPlugin.sendCoins(toAddress, cryptoAtoms, cryptoCode, transactionFee, cb)
walletPlugin.sendBitcoins(toAddress, cryptoAtoms, cryptoCode, transactionFee, cb)
}
}
function executeTx (session, tx, authority, cb) {
db.addOutgoingTx(session, tx, function (err, toSend) {
if (err) return cb(err)
var cryptoAtomsToSend = toSend.cryptoAtoms
if (err) {
logger.error(err)
return cb(err)
}
var cryptoAtomsToSend = toSend.satoshis
if (cryptoAtomsToSend === 0) {
logger.debug('No cryptoAtoms to send')
return cb(null, {statusCode: 204, txId: tx.txId, txHash: null})
}
_sendCoins(tx.toAddress, cryptoAtomsToSend, function (_err, txHash) {
var cryptoCode = tx.cryptoCode
_sendCoins(tx.toAddress, cryptoAtomsToSend, cryptoCode, function (_err, txHash) {
var fee = null // Need to fill this out in plugins
if (_err) toSend = {cryptoAtoms: new BigNumber(0), fiat: 0}
if (_err) {
logger.error(_err)
toSend = {cryptoAtoms: new BigNumber(0), fiat: 0}
}
db.sentCoins(session, tx, authority, toSend, fee, _err, txHash)
if (_err) return cb(_err)
@ -291,6 +304,7 @@ function reapTx (row) {
satoshis: row.satoshis,
toAddress: row.to_address,
currencyCode: row.currency_code,
cryptoCode: row.crypto_code,
incoming: row.incoming
}
if (!row.incoming) reapOutgoingTx(session, tx)
@ -314,6 +328,8 @@ function reapTxs () {
// TODO: Run these in parallel and return success
exports.trade = function trade (session, rawTrade, cb) {
logger.debug('DEBUG2')
// TODO: move this to DB, too
// add bill to trader queue (if trader is enabled)
var cryptoCode = rawTrade.cryptoCode || 'BTC'
@ -328,14 +344,18 @@ exports.trade = function trade (session, rawTrade, cb) {
})
}
logger.debug('DEBUG3')
if (!rawTrade.toAddress) {
var newRawTrade = _.cloneDeep(rawTrade)
newRawTrade.toAddress = 'remit'
return db.recordBill(session, newRawTrade, cb)
}
logger.debug('DEBUG1')
async.parallel([
async.apply(db.addOutgoingPending, session, rawTrade.currency, rawTrade.toAddress),
async.apply(db.addOutgoingPending, session, rawTrade.currency, rawTrade.cryptoCode, rawTrade.toAddress),
async.apply(db.recordBill, session, rawTrade)
], cb)
}
@ -376,17 +396,14 @@ exports.fiatBalance = function fiatBalance (cryptoCode) {
if (!rawRate || !lastBalance) return null
// The rate is actually our commission times real rate.
var rate = commission * rawRate
var rate = rawRate.times(commission)
// `lowBalanceMargin` is our safety net. It's a number > 1, and we divide
// all our balances by it to provide a safety margin.
var lowBalanceMargin = cachedConfig.exchanges.settings.lowBalanceMargin
// `balance.transferBalance` is the balance of our transfer account (the one
// we use to send Bitcoins to clients) in satoshis.
var transferBalance = lastBalance.transferBalance
var fiatTransferBalance = (transferBalance * rate) / lowBalanceMargin
var unitScale = new BigNumber(10).pow(coins[cryptoCode].unitScale)
var fiatTransferBalance = lastBalance.div(unitScale).times(rate).div(lowBalanceMargin)
return fiatTransferBalance
}
@ -441,9 +458,9 @@ function pollBalance (cryptoCode, cb) {
return cb && cb(err)
}
logger.debug('[%s] Balance update:', cryptoCode, balance)
logger.debug('[%s] Balance update: %j', cryptoCode, balance)
balance.timestamp = Date.now()
lastBalances[cryptoCode] = balance
lastBalances[cryptoCode] = new BigNumber(balance[cryptoCode])
return cb && cb(null, lastBalances)
})
@ -472,7 +489,7 @@ function pollRate (cryptoCode, cb) {
rates.ask = rates.ask && new BigNumber(rates.ask)
rates.bid = rates.bid && new BigNumber(rates.bid)
}
logger.debug('got rates: %j', resRates)
logger.debug('[%s] got rates: %j', cryptoCode, resRates)
lastRates[cryptoCode] = resRates
@ -495,9 +512,8 @@ exports.getDeviceRate = function getDeviceRate (cryptoCode) {
exports.getBalance = function getBalance (cryptoCode) {
var lastBalance = lastBalances[cryptoCode]
if (!lastBalance) return null
return lastBalance.transferBalance
return lastBalance
}
/*