@icgio/icg-utils 1.9.68 → 1.9.70
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.
- package/lib/rates.js +7 -12
- package/lib/utils.js +19 -0
- package/package.json +2 -2
- package/test/hrtime-epoch.test.js +32 -0
package/lib/rates.js
CHANGED
|
@@ -123,12 +123,12 @@ function _fetch_exchange_rates(cb, curs_dict) {
|
|
|
123
123
|
let exchange = curs_dict[cur]
|
|
124
124
|
let pair
|
|
125
125
|
|
|
126
|
-
if (exchange === '
|
|
127
|
-
pair = cur + '/USDT'
|
|
128
|
-
fetchMidPrice(cur, exchange, `https://ascendex.com/api/pro/v1/depth?symbol=${pair}`, (body) => midOf(body.data.data.asks, body.data.data.bids))
|
|
129
|
-
} else if (exchange === 'binance') {
|
|
126
|
+
if (exchange === 'binance') {
|
|
130
127
|
pair = cur + 'USDT'
|
|
131
128
|
fetchMidPrice(cur, exchange, `https://www.binance.com/api/v3/depth?symbol=${pair}&limit=20`, (body) => midOf(body.asks, body.bids))
|
|
129
|
+
} else if (exchange === 'binancefutures') {
|
|
130
|
+
pair = cur + 'USDT'
|
|
131
|
+
fetchMidPrice(cur, exchange, `https://fapi.binance.com/fapi/v1/depth?symbol=${pair}&limit=20`, (body) => midOf(body.asks, body.bids))
|
|
132
132
|
} else if (exchange === 'biconomy') {
|
|
133
133
|
pair = cur + '_USDT'
|
|
134
134
|
fetchMidPrice(cur, exchange, `https://api.biconomy.com/api/v1/depth?symbol=${pair}&size=5`, (body) => midOf(body.asks, body.bids))
|
|
@@ -138,10 +138,6 @@ function _fetch_exchange_rates(cb, curs_dict) {
|
|
|
138
138
|
} else if (exchange === 'bitget') {
|
|
139
139
|
pair = cur + 'USDT'
|
|
140
140
|
fetchMidPrice(cur, exchange, `https://api.bitget.com/api/v2/spot/market/orderbook?symbol=${pair}&type=step0`, (body) => midOf(body.data.asks, body.data.bids))
|
|
141
|
-
} else if (exchange === 'bitmart') {
|
|
142
|
-
pair = cur + '_USDT'
|
|
143
|
-
if (pair === 'ELON_USDT') pair = '$ELONSOL_USDT'
|
|
144
|
-
fetchMidPrice(cur, exchange, `https://api-cloud.bitmart.com/spot/v1/symbols/book?symbol=${pair}`, (body) => (parseFloat(body.data.sells[0].price) + parseFloat(body.data.buys[0].price)) / 2)
|
|
145
141
|
} else if (exchange === 'bybit') {
|
|
146
142
|
pair = cur + 'USDT'
|
|
147
143
|
fetchMidPrice(cur, exchange, `https://api.bybit.com/v5/market/orderbook?category=spot&symbol=${pair}&limit=20`, (body) => midOf(body.result.a, body.result.b))
|
|
@@ -154,13 +150,12 @@ function _fetch_exchange_rates(cb, curs_dict) {
|
|
|
154
150
|
fetchMidPrice(cur, exchange, `https://api.coinstore.com/api/v1/market/depth/${pair}`, (body) => midOf(body.data.a, body.data.b))
|
|
155
151
|
} else if (exchange === 'cryptocom') {
|
|
156
152
|
pair = cur + '_USDT'
|
|
157
|
-
fetchMidPrice(cur, exchange, `https://api.crypto.com/v2/public/get-book?instrument_name=${pair}&depth=20`, (body) =>
|
|
153
|
+
fetchMidPrice(cur, exchange, `https://api.crypto.com/v2/public/get-book?instrument_name=${pair}&depth=20`, (body) =>
|
|
154
|
+
midOf(body.result.data[0].asks, body.result.data[0].bids),
|
|
155
|
+
)
|
|
158
156
|
} else if (exchange === 'digifinex') {
|
|
159
157
|
pair = cur + '_USDT'
|
|
160
158
|
fetchMidPrice(cur, exchange, `https://openapi.digifinex.com/v3/spot/orderbook?symbol=${pair}`, (body) => midOf(body.data.asks, body.data.bids))
|
|
161
|
-
} else if (exchange === 'fastex') {
|
|
162
|
-
pair = cur.toUpperCase() + '-USDT'
|
|
163
|
-
fetchMidPrice(cur, exchange, `https://exchange.fastex.com/api/v1/orderbook/info?symbol=${pair}`, (body) => (parseFloat(body.response.entities.asks[0].price) + parseFloat(body.response.entities.bids[0].price)) / 2)
|
|
164
159
|
} else if (exchange === 'gate') {
|
|
165
160
|
pair = cur + '_USDT'
|
|
166
161
|
fetchMidPrice(cur, exchange, `https://api.gateio.ws/api/v4/spot/order_book?currency_pair=${pair}`, (body) => midOf(body.asks, body.bids))
|
package/lib/utils.js
CHANGED
|
@@ -103,6 +103,24 @@ function get_hhmmss() {
|
|
|
103
103
|
return new Date().toTimeString().split(' ')[0]
|
|
104
104
|
}
|
|
105
105
|
|
|
106
|
+
// hrtime as wall-clock ms with the fraction Date.now() drops; anchored once on a Date.now() tick, which lands within a microsecond of a whole millisecond
|
|
107
|
+
let hrtime_epoch_offset_ms = null
|
|
108
|
+
function hrtime_epoch_ms(hr) {
|
|
109
|
+
if (!Array.isArray(hr)) {
|
|
110
|
+
return null
|
|
111
|
+
}
|
|
112
|
+
if (hrtime_epoch_offset_ms === null) {
|
|
113
|
+
const start = Date.now()
|
|
114
|
+
let tick = start
|
|
115
|
+
while (tick === start) {
|
|
116
|
+
tick = Date.now()
|
|
117
|
+
}
|
|
118
|
+
const now = process.hrtime()
|
|
119
|
+
hrtime_epoch_offset_ms = tick - (now[0] * 1e3 + now[1] / 1e6)
|
|
120
|
+
}
|
|
121
|
+
return hr[0] * 1e3 + hr[1] / 1e6 + hrtime_epoch_offset_ms
|
|
122
|
+
}
|
|
123
|
+
|
|
106
124
|
function round(number, precision) {
|
|
107
125
|
if (typeof number === 'string') number = parseFloat(number)
|
|
108
126
|
return Math.round(number * Math.pow(10, precision)) / Math.pow(10, precision)
|
|
@@ -900,6 +918,7 @@ module.exports = {
|
|
|
900
918
|
ordered_dict,
|
|
901
919
|
random_id,
|
|
902
920
|
get_hhmmss,
|
|
921
|
+
hrtime_epoch_ms,
|
|
903
922
|
round,
|
|
904
923
|
cut_orderbook,
|
|
905
924
|
level_position,
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@icgio/icg-utils",
|
|
3
|
-
"version": "1.9.
|
|
3
|
+
"version": "1.9.70",
|
|
4
4
|
"description": "Utils package.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"directories": {
|
|
7
7
|
"lib": "lib"
|
|
8
8
|
},
|
|
9
9
|
"scripts": {
|
|
10
|
-
"test": "node test/rate-limiter.test.js && node test/combine-orderbooks.test.js && node test/ordered-dict.test.js && node test/sorted-book.test.js && node test/merge-trades.test.js"
|
|
10
|
+
"test": "node test/rate-limiter.test.js && node test/combine-orderbooks.test.js && node test/ordered-dict.test.js && node test/sorted-book.test.js && node test/merge-trades.test.js && node test/hrtime-epoch.test.js"
|
|
11
11
|
},
|
|
12
12
|
"repository": {
|
|
13
13
|
"type": "git",
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
const { hrtime_epoch_ms } = require('../lib/utils.js')
|
|
3
|
+
|
|
4
|
+
let pass = 0,
|
|
5
|
+
fail = 0
|
|
6
|
+
function assert(condition, msg) {
|
|
7
|
+
if (condition) {
|
|
8
|
+
pass++
|
|
9
|
+
} else {
|
|
10
|
+
fail++
|
|
11
|
+
console.log(' FAIL:', msg)
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
assert(hrtime_epoch_ms(null) === null, 'null for a missing hrtime')
|
|
16
|
+
assert(hrtime_epoch_ms(undefined) === null, 'null for undefined')
|
|
17
|
+
|
|
18
|
+
const epoch = hrtime_epoch_ms(process.hrtime())
|
|
19
|
+
const wall = Date.now()
|
|
20
|
+
assert(Math.abs(epoch - wall) < 2, `within 2ms of Date.now() (got ${(epoch - wall).toFixed(3)})`)
|
|
21
|
+
|
|
22
|
+
const hr1 = process.hrtime()
|
|
23
|
+
const spin_until = Date.now() + 3
|
|
24
|
+
while (Date.now() < spin_until) {}
|
|
25
|
+
const hr2 = process.hrtime()
|
|
26
|
+
const elapsed_hr = (hr2[0] - hr1[0]) * 1e3 + (hr2[1] - hr1[1]) / 1e6
|
|
27
|
+
const elapsed_epoch = hrtime_epoch_ms(hr2) - hrtime_epoch_ms(hr1)
|
|
28
|
+
assert(Math.abs(elapsed_epoch - elapsed_hr) < 1e-6, 'differences equal hrtime differences')
|
|
29
|
+
assert(elapsed_epoch >= 3 && elapsed_epoch < 10, `elapsed ${elapsed_epoch.toFixed(3)}ms after a 3ms spin`)
|
|
30
|
+
|
|
31
|
+
console.log(`hrtime-epoch tests: ${pass} passed, ${fail} failed`)
|
|
32
|
+
process.exit(fail ? 1 : 0)
|