diff --git a/lib/exchange.js b/lib/exchange.js index a0c36676..97c68616 100644 --- a/lib/exchange.js +++ b/lib/exchange.js @@ -19,23 +19,23 @@ function fetchExchange (settings, cryptoCode) { }) } -function buy (settings, cryptoAtoms, fiatCode, cryptoCode) { +function buy (settings, cryptoAtoms, fiatCode, cryptoCode, tradeId) { return fetchExchange(settings, cryptoCode) .then(r => { if (r.exchangeName === 'mock-exchange') { return mockExchange.buy(cryptoAtoms, fiatCode, cryptoCode) } - return ccxt.trade('buy', r.account, cryptoAtoms, fiatCode, cryptoCode, r.exchangeName) + return ccxt.trade('buy', r.account, cryptoAtoms, fiatCode, cryptoCode, r.exchangeName, tradeId) }) } -function sell (settings, cryptoAtoms, fiatCode, cryptoCode) { +function sell (settings, cryptoAtoms, fiatCode, cryptoCode, tradeId) { return fetchExchange(settings, cryptoCode) .then(r => { if (r.exchangeName === 'mock-exchange') { return mockExchange.sell(cryptoAtoms, fiatCode, cryptoCode) } - return ccxt.trade('sell', r.account, cryptoAtoms, fiatCode, cryptoCode, r.exchangeName) + return ccxt.trade('sell', r.account, cryptoAtoms, fiatCode, cryptoCode, r.exchangeName, tradeId) }) } diff --git a/lib/plugins.js b/lib/plugins.js index ae50e14f..895cc360 100644 --- a/lib/plugins.js +++ b/lib/plugins.js @@ -516,12 +516,16 @@ function plugins (settings, deviceId) { const tradeEntry = expand(_tradeEntry) const execute = tradeEntry.type === 'buy' ? exchange.buy : exchange.sell - return execute(settings, tradeEntry.cryptoAtoms, tradeEntry.fiatCode, tradeEntry.cryptoCode) - .then(() => recordTrade(tradeEntry)) - .catch(err => { - return recordTrade(tradeEntry, err) - .then(() => { - throw err + return recordTrade(tradeEntry) + .then(newEntry => { + return execute(settings, tradeEntry.cryptoAtoms, tradeEntry.fiatCode, tradeEntry.cryptoCode, newEntry.id) + .catch(err => { + const data = mergeTradeEntryAndError(tradeEntry, err) + const sql = pgp.helpers.update(data, ['error'], 'trades') + ` WHERE id = ${newEntry.id}` + return db.none(sql) + .then(() => { + throw err + }) }) }) } @@ -577,7 +581,6 @@ function plugins (settings, deviceId) { return t.oneOrNone(sql) .then(newTrade => recordTradeAndTx(newTrade.id, _tradeEntry, t)) }) - } function sendMessage (rec) { const notifications = configManager.getGlobalNotifications(settings.config) diff --git a/lib/plugins/exchange/bitstamp.js b/lib/plugins/exchange/bitstamp.js index 102ae6aa..63be476b 100644 --- a/lib/plugins/exchange/bitstamp.js +++ b/lib/plugins/exchange/bitstamp.js @@ -19,4 +19,6 @@ const loadConfig = (account) => { return { ...mapped, timeout: 3000 } } -module.exports = { loadConfig, REQUIRED_CONFIG_FIELDS, CRYPTO, FIAT, ORDER_TYPE, AMOUNT_PRECISION } +const loadTradeId = (options, id) => _.assign({}, options) + +module.exports = { loadTradeId, loadConfig, REQUIRED_CONFIG_FIELDS, CRYPTO, FIAT, ORDER_TYPE, AMOUNT_PRECISION } diff --git a/lib/plugins/exchange/ccxt.js b/lib/plugins/exchange/ccxt.js index 7d8601cd..783ab3ca 100644 --- a/lib/plugins/exchange/ccxt.js +++ b/lib/plugins/exchange/ccxt.js @@ -8,18 +8,19 @@ const { ORDER_TYPES } = require('./consts') const DEFAULT_PRICE_PRECISION = 2 const DEFAULT_AMOUNT_PRECISION = 8 -function trade (side, account, cryptoAtoms, fiatCode, cryptoCode, exchangeName) { +function trade (side, account, cryptoAtoms, fiatCode, cryptoCode, exchangeName, tradeId) { try { const exchangeConfig = ALL[exchangeName] if (!exchangeConfig) throw Error('Exchange configuration not found') - const { loadOptions, loadConfig = _.noop, REQUIRED_CONFIG_FIELDS, ORDER_TYPE, AMOUNT_PRECISION } = exchangeConfig + const { loadTradeId, loadOptions, loadConfig = _.noop, REQUIRED_CONFIG_FIELDS, ORDER_TYPE, AMOUNT_PRECISION } = exchangeConfig if (!isConfigValid(account, REQUIRED_CONFIG_FIELDS)) throw Error('Invalid config') const symbol = buildMarket(fiatCode, cryptoCode, exchangeName) const precision = _.defaultTo(DEFAULT_AMOUNT_PRECISION, AMOUNT_PRECISION) const amount = toUnit(cryptoAtoms, cryptoCode).toFixed(precision) - const options = _.isFunction(loadOptions) ? loadOptions(account) : {} + const accountOptions = _.isFunction(loadOptions) ? loadOptions(account) : {} + const options = loadTradeId(accountOptions, tradeId) const exchange = new ccxt[exchangeName](loadConfig(account)) if (ORDER_TYPE === ORDER_TYPES.MARKET) { diff --git a/lib/plugins/exchange/itbit.js b/lib/plugins/exchange/itbit.js index 59d0e77d..f73ec560 100644 --- a/lib/plugins/exchange/itbit.js +++ b/lib/plugins/exchange/itbit.js @@ -20,5 +20,6 @@ const loadConfig = (account) => { return { ...mapped, timeout: 3000 } } const loadOptions = ({ walletId }) => ({ walletId }) +const loadTradeId = (options, id) => _.assign({}, options) -module.exports = { loadOptions, loadConfig, REQUIRED_CONFIG_FIELDS, CRYPTO, FIAT, ORDER_TYPE, AMOUNT_PRECISION } +module.exports = { loadTradeId, loadOptions, loadConfig, REQUIRED_CONFIG_FIELDS, CRYPTO, FIAT, ORDER_TYPE, AMOUNT_PRECISION } diff --git a/lib/plugins/exchange/kraken.js b/lib/plugins/exchange/kraken.js index c3e234d1..3b2794f5 100644 --- a/lib/plugins/exchange/kraken.js +++ b/lib/plugins/exchange/kraken.js @@ -19,5 +19,6 @@ const loadConfig = (account) => { } const loadOptions = () => ({ expiretm: '+60' }) +const loadTradeId = (options, id) => _.assign({ userref: id }, options) -module.exports = { loadOptions, loadConfig, REQUIRED_CONFIG_FIELDS, CRYPTO, FIAT, ORDER_TYPE, AMOUNT_PRECISION } +module.exports = { loadTradeId, loadOptions, loadConfig, REQUIRED_CONFIG_FIELDS, CRYPTO, FIAT, ORDER_TYPE, AMOUNT_PRECISION }