diff --git a/lib/coin-utils.js b/lib/coin-utils.js index 2fdbd45b..3eab3885 100644 --- a/lib/coin-utils.js +++ b/lib/coin-utils.js @@ -67,7 +67,7 @@ const CRYPTO_CURRENCIES = [ } ] -module.exports = {buildUrl, cryptoDir, blockchainDir, configPath, cryptoCurrencies, getCryptoCurrency, toUnit} +module.exports = { buildUrl, cryptoDir, blockchainDir, configPath, cryptoCurrencies, getCryptoCurrency, toUnit, isConfigValid } function getCryptoCurrency (cryptoCode) { const cryptoCurrency = _.find(['cryptoCode', cryptoCode], CRYPTO_CURRENCIES) @@ -109,3 +109,8 @@ function toUnit (cryptoAtoms, cryptoCode) { const unitScale = cryptoRec.unitScale return cryptoAtoms.shift(-unitScale) } + +function isConfigValid (config, fields) { + const values = _.map(it => _.get(it)(config))(fields) + return _.every(it => it || it === 0)(values) +} diff --git a/lib/plugins/exchange/bitstamp.js b/lib/plugins/exchange/bitstamp.js index c8ae2131..e8945cd5 100644 --- a/lib/plugins/exchange/bitstamp.js +++ b/lib/plugins/exchange/bitstamp.js @@ -8,6 +8,7 @@ const { BTC, ETH, LTC, BCH } = COINS const CRYPTO = [BTC, ETH, LTC, BCH] const FIAT = ['USD', 'EUR'] const AMOUNT_PRECISION = 8 +const REQUIRED_CONFIG_FIELDS = ['key', 'secret', 'clientId'] const loadConfig = (account) => { const mapper = { @@ -18,10 +19,4 @@ const loadConfig = (account) => { return { ...mapped, timeout: 3000 } } -const isConfigValid = options => { - const requiredOptions = ['key', 'secret', 'clientId'] - const givenOptions = _.pick(requiredOptions, options) - return _.isEqual(_.keys(givenOptions), requiredOptions) -} - -module.exports = { loadConfig, isConfigValid, 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 f845629a..748ed06a 100644 --- a/lib/plugins/exchange/ccxt.js +++ b/lib/plugins/exchange/ccxt.js @@ -1,7 +1,7 @@ const _ = require('lodash/fp') const ccxt = require('ccxt') -const { toUnit } = require('../../coin-utils') +const { toUnit, isConfigValid } = require('../../coin-utils') const { buildMarket, ALL } = require('../common/ccxt') const { ORDER_TYPES } = require('./consts') @@ -13,8 +13,8 @@ function trade (side, account, cryptoAtoms, fiatCode, cryptoCode, exchangeName) const exchangeConfig = ALL[exchangeName] if (!exchangeConfig) throw Error('Exchange configuration not found') - const { loadOptions, loadConfig = _.noop, isConfigValid, ORDER_TYPE, AMOUNT_PRECISION } = exchangeConfig - if (_.isFunction(isConfigValid) && !isConfigValid(account)) throw Error('Invalid config') + const { 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) diff --git a/lib/plugins/exchange/itbit.js b/lib/plugins/exchange/itbit.js index c3f3f2d0..57e6522c 100644 --- a/lib/plugins/exchange/itbit.js +++ b/lib/plugins/exchange/itbit.js @@ -8,6 +8,7 @@ const { BTC, ETH } = COINS const CRYPTO = [BTC, ETH] const FIAT = ['USD'] const AMOUNT_PRECISION = 4 +const REQUIRED_CONFIG_FIELDS = ['clientKey', 'clientSecret', 'userId', 'walletId'] const loadConfig = (account) => { const mapper = { @@ -20,10 +21,4 @@ const loadConfig = (account) => { } const loadOptions = ({ walletId }) => ({ walletId }) -const isConfigValid = options => { - const requiredOptions = ['clientKey', 'clientSecret', 'userId', 'walletId'] - const givenOptions = _.pick(requiredOptions, options) - return _.isEqual(_.keys(givenOptions), requiredOptions) -} - -module.exports = { loadOptions, loadConfig, isConfigValid, 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 98dd9796..6a5ff6ca 100644 --- a/lib/plugins/exchange/kraken.js +++ b/lib/plugins/exchange/kraken.js @@ -8,6 +8,7 @@ const { BTC, BCH, DASH, ETH, LTC, ZEC } = COINS const CRYPTO = [BTC, ETH, LTC, DASH, ZEC, BCH] const FIAT = ['USD', 'EUR'] const AMOUNT_PRECISION = 6 +const REQUIRED_CONFIG_FIELDS = ['apiKey', 'privateKey'] const loadConfig = (account) => { const mapper = { @@ -19,10 +20,4 @@ const loadConfig = (account) => { const loadOptions = () => ({ expiretm: '+60' }) -const isConfigValid = options => { - const requiredOptions = ['apiKey', 'privateKey'] - const givenOptions = _.pick(requiredOptions, options) - return _.isEqual(_.keys(givenOptions), requiredOptions) -} - -module.exports = { loadOptions, loadConfig, isConfigValid, CRYPTO, FIAT, ORDER_TYPE, AMOUNT_PRECISION } +module.exports = { loadOptions, loadConfig, REQUIRED_CONFIG_FIELDS, CRYPTO, FIAT, ORDER_TYPE, AMOUNT_PRECISION }