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

@ -4,6 +4,7 @@ const { ALL } = require('../../plugins/common/ccxt')
const { COINS, ALL_CRYPTOS } = require('./coins') const { COINS, ALL_CRYPTOS } = require('./coins')
const { BTC, BCH, DASH, ETH, LTC, ZEC } = COINS const { BTC, BCH, DASH, ETH, LTC, ZEC } = COINS
const { bitpay, coinbase, itbit, bitstamp, kraken } = ALL
const TICKER = 'ticker' const TICKER = 'ticker'
const WALLET = 'wallet' const WALLET = 'wallet'
@ -15,11 +16,11 @@ const EMAIL = 'email'
const ZERO_CONF = 'zeroConf' const ZERO_CONF = 'zeroConf'
const ALL_ACCOUNTS = [ const ALL_ACCOUNTS = [
{ code: 'bitpay', display: 'Bitpay', class: TICKER, cryptos: ALL['bitpay'].CRYPTO }, { code: 'bitpay', display: 'Bitpay', class: TICKER, cryptos: bitpay.CRYPTO },
{ code: 'kraken', display: 'Kraken', class: TICKER, cryptos: ALL['kraken'].CRYPTO }, { code: 'kraken', display: 'Kraken', class: TICKER, cryptos: kraken.CRYPTO },
{ code: 'bitstamp', display: 'Bitstamp', class: TICKER, cryptos: ALL['bitstamp'].CRYPTO }, { code: 'bitstamp', display: 'Bitstamp', class: TICKER, cryptos: bitstamp.CRYPTO },
{ code: 'coinbase', display: 'Coinbase', class: TICKER, cryptos: ALL['coinbase'].CRYPTO }, { code: 'coinbase', display: 'Coinbase', class: TICKER, cryptos: coinbase.CRYPTO },
{ code: 'itbit', display: 'itBit', class: TICKER, cryptos: ALL['itbit'].CRYPTO }, { code: 'itbit', display: 'itBit', class: TICKER, cryptos: itbit.CRYPTO },
{ code: 'mock-ticker', display: 'Mock (Caution!)', class: TICKER, cryptos: ALL_CRYPTOS, dev: true }, { code: 'mock-ticker', display: 'Mock (Caution!)', class: TICKER, cryptos: ALL_CRYPTOS, dev: true },
{ code: 'bitcoind', display: 'bitcoind', class: WALLET, cryptos: [BTC] }, { code: 'bitcoind', display: 'bitcoind', class: WALLET, cryptos: [BTC] },
{ code: 'no-layer2', display: 'No Layer 2', class: LAYER_2, cryptos: ALL_CRYPTOS }, { code: 'no-layer2', display: 'No Layer 2', class: LAYER_2, cryptos: ALL_CRYPTOS },
@ -30,9 +31,9 @@ const ALL_ACCOUNTS = [
{ code: 'dashd', display: 'dashd', class: WALLET, cryptos: [DASH] }, { code: 'dashd', display: 'dashd', class: WALLET, cryptos: [DASH] },
{ code: 'bitcoincashd', display: 'bitcoincashd', class: WALLET, cryptos: [BCH] }, { code: 'bitcoincashd', display: 'bitcoincashd', class: WALLET, cryptos: [BCH] },
{ code: 'bitgo', display: 'BitGo', class: WALLET, cryptos: [BTC, ZEC, LTC, BCH, DASH] }, { code: 'bitgo', display: 'BitGo', class: WALLET, cryptos: [BTC, ZEC, LTC, BCH, DASH] },
{ code: 'bitstamp', display: 'Bitstamp', class: EXCHANGE, cryptos: ALL['bitstamp'].CRYPTO }, { code: 'bitstamp', display: 'Bitstamp', class: EXCHANGE, cryptos: bitstamp.CRYPTO },
{ code: 'itbit', display: 'itBit', class: EXCHANGE, cryptos: ALL['itbit'].CRYPTO }, { code: 'itbit', display: 'itBit', class: EXCHANGE, cryptos: itbit.CRYPTO },
{ code: 'kraken', display: 'Kraken', class: EXCHANGE, cryptos: ALL['kraken'].CRYPTO }, { code: 'kraken', display: 'Kraken', class: EXCHANGE, cryptos: kraken.CRYPTO },
{ code: 'mock-wallet', display: 'Mock (Caution!)', class: WALLET, cryptos: ALL_CRYPTOS, dev: true }, { code: 'mock-wallet', display: 'Mock (Caution!)', class: WALLET, cryptos: ALL_CRYPTOS, dev: true },
{ code: 'no-exchange', display: 'No exchange', class: EXCHANGE, cryptos: ALL_CRYPTOS }, { code: 'no-exchange', display: 'No exchange', class: EXCHANGE, cryptos: ALL_CRYPTOS },
{ code: 'mock-exchange', display: 'Mock exchange', class: EXCHANGE, cryptos: ALL_CRYPTOS, dev: true }, { code: 'mock-exchange', display: 'Mock exchange', class: EXCHANGE, cryptos: ALL_CRYPTOS, dev: true },

View file

@ -18,6 +18,9 @@ const loadConfig = (account) => {
return { ...mapped, timeout: 3000 } 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 } 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) { if (ORDER_TYPE === ORDER_TYPES.MARKET) {
return exchange.createOrder(symbol, ORDER_TYPES.MARKET, side, amount, null, options) return exchange.createOrder(symbol, ORDER_TYPES.MARKET, side, amount, null, options)
} }
return exchange.fetchOrderBook(symbol) return exchange.fetchOrderBook(symbol)
.then(orderBook => { .then(orderBook => {
const price = calculatePrice(side, amount, orderBook).toFixed(DEFAULT_PRICE_PRECISION) const price = calculatePrice(side, amount, orderBook).toFixed(DEFAULT_PRICE_PRECISION)

View file

@ -19,6 +19,10 @@ const loadConfig = (account) => {
return { ...mapped, timeout: 3000 } return { ...mapped, timeout: 3000 }
} }
const loadOptions = ({ walletId }) => ({ walletId }) 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 } 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 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 } 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)) { if (verifyFiatSupport(fiatCode, tickerName)) {
return getCurrencyRates(ticker, fiatCode, cryptoCode) return getCurrencyRates(ticker, fiatCode, cryptoCode)
} }
return axios.get('https://bitpay.com/rates') return axios.get('https://bitpay.com/rates')
.then(response => { .then(response => {
try { try {
@ -32,7 +33,9 @@ function ticker (fiatCode, cryptoCode, tickerName) {
function getCurrencyRates (ticker, fiatCode, cryptoCode) { function getCurrencyRates (ticker, fiatCode, cryptoCode) {
try { try {
if (ticker.has['fetchTicker']) { if (!ticker.has['fetchTicker']) {
throw new Error('Ticker not available')
}
const symbol = buildMarket(fiatCode, cryptoCode, ticker.id) const symbol = buildMarket(fiatCode, cryptoCode, ticker.id)
return ticker.fetchTicker(symbol) return ticker.fetchTicker(symbol)
.then(res => ({ .then(res => ({
@ -41,7 +44,6 @@ function getCurrencyRates (ticker, fiatCode, cryptoCode) {
bid: BN(res.bid) bid: BN(res.bid)
} }
})) }))
}
} catch (e) { } catch (e) {
return Promise.reject(e) return Promise.reject(e)
} }