diff --git a/lib/exchange.js b/lib/exchange.js index 97c68616..3df23e3f 100644 --- a/lib/exchange.js +++ b/lib/exchange.js @@ -19,7 +19,8 @@ function fetchExchange (settings, cryptoCode) { }) } -function buy (settings, cryptoAtoms, fiatCode, cryptoCode, tradeId) { +function buy (settings, tradeEntry, tradeId) { + const { cryptoAtoms, fiatCode, cryptoCode } = tradeEntry return fetchExchange(settings, cryptoCode) .then(r => { if (r.exchangeName === 'mock-exchange') { @@ -29,7 +30,8 @@ function buy (settings, cryptoAtoms, fiatCode, cryptoCode, tradeId) { }) } -function sell (settings, cryptoAtoms, fiatCode, cryptoCode, tradeId) { +function sell (settings, tradeEntry, tradeId) { + const { cryptoAtoms, fiatCode, cryptoCode } = tradeEntry return fetchExchange(settings, cryptoCode) .then(r => { if (r.exchangeName === 'mock-exchange') { diff --git a/lib/plugins.js b/lib/plugins.js index 453f1894..de5e4e21 100644 --- a/lib/plugins.js +++ b/lib/plugins.js @@ -517,16 +517,22 @@ function plugins (settings, deviceId) { const execute = tradeEntry.type === 'buy' ? exchange.buy : exchange.sell return recordTrade(tradeEntry) - .then(newEntry => { - return execute(settings, tradeEntry.cryptoAtoms, tradeEntry.fiatCode, tradeEntry.cryptoCode, newEntry.id) + .then(newEntry => + execute(settings, tradeEntry, 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 - }) + updateTradeEntry(tradeEntry, newEntry, err) + .catch(console.log) + throw err }) + ) + } + + function updateTradeEntry (tradeEntry, newEntry, 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 }) } diff --git a/lib/plugins/exchange/bitstamp.js b/lib/plugins/exchange/bitstamp.js index 63be476b..102ae6aa 100644 --- a/lib/plugins/exchange/bitstamp.js +++ b/lib/plugins/exchange/bitstamp.js @@ -19,6 +19,4 @@ const loadConfig = (account) => { return { ...mapped, timeout: 3000 } } -const loadTradeId = (options, id) => _.assign({}, options) - -module.exports = { loadTradeId, loadConfig, REQUIRED_CONFIG_FIELDS, CRYPTO, FIAT, ORDER_TYPE, AMOUNT_PRECISION } +module.exports = { 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 783ab3ca..bceaef31 100644 --- a/lib/plugins/exchange/ccxt.js +++ b/lib/plugins/exchange/ccxt.js @@ -13,14 +13,15 @@ function trade (side, account, cryptoAtoms, fiatCode, cryptoCode, exchangeName, const exchangeConfig = ALL[exchangeName] if (!exchangeConfig) throw Error('Exchange configuration not found') - const { loadTradeId, loadOptions, loadConfig = _.noop, REQUIRED_CONFIG_FIELDS, ORDER_TYPE, AMOUNT_PRECISION } = exchangeConfig + const { CUSTOM_KEY, 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 accountOptions = _.isFunction(loadOptions) ? loadOptions(account) : {} - const options = loadTradeId(accountOptions, tradeId) + const withCustomKey = CUSTOM_KEY ? { [CUSTOM_KEY]: tradeId } : {} + const options = _.assign(accountOptions, withCustomKey) 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 f73ec560..59d0e77d 100644 --- a/lib/plugins/exchange/itbit.js +++ b/lib/plugins/exchange/itbit.js @@ -20,6 +20,5 @@ const loadConfig = (account) => { return { ...mapped, timeout: 3000 } } const loadOptions = ({ walletId }) => ({ walletId }) -const loadTradeId = (options, id) => _.assign({}, options) -module.exports = { loadTradeId, loadOptions, loadConfig, REQUIRED_CONFIG_FIELDS, CRYPTO, FIAT, ORDER_TYPE, AMOUNT_PRECISION } +module.exports = { 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 3b2794f5..0f95d205 100644 --- a/lib/plugins/exchange/kraken.js +++ b/lib/plugins/exchange/kraken.js @@ -9,6 +9,7 @@ const CRYPTO = [BTC, ETH, LTC, DASH, ZEC, BCH, USDT] const FIAT = ['USD', 'EUR'] const AMOUNT_PRECISION = 6 const REQUIRED_CONFIG_FIELDS = ['apiKey', 'privateKey'] +const CUSTOM_KEY = 'userref' const loadConfig = (account) => { const mapper = { @@ -19,6 +20,5 @@ const loadConfig = (account) => { } const loadOptions = () => ({ expiretm: '+60' }) -const loadTradeId = (options, id) => _.assign({ userref: id }, options) -module.exports = { loadTradeId, loadOptions, loadConfig, REQUIRED_CONFIG_FIELDS, CRYPTO, FIAT, ORDER_TYPE, AMOUNT_PRECISION } +module.exports = { CUSTOM_KEY, loadOptions, loadConfig, REQUIRED_CONFIG_FIELDS, CRYPTO, FIAT, ORDER_TYPE, AMOUNT_PRECISION }