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 pathOpenContract.js
More file actions
109 lines (88 loc) · 3.64 KB
/
OpenContract.js
File metadata and controls
109 lines (88 loc) · 3.64 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import { roundBalance } from '../../common/tools';
import { doUntilDone } from '../tools';
import { contractStatus, contractSettled, contract as broadcastContract } from '../broadcast';
import { sell, openContractReceived } from './state/actions';
import { observer } from '../../../common/utils/observer';
const AFTER_FINISH_TIMEOUT = 5;
export default Engine =>
class OpenContract extends Engine {
observeOpenContract() {
this.listen('proposal_open_contract', r => {
const contract = r.proposal_open_contract;
if (!this.expectedContractId(contract.contract_id)) {
return;
}
this.setContractFlags(contract);
this.data.contract = contract;
broadcastContract({ accountID: this.accountInfo.loginid, ...contract });
if (this.isSold) {
contractStatus({
id : 'contract.sold',
data: contract.transaction_ids.sell,
contract,
});
contractSettled(contract);
this.contractId = '';
this.updateTotals(contract);
if (this.afterPromise) {
this.afterPromise();
}
this.store.dispatch(sell());
this.cancelSubscriptionTimeout();
} else {
this.store.dispatch(openContractReceived());
if (!this.isExpired) {
this.resetSubscriptionTimeout();
}
}
});
}
waitForAfter() {
return new Promise(resolve => {
this.afterPromise = resolve;
});
}
subscribeToOpenContract(contractId = this.contractId) {
if (this.contractId !== contractId) {
this.resetSubscriptionTimeout();
}
this.contractId = contractId;
doUntilDone(() =>
this.api.subscribeToOpenContract(contractId).then(response => {
this.openContractId = response.proposal_open_contract.id;
})
).catch(error => {
observer.emit('reset_animation');
observer.emit('Error', error);
});
}
resetSubscriptionTimeout(timeout = this.getContractDuration() + AFTER_FINISH_TIMEOUT) {
this.cancelSubscriptionTimeout();
this.subscriptionTimeout = setInterval(() => {
this.subscribeToOpenContract();
this.resetSubscriptionTimeout(timeout);
}, timeout * 1000);
}
cancelSubscriptionTimeout() {
clearTimeout(this.subscriptionTimeout);
}
setContractFlags(contract) {
const {
is_expired: isExpired,
is_valid_to_sell: isValidToSell,
is_sold: isSold,
entry_tick: entryTick,
} = contract;
this.isSold = Boolean(isSold);
this.isSellAvailable = !this.isSold && Boolean(isValidToSell);
this.isExpired = Boolean(isExpired);
this.hasEntryTick = Boolean(entryTick);
}
expectedContractId(contractId) {
return this.contractId && contractId === this.contractId;
}
getSellPrice() {
const { bid_price: bidPrice, buy_price: buyPrice, currency } = this.data.contract;
return Number(roundBalance({ currency, balance: Number(bidPrice) - Number(buyPrice) }));
}
};