@icgio/icg-exchanges-wrapper 1.14.265 → 1.14.267

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.
@@ -59,21 +59,26 @@ module.exports = class Account {
59
59
  })
60
60
  }
61
61
  }
62
- get_rate_usd(cur, cmc_rates) {
63
- // Unified rate resolution: forex_rates first, then cmc_rates fallback
62
+ get_rate_usd(cur, crypto_rates) {
63
+ // Unified rate resolution: forex_rates first, then crypto_rates fallback
64
64
  if (this.forex_rates[cur]) {
65
65
  return this.forex_rates[cur]
66
66
  }
67
+ // Check if crypto_rates exists before accessing it
68
+ if (!crypto_rates) {
69
+ console.log('get_rate_usd crypto_rates is undefined for %s', cur)
70
+ return 0
71
+ }
67
72
  // Log warnings for missing/expired CMC rates
68
- if (!cmc_rates[cur]) {
73
+ if (!crypto_rates[cur]) {
69
74
  console.log('get_rate_usd rates_cmc %s does not exist', cur)
70
- } else if (cmc_rates[cur].update_time < new Date().getTime() - CMC_RATES_EXPIRY_TIMEOUT) {
75
+ } else if (crypto_rates[cur].update_time < new Date().getTime() - CMC_RATES_EXPIRY_TIMEOUT) {
71
76
  // some currencies like USDT are updating slow
72
77
  console.log('get_rate_usd rates_cmc %s expired', cur)
73
78
  }
74
- return _.get(cmc_rates, [cur, 'USD'], 0)
79
+ return _.get(crypto_rates, [cur, 'USD'], 0)
75
80
  }
76
- get_depth(rates, cmc_rates, percentages) {
81
+ get_depth(rates, crypto_rates, percentages) {
77
82
  let depth = {}
78
83
  for (let exchange in rates) {
79
84
  for (let pair in rates[exchange]) {
@@ -84,7 +89,7 @@ module.exports = class Account {
84
89
  continue
85
90
  }
86
91
  let mid = (asks[0][0] + bids[0][0]) / 2
87
- let quote_cur_rate = this.get_rate_usd(quote_cur, cmc_rates)
92
+ let quote_cur_rate = this.get_rate_usd(quote_cur, crypto_rates)
88
93
  for (let percentage of percentages) {
89
94
  let ask_depth = utils.price_position(asks, mid * (1 + percentage)).sum
90
95
  let bid_depth = utils.price_position(bids, mid / (1 + percentage)).sum
@@ -149,7 +154,7 @@ module.exports = class Account {
149
154
  }
150
155
  return position
151
156
  }
152
- get_exchange_profit(balances, cmc_rates, type = 'initial') {
157
+ get_exchange_profit(balances, crypto_rates, type = 'initial') {
153
158
  let exchange_position = this.get_exchange_position(balances, type)
154
159
  let exchange_profit = {}
155
160
  for (let exchange in exchange_position) {
@@ -158,15 +163,15 @@ module.exports = class Account {
158
163
  }
159
164
  for (let cur in exchange_position[exchange]) {
160
165
  let position = exchange_position[exchange][cur].amount
161
- let rate = this.get_rate_usd(cur, cmc_rates)
166
+ let rate = this.get_rate_usd(cur, crypto_rates)
162
167
  profit.USD += position * rate
163
168
  }
164
169
  _.set(exchange_profit, [exchange], profit)
165
170
  }
166
171
  return exchange_profit
167
172
  }
168
- get_profit(balances, cmc_rates, type = 'initial') {
169
- let exchange_profit = this.get_exchange_profit(balances, cmc_rates, type)
173
+ get_profit(balances, crypto_rates, type = 'initial') {
174
+ let exchange_profit = this.get_exchange_profit(balances, crypto_rates, type)
170
175
  let profit = {
171
176
  USD: 0,
172
177
  }
@@ -175,15 +180,15 @@ module.exports = class Account {
175
180
  }
176
181
  return profit
177
182
  }
178
- get_exchange_distribution(balances, cmc_rates) {
183
+ get_exchange_distribution(balances, crypto_rates) {
179
184
  let exchange_distribution = {}
180
185
  for (let exchange in balances) {
181
186
  let total_in_USD = 0
182
187
  for (let cur in balances[exchange].total) {
183
- total_in_USD += balances[exchange].total[cur] * this.get_rate_usd(cur, cmc_rates)
188
+ total_in_USD += balances[exchange].total[cur] * this.get_rate_usd(cur, crypto_rates)
184
189
  }
185
190
  for (let cur in balances[exchange].total) {
186
- let rate = this.get_rate_usd(cur, cmc_rates)
191
+ let rate = this.get_rate_usd(cur, crypto_rates)
187
192
  let percentage = (balances[exchange].total[cur] * rate) / total_in_USD
188
193
  if (percentage > DISTRIBUTION_MIN_BENCHMARK) {
189
194
  _.set(exchange_distribution, [exchange, cur], (percentage * 100).toPrecision(3) + '%')
@@ -192,17 +197,17 @@ module.exports = class Account {
192
197
  }
193
198
  return exchange_distribution
194
199
  }
195
- get_total_distribution(balances, cmc_rates) {
200
+ get_total_distribution(balances, crypto_rates) {
196
201
  let total_distribution = {}
197
202
  let total_in_USD = 0
198
203
  for (let exchange in balances) {
199
204
  for (let cur in balances[exchange].total) {
200
- total_in_USD += balances[exchange].total[cur] * this.get_rate_usd(cur, cmc_rates)
205
+ total_in_USD += balances[exchange].total[cur] * this.get_rate_usd(cur, crypto_rates)
201
206
  }
202
207
  }
203
208
  for (let exchange in balances) {
204
209
  for (let cur in balances[exchange].total) {
205
- let rate = this.get_rate_usd(cur, cmc_rates)
210
+ let rate = this.get_rate_usd(cur, crypto_rates)
206
211
  let percentage = (balances[exchange].total[cur] * rate) / total_in_USD
207
212
  if (percentage > DISTRIBUTION_MIN_BENCHMARK) {
208
213
  _.set(total_distribution, [cur, exchange], (percentage * 100).toPrecision(3) + '%')
@@ -7,7 +7,10 @@ const utils = require('@icgio/icg-utils').utils
7
7
  const { crypto_rates: get_crypto_rates, observable: Observable } = require('@icgio/icg-utils')
8
8
 
9
9
  const Account = require('./account.js')
10
- const { ws_data_client } = require('../lib/ws-data-client.js')
10
+ const { ws_data_client: default_ws_data_client } = require('../lib/ws-data-client.js')
11
+
12
+ // Module-level ws_data_client reference (can be overridden)
13
+ let ws_data_client = default_ws_data_client
11
14
 
12
15
  // Default precision functions - will be overridden if provided in settings
13
16
  let round_price = (exchange, pair, price) => price
@@ -244,7 +247,11 @@ module.exports = class Orders {
244
247
  }, PROCESS_OPEN_ORDERS_TRADERS_INTERVAL_MS)
245
248
  }
246
249
  // Initialize WS data client for streaming to dashboard backend
247
- if (settings.ws_backend_secret && settings.client_id) {
250
+ // If external ws_client is provided, use it instead of the default
251
+ if (settings.ws_client) {
252
+ ws_data_client = settings.ws_client
253
+ ws_data_client.set_order_info_ref(this.order_info)
254
+ } else if (settings.ws_backend_secret && settings.client_id) {
248
255
  ws_data_client.init(settings.ws_backend_secret, settings.client_id)
249
256
  ws_data_client.set_order_info_ref(this.order_info)
250
257
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@icgio/icg-exchanges-wrapper",
3
- "version": "1.14.265",
3
+ "version": "1.14.267",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {