Skip to content
This repository was archived by the owner on Feb 22, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
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
28 changes: 10 additions & 18 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
"jquery": "^3.4.1",
"jquery-ui": "1.12.1",
"jquery-ui-css": "1.11.4",
"js-interpreter": "^1.4.6",
"js-interpreter": "^2.1.0",
"json2csv": "^3.11.5",
"lint-staged": "^8.1.7",
"loader-utils": "^1.1.0",
Expand All @@ -105,6 +105,7 @@
"@binary-com/smartcharts": "^0.6.1",
"binary-style": "^0.2.4",
"blockly": "github:google/blockly#59e5ac6",
"clone": "aminmarashi/clone#d97b4f",
"commander": "^2.20.0",
"concat-stream": "^2.0.0",
"core-js": "^2.6.5",
Expand Down
42 changes: 35 additions & 7 deletions src/botPage/bot/Interpreter.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
import clone from 'clone';
import JSInterpreter from 'js-interpreter';
import { observer as globalObserver } from '../../common/utils/observer';
import { createScope } from './CliTools';
import Interface from './Interface';

/* eslint-disable func-names, no-underscore-dangle */
JSInterpreter.prototype.takeStateSnapshot = function() {
const newStateStack = clone(this.stateStack, undefined, undefined, undefined, true);
return newStateStack;
};

JSInterpreter.prototype.restoreStateSnapshot = function(snapshot) {
this.stateStack = clone(snapshot, undefined, undefined, undefined, true);
this.global = this.stateStack[0].scope;
this.initFunc_(this, this.global);
};
/* eslint-enable */

const unrecoverableErrors = [
'InsufficientBalance',
'CustomLimitsReached',
'OfferingsValidationError',
'InvalidCurrency',
'ContractBuyValidationError',
'NotDefaultCurrency',
'PleaseAuthenticate',
'FinancialAssessmentRequired',
Expand Down Expand Up @@ -56,9 +69,9 @@ export default class Interpreter {

const pseudoBotIf = interpreter.nativeToPseudo(BotIf);

Object.entries(ticksIf).forEach(([name, f]) =>
interpreter.setProperty(pseudoBotIf, name, this.createAsync(interpreter, f))
);
Object.entries(ticksIf).forEach(([name, f]) => {
interpreter.setProperty(pseudoBotIf, name, this.createAsync(interpreter, f));
});

interpreter.setProperty(
pseudoBotIf,
Expand Down Expand Up @@ -169,16 +182,31 @@ export default class Interpreter {
}
}
createAsync(interpreter, func) {
return interpreter.createAsyncFunction((...args) => {
const asyncFunc = (...args) => {
const callback = args.pop();

func(...args.map(arg => interpreter.pseudoToNative(arg)))
// Workaround for unknown number of args
const reversedArgs = args.slice().reverse();
const firsDefinedArgIdx = reversedArgs.findIndex(arg => arg !== undefined);

// Remove extra undefined args from end of the args
const functionArgs = firsDefinedArgIdx < 0 ? [] : reversedArgs.slice(firsDefinedArgIdx).reverse();
// End of workaround

func(...functionArgs.map(arg => interpreter.pseudoToNative(arg)))
.then(rv => {
callback(interpreter.nativeToPseudo(rv));
this.loop();
})
.catch(e => this.$scope.observer.emit('Error', e));
});
};

// TODO: This is a workaround, create issue on original repo, once fixed
// remove this. We don't know how many args are going to be passed, so we
// assume a max of 100.
const MAX_ACCEPTABLE_FUNC_ARGS = 100;
Object.defineProperty(asyncFunc, 'length', { value: MAX_ACCEPTABLE_FUNC_ARGS + 1 });
return interpreter.createAsyncFunction(asyncFunc);
}
hasStarted() {
return !this.stopped;
Expand Down
36 changes: 22 additions & 14 deletions src/botPage/bot/TradeEngine/Proposal.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { translate } from '../../../common/i18n';
import { tradeOptionToProposal, doUntilDone } from '../tools';
import { proposalsReady, clearProposals } from './state/actions';
import { TrackJSError } from '../../view/logger';

export default Engine =>
class Proposal extends Engine {
Expand All @@ -22,15 +23,20 @@ export default Engine =>
this.data.get('proposals').forEach(proposal => {
if (proposal.contractType === contractType) {
if (proposal.error) {
throw Error(proposal.error.error.error.message);
const { error } = proposal.error;
throw new TrackJSError(error.error.code, error.error.message, error);
} else {
toBuy = proposal;
}
}
});

if (!toBuy) {
throw Error(translate('Selected proposal does not exist'));
throw new TrackJSError(
'CustomInvalidProposal',
translate('Selected proposal does not exist'),
Array.from(this.data.get('proposals')).map(proposal => proposal[1])
);
}

return {
Expand All @@ -46,14 +52,12 @@ export default Engine =>
this.store.dispatch(clearProposals());
}
requestProposals() {
this.proposalTemplates.map(proposal =>
doUntilDone(() =>
this.api
.subscribeToPriceForContractProposal(proposal)
// eslint-disable-next-line consistent-return
.catch(e => {
Promise.all(
this.proposalTemplates.map(proposal =>
doUntilDone(() =>
this.api.subscribeToPriceForContractProposal(proposal).catch(e => {
if (e && e.name === 'RateLimit') {
return Promise.reject(e);
throw e;
}

const errorCode = e.error && e.error.error && e.error.error.code;
Expand All @@ -62,18 +66,22 @@ export default Engine =>
const { uuid } = e.error.echo_req.passthrough;

if (!this.data.hasIn(['forgetProposals', uuid])) {
// Add to proposals map with error. Will later be shown to user, see selectProposal.
this.data = this.data.setIn(['proposals', uuid], {
...proposal,
contractType: proposal.contract_type,
error : e,
...proposal.passthrough,
error: e,
});
}
} else {
this.$scope.observer.emit('Error', e);

return null;
}

throw e;
})
)
)
);
).catch(e => this.$scope.observer.emit('Error', e));
}
observeProposals() {
this.listen('proposal', r => {
Expand Down
29 changes: 23 additions & 6 deletions src/botPage/bot/tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,29 @@ const getBackoffDelay = (error, delayIndex) => {
return linearIncrease * 1000;
};

export const shouldThrowError = (e, types = [], delayIndex = 0) =>
e &&
(!types
.concat(['CallError', 'WrongResponse', 'GetProposalFailure', 'RateLimit', 'DisconnectError'])
.includes(e.name) ||
(e.name !== 'DisconnectError' && delayIndex > maxRetries));
export const shouldThrowError = (error, types = [], delayIndex = 0) => {
if (!error) {
return false;
}

const defaultErrors = ['CallError', 'WrongResponse', 'GetProposalFailure', 'RateLimit', 'DisconnectError'];
const authErrors = ['InvalidToken', 'AuthorizationRequired'];
const errors = types.concat(defaultErrors);

if (authErrors.includes(error.name)) {
// If auth error, reload page.
window.location.reload();
return true;
} else if (!errors.includes(error.name)) {
// If error is unrecoverable, throw error.
return true;
} else if (error.name !== 'DisconnectError' && delayIndex > maxRetries) {
// If exceeded maxRetries, throw error.
return true;
}

return false;
};

export const recoverFromError = (f, r, types, delayIndex) =>
new Promise((resolve, reject) => {
Expand Down
2 changes: 1 addition & 1 deletion src/botPage/view/LogTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export default class LogTable extends Component {
type : PropTypes.string,
timestamp: PropTypes.string,
message : PropTypes.string,
}).isRequired,
}),
};
constructor() {
super();
Expand Down