refactor: exchanges config validation and error handling
This commit is contained in:
parent
60a19af1a8
commit
dabe21f834
6 changed files with 34 additions and 20 deletions
|
|
@ -4,6 +4,7 @@ const { ALL } = require('../../plugins/common/ccxt')
|
|||
const { COINS, ALL_CRYPTOS } = require('./coins')
|
||||
|
||||
const { BTC, BCH, DASH, ETH, LTC, ZEC } = COINS
|
||||
const { bitpay, coinbase, itbit, bitstamp, kraken } = ALL
|
||||
|
||||
const TICKER = 'ticker'
|
||||
const WALLET = 'wallet'
|
||||
|
|
@ -15,11 +16,11 @@ const EMAIL = 'email'
|
|||
const ZERO_CONF = 'zeroConf'
|
||||
|
||||
const ALL_ACCOUNTS = [
|
||||
{ code: 'bitpay', display: 'Bitpay', class: TICKER, cryptos: ALL['bitpay'].CRYPTO },
|
||||
{ code: 'kraken', display: 'Kraken', class: TICKER, cryptos: ALL['kraken'].CRYPTO },
|
||||
{ code: 'bitstamp', display: 'Bitstamp', class: TICKER, cryptos: ALL['bitstamp'].CRYPTO },
|
||||
{ code: 'coinbase', display: 'Coinbase', class: TICKER, cryptos: ALL['coinbase'].CRYPTO },
|
||||
{ code: 'itbit', display: 'itBit', class: TICKER, cryptos: ALL['itbit'].CRYPTO },
|
||||
{ code: 'bitpay', display: 'Bitpay', class: TICKER, cryptos: bitpay.CRYPTO },
|
||||
{ code: 'kraken', display: 'Kraken', class: TICKER, cryptos: kraken.CRYPTO },
|
||||
{ code: 'bitstamp', display: 'Bitstamp', class: TICKER, cryptos: bitstamp.CRYPTO },
|
||||
{ code: 'coinbase', display: 'Coinbase', class: TICKER, cryptos: coinbase.CRYPTO },
|
||||
{ code: 'itbit', display: 'itBit', class: TICKER, cryptos: itbit.CRYPTO },
|
||||
{ code: 'mock-ticker', display: 'Mock (Caution!)', class: TICKER, cryptos: ALL_CRYPTOS, dev: true },
|
||||
{ code: 'bitcoind', display: 'bitcoind', class: WALLET, cryptos: [BTC] },
|
||||
{ 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: 'bitcoincashd', display: 'bitcoincashd', class: WALLET, cryptos: [BCH] },
|
||||
{ code: 'bitgo', display: 'BitGo', class: WALLET, cryptos: [BTC, ZEC, LTC, BCH, DASH] },
|
||||
{ code: 'bitstamp', display: 'Bitstamp', class: EXCHANGE, cryptos: ALL['bitstamp'].CRYPTO },
|
||||
{ code: 'itbit', display: 'itBit', class: EXCHANGE, cryptos: ALL['itbit'].CRYPTO },
|
||||
{ code: 'kraken', display: 'Kraken', class: EXCHANGE, cryptos: ALL['kraken'].CRYPTO },
|
||||
{ code: 'bitstamp', display: 'Bitstamp', class: EXCHANGE, cryptos: bitstamp.CRYPTO },
|
||||
{ code: 'itbit', display: 'itBit', class: EXCHANGE, cryptos: itbit.CRYPTO },
|
||||
{ code: 'kraken', display: 'Kraken', class: EXCHANGE, cryptos: kraken.CRYPTO },
|
||||
{ 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: 'mock-exchange', display: 'Mock exchange', class: EXCHANGE, cryptos: ALL_CRYPTOS, dev: true },
|
||||
|
|
|
|||
|
|
@ -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 }
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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 }
|
||||
|
|
|
|||
|
|
@ -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 }
|
||||
|
|
|
|||
|
|
@ -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,7 +33,9 @@ function ticker (fiatCode, cryptoCode, tickerName) {
|
|||
|
||||
function getCurrencyRates (ticker, fiatCode, cryptoCode) {
|
||||
try {
|
||||
if (ticker.has['fetchTicker']) {
|
||||
if (!ticker.has['fetchTicker']) {
|
||||
throw new Error('Ticker not available')
|
||||
}
|
||||
const symbol = buildMarket(fiatCode, cryptoCode, ticker.id)
|
||||
return ticker.fetchTicker(symbol)
|
||||
.then(res => ({
|
||||
|
|
@ -41,7 +44,6 @@ function getCurrencyRates (ticker, fiatCode, cryptoCode) {
|
|||
bid: BN(res.bid)
|
||||
}
|
||||
}))
|
||||
}
|
||||
} catch (e) {
|
||||
return Promise.reject(e)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue