From 7ddda203647dd4f40237d024772763e987bb11b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20S=C3=A1?= Date: Tue, 19 Apr 2022 12:43:11 +0100 Subject: [PATCH 1/6] refactor: use Unix fileformat --- lib/plugins/ticker/ccxt.js | 98 +++++++++++++++++++------------------- 1 file changed, 49 insertions(+), 49 deletions(-) diff --git a/lib/plugins/ticker/ccxt.js b/lib/plugins/ticker/ccxt.js index 4f74f811..6c495a1c 100644 --- a/lib/plugins/ticker/ccxt.js +++ b/lib/plugins/ticker/ccxt.js @@ -1,49 +1,49 @@ -const ccxt = require('ccxt') - -const BN = require('../../bn') -const { buildMarket, verifyFiatSupport } = require('../common/ccxt') -const { getRate } = require('../../../lib/forex') - -const RETRIES = 2 - -function ticker (fiatCode, cryptoCode, tickerName) { - const ticker = new ccxt[tickerName]({ timeout: 3000 }) - if (verifyFiatSupport(fiatCode, tickerName)) { - return getCurrencyRates(ticker, fiatCode, cryptoCode) - } - - return getRate(RETRIES, fiatCode) - .then(({ fxRate }) => { - try { - return getCurrencyRates(ticker, 'USD', cryptoCode) - .then(res => ({ - rates: { - ask: res.rates.ask.times(fxRate), - bid: res.rates.bid.times(fxRate) - } - })) - } catch (e) { - return Promise.reject(e) - } - }) -} - -function getCurrencyRates (ticker, fiatCode, cryptoCode) { - try { - 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: new BN(res.ask), - bid: new BN(res.bid) - } - })) - } catch (e) { - return Promise.reject(e) - } -} - -module.exports = { ticker } +const ccxt = require('ccxt') + +const BN = require('../../bn') +const { buildMarket, verifyFiatSupport } = require('../common/ccxt') +const { getRate } = require('../../../lib/forex') + +const RETRIES = 2 + +function ticker (fiatCode, cryptoCode, tickerName) { + const ticker = new ccxt[tickerName]({ timeout: 3000 }) + if (verifyFiatSupport(fiatCode, tickerName)) { + return getCurrencyRates(ticker, fiatCode, cryptoCode) + } + + return getRate(RETRIES, fiatCode) + .then(({ fxRate }) => { + try { + return getCurrencyRates(ticker, 'USD', cryptoCode) + .then(res => ({ + rates: { + ask: res.rates.ask.times(fxRate), + bid: res.rates.bid.times(fxRate) + } + })) + } catch (e) { + return Promise.reject(e) + } + }) +} + +function getCurrencyRates (ticker, fiatCode, cryptoCode) { + try { + 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: new BN(res.ask), + bid: new BN(res.bid) + } + })) + } catch (e) { + return Promise.reject(e) + } +} + +module.exports = { ticker } From 4747e0b7f69f9fcde29d523d3addd5a3763d5258 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20S=C3=A1?= Date: Tue, 19 Apr 2022 12:43:54 +0100 Subject: [PATCH 2/6] refactor: save CCXT ticker instance --- lib/plugins/ticker/ccxt.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/plugins/ticker/ccxt.js b/lib/plugins/ticker/ccxt.js index 6c495a1c..09d8dbd7 100644 --- a/lib/plugins/ticker/ccxt.js +++ b/lib/plugins/ticker/ccxt.js @@ -6,8 +6,13 @@ const { getRate } = require('../../../lib/forex') const RETRIES = 2 +const tickerObjects = {} + function ticker (fiatCode, cryptoCode, tickerName) { - const ticker = new ccxt[tickerName]({ timeout: 3000 }) + const ticker = tickerObjects[tickerName] ? + tickerObjects[tickerName] : + tickerObjects[tickerName] = new ccxt[tickerName]({ timeout: 3000 }) + if (verifyFiatSupport(fiatCode, tickerName)) { return getCurrencyRates(ticker, fiatCode, cryptoCode) } From 6c2c6353aa7658bcf4ecf8134f06a35f6558f9a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20S=C3=A1?= Date: Tue, 19 Apr 2022 13:32:47 +0100 Subject: [PATCH 3/6] feat: lower the ticker rate limit --- lib/plugins/ticker/ccxt.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/plugins/ticker/ccxt.js b/lib/plugins/ticker/ccxt.js index 09d8dbd7..154d7eb4 100644 --- a/lib/plugins/ticker/ccxt.js +++ b/lib/plugins/ticker/ccxt.js @@ -11,7 +11,11 @@ const tickerObjects = {} function ticker (fiatCode, cryptoCode, tickerName) { const ticker = tickerObjects[tickerName] ? tickerObjects[tickerName] : - tickerObjects[tickerName] = new ccxt[tickerName]({ timeout: 3000 }) + tickerObjects[tickerName] = new ccxt[tickerName]({ + timeout: 3000, + enableRateLimit: true, + rateLimit: 333, + }) if (verifyFiatSupport(fiatCode, tickerName)) { return getCurrencyRates(ticker, fiatCode, cryptoCode) From 396e7058f5b29bb7b6857bd22a7a09cf09726c00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20S=C3=A1?= Date: Tue, 19 Apr 2022 14:20:09 +0100 Subject: [PATCH 4/6] refactor: disable CCXT's rate limitting --- lib/plugins/ticker/ccxt.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/plugins/ticker/ccxt.js b/lib/plugins/ticker/ccxt.js index 154d7eb4..410b24fa 100644 --- a/lib/plugins/ticker/ccxt.js +++ b/lib/plugins/ticker/ccxt.js @@ -13,8 +13,7 @@ function ticker (fiatCode, cryptoCode, tickerName) { tickerObjects[tickerName] : tickerObjects[tickerName] = new ccxt[tickerName]({ timeout: 3000, - enableRateLimit: true, - rateLimit: 333, + enableRateLimit: false, }) if (verifyFiatSupport(fiatCode, tickerName)) { From 2fe92c2ebf5dd9736d833312a0b6082fa572e684 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20S=C3=A1?= Date: Tue, 19 Apr 2022 15:38:13 +0100 Subject: [PATCH 5/6] feat: periodically update ticker rates --- lib/poller.js | 2 ++ lib/ticker.js | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/poller.js b/lib/poller.js index 99ea5543..41f1850d 100644 --- a/lib/poller.js +++ b/lib/poller.js @@ -33,6 +33,7 @@ const SANCTIONS_UPDATE_INTERVAL = 1 * T.day const RADAR_UPDATE_INTERVAL = 5 * T.minutes const PRUNE_MACHINES_HEARTBEAT = 1 * T.day const TRANSACTION_BATCH_LIFECYCLE = 20 * T.minutes +const TICKER_RATES_INTERVAL = 59 * T.seconds const CHECK_NOTIFICATION_INTERVAL = 20 * T.seconds const PENDING_INTERVAL = 10 * T.seconds @@ -178,6 +179,7 @@ function doPolling (schema) { notifier.checkNotification(pi()) updateCoinAtmRadar() + addToQueue(pi().getRawRates, TICKER_RATES_INTERVAL, schema, QUEUE.FAST) addToQueue(pi().executeTrades, TRADE_INTERVAL, schema, QUEUE.FAST) addToQueue(cashOutTx.monitorLiveIncoming, LIVE_INCOMING_TX_INTERVAL, schema, QUEUE.FAST, settings, false, coinFilter) addToQueue(cashOutTx.monitorStaleIncoming, INCOMING_TX_INTERVAL, schema, QUEUE.FAST, settings, false, coinFilter) diff --git a/lib/ticker.js b/lib/ticker.js index c032a079..79ff7737 100644 --- a/lib/ticker.js +++ b/lib/ticker.js @@ -7,7 +7,7 @@ const ccxt = require('./plugins/ticker/ccxt') const mockTicker = require('./plugins/ticker/mock-ticker') const bitpay = require('./plugins/ticker/bitpay') -const FETCH_INTERVAL = 60000 +const FETCH_INTERVAL = 58000 function _getRates (settings, fiatCode, cryptoCode) { return Promise.resolve() From ad5293522dc1cb30953a7f239e7316f2dfdf676e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20S=C3=A1?= Date: Tue, 26 Apr 2022 10:06:36 +0100 Subject: [PATCH 6/6] refactor: use a more idiomatic conditional set --- lib/plugins/ticker/ccxt.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/plugins/ticker/ccxt.js b/lib/plugins/ticker/ccxt.js index 410b24fa..080b2f18 100644 --- a/lib/plugins/ticker/ccxt.js +++ b/lib/plugins/ticker/ccxt.js @@ -9,12 +9,14 @@ const RETRIES = 2 const tickerObjects = {} function ticker (fiatCode, cryptoCode, tickerName) { - const ticker = tickerObjects[tickerName] ? - tickerObjects[tickerName] : + if (!tickerObjects[tickerName]) { tickerObjects[tickerName] = new ccxt[tickerName]({ timeout: 3000, enableRateLimit: false, }) + } + + const ticker = tickerObjects[tickerName] if (verifyFiatSupport(fiatCode, tickerName)) { return getCurrencyRates(ticker, fiatCode, cryptoCode)