From dabe21f834c035209d4868b72a9b4e9fb49a2b19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Oliveira?= Date: Tue, 11 May 2021 18:57:46 +0100 Subject: [PATCH] refactor: exchanges config validation and error handling --- lib/new-admin/config/accounts.js | 17 +++++++++-------- lib/plugins/exchange/bitstamp.js | 5 ++++- lib/plugins/exchange/ccxt.js | 1 + lib/plugins/exchange/itbit.js | 6 +++++- lib/plugins/exchange/kraken.js | 5 ++++- lib/plugins/ticker/ccxt.js | 20 +++++++++++--------- 6 files changed, 34 insertions(+), 20 deletions(-) diff --git a/lib/new-admin/config/accounts.js b/lib/new-admin/config/accounts.js index 0466be24..0221289e 100644 --- a/lib/new-admin/config/accounts.js +++ b/lib/new-admin/config/accounts.js @@ -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 }, diff --git a/lib/plugins/exchange/bitstamp.js b/lib/plugins/exchange/bitstamp.js index 6a88d223..7cbb956b 100644 --- a/lib/plugins/exchange/bitstamp.js +++ b/lib/plugins/exchange/bitstamp.js @@ -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 } diff --git a/lib/plugins/exchange/ccxt.js b/lib/plugins/exchange/ccxt.js index 4bfa2951..f845629a 100644 --- a/lib/plugins/exchange/ccxt.js +++ b/lib/plugins/exchange/ccxt.js @@ -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) diff --git a/lib/plugins/exchange/itbit.js b/lib/plugins/exchange/itbit.js index 3619b2e3..aff829bf 100644 --- a/lib/plugins/exchange/itbit.js +++ b/lib/plugins/exchange/itbit.js @@ -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 } diff --git a/lib/plugins/exchange/kraken.js b/lib/plugins/exchange/kraken.js index e766fdc3..03d561d2 100644 --- a/lib/plugins/exchange/kraken.js +++ b/lib/plugins/exchange/kraken.js @@ -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 } diff --git a/lib/plugins/ticker/ccxt.js b/lib/plugins/ticker/ccxt.js index 0eda5fff..210585e4 100644 --- a/lib/plugins/ticker/ccxt.js +++ b/lib/plugins/ticker/ccxt.js @@ -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) }