fix: verify config field existance and content

This commit is contained in:
José Oliveira 2021-05-13 16:13:27 +01:00 committed by Josh Harvey
parent a8085f3f1d
commit 0c0ca09b4e
5 changed files with 15 additions and 25 deletions

View file

@ -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) { function getCryptoCurrency (cryptoCode) {
const cryptoCurrency = _.find(['cryptoCode', cryptoCode], CRYPTO_CURRENCIES) const cryptoCurrency = _.find(['cryptoCode', cryptoCode], CRYPTO_CURRENCIES)
@ -109,3 +109,8 @@ function toUnit (cryptoAtoms, cryptoCode) {
const unitScale = cryptoRec.unitScale const unitScale = cryptoRec.unitScale
return cryptoAtoms.shift(-unitScale) return cryptoAtoms.shift(-unitScale)
} }
function isConfigValid (config, fields) {
const values = _.map(it => _.get(it)(config))(fields)
return _.every(it => it || it === 0)(values)
}

View file

@ -8,6 +8,7 @@ const { BTC, ETH, LTC, BCH } = COINS
const CRYPTO = [BTC, ETH, LTC, BCH] const CRYPTO = [BTC, ETH, LTC, BCH]
const FIAT = ['USD', 'EUR'] const FIAT = ['USD', 'EUR']
const AMOUNT_PRECISION = 8 const AMOUNT_PRECISION = 8
const REQUIRED_CONFIG_FIELDS = ['key', 'secret', 'clientId']
const loadConfig = (account) => { const loadConfig = (account) => {
const mapper = { const mapper = {
@ -18,10 +19,4 @@ const loadConfig = (account) => {
return { ...mapped, timeout: 3000 } return { ...mapped, timeout: 3000 }
} }
const isConfigValid = options => { module.exports = { loadConfig, REQUIRED_CONFIG_FIELDS, CRYPTO, FIAT, ORDER_TYPE, AMOUNT_PRECISION }
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 }

View file

@ -1,7 +1,7 @@
const _ = require('lodash/fp') const _ = require('lodash/fp')
const ccxt = require('ccxt') const ccxt = require('ccxt')
const { toUnit } = require('../../coin-utils') const { toUnit, isConfigValid } = require('../../coin-utils')
const { buildMarket, ALL } = require('../common/ccxt') const { buildMarket, ALL } = require('../common/ccxt')
const { ORDER_TYPES } = require('./consts') const { ORDER_TYPES } = require('./consts')
@ -13,8 +13,8 @@ function trade (side, account, cryptoAtoms, fiatCode, cryptoCode, exchangeName)
const exchangeConfig = ALL[exchangeName] const exchangeConfig = ALL[exchangeName]
if (!exchangeConfig) throw Error('Exchange configuration not found') if (!exchangeConfig) throw Error('Exchange configuration not found')
const { loadOptions, loadConfig = _.noop, isConfigValid, ORDER_TYPE, AMOUNT_PRECISION } = exchangeConfig const { loadOptions, loadConfig = _.noop, REQUIRED_CONFIG_FIELDS, ORDER_TYPE, AMOUNT_PRECISION } = exchangeConfig
if (_.isFunction(isConfigValid) && !isConfigValid(account)) throw Error('Invalid config') if (!isConfigValid(account, REQUIRED_CONFIG_FIELDS)) throw Error('Invalid config')
const symbol = buildMarket(fiatCode, cryptoCode, exchangeName) const symbol = buildMarket(fiatCode, cryptoCode, exchangeName)
const precision = _.defaultTo(DEFAULT_AMOUNT_PRECISION, AMOUNT_PRECISION) const precision = _.defaultTo(DEFAULT_AMOUNT_PRECISION, AMOUNT_PRECISION)

View file

@ -8,6 +8,7 @@ const { BTC, ETH } = COINS
const CRYPTO = [BTC, ETH] const CRYPTO = [BTC, ETH]
const FIAT = ['USD'] const FIAT = ['USD']
const AMOUNT_PRECISION = 4 const AMOUNT_PRECISION = 4
const REQUIRED_CONFIG_FIELDS = ['clientKey', 'clientSecret', 'userId', 'walletId']
const loadConfig = (account) => { const loadConfig = (account) => {
const mapper = { const mapper = {
@ -20,10 +21,4 @@ const loadConfig = (account) => {
} }
const loadOptions = ({ walletId }) => ({ walletId }) const loadOptions = ({ walletId }) => ({ walletId })
const isConfigValid = options => { module.exports = { loadOptions, loadConfig, REQUIRED_CONFIG_FIELDS, CRYPTO, FIAT, ORDER_TYPE, AMOUNT_PRECISION }
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 }

View file

@ -8,6 +8,7 @@ const { BTC, BCH, DASH, ETH, LTC, ZEC } = COINS
const CRYPTO = [BTC, ETH, LTC, DASH, ZEC, BCH] const CRYPTO = [BTC, ETH, LTC, DASH, ZEC, BCH]
const FIAT = ['USD', 'EUR'] const FIAT = ['USD', 'EUR']
const AMOUNT_PRECISION = 6 const AMOUNT_PRECISION = 6
const REQUIRED_CONFIG_FIELDS = ['apiKey', 'privateKey']
const loadConfig = (account) => { const loadConfig = (account) => {
const mapper = { const mapper = {
@ -19,10 +20,4 @@ const loadConfig = (account) => {
const loadOptions = () => ({ expiretm: '+60' }) const loadOptions = () => ({ expiretm: '+60' })
const isConfigValid = options => { module.exports = { loadOptions, loadConfig, REQUIRED_CONFIG_FIELDS, CRYPTO, FIAT, ORDER_TYPE, AMOUNT_PRECISION }
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 }