Skip to content
This repository has been archived by the owner on Sep 12, 2024. It is now read-only.

restart the bot #463

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 20 additions & 19 deletions src/api-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand All @@ -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();
}
Expand Down
39 changes: 27 additions & 12 deletions src/blockly/bot/Interpreter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 */

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
});
};

Expand All @@ -174,20 +189,20 @@ export default class Interpreter {
this.onFinish = resolve;
this.loop();
});
}
};

loop() {
if (this.stopped || !this.interpreter.run()) {
this.onFinish(this.interpreter.pseudoToNative(this.interpreter.value));
}
}

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) => {
Expand Down
8 changes: 7 additions & 1 deletion src/blockly/bot/TradeEngine/OpenContract.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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) {
Expand All @@ -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() {
Expand Down
8 changes: 4 additions & 4 deletions src/botPage/view/TradeInfoPanel/AnimateTrade.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
Loading