WIP
This commit is contained in:
parent
258a175dc6
commit
316a240130
3 changed files with 59 additions and 70 deletions
|
|
@ -232,7 +232,7 @@ exports.getConfig = function getConfig () {
|
|||
}
|
||||
|
||||
exports.logEvent = function event (session, rawEvent) {
|
||||
db.recordDeviceEvent(session, rawEvent)
|
||||
return db.recordDeviceEvent(session, rawEvent)
|
||||
}
|
||||
|
||||
function buildCartridges (cartridges, virtualCartridges, rec) {
|
||||
|
|
@ -257,18 +257,26 @@ exports.pollQueries = function pollQueries (session, cb) {
|
|||
if (!cartridges) return cb(null, {})
|
||||
var virtualCartridges = cachedConfig.exchanges.settings.virtualCartridges
|
||||
|
||||
db.cartridgeCounts(session, function (err, result) {
|
||||
if (err) return cb(err)
|
||||
return cb(null, {
|
||||
cartridges: buildCartridges(cartridges, virtualCartridges, result)
|
||||
return db.cartridgeCounts(session)
|
||||
.then(result => ({
|
||||
cartridges: buildCartridges(cartridges, virtualCartridges, result)
|
||||
}))
|
||||
}
|
||||
|
||||
function _sendCoins (toAddress, cryptoAtoms, cryptoCode) {
|
||||
return new Promise((resolve, reject) => {
|
||||
_sendCoinsCb(toAddress, cryptoAtoms, cryptoCode, (err, txHash) => {
|
||||
if (err) return reject(err)
|
||||
return resolve(txHash)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
function _sendCoins (toAddress, cryptoAtoms, cryptoCode, cb) {
|
||||
function _sendCoinsCb (toAddress, cryptoAtoms, cryptoCode, cb) {
|
||||
var walletPlugin = walletPlugins[cryptoCode]
|
||||
var transactionFee = cachedConfig.exchanges.settings.transactionFee
|
||||
logger.debug('Sending coins [%s] to: %s', cryptoCode, toAddress)
|
||||
|
||||
if (cryptoCode === 'BTC') {
|
||||
walletPlugin.sendBitcoins(toAddress, cryptoAtoms.truncated().toNumber(), transactionFee, cb)
|
||||
} else {
|
||||
|
|
@ -276,39 +284,25 @@ function _sendCoins (toAddress, cryptoAtoms, cryptoCode, cb) {
|
|||
}
|
||||
}
|
||||
|
||||
function executeTx (session, tx, cb) {
|
||||
db.addOutgoingTx(session, tx, function (err) {
|
||||
if (err) {
|
||||
logger.error(err)
|
||||
return cb(err)
|
||||
}
|
||||
function executeTx (session, tx) {
|
||||
return db.addOutgoingTx(session, tx)
|
||||
.then(() => _sendCoins(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}
|
||||
|
||||
var cryptoCode = tx.cryptoCode
|
||||
_sendCoins(tx.toAddress, tx.cryptoAtoms, cryptoCode, function (_err, txHash) {
|
||||
var fee = null // Need to fill this out in plugins
|
||||
var toSend = {cryptoAtoms: tx.cryptoAtoms, fiat: tx.fiat}
|
||||
|
||||
if (_err) {
|
||||
logger.error(_err)
|
||||
toSend = {cryptoAtoms: new BigNumber(0), fiat: 0}
|
||||
}
|
||||
db.sentCoins(session, tx, toSend, fee, _err, txHash)
|
||||
|
||||
if (_err) return cb(_err)
|
||||
|
||||
pollBalance(cryptoCode)
|
||||
|
||||
cb(null, {
|
||||
statusCode: 201, // Created
|
||||
txHash: txHash,
|
||||
txId: tx.txId
|
||||
})
|
||||
})
|
||||
return db.sentCoins(session, tx, toSend, fee, null, txHash)
|
||||
.then(() => pollBalance(tx.cryptoCode))
|
||||
.then(() => ({
|
||||
statusCode: 201, // Created
|
||||
txHash: txHash,
|
||||
txId: tx.txId
|
||||
}))
|
||||
})
|
||||
}
|
||||
|
||||
// TODO: Run these in parallel and return success
|
||||
exports.trade = function trade (session, rawTrade, cb) {
|
||||
exports.trade = function trade (session, rawTrade) {
|
||||
// TODO: move this to DB, too
|
||||
// add bill to trader queue (if trader is enabled)
|
||||
var cryptoCode = rawTrade.cryptoCode || 'BTC'
|
||||
|
|
@ -323,7 +317,7 @@ exports.trade = function trade (session, rawTrade, cb) {
|
|||
})
|
||||
}
|
||||
|
||||
db.recordBill(session, rawTrade, cb)
|
||||
return db.recordBill(session, rawTrade)
|
||||
}
|
||||
|
||||
exports.stateChange = function stateChange (session, rec, cb) {
|
||||
|
|
@ -334,7 +328,7 @@ exports.stateChange = function stateChange (session, rec, cb) {
|
|||
note: JSON.stringify({state: rec.state, isIdle: rec.isIdle, sessionId: session.id}),
|
||||
deviceTime: session.deviceTime
|
||||
}
|
||||
db.machineEvent(event, cb)
|
||||
return db.machineEvent(event)
|
||||
}
|
||||
|
||||
exports.recordPing = function recordPing (session, rec, cb) {
|
||||
|
|
@ -345,15 +339,15 @@ exports.recordPing = function recordPing (session, rec, cb) {
|
|||
note: JSON.stringify({state: rec.state, isIdle: rec.idle === 'true', sessionId: session.id}),
|
||||
deviceTime: session.deviceTime
|
||||
}
|
||||
db.machineEvent(event, cb)
|
||||
return db.machineEvent(event)
|
||||
}
|
||||
|
||||
exports.sendCoins = function sendCoins (session, rawTx, cb) {
|
||||
exports.sendCoins = function sendCoins (session, rawTx) {
|
||||
var _session = {id: rawTx.sessionId || session.id, fingerprint: session.fingerprint}
|
||||
executeTx(_session, rawTx, cb)
|
||||
return executeTx(_session, rawTx)
|
||||
}
|
||||
|
||||
exports.cashOut = function cashOut (session, tx, cb) {
|
||||
exports.cashOut = function cashOut (session, tx) {
|
||||
var tmpInfo = {
|
||||
label: 'TX ' + Date.now(),
|
||||
account: 'deposit'
|
||||
|
|
@ -362,12 +356,13 @@ exports.cashOut = function cashOut (session, tx, cb) {
|
|||
var cryptoCode = tx.cryptoCode || 'BTC'
|
||||
var walletPlugin = walletPlugins[cryptoCode]
|
||||
|
||||
walletPlugin.newAddress(tmpInfo, function (err, address) {
|
||||
if (err) return cb(err)
|
||||
return new Promise((resolve, reject) => {
|
||||
walletPlugin.newAddress(tmpInfo, function (err, address) {
|
||||
if (err) return reject(err)
|
||||
|
||||
const newTx = R.assoc('toAddress', address, tx)
|
||||
db.addInitialIncoming(session, newTx, function (_err) {
|
||||
cb(_err, address)
|
||||
const newTx = R.assoc('toAddress', address, tx)
|
||||
return db.addInitialIncoming(session, newTx)
|
||||
.then(() => resolve(address))
|
||||
})
|
||||
})
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue