From 62af2cf893a5fd371114dc8d3e70f3f5c3e03ff7 Mon Sep 17 00:00:00 2001 From: Sandeep Rajput <90243468+sandeep-deriv@users.noreply.github.com> Date: Tue, 15 Oct 2024 12:20:37 +0800 Subject: [PATCH] [BOT] sandeep/refactor: :fire: remove binary-utils package (#17169) * refactor: :fire: remove binary-utils package * refactor: :fire: moved common function into binary-utils.js --- package-lock.json | 124 ------------------ packages/bot-skeleton/package.json | 3 +- .../src/services/api/binary-utils.js | 43 ++++++ .../src/services/api/ticks_service.js | 45 ++----- .../src/services/tradeEngine/trade/Ticks.js | 2 +- 5 files changed, 54 insertions(+), 163 deletions(-) create mode 100644 packages/bot-skeleton/src/services/api/binary-utils.js diff --git a/package-lock.json b/package-lock.json index 2c548106c628..8ee742973075 100644 --- a/package-lock.json +++ b/package-lock.json @@ -76,7 +76,6 @@ "babel-jest": "^27.3.1", "babel-loader": "^8.1.0", "babel-plugin-transform-barrels": "1.0.12", - "binary-utils": "^4.23.0", "blockly": "^10.4.3", "bowser": "^2.9.0", "canvas-toBlob": "^1.0.0", @@ -20474,14 +20473,6 @@ "url": "https://github.com/fb55/entities?sponsor=1" } }, - "node_modules/ansi-wrap": { - "version": "0.1.0", - "resolved": "https://registry.npmjs.org/ansi-wrap/-/ansi-wrap-0.1.0.tgz", - "integrity": "sha512-ZyznvL8k/FZeQHr2T6LzcJ/+vBApDnMNZvfVFy3At0knswWd6rJ3/0Hhmpu8oqa6C92npmozs890sX9Dl6q+Qw==", - "engines": { - "node": ">=0.10.0" - } - }, "node_modules/any-promise": { "version": "1.3.0", "resolved": "https://registry.npmjs.org/any-promise/-/any-promise-1.3.0.tgz", @@ -21910,15 +21901,6 @@ "node": ">=8" } }, - "node_modules/binary-utils": { - "version": "4.25.0", - "resolved": "https://registry.npmjs.org/binary-utils/-/binary-utils-4.25.0.tgz", - "integrity": "sha512-OwsckAI4gLzHOsnWp5Y/nz4LIDrr7+nVKUogMhcSNCMjBJ7W6Vmci3Hf2yIdczwsJkm19TTIpI2Wbe2ni9iTBA==", - "deprecated": "Package no longer supported. Contact Support at https://www.npmjs.com/support for more info.", - "dependencies": { - "gulp-babel": "^6.1.2" - } - }, "node_modules/bindings": { "version": "1.5.0", "resolved": "https://registry.npmjs.org/bindings/-/bindings-1.5.0.tgz", @@ -31413,63 +31395,6 @@ "resolved": "https://registry.npmjs.org/gud/-/gud-1.0.0.tgz", "integrity": "sha512-zGEOVKFM5sVPPrYs7J5/hYEw2Pof8KCyOwyhG8sAF26mCAeUFAcYPu1mwB7hhpIP29zOIBaDqwuHdLp0jvZXjw==" }, - "node_modules/gulp-babel": { - "version": "6.1.3", - "resolved": "https://registry.npmjs.org/gulp-babel/-/gulp-babel-6.1.3.tgz", - "integrity": "sha512-tm15R3rt4gO59WXCuqrwf4QXJM9VIJC+0J2NPYSC6xZn+cZRD5y5RPGAiHaDxCJq7Rz5BDljlrk3cEjWADF+wQ==", - "dependencies": { - "babel-core": "^6.23.1", - "object-assign": "^4.0.1", - "plugin-error": "^1.0.1", - "replace-ext": "0.0.1", - "through2": "^2.0.0", - "vinyl-sourcemaps-apply": "^0.2.0" - }, - "engines": { - "node": ">=0.10.0" - } - }, - "node_modules/gulp-babel/node_modules/isarray": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz", - "integrity": "sha512-VLghIWNM6ELQzo7zwmcg0NmTVyWKYjvIeM83yjp0wRDTmUnrM678fQbcKBo6n2CJEF0szoG//ytg+TKla89ALQ==" - }, - "node_modules/gulp-babel/node_modules/readable-stream": { - "version": "2.3.8", - "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-2.3.8.tgz", - "integrity": "sha512-8p0AUk4XODgIewSi0l8Epjs+EVnWiK7NoDIEGU0HhE7+ZyY8D1IMY7odu5lRrFXGg71L15KG8QrPmum45RTtdA==", - "dependencies": { - "core-util-is": "~1.0.0", - "inherits": "~2.0.3", - "isarray": "~1.0.0", - "process-nextick-args": "~2.0.0", - "safe-buffer": "~5.1.1", - "string_decoder": "~1.1.1", - "util-deprecate": "~1.0.1" - } - }, - "node_modules/gulp-babel/node_modules/safe-buffer": { - "version": "5.1.2", - "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", - "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==" - }, - "node_modules/gulp-babel/node_modules/string_decoder": { - "version": "1.1.1", - "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.1.1.tgz", - "integrity": "sha512-n/ShnvDi6FHbbVfviro+WojiFzv+s8MPMHBczVePfUpDJLwoLT0ht1l4YwBCbi8pJAveEEdnkHyPyTP/mzRfwg==", - "dependencies": { - "safe-buffer": "~5.1.0" - } - }, - "node_modules/gulp-babel/node_modules/through2": { - "version": "2.0.5", - "resolved": "https://registry.npmjs.org/through2/-/through2-2.0.5.tgz", - "integrity": "sha512-/mrRod8xqpA+IHSLyGCQ2s8SPHiCDEeQJSep1jqLYeEUClOFG2Qsh+4FU6G9VeqpZnGW/Su8LQGc4YKni5rYSQ==", - "dependencies": { - "readable-stream": "~2.3.6", - "xtend": "~4.0.1" - } - }, "node_modules/gzip-size": { "version": "6.0.0", "resolved": "https://registry.npmjs.org/gzip-size/-/gzip-size-6.0.0.tgz", @@ -44552,31 +44477,6 @@ "semver-compare": "^1.0.0" } }, - "node_modules/plugin-error": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/plugin-error/-/plugin-error-1.0.1.tgz", - "integrity": "sha512-L1zP0dk7vGweZME2i+EeakvUNqSrdiI3F91TwEoYiGrAfUXmVv6fJIq4g82PAXxNsWOp0J7ZqQy/3Szz0ajTxA==", - "dependencies": { - "ansi-colors": "^1.0.1", - "arr-diff": "^4.0.0", - "arr-union": "^3.1.0", - "extend-shallow": "^3.0.2" - }, - "engines": { - "node": ">= 0.10" - } - }, - "node_modules/plugin-error/node_modules/ansi-colors": { - "version": "1.1.0", - "resolved": "https://registry.npmjs.org/ansi-colors/-/ansi-colors-1.1.0.tgz", - "integrity": "sha512-SFKX67auSNoVR38N3L+nvsPjOE0bybKTYbkf5tRvushrAPQ9V75huw0ZxBkKVeRU9kqH3d6HA4xTckbwZ4ixmA==", - "dependencies": { - "ansi-wrap": "^0.1.0" - }, - "engines": { - "node": ">=0.10.0" - } - }, "node_modules/plur": { "version": "4.0.0", "license": "MIT", @@ -48714,14 +48614,6 @@ "node": ">=0.10.0" } }, - "node_modules/replace-ext": { - "version": "0.0.1", - "resolved": "https://registry.npmjs.org/replace-ext/-/replace-ext-0.0.1.tgz", - "integrity": "sha512-AFBWBy9EVRTa/LhEcG8QDP3FvpwZqmvN2QFDuJswFeaVhWnZMp8q3E6Zd90SR04PlIwfGdyVjNyLPyen/ek5CQ==", - "engines": { - "node": ">= 0.4" - } - }, "node_modules/require-directory": { "version": "2.1.1", "license": "MIT", @@ -54893,22 +54785,6 @@ "url": "https://opencollective.com/unified" } }, - "node_modules/vinyl-sourcemaps-apply": { - "version": "0.2.1", - "resolved": "https://registry.npmjs.org/vinyl-sourcemaps-apply/-/vinyl-sourcemaps-apply-0.2.1.tgz", - "integrity": "sha512-+oDh3KYZBoZC8hfocrbrxbLUeaYtQK7J5WU5Br9VqWqmCll3tFJqKp97GC9GmMsVIL0qnx2DgEDVxdo5EZ5sSw==", - "dependencies": { - "source-map": "^0.5.1" - } - }, - "node_modules/vinyl-sourcemaps-apply/node_modules/source-map": { - "version": "0.5.7", - "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.5.7.tgz", - "integrity": "sha512-LbrmJOMUSdEVxIKvdcJzQC+nQhe8FUZQTXQy6+I75skNgn3OoQ0DZA8YnFa7gp8tqtL3KPf1kmo0R5DoApeSGQ==", - "engines": { - "node": ">=0.10.0" - } - }, "node_modules/vm-browserify": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/vm-browserify/-/vm-browserify-1.1.2.tgz", diff --git a/packages/bot-skeleton/package.json b/packages/bot-skeleton/package.json index 1d10b703fdde..fc1b3d96db36 100644 --- a/packages/bot-skeleton/package.json +++ b/packages/bot-skeleton/package.json @@ -35,13 +35,12 @@ "typescript": "^4.6.3" }, "dependencies": { - "blockly": "^10.4.3", "@deriv/deriv-api": "^1.0.15", "@deriv/indicators": "^1.0.0", "@deriv/js-interpreter": "^3.0.0", "@deriv/shared": "^1.0.0", "@deriv/translations": "^1.0.0", - "binary-utils": "^4.23.0", + "blockly": "^10.4.3", "file-saver": "^2.0.2", "immutable": "^3.8.2", "localforage": "^1.9.0", diff --git a/packages/bot-skeleton/src/services/api/binary-utils.js b/packages/bot-skeleton/src/services/api/binary-utils.js new file mode 100644 index 000000000000..5bf8cf3d3ef6 --- /dev/null +++ b/packages/bot-skeleton/src/services/api/binary-utils.js @@ -0,0 +1,43 @@ +export const historyToTicks = history => + history.times.map((t, idx) => ({ + epoch: +t, + quote: +history.prices[idx], + })); + +export const getLast = arr => arr && (arr.length === 0 ? undefined : arr[arr.length - 1]); + +export const parseTick = tick => ({ + epoch: +tick.epoch, + quote: +tick.quote, +}); + +export const parseOhlc = ohlc => ({ + open: +ohlc.open, + high: +ohlc.high, + low: +ohlc.low, + close: +ohlc.close, + epoch: +(ohlc.open_time || ohlc.epoch), +}); + +export const parseCandles = candles => candles.map(t => parseOhlc(t)); + +export const updateTicks = (ticks, newTick) => + getLast(ticks).epoch >= newTick.epoch ? ticks : [...ticks.slice(1), newTick]; + +export const updateCandles = (candles, ohlc) => { + const lastCandle = getLast(candles); + if ( + (lastCandle.open === ohlc.open && + lastCandle.high === ohlc.high && + lastCandle.low === ohlc.low && + lastCandle.close === ohlc.close && + lastCandle.epoch === ohlc.epoch) || + lastCandle.epoch > ohlc.epoch + ) { + return candles; + } + const prevCandles = lastCandle.epoch === ohlc.epoch ? candles.slice(0, -1) : candles.slice(1); + return [...prevCandles, ohlc]; +}; + +export const getType = isCandle => (isCandle ? 'candles' : 'ticks'); diff --git a/packages/bot-skeleton/src/services/api/ticks_service.js b/packages/bot-skeleton/src/services/api/ticks_service.js index 597bd399124f..1afe4b9a0cd9 100644 --- a/packages/bot-skeleton/src/services/api/ticks_service.js +++ b/packages/bot-skeleton/src/services/api/ticks_service.js @@ -1,43 +1,16 @@ import { Map } from 'immutable'; -import { historyToTicks, getLast } from 'binary-utils'; import { doUntilDone, getUUID } from '../tradeEngine/utils/helpers'; import { observer as globalObserver } from '../../utils/observer'; import { api_base } from './api-base'; - -const parseTick = tick => ({ - epoch: +tick.epoch, - quote: +tick.quote, -}); - -const parseOhlc = ohlc => ({ - open: +ohlc.open, - high: +ohlc.high, - low: +ohlc.low, - close: +ohlc.close, - epoch: +(ohlc.open_time || ohlc.epoch), -}); - -const parseCandles = candles => candles.map(t => parseOhlc(t)); - -const updateTicks = (ticks, newTick) => (getLast(ticks).epoch >= newTick.epoch ? ticks : [...ticks.slice(1), newTick]); - -const updateCandles = (candles, ohlc) => { - const lastCandle = getLast(candles); - if ( - (lastCandle.open === ohlc.open && - lastCandle.high === ohlc.high && - lastCandle.low === ohlc.low && - lastCandle.close === ohlc.close && - lastCandle.epoch === ohlc.epoch) || - lastCandle.epoch > ohlc.epoch - ) { - return candles; - } - const prevCandles = lastCandle.epoch === ohlc.epoch ? candles.slice(0, -1) : candles.slice(1); - return [...prevCandles, ohlc]; -}; - -const getType = isCandle => (isCandle ? 'candles' : 'ticks'); +import { + historyToTicks, + parseTick, + parseOhlc, + parseCandles, + updateTicks, + updateCandles, + getType, +} from './binary-utils'; export default class TicksService { constructor() { diff --git a/packages/bot-skeleton/src/services/tradeEngine/trade/Ticks.js b/packages/bot-skeleton/src/services/tradeEngine/trade/Ticks.js index a414d19d7e7e..50285137843a 100644 --- a/packages/bot-skeleton/src/services/tradeEngine/trade/Ticks.js +++ b/packages/bot-skeleton/src/services/tradeEngine/trade/Ticks.js @@ -1,4 +1,3 @@ -import { getLast } from 'binary-utils'; import { localize } from '@deriv/translations'; import * as constants from './state/constants'; import { getDirection, getLastDigit } from '../utils/helpers'; @@ -6,6 +5,7 @@ import { expectPositiveInteger } from '../utils/sanitize'; import { observer as globalObserver } from '../../../utils/observer'; import { api_base } from '../../api/api-base'; import debounce from 'lodash.debounce'; +import { getLast } from '../../api/binary-utils'; let tickListenerKey;