@icgio/icg-exchanges-wrapper 1.14.102 → 1.14.103

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -134,11 +134,12 @@ const get_exchanges_rates = function (Exchanges, exchange_pair_dict, cb, ws_enab
134
134
  })
135
135
  }
136
136
 
137
- let checked_market_history = {},
138
- exchange_pair_market_history = {},
137
+ let exchange_pair_market_history = {},
139
138
  pair_exchange_market_history = {}
140
139
  const get_exchanges_market_history = function (Exchanges, exchange_pair_dict, timeframe_in_ms, cb, ws_enabled = true) {
141
- checked_market_history = {}
140
+ let checked_market_history = {}
141
+ let promises = []
142
+
142
143
  if (!Array.isArray(Exchanges)) {
143
144
  console.error('Exchanges should be an array')
144
145
  return
@@ -149,16 +150,62 @@ const get_exchanges_market_history = function (Exchanges, exchange_pair_dict, ti
149
150
  continue
150
151
  }
151
152
  checked_market_history[Exchange_name] = true
153
+
152
154
  if (!exchange_pair_dict[Exchange_name]) {
153
155
  continue
154
156
  }
157
+
155
158
  if (Exchange.ws_market && ws_enabled) {
156
- if (!first_time[Exchange_name]) {
157
- Exchange.ws_market(exchange_pair_dict[Exchange_name])
158
- first_time[Exchange_name] = true
159
- } else {
160
- for (let pair of exchange_pair_dict[Exchange_name]) {
161
- Exchange.market_history_ws(pair, timeframe_in_ms, function (res) {
159
+ let ws_promise = new Promise((resolve, reject) => {
160
+ if (!first_time[Exchange_name]) {
161
+ Exchange.ws_market(exchange_pair_dict[Exchange_name])
162
+ first_time[Exchange_name] = true
163
+ }
164
+
165
+ const check_status = () => {
166
+ let all_pairs_opened = exchange_pair_dict[Exchange_name].every((pair) => {
167
+ return Exchange.ws.market.pair_status[pair] === 'opened' && Exchange.ws.market.market_history_dict[pair].length >= 0
168
+ })
169
+
170
+ if (all_pairs_opened) {
171
+ let market_history_promises = exchange_pair_dict[Exchange_name].map((pair) => {
172
+ return new Promise((rate_resolve) => {
173
+ Exchange.market_history_ws(pair, timeframe_in_ms, function (res) {
174
+ if (res.success) {
175
+ _.set(exchange_pair_market_history, [Exchange_name, pair], res.body)
176
+ _.set(pair_exchange_market_history, [pair, Exchange_name], res.body)
177
+ } else {
178
+ if (exchange_pair_market_history[Exchange_name]) {
179
+ delete exchange_pair_market_history[Exchange_name][pair]
180
+ }
181
+ if (pair_exchange_market_history[pair]) {
182
+ delete pair_exchange_market_history[pair][Exchange_name]
183
+ }
184
+ }
185
+ rate_resolve()
186
+ })
187
+ })
188
+ })
189
+
190
+ Promise.all(market_history_promises)
191
+ .then(() => resolve())
192
+ .catch((err) => {
193
+ console.error(`Error in market_history_ws for ${Exchange_name}:`, err)
194
+ resolve()
195
+ })
196
+ } else {
197
+ setTimeout(check_status, 100)
198
+ }
199
+ }
200
+
201
+ check_status()
202
+ })
203
+
204
+ promises.push(ws_promise)
205
+ } else if (Exchange.market_history) {
206
+ let market_history_promises = exchange_pair_dict[Exchange_name].map((pair) => {
207
+ return new Promise((resolve) => {
208
+ Exchange.market_history(pair, timeframe_in_ms, function (res) {
162
209
  if (res.success) {
163
210
  _.set(exchange_pair_market_history, [Exchange_name, pair], res.body)
164
211
  _.set(pair_exchange_market_history, [pair, Exchange_name], res.body)
@@ -170,29 +217,24 @@ const get_exchanges_market_history = function (Exchanges, exchange_pair_dict, ti
170
217
  delete pair_exchange_market_history[pair][Exchange_name]
171
218
  }
172
219
  }
220
+ resolve()
173
221
  })
174
- }
175
- }
176
- } else if (Exchange.market_history) {
177
- for (let pair of exchange_pair_dict[Exchange_name]) {
178
- Exchange.market_history(pair, timeframe_in_ms, function (res) {
179
- if (res.success) {
180
- _.set(exchange_pair_market_history, [Exchange_name, pair], res.body)
181
- _.set(pair_exchange_market_history, [pair, Exchange_name], res.body)
182
- } else {
183
- if (exchange_pair_market_history[Exchange_name]) {
184
- delete exchange_pair_market_history[Exchange_name][pair]
185
- }
186
- if (pair_exchange_market_history[pair]) {
187
- delete pair_exchange_market_history[pair][Exchange_name]
188
- }
189
- }
190
222
  })
191
- }
223
+ })
224
+
225
+ promises = promises.concat(market_history_promises)
192
226
  }
193
227
  }
194
228
  }
195
- cb({ exchange_pair_market_history, pair_exchange_market_history })
229
+
230
+ Promise.all(promises)
231
+ .then(() => {
232
+ cb({ exchange_pair_market_history, pair_exchange_market_history })
233
+ })
234
+ .catch((error) => {
235
+ console.error('Error in fetching market history:', error)
236
+ cb({ exchange_pair_market_history, pair_exchange_market_history })
237
+ })
196
238
  }
197
239
 
198
240
  let checked_ohlcv_exchange = {},
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@icgio/icg-exchanges-wrapper",
3
- "version": "1.14.102",
3
+ "version": "1.14.103",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -17,7 +17,7 @@
17
17
  },
18
18
  "homepage": "https://github.com/icgio/icg-exchanges-wrapper#readme",
19
19
  "dependencies": {
20
- "@icgio/icg-exchanges": "^1.32.56",
20
+ "@icgio/icg-exchanges": "^1.32.58",
21
21
  "@icgio/icg-exchanges-data": "^1.10.55",
22
22
  "@icgio/icg-utils": "^1.9.16",
23
23
  "influx": "^5.10.0",