@icgio/icg-exchanges-wrapper 1.9.101 → 1.9.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.
Files changed (2) hide show
  1. package/middleware/orders.js +128 -74
  2. package/package.json +1 -1
@@ -41,12 +41,19 @@ const MARKET_HISTORY_DEFAULT_TIME = 10 * 60 * 1000
41
41
  const TRADES_DEFAULT_TIME = 12 * 60 * 60 * 1000
42
42
 
43
43
  module.exports = class Orders {
44
- constructor(exchange_basecur_curs, cd, settings) {
44
+ constructor(exchange_basecur_curs_dict, cd, settings) {
45
+ let { exchange_basecur_curs, exchange_basecur_curs_rates_only } = exchange_basecur_curs_dict
45
46
  this.exchange_basecur_curs = exchange_basecur_curs
46
- let { exchange_pairs, exchange_curs, pair_exchanges } = utils.exchange_basecur_curs_converter(exchange_basecur_curs)
47
+ let { exchange_pairs, pair_exchanges } = utils.exchange_basecur_curs_converter(exchange_basecur_curs)
48
+ this.exchange_basecur_curs_rates_only = exchange_basecur_curs_rates_only
49
+ let { exchange_pairs: exchange_pairs_rates_only, pair_exchanges: pair_exchanges_rates_only } = utils.exchange_basecur_curs_converter(exchange_basecur_curs_rates_only)
47
50
  this.exchange_pairs = exchange_pairs
48
51
  this.pair_exchanges = pair_exchanges
52
+ this.exchange_pairs_rates_only = exchange_pairs_rates_only
53
+ this.pair_exchanges_rates_only = pair_exchanges_rates_only
54
+ this.rates_only_exchanges = pair_exchanges_rates_only
49
55
  this.Exchanges = {}
56
+ this.ExchangesRatesOnly = {}
50
57
  this.settings = settings || {}
51
58
  this.ws_enabled = settings.ws_enabled || false
52
59
  this.interval_dict = settings.interval_dict || {}
@@ -103,6 +110,9 @@ module.exports = class Orders {
103
110
  this.interval_dict[exchange].market_history = this.interval_dict[exchange].market_history || 2 * 1000
104
111
  }
105
112
  }
113
+ for (let exchange in this.exchange_basecur_curs_rates_only) {
114
+ this.ExchangesRatesOnly[exchange] = new icg_exchanges[_.upperFirst(exchange)]('', '', STANDARD_LIMITS)
115
+ }
106
116
  ;(this.balances = {}), (this.open_orders = {}), (this.trades = {}), (this.rates = {}), (this.market_history = {}), (this.cmc_rates = {})
107
117
  ;(this.rates_combined_all = {}),
108
118
  (this.rates_combined_all_with_buy_sell_limits_against_reserve_balance = {}),
@@ -213,7 +223,7 @@ module.exports = class Orders {
213
223
  limit_trade((res) => {
214
224
  if (res.success) {
215
225
  let order_id = res.body
216
- this.delete_order_info(exchange, pair, method, id, 'limit-trade-success')
226
+ this.delete_order_info(exchange, pair, method, id, order_type + ' n-filled')
217
227
  this.update_order_info(
218
228
  exchange,
219
229
  pair,
@@ -236,10 +246,10 @@ module.exports = class Orders {
236
246
  order_type + ' n-filled'
237
247
  )
238
248
  } else if (res.error === 'amount-too-small' || res.error === 'insufficient-funds') {
239
- this.delete_order_info(exchange, pair, method, id, 'limit-trade-' + res.error)
249
+ this.delete_order_info(exchange, pair, method, id, order_type + ' limit-trade-' + res.error)
240
250
  } else {
241
251
  if (order_type.startsWith('self') || order_type.startsWith('maker_2')) {
242
- this.delete_order_info(exchange, pair, method, id, 'limit-trade-timeout-' + order_type)
252
+ this.delete_order_info(exchange, pair, method, id, order_type + ' limit-trade-timeout')
243
253
  }
244
254
  // else if (_.get(this.order_info, [exchange, pair, method, id, 'status']) === 'INIT') { // make sure no_callback_timeout is NOT running zero
245
255
  // this.update_order_info(exchange, pair, method, id, {
@@ -287,7 +297,7 @@ module.exports = class Orders {
287
297
  order.order_type + ' canceled'
288
298
  )
289
299
  setTimeout(() => {
290
- this.delete_order_info(exchange, pair, order.type, order.order_id, 'cancel-order-success')
300
+ this.delete_order_info(exchange, pair, order.type, order.order_id, order.order_type + ' cancel-order-success')
291
301
  }, this.delete_finished_orders_timeout)
292
302
  } else if (!res.success && res.error === 'order-not-open' && original_status !== 'CANCELED') {
293
303
  // this.info_log('%s ORDER CANNOT BE CANCELED: %j', original_status, order)
@@ -306,7 +316,7 @@ module.exports = class Orders {
306
316
  order.order_type + ' cannot-be-canceled'
307
317
  )
308
318
  setTimeout(() => {
309
- this.delete_order_info(exchange, pair, order.type, order.order_id, 'cannot-be-canceled')
319
+ this.delete_order_info(exchange, pair, order.type, order.order_id, order.order_type + 'cannot-be-canceled')
310
320
  }, this.delete_finished_orders_timeout)
311
321
  } else {
312
322
  console.error('cancel order:', exchange, pair, order, res)
@@ -386,7 +396,7 @@ module.exports = class Orders {
386
396
  order_info.order_type + ' canceled-order-filled'
387
397
  )
388
398
  setTimeout(() => {
389
- this.delete_order_info(exchange, pair, type, order_id, 'canceled-order-filled')
399
+ this.delete_order_info(exchange, pair, type, order_id, order.order_type + ' canceled-order-filled')
390
400
  }, this.delete_finished_orders_timeout)
391
401
  } else if (order_info.status === 'F-FILLED' && trade.amount > order_info.f_amount) {
392
402
  // this.info_log('TRADES: FILLED ORDER FILLED MORE', trade, order_info)
@@ -424,7 +434,7 @@ module.exports = class Orders {
424
434
  order_info.order_type + ' order-f-filled-1'
425
435
  )
426
436
  setTimeout(() => {
427
- this.delete_order_info(exchange, pair, order_info.type, order_info.order_id, 'f-filled-trades')
437
+ this.delete_order_info(exchange, pair, order_info.type, order_info.order_id, order.order_type + ' order-f-filled-1')
428
438
  }, this.delete_finished_orders_timeout)
429
439
  } else if (
430
440
  _.includes(['N-FILLED', 'P-FILLED'], order_info.status) &&
@@ -485,7 +495,7 @@ module.exports = class Orders {
485
495
  order_info_order.order_type + ' order-f-filled-2'
486
496
  )
487
497
  setTimeout(() => {
488
- this.delete_order_info(exchange, pair, order_info_order.type, order_info_order.order_id, 'f-filled-no-trades-function')
498
+ this.delete_order_info(exchange, pair, order_info_order.type, order_info_order.order_id, order_info_order.order_type + ' order-f-filled-2')
489
499
  }, this.delete_finished_orders_timeout)
490
500
  }
491
501
  }
@@ -515,7 +525,7 @@ module.exports = class Orders {
515
525
  order_info.order_type + ' order-f-filled-3'
516
526
  )
517
527
  setTimeout(() => {
518
- this.delete_order_info(exchange, pair, order_info.type, order_info.order_id, 'f-filled-open-orders')
528
+ this.delete_order_info(exchange, pair, order_info.type, order_info.order_id, order_info.order_type + ' order-f-filled-3')
519
529
  }, this.delete_finished_orders_timeout)
520
530
  } else if (
521
531
  _.includes(['N-FILLED', 'P-FILLED'], order_info.status) &&
@@ -550,7 +560,7 @@ module.exports = class Orders {
550
560
  order_time - init_order_time < this.check_init_order_opened_timeout
551
561
  ) {
552
562
  // this.info_log('ORDERS: %s %s %s %s INIT ORDER OPENED', init_order.order_id, order.order_id, exchange, pair)
553
- this.delete_order_info(exchange, pair, init_order.type, init_order.order_id, 'init-order-opened')
563
+ this.delete_order_info(exchange, pair, init_order.type, init_order.order_id, init_order.order_type + ' init-order-opened')
554
564
  this.update_order_info(
555
565
  exchange,
556
566
  pair,
@@ -611,14 +621,18 @@ module.exports = class Orders {
611
621
  let pair = cur + base_cur
612
622
  let max_cur_amount = cur_balance - _.get(this.min_reserve_balance, [exchange, cur], 0)
613
623
  let max_base_cur_amount = base_cur_balance - _.get(this.min_reserve_balance, [exchange, base_cur], 0)
614
- if (max_cur_amount > 0 || cur_balance === 0) {
624
+ if (max_cur_amount > 0) {
615
625
  } else {
616
- console.error('balance low', exchange, cur, cur_balance, this.min_reserve_balance[exchange][cur])
626
+ if (cur_balance > 0) {
627
+ console.error('balance low', exchange, cur, cur_balance, this.min_reserve_balance[exchange][cur])
628
+ }
617
629
  max_cur_amount = 0
618
630
  }
619
- if (max_base_cur_amount > 0 || base_cur_balance === 0) {
631
+ if (max_base_cur_amount > 0) {
620
632
  } else {
621
- console.error('balance low', exchange, cur, base_cur_balance, this.min_reserve_balance[exchange][base_cur])
633
+ if (base_cur_balance > 0) {
634
+ console.error('balance low', exchange, cur, base_cur_balance, this.min_reserve_balance[exchange][base_cur])
635
+ }
622
636
  max_base_cur_amount = 0
623
637
  }
624
638
  _.set(this.buy_sell_limits_against_reserve_balance, [exchange, pair], {
@@ -971,44 +985,49 @@ module.exports = class Orders {
971
985
  get_trades_all(this.Exchanges, this.exchange_pairs, this.interval_dict)
972
986
  }
973
987
  check_rates() {
974
- let rates_handler = (exchange, pair, res) => {
975
- if (res.success) {
976
- res.body.asks = res.body.asks.slice(0, RATES_LENGTH_MAX)
977
- res.body.bids = res.body.bids.slice(0, RATES_LENGTH_MAX)
978
- _.set(this.rates, [exchange, pair], Object.assign(res.body, { update_time: new Date() }))
979
- for (let pair in this.pair_exchanges) {
980
- let orderbook_all = {},
981
- orderbook_all_with_buy_sell_limits_against_reserve_balance = {},
982
- orderbook_major = {},
983
- orderbook_major_temp = {},
984
- orderbook_good_with_buy_sell_limits_against_reserve_balance = {},
985
- orderbook_good_temp = {}
986
- for (let exchange of this.pair_exchanges[pair]) {
987
- if (_.get(this.rates, [exchange, pair, 'asks'], []).length > 0 && _.get(this.rates, [exchange, pair, 'bids'], []).length > 0) {
988
- orderbook_all[exchange] = this.rates[exchange][pair]
989
- } else {
990
- orderbook_all = _.omit(orderbook_all, [exchange])
988
+ let process_rates = () => {
989
+ for (let pair in this.pair_exchanges) {
990
+ let orderbook_all = {},
991
+ orderbook_all_with_buy_sell_limits_against_reserve_balance = {},
992
+ orderbook_major = {},
993
+ orderbook_major_temp = {},
994
+ orderbook_good_with_buy_sell_limits_against_reserve_balance = {},
995
+ orderbook_good_temp = {}
996
+ let exchanges_list = _.concat(this.pair_exchanges[pair], this.pair_exchanges_rates_only[pair])
997
+ for (let exchange of exchanges_list) {
998
+ if (_.get(this.rates, [exchange, pair, 'asks'], []).length > 0 && _.get(this.rates, [exchange, pair, 'bids'], []).length > 0) {
999
+ orderbook_all[exchange] = this.rates[exchange][pair]
1000
+ } else {
1001
+ orderbook_all = _.omit(orderbook_all, [exchange])
1002
+ }
1003
+ if (_.get(this.rates, [exchange, pair, 'asks'], []).length > 0 && _.get(this.rates, [exchange, pair, 'bids'], []).length > 0) {
1004
+ let rates = JSON.parse(JSON.stringify(this.rates[exchange][pair]))
1005
+
1006
+ let major_rates_only_exchanges_length = this.major_exchanges[pair].length + this.rates_only_exchanges[pair].length
1007
+ if (this.major_exchanges[pair] && _.includes(this.major_exchanges[pair], exchange)) {
1008
+ orderbook_major_temp[exchange] = rates
1009
+ if (_.keys(orderbook_major_temp).length === major_rates_only_exchanges_length) {
1010
+ orderbook_major = orderbook_major_temp
1011
+ }
1012
+ } else if (this.rates_only_exchanges[pair] && _.includes(this.rates_only_exchanges[pair], exchange)) {
1013
+ orderbook_major_temp[exchange] = rates
1014
+ if (_.keys(orderbook_major_temp).length === major_rates_only_exchanges_length) {
1015
+ orderbook_major = orderbook_major_temp
1016
+ }
991
1017
  }
992
1018
 
993
- if (
994
- _.get(this.rates, [exchange, pair, 'asks'], []).length > 0 &&
995
- _.get(this.rates, [exchange, pair, 'bids'], []).length > 0 &&
1019
+ if (_.includes(this.pair_exchanges_rates_only[pair], exchange)) {
1020
+ } else if (
996
1021
  _.get(this.buy_sell_limits_against_reserve_balance, [exchange, pair, 'buy']) &&
997
1022
  _.get(this.buy_sell_limits_against_reserve_balance, [exchange, pair, 'sell'])
998
1023
  ) {
999
- let rates = JSON.parse(JSON.stringify(this.rates[exchange][pair]))
1000
1024
  let rates_with_buy_sell_limits_against_reserve_balance = {
1001
1025
  asks: utils.sum_position_orderbook(rates.asks, this.buy_sell_limits_against_reserve_balance[exchange][pair].buy),
1002
1026
  bids: utils.vol_position_orderbook(rates.bids, this.buy_sell_limits_against_reserve_balance[exchange][pair].sell),
1003
1027
  }
1004
1028
  orderbook_all_with_buy_sell_limits_against_reserve_balance[exchange] = rates_with_buy_sell_limits_against_reserve_balance
1005
- if (this.major_exchanges[pair] && _.includes(this.major_exchanges[pair], exchange)) {
1006
- orderbook_major_temp[exchange] = rates
1007
- if (_.keys(orderbook_major_temp).length === this.major_exchanges[pair].length) {
1008
- orderbook_major = orderbook_major_temp
1009
- }
1010
- }
1011
- if (!_.includes(this.no_arb_exchanges, exchange)) {
1029
+
1030
+ if (!_.includes(this.no_arb_exchanges, exchange) || !this.exchange_pairs_rates_only[exchange]) {
1012
1031
  orderbook_good_temp[exchange] = rates_with_buy_sell_limits_against_reserve_balance
1013
1032
  if (_.keys(orderbook_good_temp).length === this.pair_exchanges[pair].length - this.no_arb_exchanges.length) {
1014
1033
  orderbook_good_with_buy_sell_limits_against_reserve_balance = orderbook_good_temp
@@ -1021,46 +1040,57 @@ module.exports = class Orders {
1021
1040
  orderbook_good_with_buy_sell_limits_against_reserve_balance = _.omit(orderbook_good_with_buy_sell_limits_against_reserve_balance, [exchange])
1022
1041
  orderbook_good_temp = _.omit(orderbook_good_temp, [exchange])
1023
1042
  }
1043
+ } else {
1044
+ orderbook_major = _.omit(orderbook_major, [exchange])
1045
+ orderbook_major_temp = _.omit(orderbook_major_temp, [exchange])
1024
1046
  }
1025
- if (_.keys(orderbook_all).length > 0) {
1026
- let o = utils.combine_orderbooks(orderbook_all, 100)
1027
- if (o.asks.length > 0 && o.bids.length > 0) {
1028
- _.set(this.rates_combined_all, [pair], o)
1029
- } else {
1030
- this.rates_combined_all = _.omit(this.rates_combined_all, [pair])
1031
- }
1047
+ }
1048
+ if (_.keys(orderbook_all).length > 0) {
1049
+ let o = utils.combine_orderbooks(orderbook_all, 100)
1050
+ if (o.asks.length > 0 && o.bids.length > 0) {
1051
+ _.set(this.rates_combined_all, [pair], o)
1052
+ } else {
1053
+ this.rates_combined_all = _.omit(this.rates_combined_all, [pair])
1032
1054
  }
1033
- if (_.keys(orderbook_all_with_buy_sell_limits_against_reserve_balance).length > 0) {
1034
- let o = utils.combine_orderbooks(orderbook_all_with_buy_sell_limits_against_reserve_balance, 100)
1035
- if (o.asks.length > 0 && o.bids.length > 0) {
1036
- _.set(this.rates_combined_all_with_buy_sell_limits_against_reserve_balance, [pair], o)
1037
- } else {
1038
- this.rates_combined_all_with_buy_sell_limits_against_reserve_balance = _.omit(this.rates_combined_all_with_buy_sell_limits_against_reserve_balance, [pair])
1039
- }
1055
+ }
1056
+ if (_.keys(orderbook_all_with_buy_sell_limits_against_reserve_balance).length > 0) {
1057
+ let o = utils.combine_orderbooks(orderbook_all_with_buy_sell_limits_against_reserve_balance, 100)
1058
+ if (o.asks.length > 0 && o.bids.length > 0) {
1059
+ _.set(this.rates_combined_all_with_buy_sell_limits_against_reserve_balance, [pair], o)
1060
+ } else {
1061
+ this.rates_combined_all_with_buy_sell_limits_against_reserve_balance = _.omit(this.rates_combined_all_with_buy_sell_limits_against_reserve_balance, [pair])
1040
1062
  }
1041
- if (_.keys(orderbook_major).length > 0) {
1042
- let o = utils.combine_orderbooks(orderbook_major, 100)
1043
- if (o.asks.length > 0 && o.bids.length > 0) {
1044
- _.set(this.rates_combined_major, [pair], o)
1045
- } else {
1046
- this.rates_combined_major = _.omit(this.rates_combined_major, [pair])
1047
- }
1063
+ }
1064
+ if (_.keys(orderbook_major).length > 0) {
1065
+ let o = utils.combine_orderbooks(orderbook_major, 100)
1066
+ if (o.asks.length > 0 && o.bids.length > 0) {
1067
+ _.set(this.rates_combined_major, [pair], o)
1068
+ } else {
1069
+ this.rates_combined_major = _.omit(this.rates_combined_major, [pair])
1048
1070
  }
1049
- if (_.keys(orderbook_good_with_buy_sell_limits_against_reserve_balance).length > 0) {
1050
- let o = utils.combine_orderbooks(orderbook_good_with_buy_sell_limits_against_reserve_balance, 100)
1051
- if (o.asks.length > 0 && o.bids.length > 0) {
1052
- _.set(this.rates_combined_good_with_buy_sell_limits_against_reserve_balance, [pair], o)
1053
- } else {
1054
- this.rates_combined_good_with_buy_sell_limits_against_reserve_balance = _.omit(this.rates_combined_good_with_buy_sell_limits_against_reserve_balance, [pair])
1055
- }
1071
+ }
1072
+ if (_.keys(orderbook_good_with_buy_sell_limits_against_reserve_balance).length > 0) {
1073
+ let o = utils.combine_orderbooks(orderbook_good_with_buy_sell_limits_against_reserve_balance, 100)
1074
+ if (o.asks.length > 0 && o.bids.length > 0) {
1075
+ _.set(this.rates_combined_good_with_buy_sell_limits_against_reserve_balance, [pair], o)
1076
+ } else {
1077
+ this.rates_combined_good_with_buy_sell_limits_against_reserve_balance = _.omit(this.rates_combined_good_with_buy_sell_limits_against_reserve_balance, [pair])
1056
1078
  }
1057
1079
  }
1080
+ }
1081
+ }
1082
+ let rates_handler = (exchange, pair, res) => {
1083
+ if (res.success) {
1084
+ res.body.asks = res.body.asks.slice(0, RATES_LENGTH_MAX)
1085
+ res.body.bids = res.body.bids.slice(0, RATES_LENGTH_MAX)
1086
+ _.set(this.rates, [exchange, pair], Object.assign(res.body, { update_time: new Date() }))
1087
+ process_rates()
1058
1088
  } else if (_.get(this.rates, [exchange, pair, 'update_time']) < new Date().getTime() - this.interval_dict[exchange].rates) {
1059
1089
  this.info_log('%s %s rates expired: %j', exchange, pair, res)
1060
1090
  this.rates[exchange] = _.omit(this.rates[exchange], [pair])
1061
1091
  }
1062
1092
  }
1063
- let get_rates = (Exchanges, exchange_pairs, interval_dict) => {
1093
+ let get_rates = (Exchanges, ExchangesRatesOnly, exchange_pairs, exchange_pairs_rates_only, interval_dict) => {
1064
1094
  for (let exchange in Exchanges) {
1065
1095
  if (Exchanges[exchange].ws_market) {
1066
1096
  setInterval(() => {
@@ -1085,8 +1115,32 @@ module.exports = class Orders {
1085
1115
  }
1086
1116
  }
1087
1117
  }
1118
+ for (let exchange in ExchangesRatesOnly) {
1119
+ if (ExchangesRatesOnly[exchange].ws_market) {
1120
+ setInterval(() => {
1121
+ if (!this.ws_market_first_time[exchange]) {
1122
+ ExchangesRatesOnly[exchange].ws_market(exchange_pairs[exchange])
1123
+ this.ws_market_first_time[exchange] = true
1124
+ } else {
1125
+ for (let pair of exchange_pairs[exchange]) {
1126
+ ExchangesRatesOnly[exchange].rate_ws(pair, (res) => {
1127
+ rates_handler(exchange, pair, res)
1128
+ })
1129
+ }
1130
+ }
1131
+ }, interval_dict[exchange].rates)
1132
+ } else {
1133
+ for (let pair of exchange_pairs_rates_only[exchange]) {
1134
+ setInterval(() => {
1135
+ ExchangesRatesOnly[exchange].rate(pair, RATES_LENGTH_DEFAULT, (res) => {
1136
+ rates_handler(exchange, pair, res)
1137
+ })
1138
+ }, interval_dict[exchange].rates)
1139
+ }
1140
+ }
1141
+ }
1088
1142
  }
1089
- get_rates(this.Exchanges, this.exchange_pairs, this.interval_dict)
1143
+ get_rates(this.Exchanges, this.ExchangesRatesOnly, this.exchange_pairs, this.exchange_pairs_rates_only, this.interval_dict)
1090
1144
  }
1091
1145
  check_market_history() {
1092
1146
  let market_history_handler = (exchange, pair, res) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@icgio/icg-exchanges-wrapper",
3
- "version": "1.9.101",
3
+ "version": "1.9.103",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {