refactor: exchanges config validation and error handling

This commit is contained in:
José Oliveira 2021-05-11 18:57:46 +01:00 committed by Josh Harvey
parent 60a19af1a8
commit dabe21f834
6 changed files with 34 additions and 20 deletions

View file

@ -18,6 +18,9 @@ const loadConfig = (account) => {
return { ...mapped, timeout: 3000 }
}
const isConfigValid = ({ key, clientId, secret }) => key && secret && clientId
const isConfigValid = options => {
const requiredOptions = _.pick(['key', 'secret', 'clientId'], options)
return _.isEqual(options, requiredOptions)
}
module.exports = { loadConfig, isConfigValid, CRYPTO, FIAT, ORDER_TYPE, AMOUNT_PRECISION }

View file

@ -25,6 +25,7 @@ function trade (side, account, cryptoAtoms, fiatCode, cryptoCode, exchangeName)
if (ORDER_TYPE === ORDER_TYPES.MARKET) {
return exchange.createOrder(symbol, ORDER_TYPES.MARKET, side, amount, null, options)
}
return exchange.fetchOrderBook(symbol)
.then(orderBook => {
const price = calculatePrice(side, amount, orderBook).toFixed(DEFAULT_PRICE_PRECISION)

View file

@ -19,6 +19,10 @@ const loadConfig = (account) => {
return { ...mapped, timeout: 3000 }
}
const loadOptions = ({ walletId }) => ({ walletId })
const isConfigValid = ({ clientKey, clientSecret, userId, walletId }) => clientKey && clientSecret && userId && walletId
const isConfigValid = options => {
const requiredOptions = _.pick(['clientKey', 'clientSecret', 'userId', 'walletId'], options)
return _.isEqual(options, requiredOptions)
}
module.exports = { loadOptions, loadConfig, isConfigValid, CRYPTO, FIAT, ORDER_TYPE, AMOUNT_PRECISION }

View file

@ -18,6 +18,9 @@ const loadConfig = (account) => {
}
const loadOptions = () => ({ expiretm: '+60' })
const isConfigValid = ({ apiKey, privateKey }) => apiKey && privateKey
const isConfigValid = options => {
const requiredOptions = _.pick(['apiKey', 'privateKey'], options)
return _.isEqual(options, requiredOptions)
}
module.exports = { loadOptions, loadConfig, isConfigValid, CRYPTO, FIAT, ORDER_TYPE, AMOUNT_PRECISION }

View file

@ -10,6 +10,7 @@ function ticker (fiatCode, cryptoCode, tickerName) {
if (verifyFiatSupport(fiatCode, tickerName)) {
return getCurrencyRates(ticker, fiatCode, cryptoCode)
}
return axios.get('https://bitpay.com/rates')
.then(response => {
try {
@ -32,16 +33,17 @@ function ticker (fiatCode, cryptoCode, tickerName) {
function getCurrencyRates (ticker, fiatCode, cryptoCode) {
try {
if (ticker.has['fetchTicker']) {
const symbol = buildMarket(fiatCode, cryptoCode, ticker.id)
return ticker.fetchTicker(symbol)
.then(res => ({
rates: {
ask: BN(res.ask),
bid: BN(res.bid)
}
}))
if (!ticker.has['fetchTicker']) {
throw new Error('Ticker not available')
}
const symbol = buildMarket(fiatCode, cryptoCode, ticker.id)
return ticker.fetchTicker(symbol)
.then(res => ({
rates: {
ask: BN(res.ask),
bid: BN(res.bid)
}
}))
} catch (e) {
return Promise.reject(e)
}