This repository was archived by the owner on May 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathdata.js
More file actions
126 lines (106 loc) · 3.75 KB
/
data.js
File metadata and controls
126 lines (106 loc) · 3.75 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import endpoints from './api/endpoints';
import Ticker from './core/ticker';
import { configEnabled } from './config';
import cache from './optional/securities-cache';
class Data {
/**
* Creates a new Data pbject associated with the given Https worker state.
*
* @param {*} httpsWorker
*/
constructor(httpsWorker) {
this.worker = httpsWorker;
}
/**
* A snapshot of the current USD/CAD exchange rates on the Wealthsimple Trade
* platform.
*/
async exchangeRates() {
return this.worker.handleRequest(endpoints.EXCHANGE_RATES, {});
}
/**
* Information about a security on the Wealthsimple Trade Platform.
*
* @param {string|object} userTicker The security id
* @param {boolean} extensive Pulls a more detailed report of the security using the
* /securities/{id} API
*/
async getSecurity(userTicker, extensive) {
let result = null;
// Run some validation on the ticker
const ticker = new Ticker(userTicker);
if (!extensive && configEnabled('securities_cache')) {
result = cache.get(ticker);
if (result) {
return result;
}
}
if (ticker.id) {
// We will immediately call the extensive details API since we have the unique id.
result = await this.worker.handleRequest(endpoints.EXTENSIVE_SECURITY_DETAILS, {
id: ticker.id,
});
} else {
result = await this.worker.handleRequest(endpoints.SECURITY, { ticker: ticker.symbol });
result = result.filter((security) => security.stock.symbol === ticker.symbol);
if (ticker.crypto) {
result = result.filter((security) => security.security_type === 'cryptocurrency');
} else if (ticker.exchange) {
result = result.filter((security) => security.stock.primary_exchange === ticker.exchange);
}
if (result.length > 1) {
throw new Error('Multiple securities matched query.');
} if (result.length === 0) {
throw new Error('No securities matched query.');
}
// Convert result from a singleton list to its raw entry
[result] = result;
if (extensive) {
// The caller has opted to receive the extensive details about the security.
result = await this.worker.handleRequest(endpoints.EXTENSIVE_SECURITY_DETAILS, {
id: result.id,
});
}
}
if (configEnabled('securities_cache') && cache.get(ticker) === null) {
cache.insert(ticker, result);
}
return result;
}
/**
* Fetches a mapping of all security groups (available on the Trade platform) to
* their group ids.
*/
async securityGroups() {
const result = await this.worker.handleRequest(endpoints.SECURITY_GROUPS, {});
// Construct a map of category name to category id
return result.reduce((map, item) => ({
...map, [item.name]: item.external_security_group_id,
}), {});
}
/**
* Retrieves all securities associated with the group name or id.
*
* - If you provide the group name, we will automatically do a lookup
* from the Trade servers to get its identifier.
*
* - Alternatively, You can get a list of all groups (with their group ids) from
* data.groups() and provide the group identifier directly.
*
* @param {*} group The security group name or identifier
*/
async getSecurityGroup(group) {
let groupId = group;
// Fetch the group id from the group name
if (!group?.startsWith('security-group')) {
const groups = await this.securityGroups();
if (!(group in groups)) {
throw new Error(`'${group}' is not a valid group name!`);
}
// record the matched group id.
groupId = groups[group];
}
return this.worker.handleRequest(endpoints.SECURITY_GROUP, { groupId });
}
}
export default Data;