From 4f3ffcc79cc55787e52506c3bfb22c21881b868b Mon Sep 17 00:00:00 2001 From: Carol Sachdeva <58209918+carol-binary@users.noreply.github.com> Date: Wed, 28 Sep 2022 16:35:42 +0800 Subject: [PATCH] =?UTF-8?q?Revert=20"sandeep/65842/unifiy-websocket=20--?= =?UTF-8?q?=20using=20the=20same=20websocket=20instance=20from=20the=20cor?= =?UTF-8?q?e=20=E2=80=A6=20(#6555)"=20(#6623)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit e2b3d2e2f6cdd2b1af097ce14f84d3bf03baf87f. --- .../services/api/__tests__/ticks-service.js | 4 ++-- .../bot-skeleton/src/services/api/appId.js | 12 ++++++++++++ .../src/services/tradeEngine/trade/Balance.js | 3 ++- .../src/services/tradeEngine/trade/index.js | 18 +++--------------- .../src/services/tradeEngine/utils/cliTools.js | 4 ++-- .../services/tradeEngine/utils/interpreter.js | 7 +++++++ packages/bot-web-ui/src/app/app.jsx | 3 +-- 7 files changed, 29 insertions(+), 22 deletions(-) create mode 100644 packages/bot-skeleton/src/services/api/appId.js diff --git a/packages/bot-skeleton/src/services/api/__tests__/ticks-service.js b/packages/bot-skeleton/src/services/api/__tests__/ticks-service.js index c7d04207b141..4fdb8fed1ffa 100644 --- a/packages/bot-skeleton/src/services/api/__tests__/ticks-service.js +++ b/packages/bot-skeleton/src/services/api/__tests__/ticks-service.js @@ -1,10 +1,10 @@ /* eslint-disable no-unused-expressions */ +import { generateDerivApiInstance } from '../appId'; import TicksService from '../ticks_service'; -import { WS } from '@deriv/shared'; jasmine.DEFAULT_TIMEOUT_INTERVAL = 20000; -const ticksService = new TicksService(WS.get()); +const ticksService = new TicksService(generateDerivApiInstance()); const isTick = t => Number.isInteger(t.epoch) && Number.isFinite(t.quote); diff --git a/packages/bot-skeleton/src/services/api/appId.js b/packages/bot-skeleton/src/services/api/appId.js new file mode 100644 index 000000000000..ed7417a9d34d --- /dev/null +++ b/packages/bot-skeleton/src/services/api/appId.js @@ -0,0 +1,12 @@ +import DerivAPIBasic from '@deriv/deriv-api/dist/DerivAPIBasic'; +import { getAppId, getSocketURL, website_name } from '@deriv/shared'; +import { getLanguage } from '@deriv/translations'; + +export const generateDerivApiInstance = () => { + const socket_url = `wss://${getSocketURL()}/websockets/v3?app_id=${getAppId()}&l=${getLanguage()}&brand=${website_name.toLowerCase()}`; + const deriv_socket = new WebSocket(socket_url); + const deriv_api = new DerivAPIBasic({ + connection: deriv_socket, + }); + return deriv_api; +}; diff --git a/packages/bot-skeleton/src/services/tradeEngine/trade/Balance.js b/packages/bot-skeleton/src/services/tradeEngine/trade/Balance.js index 641c73473a6e..19f3aad85318 100644 --- a/packages/bot-skeleton/src/services/tradeEngine/trade/Balance.js +++ b/packages/bot-skeleton/src/services/tradeEngine/trade/Balance.js @@ -14,7 +14,8 @@ export default Engine => } = data; balance_string = getFormattedText(b, currency); - info({ accountID: this.accountInfo?.loginid, balance: balance_string }); + + info({ accountID: this.accountInfo.loginid, balance: balance_string }); } }); } diff --git a/packages/bot-skeleton/src/services/tradeEngine/trade/index.js b/packages/bot-skeleton/src/services/tradeEngine/trade/index.js index 8a0b80abab21..145bfcf67219 100644 --- a/packages/bot-skeleton/src/services/tradeEngine/trade/index.js +++ b/packages/bot-skeleton/src/services/tradeEngine/trade/index.js @@ -74,9 +74,6 @@ export default class TradeEngine extends Balance(Purchase(Sell(OpenContract(Prop forget_proposal_ids: [], }; this.store = createStore(rootReducer, applyMiddleware(thunk)); - this.api.connection.onclose = () => { - globalObserver.setState({ transaction_subscription_id: null }); - }; } init(...args) { @@ -133,28 +130,19 @@ export default class TradeEngine extends Balance(Purchase(Sell(OpenContract(Prop }, 1500); } if (data.msg_type === 'authorize') { - const { authorize = {} } = data; - this.accountInfo = authorize; + this.accountInfo = data; this.token = token; // Only subscribe to balance in browser, not for tests. if (document) { - doUntilDone(() => this.api.send({ balance: 1 })).then(r => { + doUntilDone(() => this.api.send({ balance: 1, subscribe: 1 })).then(r => { this.balance = Number(r.balance.balance); resolve(); }); } else { resolve(); } - if (!globalObserver.getState('transaction_subscription_id')) { - doUntilDone(() => this.api.send({ transaction: 1, subscribe: 1 })) - .then(({ transaction }) => { - globalObserver.setState({ transaction_subscription_id: transaction.id }); - }) - .catch(err => { - this.$scope.observer.emit('Error', err); - }); - } + doUntilDone(() => this.api.send({ transaction: 1, subscribe: 1 })); } }); }); diff --git a/packages/bot-skeleton/src/services/tradeEngine/utils/cliTools.js b/packages/bot-skeleton/src/services/tradeEngine/utils/cliTools.js index 6c04283ce5f3..bac5c53d459d 100644 --- a/packages/bot-skeleton/src/services/tradeEngine/utils/cliTools.js +++ b/packages/bot-skeleton/src/services/tradeEngine/utils/cliTools.js @@ -1,10 +1,10 @@ import TicksService from '../../api/ticks_service'; import Observer from '../../../utils/observer'; -import { WS } from '@deriv/shared'; +import { generateDerivApiInstance } from '../../api/appId'; export const createScope = () => { const observer = new Observer(); - const api = WS.get(); + const api = generateDerivApiInstance(); const ticksService = new TicksService(api); const stopped = false; return { observer, api, ticksService, stopped }; diff --git a/packages/bot-skeleton/src/services/tradeEngine/utils/interpreter.js b/packages/bot-skeleton/src/services/tradeEngine/utils/interpreter.js index 0cb220e467c0..d43cb67f01d0 100644 --- a/packages/bot-skeleton/src/services/tradeEngine/utils/interpreter.js +++ b/packages/bot-skeleton/src/services/tradeEngine/utils/interpreter.js @@ -180,6 +180,13 @@ const Interpreter = () => { } function terminateSession() { + const { connection } = $scope.api; + if (connection.readyState === 0) { + connection.addEventListener('open', () => connection.close()); + } else if (connection.readyState === 1) { + connection.close(); + } + $scope.stopped = true; $scope.is_error_triggered = false; globalObserver.emit('bot.stop'); diff --git a/packages/bot-web-ui/src/app/app.jsx b/packages/bot-web-ui/src/app/app.jsx index 4d02db44f6d3..365468c0c6f0 100644 --- a/packages/bot-web-ui/src/app/app.jsx +++ b/packages/bot-web-ui/src/app/app.jsx @@ -14,7 +14,7 @@ import { Toolbar, NetworkToastPopup, } from 'Components'; -import { LocalStore, setWebsocket } from '@deriv/shared'; +import { LocalStore } from '@deriv/shared'; import { MobxContentProvider } from 'Stores/connect'; import RootStore from 'Stores'; import GTM from 'Utils/gtm'; @@ -31,7 +31,6 @@ const App = ({ passthrough }) => { const { onMount, onUnmount, showDigitalOptionsMaltainvestError } = app; React.useEffect(() => { - setWebsocket(WS); /** * Inject: External Script Hotjar - for DBot only */