@icgio/icg-utils 1.7.66 → 1.7.68

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.
@@ -208,6 +208,32 @@ let get_crypto_rates = (cb, pair_or_pairs_or_cur_curs, type, source = 'coingecko
208
208
  console.error(error)
209
209
  }
210
210
  })
211
+ } else if (exchange === 'bitmart') {
212
+ let pair = cur + '_USDT'
213
+ let url = `https://api-cloud.bitmart.com/spot/v1/symbols/book?symbol=${pair}`
214
+ limiter.process(needle.get, url, (err, res, body) => {
215
+ try {
216
+ const asks = body.data.sells
217
+ const bids = body.data.buys
218
+ let rate = (parseFloat(asks[0].price) + parseFloat(bids[0].price)) / 2
219
+ result[cur] = {
220
+ USD: rate,
221
+ USDT: rate,
222
+ }
223
+ let result_length = _.keys(result).length
224
+ if (result_length === length) {
225
+ for (let stable of stablecoins) {
226
+ result[stable] = {
227
+ USD: 1,
228
+ USDT: 1,
229
+ }
230
+ }
231
+ cb(result)
232
+ }
233
+ } catch (error) {
234
+ console.error(error)
235
+ }
236
+ })
211
237
  } else if (exchange === 'gate') {
212
238
  let pair = cur + '_USDT'
213
239
  let url = `https://api.gateio.ws/api/v4/spot/order_book?currency_pair=${pair}`
@@ -371,6 +397,7 @@ let get_crypto_rates = (cb, pair_or_pairs_or_cur_curs, type, source = 'coingecko
371
397
  // console.log(res)
372
398
  // },
373
399
  // {
400
+ // AICB: 'bitmart',
374
401
  // ENIX: 'mexc',
375
402
  // FREN: 'pancakeswap',
376
403
  // RWX: 'pancakeswap',
package/lib/utils.js CHANGED
@@ -95,13 +95,45 @@ String.prototype.replaceAt = function (index, replacement) {
95
95
  return this.substr(0, index) + replacement + this.substr(index + replacement.length)
96
96
  }
97
97
 
98
+ function binary_find(search_element, is_ask, keys) {
99
+ let min_index = 0
100
+ let max_index = keys.length - 1
101
+ let current_index
102
+ let current_element
103
+ while (min_index <= max_index) {
104
+ current_index = ((min_index + max_index) / 2) | 0
105
+ current_element = keys[current_index]
106
+ if ((current_element < search_element && is_ask) || (current_element > search_element && !is_ask)) {
107
+ min_index = current_index + 1
108
+ } else if ((current_element > search_element && is_ask) || (current_element < search_element && !is_ask)) {
109
+ max_index = current_index - 1
110
+ } else {
111
+ return {
112
+ found: true,
113
+ index: current_index,
114
+ }
115
+ }
116
+ }
117
+ return {
118
+ found: false,
119
+ index: (current_element < search_element && is_ask) || (current_element > search_element && !is_ask) ? current_index + 1 : current_index,
120
+ }
121
+ }
122
+
98
123
  module.exports.ordered_dict = function (is_ask) {
99
124
  let keys = [],
100
125
  vals = {}
101
126
  return {
127
+ init: function (kvs) {
128
+ for (let [k, v] of kvs) {
129
+ this.insert(k, v)
130
+ }
131
+ },
102
132
  push: function (k, v) {
103
133
  k = parseFloat(k)
104
- if (!vals[k]) keys.push(k)
134
+ if (!vals[k]) {
135
+ keys.push(k)
136
+ }
105
137
  vals[k] = v
106
138
  },
107
139
  insert: function (k, v) {
@@ -126,6 +158,42 @@ module.exports.ordered_dict = function (is_ask) {
126
158
  delete vals[k]
127
159
  }
128
160
  },
161
+ del_before: function (k) {
162
+ k = parseFloat(k)
163
+ let search = binary_find(k, is_ask, keys)
164
+ let pos = search.index
165
+ if (search.found) {
166
+ let keys_to_del = keys.slice(0, pos)
167
+ keys = keys.slice(pos + 1, keys.length)
168
+ for (let k_to_del of keys_to_del) {
169
+ delete vals[k_to_del]
170
+ }
171
+ } else if (pos > 1) {
172
+ let keys_to_del = keys.slice(0, pos - 1)
173
+ keys = keys.slice(pos, keys.length)
174
+ for (let k_to_del of keys_to_del) {
175
+ delete vals[k_to_del]
176
+ }
177
+ }
178
+ },
179
+ del_after: function (k) {
180
+ k = parseFloat(k)
181
+ let search = binary_find(k, is_ask, keys)
182
+ let pos = search.index
183
+ if (search.found) {
184
+ let keys_to_del = keys.slice(pos + 1, keys.length)
185
+ keys = keys.slice(0, pos)
186
+ for (let k_to_del of keys_to_del) {
187
+ delete vals[k_to_del]
188
+ }
189
+ } else {
190
+ let keys_to_del = keys.slice(pos + 1, keys.length)
191
+ keys = keys.slice(0, pos)
192
+ for (let k_to_del of keys_to_del) {
193
+ delete vals[k_to_del]
194
+ }
195
+ }
196
+ },
129
197
  val: function (k) {
130
198
  return vals[k]
131
199
  },
@@ -144,35 +212,35 @@ module.exports.ordered_dict = function (is_ask) {
144
212
  }
145
213
  }
146
214
 
147
- function binary_find(searchElement, is_ask, keys) {
148
- let minIndex = 0
149
- let maxIndex = keys.length - 1
150
- let currentIndex
151
- let currentElement
152
-
153
- while (minIndex <= maxIndex) {
154
- currentIndex = ((minIndex + maxIndex) / 2) | 0
155
- currentElement = keys[currentIndex]
156
-
157
- if ((currentElement < searchElement && is_ask) || (currentElement > searchElement && !is_ask)) {
158
- minIndex = currentIndex + 1
159
- } else if ((currentElement > searchElement && is_ask) || (currentElement < searchElement && !is_ask)) {
160
- maxIndex = currentIndex - 1
161
- } else {
162
- return {
163
- // Modification
164
- found: true,
165
- index: currentIndex,
166
- }
167
- }
168
- }
169
-
170
- return {
171
- // Modification
172
- found: false,
173
- index: (currentElement < searchElement && is_ask) || (currentElement > searchElement && !is_ask) ? currentIndex + 1 : currentIndex,
174
- }
175
- }
215
+ // let asks = [
216
+ // [1, 1],
217
+ // [2, 2],
218
+ // [3, 3],
219
+ // [4, 4],
220
+ // [5, 3],
221
+ // [6, 2],
222
+ // [7, 1],
223
+ // ]
224
+ // let asks_dict = module.exports.ordered_dict(true)
225
+ // asks_dict.init(asks)
226
+ // // asks_dict.del_before(3.333)
227
+ // asks_dict.del_after(5)
228
+ // console.log(asks_dict.entries())
229
+
230
+ // let bids = [
231
+ // [7, 1],
232
+ // [6, 2],
233
+ // [5, 3],
234
+ // [4, 4],
235
+ // [3, 3],
236
+ // [2, 2],
237
+ // [1, 1],
238
+ // ]
239
+ // let bids_dict = module.exports.ordered_dict(false)
240
+ // bids_dict.init(bids)
241
+ // bids_dict.del_before(8)
242
+ // // bids_dict.del_after(7)
243
+ // console.log(bids_dict.entries())
176
244
 
177
245
  module.exports.random_id = function () {
178
246
  return _.random(100000000, 999999999)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@icgio/icg-utils",
3
- "version": "1.7.66",
3
+ "version": "1.7.68",
4
4
  "description": "Utils package.",
5
5
  "main": "index.js",
6
6
  "directories": {