From 70c1d9788489b691d74b7fe3e359c5d981e5efc6 Mon Sep 17 00:00:00 2001 From: vinu-deriv Date: Mon, 17 Jun 2024 13:52:15 +0400 Subject: [PATCH] restart the bot --- src/api-base.js | 39 ++++++++++--------- src/blockly/bot/Interpreter.js | 39 +++++++++++++------ src/blockly/bot/TradeEngine/OpenContract.js | 8 +++- .../view/TradeInfoPanel/AnimateTrade.js | 8 ++-- 4 files changed, 58 insertions(+), 36 deletions(-) diff --git a/src/api-base.js b/src/api-base.js index fd6207543..5b1484656 100644 --- a/src/api-base.js +++ b/src/api-base.js @@ -28,24 +28,26 @@ class APIBase { this.init(); } - init() { - try { - this.api = new DerivAPIBasic({ - connection: new WebSocket(socket_url), - middleware: new APIMiddleware(), - }); - - this.api_chart = null; - - this.api.onOpen().subscribe(() => { - // eslint-disable-next-line no-console - console.log('Connection has been established!'); - this.initEventListeners(); - }); - } catch (error) { - globalObserver.emit('Error', error); - } - } + init = () => + new Promise(resolve => { + try { + this.api = new DerivAPIBasic({ + connection: new WebSocket(socket_url), + middleware: new APIMiddleware(), + }); + this.api_chart = null; + this.api.onOpen().subscribe(async () => { + // eslint-disable-next-line no-console + console.log('Connection has been established!'); + this.initEventListeners(); + this.api.send({ authorize: this.token }).then(async () => { + resolve(); + }); + }); + } catch (error) { + globalObserver.emit('Error', error); + } + }); async authorize(token) { if (this.token === token) return { authorize: this.account_info }; @@ -58,7 +60,6 @@ class APIBase { this.getLandingCompanyDetails(); this.getLandingCompany(); this.getAccountStatus(); - this.api.send({ proposal_open_contract: 1, subscribe: 1 }); if (!this.balance_subscription_id) { this.getAllBalances(); } diff --git a/src/blockly/bot/Interpreter.js b/src/blockly/bot/Interpreter.js index 745f1bfcd..ff632d73e 100644 --- a/src/blockly/bot/Interpreter.js +++ b/src/blockly/bot/Interpreter.js @@ -3,19 +3,18 @@ import { observer as globalObserver } from '@utilities/observer'; import { createScope } from './CliTools'; import Interface from './Interface'; import { clone } from '../../botPage/common/clone'; +import { api_base } from '../../api-base'; /* eslint-disable func-names, no-underscore-dangle */ JSInterpreter.prototype.takeStateSnapshot = function () { - const new_state_stack = clone(this.state_stack, undefined, undefined, undefined, true); - return new_state_stack; + const newStateStack = clone(this.stateStack, undefined, undefined, undefined, true); + return newStateStack; }; JSInterpreter.prototype.restoreStateSnapshot = function (snapshot) { - this.state_stack = clone(snapshot, undefined, undefined, undefined, true); - if (this.state_stack?.length) { - this.global_object = this.state_stack[0]?.scope?.object; - this.initFunc_(this, this.global_object); - } + this.stateStack = clone(snapshot, undefined, undefined, undefined, true); + this.globalObject = this.stateStack[0]?.scope?.object; + this.initFunc_(this, this.globalObject); }; /* eslint-enable */ @@ -69,9 +68,24 @@ export default class Interpreter { this.$scope.observer.register('REVERT', watchName => this.revert(watchName === 'before' ? this.beforeState : this.duringState) ); + + api_base.api.onClose().subscribe(() => { + const reRunBot = e => { + if (e.id === 'contract.sold') { + // TODO check for trage again block and run it + this.revert(this.startState); + globalObserver.unregister('contract.status', reRunBot); + } + }; + api_base.init().then(() => { + globalObserver.register('contract.status', reRunBot); + + this.$scope.observer.emit('Error', { error: { code: 'DisconnectError', message: '' } }); + }); + }); } - run(code) { + run = code => { const initFunc = (interpreter, scope) => { const botInterface = this.bot.getInterface('Bot'); const ticksInterface = this.bot.getTicksInterface(); @@ -165,7 +179,8 @@ export default class Interpreter { this.$scope.observer.register('Error', onError); this.bot.tradeEngine.init(...initArgs); this.bot.tradeEngine.start(tradeOptions); - this.revert(this.startState); + // TODO to add it back in case of normal error and we bot the coninue + // this.revert(this.startState); }); }; @@ -174,7 +189,7 @@ export default class Interpreter { this.onFinish = resolve; this.loop(); }); - } + }; loop() { if (this.stopped || !this.interpreter.run()) { @@ -182,12 +197,12 @@ export default class Interpreter { } } - revert(state) { + revert = state => { this.interpreter.restoreStateSnapshot(state); // eslint-disable-next-line no-underscore-dangle this.interpreter.paused_ = false; this.loop(); - } + }; terminateSession() { return new Promise((resolve, reject) => { diff --git a/src/blockly/bot/TradeEngine/OpenContract.js b/src/blockly/bot/TradeEngine/OpenContract.js index 02856b79d..4f129ab1e 100644 --- a/src/blockly/bot/TradeEngine/OpenContract.js +++ b/src/blockly/bot/TradeEngine/OpenContract.js @@ -2,6 +2,7 @@ import { api_base } from '@api-base'; import { roundBalance } from '../helpers'; import { contractStatus, contractSettled, contract as broadcastContract } from '../broadcast'; import { sell, openContractReceived } from './state/actions'; +import { doUntilDone } from '../tools'; const AFTER_FINISH_TIMEOUT = 5; @@ -58,6 +59,11 @@ export default Engine => subscribeToOpenContract(contract_id = this.contractId) { this.contractId = contract_id; + doUntilDone(() => + api_base.api.send({ proposal_open_contract: 1, contract_id }).then(response => { + this.contractId = response.proposal_open_contract.contract_id; + }) + ); } resetSubscriptionTimeout(timeout = this.getContractDuration() + AFTER_FINISH_TIMEOUT) { @@ -80,7 +86,7 @@ export default Engine => } expectedContractId(contract_id) { - return this.contractId && contract_id === this.contractId; + return (this.contractId && contract_id === this.contractId) || this.contractId === undefined; } getSellPrice() { diff --git a/src/botPage/view/TradeInfoPanel/AnimateTrade.js b/src/botPage/view/TradeInfoPanel/AnimateTrade.js index 6c7834a37..ee3c75338 100644 --- a/src/botPage/view/TradeInfoPanel/AnimateTrade.js +++ b/src/botPage/view/TradeInfoPanel/AnimateTrade.js @@ -29,6 +29,10 @@ const AnimateTrade = () => { const [contract_status, setContractStatus] = React.useState(CONTRACT_STATUS.not_running); const isMounted = useIsMounted(); + const resetSummary = () => { + setIndicatorMessage(INDICATOR_MESSAGES.not_running); + }; + React.useEffect(() => { globalObserver.register('reset_animation', resetSummary); globalObserver.register('summary.clear', resetSummary); @@ -58,10 +62,6 @@ const AnimateTrade = () => { // eslint-disable-next-line react-hooks/exhaustive-deps }, []); - const resetSummary = () => { - setIndicatorMessage(INDICATOR_MESSAGES.not_running); - }; - const animateStage = contract => { if (contract.id === 'contract.purchase_sent') { setContractStatus(CONTRACT_STATUS.attempting_to_buy);