This repository was archived by the owner on Feb 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 543
Expand file tree
/
Copy pathPurchase.js
More file actions
93 lines (78 loc) · 3.54 KB
/
Purchase.js
File metadata and controls
93 lines (78 loc) · 3.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import { translate } from '../../../common/i18n';
import { getUUID, recoverFromError, doUntilDone } from '../tools';
import { contractStatus, info, notify } from '../broadcast';
import { purchaseSuccessful } from './state/actions';
import { BEFORE_PURCHASE } from './state/constants';
import GTM from '../../../common/gtm';
let delayIndex = 0;
let purchaseReference;
export default Engine =>
class Purchase extends Engine {
purchase(contractType) {
// Prevent calling purchase twice
if (this.store.getState().scope !== BEFORE_PURCHASE) {
return Promise.resolve();
}
const { currency, proposal } = this.selectProposal(contractType);
const onSuccess = response => {
// Don't unnecessarily send a forget request for a purchased contract.
this.data.proposals = this.data.proposals.filter(p => p.id !== response.echo_req.buy);
const { buy } = response;
/* eslint-disable-next-line no-unused-expressions */
GTM?.pushDataLayer?.({ event: 'bot_purchase', buy_price: proposal.ask_price });
contractStatus({
id : 'contract.purchase_recieved',
data: buy.transaction_id,
proposal,
currency,
});
this.subscribeToOpenContract(buy.contract_id);
this.store.dispatch(purchaseSuccessful());
this.renewProposalsOnPurchase();
delayIndex = 0;
notify('info', `${translate('Bought')}: ${buy.longcode} (${translate('ID')}: ${buy.transaction_id})`);
info({
accountID : this.accountInfo.loginid,
totalRuns : this.updateAndReturnTotalRuns(),
transaction_ids: { buy: buy.transaction_id },
contract_type : contractType,
buy_price : buy.buy_price,
});
};
this.isSold = false;
contractStatus({
id : 'contract.purchase_sent',
data: proposal.ask_price,
proposal,
currency,
});
const action = () => this.api.buyContract(proposal.id, proposal.ask_price);
if (!this.options.timeMachineEnabled) {
return doUntilDone(action).then(onSuccess);
}
return recoverFromError(
action,
(errorCode, makeDelay) => {
// if disconnected no need to resubscription (handled by live-api)
if (errorCode !== 'DisconnectError') {
this.renewProposalsOnPurchase();
} else {
this.clearProposals();
}
const unsubscribe = this.store.subscribe(() => {
const { scope, proposalsReady } = this.store.getState();
if (scope === BEFORE_PURCHASE && proposalsReady) {
makeDelay().then(() => this.observer.emit('REVERT', 'before'));
unsubscribe();
}
});
},
['PriceMoved', 'InvalidContractProposal'],
delayIndex++
).then(onSuccess);
}
getPurchaseReference = () => purchaseReference;
regeneratePurchaseReference = () => {
purchaseReference = getUUID();
};
};