@icgio/icg-utils 1.9.50 → 1.9.52
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/index.js +5 -2
- package/lib/rate-limiter.js +43 -8
- package/lib/rates.js +247 -0
- package/package.json +2 -2
- package/patches/README.md +18 -0
- package/patches/icg-exchanges-wrapper-strategy-priority.patch +75 -0
- package/patches/icg-market-maker-dependency-bump.patch +14 -0
- package/test/rate-limiter.test.js +60 -0
- package/lib/crypto-rates.js +0 -586
- package/lib/dex-rate.js +0 -99
- package/lib/forex-rate.js +0 -20
package/index.js
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
|
+
const rates = require('./lib/rates.js')
|
|
2
|
+
|
|
1
3
|
module.exports = {
|
|
2
|
-
crypto_rates:
|
|
3
|
-
forex_rate:
|
|
4
|
+
crypto_rates: rates.get_crypto_rates,
|
|
5
|
+
forex_rate: { get_forex_rate_usd: rates.get_forex_rate_usd },
|
|
6
|
+
rates,
|
|
4
7
|
influx: require('./lib/influxdb.js'),
|
|
5
8
|
ip: require('./lib/ip.js'),
|
|
6
9
|
logger: require('./lib/logger.js'),
|
package/lib/rate-limiter.js
CHANGED
|
@@ -4,10 +4,18 @@ class RateLimiter {
|
|
|
4
4
|
this.interval_concurrent = settings.interval_concurrent ? settings.interval_concurrent : null
|
|
5
5
|
this.abandon_tx = settings.abandon_tx ? settings.abandon_tx : false
|
|
6
6
|
this.only_same_args = settings.only_same_args ? settings.only_same_args : false
|
|
7
|
+
this.priority = null
|
|
8
|
+
this.queue = []
|
|
9
|
+
this._resetTimer = null
|
|
7
10
|
this.count = 0
|
|
8
11
|
this.keyToState = new Map()
|
|
9
12
|
}
|
|
10
13
|
process(func, ...args) {
|
|
14
|
+
let priority = Infinity
|
|
15
|
+
if (!this.only_same_args) {
|
|
16
|
+
priority = this.priority != null ? this.priority : Infinity
|
|
17
|
+
this.priority = null
|
|
18
|
+
}
|
|
11
19
|
if (!this.max_concurrent || !this.interval_concurrent) {
|
|
12
20
|
func(...args)
|
|
13
21
|
return
|
|
@@ -36,21 +44,48 @@ class RateLimiter {
|
|
|
36
44
|
}, this.interval_concurrent)
|
|
37
45
|
}
|
|
38
46
|
} else {
|
|
39
|
-
if (this.count === 0) {
|
|
40
|
-
setTimeout(() => {
|
|
41
|
-
this.count = 0
|
|
42
|
-
}, this.interval_concurrent)
|
|
43
|
-
}
|
|
44
47
|
if (this.count < this.max_concurrent) {
|
|
45
48
|
this.count++
|
|
49
|
+
this._scheduleResetTimer()
|
|
46
50
|
func(...args)
|
|
47
51
|
} else if (!this.abandon_tx) {
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
52
|
+
this._enqueue({ func, args, priority })
|
|
53
|
+
if (!this._resetTimer) {
|
|
54
|
+
if (this.count === 0) {
|
|
55
|
+
this._drainQueue()
|
|
56
|
+
} else {
|
|
57
|
+
this._scheduleResetTimer()
|
|
58
|
+
}
|
|
59
|
+
}
|
|
51
60
|
}
|
|
52
61
|
}
|
|
53
62
|
}
|
|
63
|
+
_enqueue(item) {
|
|
64
|
+
const index = this.queue.findIndex((queuedItem) => queuedItem.priority > item.priority)
|
|
65
|
+
if (index === -1) {
|
|
66
|
+
this.queue.push(item)
|
|
67
|
+
} else {
|
|
68
|
+
this.queue.splice(index, 0, item)
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
_scheduleResetTimer() {
|
|
72
|
+
if (this._resetTimer || this.count === 0) {
|
|
73
|
+
return
|
|
74
|
+
}
|
|
75
|
+
this._resetTimer = setTimeout(() => {
|
|
76
|
+
this.count = 0
|
|
77
|
+
this._resetTimer = null
|
|
78
|
+
this._drainQueue()
|
|
79
|
+
}, this.interval_concurrent)
|
|
80
|
+
}
|
|
81
|
+
_drainQueue() {
|
|
82
|
+
while (this.count < this.max_concurrent && this.queue.length) {
|
|
83
|
+
const { func, args } = this.queue.shift()
|
|
84
|
+
this.count++
|
|
85
|
+
func(...args)
|
|
86
|
+
}
|
|
87
|
+
this._scheduleResetTimer()
|
|
88
|
+
}
|
|
54
89
|
_serializeArgs(args) {
|
|
55
90
|
try {
|
|
56
91
|
return JSON.stringify(args)
|
package/lib/rates.js
ADDED
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
const _ = require('lodash')
|
|
2
|
+
const needle = require('needle')
|
|
3
|
+
const Limiter = require('./rate-limiter.js')
|
|
4
|
+
|
|
5
|
+
let limiter = new Limiter({
|
|
6
|
+
max_concurrent: 1,
|
|
7
|
+
interval_concurrent: 3000,
|
|
8
|
+
})
|
|
9
|
+
|
|
10
|
+
const options = {}
|
|
11
|
+
|
|
12
|
+
// --- Forex rates ---
|
|
13
|
+
|
|
14
|
+
function get_forex_rate_usd(curs, cb) {
|
|
15
|
+
needle.get('https://cdn.jsdelivr.net/npm/@fawazahmed0/currency-api@latest/v1/currencies/usd.json', (err, res, body) => {
|
|
16
|
+
if (body && body.usd) {
|
|
17
|
+
let rates = {}
|
|
18
|
+
for (let cur of curs) {
|
|
19
|
+
rates[cur] = 1 / body.usd[cur.toLowerCase()]
|
|
20
|
+
}
|
|
21
|
+
cb(rates)
|
|
22
|
+
} else {
|
|
23
|
+
console.error('forex_rate error:', err || body)
|
|
24
|
+
cb({})
|
|
25
|
+
}
|
|
26
|
+
})
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// --- Crypto rates (Coinpaprika + exchange orderbook) ---
|
|
30
|
+
|
|
31
|
+
let get_crypto_rates = (cb, pair_or_pairs_or_cur_curs, type) => {
|
|
32
|
+
if (type === 'cur') {
|
|
33
|
+
let cur = pair_or_pairs_or_cur_curs
|
|
34
|
+
limiter.process(needle.get, 'https://api.coinpaprika.com/v1/tickers', (err, res, body) => {
|
|
35
|
+
if (err) {
|
|
36
|
+
console.error(cur, err)
|
|
37
|
+
cb(null)
|
|
38
|
+
return
|
|
39
|
+
}
|
|
40
|
+
if (body && Array.isArray(body)) {
|
|
41
|
+
for (let ticker of body) {
|
|
42
|
+
if (ticker.symbol.toUpperCase() === cur.toUpperCase()) {
|
|
43
|
+
cb(ticker.quotes.USD.price)
|
|
44
|
+
return
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
console.error('coinpaprika cur not found:', cur)
|
|
49
|
+
cb(null)
|
|
50
|
+
})
|
|
51
|
+
} else if (type === 'curs') {
|
|
52
|
+
let curs = pair_or_pairs_or_cur_curs
|
|
53
|
+
let curs_upper = curs.map((c) => c.toUpperCase())
|
|
54
|
+
let result = {}
|
|
55
|
+
limiter.process(needle.get, 'https://api.coinpaprika.com/v1/tickers', (err, res, body) => {
|
|
56
|
+
if (err) {
|
|
57
|
+
console.error(err)
|
|
58
|
+
cb(result)
|
|
59
|
+
return
|
|
60
|
+
}
|
|
61
|
+
if (body && Array.isArray(body)) {
|
|
62
|
+
for (let ticker of body) {
|
|
63
|
+
let symbol = ticker.symbol.toUpperCase()
|
|
64
|
+
if (curs_upper.includes(symbol)) {
|
|
65
|
+
let price = ticker.quotes.USD.price
|
|
66
|
+
result[symbol] = { USD: price, USDT: price }
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
cb(result)
|
|
71
|
+
})
|
|
72
|
+
} else if (type === 'curs_dict') {
|
|
73
|
+
_fetch_exchange_rates(cb, pair_or_pairs_or_cur_curs)
|
|
74
|
+
} else {
|
|
75
|
+
_fetch_all_coinpaprika(cb)
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// --- curs_dict: per-exchange orderbook mid-price ---
|
|
80
|
+
|
|
81
|
+
function _fetch_exchange_rates(cb, curs_dict) {
|
|
82
|
+
let length = _.keys(curs_dict).length
|
|
83
|
+
let result = {}
|
|
84
|
+
let completed = 0
|
|
85
|
+
|
|
86
|
+
if (length === 0) {
|
|
87
|
+
cb(result)
|
|
88
|
+
return
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function onResult(cur, rate) {
|
|
92
|
+
if (rate !== null) {
|
|
93
|
+
result[cur] = { USD: rate, USDT: rate }
|
|
94
|
+
}
|
|
95
|
+
completed++
|
|
96
|
+
if (completed === length) {
|
|
97
|
+
cb(result)
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function fetchMidPrice(cur, exchange, url, parseBody) {
|
|
102
|
+
limiter.process(needle.get, url, (err, res, body) => {
|
|
103
|
+
try {
|
|
104
|
+
if (err) throw err
|
|
105
|
+
onResult(cur, parseBody(body))
|
|
106
|
+
} catch (error) {
|
|
107
|
+
console.error(`${exchange} ${cur}:`, error.message || error)
|
|
108
|
+
onResult(cur, null)
|
|
109
|
+
}
|
|
110
|
+
})
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function midOf(asks, bids) {
|
|
114
|
+
return (parseFloat(asks[0][0]) + parseFloat(bids[0][0])) / 2
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
for (let cur in curs_dict) {
|
|
118
|
+
let exchange = curs_dict[cur]
|
|
119
|
+
let pair
|
|
120
|
+
|
|
121
|
+
if (exchange === 'ascendex') {
|
|
122
|
+
pair = cur + '/USDT'
|
|
123
|
+
fetchMidPrice(cur, exchange, `https://ascendex.com/api/pro/v1/depth?symbol=${pair}`, (body) => midOf(body.data.data.asks, body.data.data.bids))
|
|
124
|
+
} else if (exchange === 'binance') {
|
|
125
|
+
pair = cur + 'USDT'
|
|
126
|
+
fetchMidPrice(cur, exchange, `https://www.binance.com/api/v3/depth?symbol=${pair}&limit=20`, (body) => midOf(body.asks, body.bids))
|
|
127
|
+
} else if (exchange === 'biconomy') {
|
|
128
|
+
pair = cur + '_USDT'
|
|
129
|
+
fetchMidPrice(cur, exchange, `https://api.biconomy.com/api/v1/depth?symbol=${pair}&size=5`, (body) => midOf(body.asks, body.bids))
|
|
130
|
+
} else if (exchange === 'bingx') {
|
|
131
|
+
pair = cur + '-USDT'
|
|
132
|
+
fetchMidPrice(cur, exchange, `https://open-api.bingx.com/openApi/spot/v1/market/depth?symbol=${pair}&limit=20`, (body) => midOf(body.data.asks, body.data.bids))
|
|
133
|
+
} else if (exchange === 'bitget') {
|
|
134
|
+
pair = cur + 'USDT'
|
|
135
|
+
fetchMidPrice(cur, exchange, `https://api.bitget.com/api/v2/spot/market/orderbook?symbol=${pair}&type=step0`, (body) => midOf(body.data.asks, body.data.bids))
|
|
136
|
+
} else if (exchange === 'bitmart') {
|
|
137
|
+
pair = cur + '_USDT'
|
|
138
|
+
if (pair === 'ELON_USDT') pair = '$ELONSOL_USDT'
|
|
139
|
+
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)
|
|
140
|
+
} else if (exchange === 'bybit') {
|
|
141
|
+
pair = cur + 'USDT'
|
|
142
|
+
fetchMidPrice(cur, exchange, `https://api.bybit.com/v5/market/orderbook?category=spot&symbol=${pair}&limit=20`, (body) => midOf(body.result.a, body.result.b))
|
|
143
|
+
} else if (exchange === 'coinex') {
|
|
144
|
+
pair = cur.toLowerCase() + 'usdt'
|
|
145
|
+
fetchMidPrice(cur, exchange, `https://api.coinex.com/v1/market/depth?market=${pair}&limit=20&merge=0`, (body) => midOf(body.data.asks, body.data.bids))
|
|
146
|
+
} else if (exchange === 'coinstore') {
|
|
147
|
+
let storeCur = cur === 'SWAGG' ? 'SWAGGERC20' : cur
|
|
148
|
+
pair = storeCur.toLowerCase() + 'usdt'
|
|
149
|
+
fetchMidPrice(cur, exchange, `https://api.coinstore.com/api/v1/market/depth/${pair}`, (body) => midOf(body.data.a, body.data.b))
|
|
150
|
+
} else if (exchange === 'cryptocom') {
|
|
151
|
+
pair = cur + '_USDT'
|
|
152
|
+
fetchMidPrice(cur, exchange, `https://api.crypto.com/v2/public/get-book?instrument_name=${pair}&depth=20`, (body) => midOf(body.result.data[0].asks, body.result.data[0].bids))
|
|
153
|
+
} else if (exchange === 'digifinex') {
|
|
154
|
+
pair = cur + '_USDT'
|
|
155
|
+
fetchMidPrice(cur, exchange, `https://openapi.digifinex.com/v3/spot/orderbook?symbol=${pair}`, (body) => midOf(body.data.asks, body.data.bids))
|
|
156
|
+
} else if (exchange === 'fastex') {
|
|
157
|
+
pair = cur.toUpperCase() + '-USDT'
|
|
158
|
+
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)
|
|
159
|
+
} else if (exchange === 'gate') {
|
|
160
|
+
pair = cur + '_USDT'
|
|
161
|
+
fetchMidPrice(cur, exchange, `https://api.gateio.ws/api/v4/spot/order_book?currency_pair=${pair}`, (body) => midOf(body.asks, body.bids))
|
|
162
|
+
} else if (exchange === 'htx') {
|
|
163
|
+
pair = cur.toLowerCase() + 'usdt'
|
|
164
|
+
fetchMidPrice(cur, exchange, `https://api.htx.com/market/depth?symbol=${pair}&type=step0`, (body) => midOf(body.tick.asks, body.tick.bids))
|
|
165
|
+
} else if (exchange === 'kucoin') {
|
|
166
|
+
pair = cur + '-USDT'
|
|
167
|
+
fetchMidPrice(cur, exchange, `https://api.kucoin.com/api/v1/market/orderbook/level2_100?symbol=${pair}`, (body) => midOf(body.data.asks, body.data.bids))
|
|
168
|
+
} else if (exchange === 'lbank') {
|
|
169
|
+
pair = cur.toLowerCase() + '_usdt'
|
|
170
|
+
fetchMidPrice(cur, exchange, `https://api.lbank.info/v2/depth.do?symbol=${pair}&size=10`, (body) => midOf(body.data.asks, body.data.bids))
|
|
171
|
+
} else if (exchange === 'mexc') {
|
|
172
|
+
pair = cur + 'USDT'
|
|
173
|
+
fetchMidPrice(cur, exchange, `https://api.mexc.com/api/v3/depth?symbol=${pair}`, (body) => midOf(body.asks, body.bids))
|
|
174
|
+
} else if (exchange === 'okx') {
|
|
175
|
+
pair = cur + 'USDT'
|
|
176
|
+
fetchMidPrice(cur, exchange, `https://www.okx.com/api/v5/market/books?instId=${pair}&sz=20`, (body) => {
|
|
177
|
+
if (typeof body === 'string') body = JSON.parse(body)
|
|
178
|
+
return midOf(body.data[0].asks, body.data[0].bids)
|
|
179
|
+
})
|
|
180
|
+
} else if (exchange === 'p2b') {
|
|
181
|
+
pair = cur + '_USDT'
|
|
182
|
+
fetchMidPrice(cur, exchange, `https://api.p2pb2b.com/api/v2/public/depth/result?market=${pair}`, (body) => midOf(body.result.asks, body.result.bids))
|
|
183
|
+
} else if (exchange === 'weex') {
|
|
184
|
+
pair = cur.toUpperCase() + 'USDT'
|
|
185
|
+
fetchMidPrice(cur, exchange, `https://api-spot.weex.com/api/v2/market/depth?symbol=${pair}_SPBL&type=step0&limit=200`, (body) => midOf(body.data.asks, body.data.bids))
|
|
186
|
+
} else if (exchange === 'xt') {
|
|
187
|
+
pair = cur.toLowerCase() + '_usdt'
|
|
188
|
+
fetchMidPrice(cur, exchange, `https://sapi.xt.com/v4/public/depth?symbol=${pair}&limit=20`, (body) => midOf(body.result.asks, body.result.bids))
|
|
189
|
+
} else {
|
|
190
|
+
console.error(`crypto_rates: unsupported exchange "${exchange}" for ${cur}`)
|
|
191
|
+
onResult(cur, null)
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
// --- Default mode: fetch all coinpaprika tickers ---
|
|
197
|
+
|
|
198
|
+
function _fetch_all_coinpaprika(cb) {
|
|
199
|
+
let result = {}
|
|
200
|
+
function fetch(cb) {
|
|
201
|
+
limiter.process(needle.get, 'https://api.coinpaprika.com/v1/tickers', options, (err, res, body) => {
|
|
202
|
+
console.log('fetching coinpaprika rate: body length', _.get(body, 'length', body))
|
|
203
|
+
if (body && Array.isArray(body) && body.length > 0) {
|
|
204
|
+
for (let coin of body) {
|
|
205
|
+
let symbol = coin.symbol
|
|
206
|
+
if (!symbol || !coin.quotes || !coin.quotes.USD || !coin.quotes.USD.price) {
|
|
207
|
+
continue
|
|
208
|
+
}
|
|
209
|
+
symbol = symbol.toUpperCase()
|
|
210
|
+
if (symbol === 'BAT' && coin.id !== 'bat-basic-attention-token') {
|
|
211
|
+
continue
|
|
212
|
+
} else if (symbol === 'SBTC' && coin.id !== 'sbtc-super-bitcoin') {
|
|
213
|
+
continue
|
|
214
|
+
}
|
|
215
|
+
result[symbol] = {
|
|
216
|
+
USD: parseFloat(coin.quotes.USD.price),
|
|
217
|
+
USDT: parseFloat(coin.quotes.USD.price),
|
|
218
|
+
'24h_change': parseFloat(coin.quotes.USD.percent_change_24h),
|
|
219
|
+
'24h_volume_usd': parseFloat(coin.quotes.USD.volume_24h),
|
|
220
|
+
rank: parseInt(coin.rank) || 10000,
|
|
221
|
+
market_cap: parseInt(coin.quotes.USD.market_cap),
|
|
222
|
+
update_time: new Date(coin.last_updated),
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
// Calculate BTC/ETH rates for all currencies
|
|
226
|
+
for (let cur in result) {
|
|
227
|
+
if (result.BTC) result[cur].BTC = result[cur].USD / result.BTC.USD
|
|
228
|
+
if (result.ETH) result[cur].ETH = result[cur].USD / result.ETH.USD
|
|
229
|
+
}
|
|
230
|
+
cb(result)
|
|
231
|
+
} else {
|
|
232
|
+
console.error('Coinpaprika error:', err || (body && body.error) || body)
|
|
233
|
+
setTimeout(() => {
|
|
234
|
+
console.log('fetching coinpaprika rate: retry')
|
|
235
|
+
fetch(cb)
|
|
236
|
+
}, 60000)
|
|
237
|
+
}
|
|
238
|
+
})
|
|
239
|
+
}
|
|
240
|
+
console.log('fetching coinpaprika rate: starting')
|
|
241
|
+
fetch(cb)
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
module.exports = {
|
|
245
|
+
get_crypto_rates,
|
|
246
|
+
get_forex_rate_usd,
|
|
247
|
+
}
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@icgio/icg-utils",
|
|
3
|
-
"version": "1.9.
|
|
3
|
+
"version": "1.9.52",
|
|
4
4
|
"description": "Utils package.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"directories": {
|
|
7
7
|
"lib": "lib"
|
|
8
8
|
},
|
|
9
9
|
"scripts": {
|
|
10
|
-
"test": "
|
|
10
|
+
"test": "node test/rate-limiter.test.js"
|
|
11
11
|
},
|
|
12
12
|
"repository": {
|
|
13
13
|
"type": "git",
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
These patches target sibling repos that are read-only in this session.
|
|
2
|
+
|
|
3
|
+
Apply them from the repo they target:
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
cd /Users/lm/icg-exchanges-wrapper
|
|
7
|
+
git apply /Users/lm/icg-utils/patches/icg-exchanges-wrapper-strategy-priority.patch
|
|
8
|
+
|
|
9
|
+
cd /Users/lm/icg-market-maker
|
|
10
|
+
git apply /Users/lm/icg-utils/patches/icg-market-maker-dependency-bump.patch
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
After publishing `@icgio/icg-utils@1.9.52` and `@icgio/icg-exchanges-wrapper@1.15.36`, regenerate the market maker lockfile with:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
cd /Users/lm/icg-market-maker
|
|
17
|
+
npm install
|
|
18
|
+
```
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
diff --git a/middleware/orders.js b/middleware/orders.js
|
|
2
|
+
--- a/middleware/orders.js
|
|
3
|
+
+++ b/middleware/orders.js
|
|
4
|
+
@@ -73,7 +73,23 @@
|
|
5
|
+
const TRADES_DEFAULT_MS = 7 * 24 * 60 * 60 * 1000
|
|
6
|
+
const NO_CALLBACK_TIMEOUT_MS = 2 * 60 * 1000
|
|
7
|
+
const PROCESS_RATES_THROTTLE_MS = 75
|
|
8
|
+
+const ORDER_TYPE_PRIORITY = {
|
|
9
|
+
+ arb: 1,
|
|
10
|
+
+ self: 2,
|
|
11
|
+
+ maker_1: 3,
|
|
12
|
+
+ maker_1_taker: 3,
|
|
13
|
+
+ maker_2: 4,
|
|
14
|
+
+ maker_3: 5,
|
|
15
|
+
+}
|
|
16
|
+
|
|
17
|
+
+const getOrderPriority = (order_type, is_cancel = false) => {
|
|
18
|
+
+ const priority = ORDER_TYPE_PRIORITY[order_type]
|
|
19
|
+
+ if (priority == null) {
|
|
20
|
+
+ return Infinity
|
|
21
|
+
+ }
|
|
22
|
+
+ return is_cancel ? priority - 0.5 : priority
|
|
23
|
+
+}
|
|
24
|
+
+
|
|
25
|
+
module.exports = class Orders {
|
|
26
|
+
constructor(exchange_cur_quote_cur_dict, cd, settings) {
|
|
27
|
+
let { exchange_cur_quote_cur, exchange_cur_quote_cur_rates_only, pair_map_rates_only } = exchange_cur_quote_cur_dict
|
|
28
|
+
@@ -332,11 +348,17 @@
|
|
29
|
+
// }
|
|
30
|
+
}
|
|
31
|
+
if (routes) {
|
|
32
|
+
+ if (this.Exchanges[exchange].private_limiter) {
|
|
33
|
+
+ this.Exchanges[exchange].private_limiter.priority = getOrderPriority(order_type)
|
|
34
|
+
+ }
|
|
35
|
+
this.Exchanges[exchange].limit_trade_with_routes(pair, method, price, amount, routes, cb)
|
|
36
|
+
} else {
|
|
37
|
+
console.error('dex no routes:', rates_with_routes, price)
|
|
38
|
+
}
|
|
39
|
+
} else if (limiter_on) {
|
|
40
|
+
+ if (this.Exchanges[exchange].private_limiter) {
|
|
41
|
+
+ this.Exchanges[exchange].private_limiter.priority = getOrderPriority(order_type)
|
|
42
|
+
+ }
|
|
43
|
+
this.Exchanges[exchange].limit_trade(pair, method, price, amount, cb)
|
|
44
|
+
} else {
|
|
45
|
+
this.Exchanges[exchange].limit_trade_base(pair, method, price, amount, cb)
|
|
46
|
+
@@ -403,6 +425,9 @@
|
|
47
|
+
order.order_type + ' canceling',
|
|
48
|
+
)
|
|
49
|
+
;((order.exchange = exchange), (order.pair = pair))
|
|
50
|
+
+ if (this.Exchanges[exchange].private_limiter) {
|
|
51
|
+
+ this.Exchanges[exchange].private_limiter.priority = getOrderPriority(order.order_type, true)
|
|
52
|
+
+ }
|
|
53
|
+
this.Exchanges[exchange].cancel_order_by_order(order, (res) => {
|
|
54
|
+
if (res.success || (!res.success && res.error === 'order-not-open' && original_status === 'CANCELING')) {
|
|
55
|
+
this.update_order_info(
|
|
56
|
+
diff --git a/package.json b/package.json
|
|
57
|
+
--- a/package.json
|
|
58
|
+
+++ b/package.json
|
|
59
|
+
@@ -1,6 +1,6 @@
|
|
60
|
+
{
|
|
61
|
+
"name": "@icgio/icg-exchanges-wrapper",
|
|
62
|
+
- "version": "1.15.35",
|
|
63
|
+
+ "version": "1.15.36",
|
|
64
|
+
"description": "",
|
|
65
|
+
"main": "index.js",
|
|
66
|
+
"scripts": {
|
|
67
|
+
@@ -18,7 +18,7 @@
|
|
68
|
+
"homepage": "https://github.com/icgio/icg-exchanges-wrapper#readme",
|
|
69
|
+
"dependencies": {
|
|
70
|
+
"@icgio/icg-exchanges": "^1.32.187",
|
|
71
|
+
- "@icgio/icg-utils": "^1.9.51",
|
|
72
|
+
+ "@icgio/icg-utils": "^1.9.52",
|
|
73
|
+
"lodash": "^4.17.23",
|
|
74
|
+
"pg": "^8.17.2"
|
|
75
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
diff --git a/package.json b/package.json
|
|
2
|
+
--- a/package.json
|
|
3
|
+
+++ b/package.json
|
|
4
|
+
@@ -3,8 +3,8 @@
|
|
5
|
+
"version": "1.0.0",
|
|
6
|
+
"description": "ICG Market Maker",
|
|
7
|
+
"dependencies": {
|
|
8
|
+
- "@icgio/icg-exchanges-wrapper": "^1.15.35",
|
|
9
|
+
- "@icgio/icg-utils": "^1.9.51",
|
|
10
|
+
+ "@icgio/icg-exchanges-wrapper": "^1.15.36",
|
|
11
|
+
+ "@icgio/icg-utils": "^1.9.52",
|
|
12
|
+
"form-data": "^4.0.5",
|
|
13
|
+
"lodash": "^4.17.23",
|
|
14
|
+
"mailgun.js": "^12.7.0",
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
const assert = require('node:assert/strict')
|
|
2
|
+
const RateLimiter = require('../lib/rate-limiter')
|
|
3
|
+
|
|
4
|
+
const wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms))
|
|
5
|
+
|
|
6
|
+
async function testPriorityDrainOrder() {
|
|
7
|
+
const limiter = new RateLimiter({ max_concurrent: 2, interval_concurrent: 25 })
|
|
8
|
+
const executed = []
|
|
9
|
+
|
|
10
|
+
for (const priority of [5, 4, 3, 2, 1]) {
|
|
11
|
+
limiter.priority = priority
|
|
12
|
+
limiter.process((value) => {
|
|
13
|
+
executed.push(value)
|
|
14
|
+
}, priority)
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
await wait(10)
|
|
18
|
+
assert.deepStrictEqual(executed, [5, 4], 'first window should execute immediately in submission order')
|
|
19
|
+
|
|
20
|
+
await wait(30)
|
|
21
|
+
assert.deepStrictEqual(executed, [5, 4, 1, 2], 'queued work should drain by ascending priority')
|
|
22
|
+
|
|
23
|
+
await wait(30)
|
|
24
|
+
assert.deepStrictEqual(executed, [5, 4, 1, 2, 3], 'remaining queued work should continue draining in priority order')
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
async function testDefaultPriorityStaysFifo() {
|
|
28
|
+
const limiter = new RateLimiter({ max_concurrent: 1, interval_concurrent: 20 })
|
|
29
|
+
const executed = []
|
|
30
|
+
|
|
31
|
+
limiter.process((value) => {
|
|
32
|
+
executed.push(value)
|
|
33
|
+
}, 'first')
|
|
34
|
+
limiter.process((value) => {
|
|
35
|
+
executed.push(value)
|
|
36
|
+
}, 'second')
|
|
37
|
+
limiter.process((value) => {
|
|
38
|
+
executed.push(value)
|
|
39
|
+
}, 'third')
|
|
40
|
+
|
|
41
|
+
await wait(10)
|
|
42
|
+
assert.deepStrictEqual(executed, ['first'], 'first call should execute immediately')
|
|
43
|
+
|
|
44
|
+
await wait(25)
|
|
45
|
+
assert.deepStrictEqual(executed, ['first', 'second'], 'default priority should keep queued calls FIFO')
|
|
46
|
+
|
|
47
|
+
await wait(25)
|
|
48
|
+
assert.deepStrictEqual(executed, ['first', 'second', 'third'], 'later FIFO work should continue in order')
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
async function main() {
|
|
52
|
+
await testPriorityDrainOrder()
|
|
53
|
+
await testDefaultPriorityStaysFifo()
|
|
54
|
+
console.log('rate-limiter tests passed')
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
main().catch((err) => {
|
|
58
|
+
console.error(err)
|
|
59
|
+
process.exit(1)
|
|
60
|
+
})
|
package/lib/crypto-rates.js
DELETED
|
@@ -1,586 +0,0 @@
|
|
|
1
|
-
const _ = require('lodash')
|
|
2
|
-
|
|
3
|
-
const needle = require('needle')
|
|
4
|
-
|
|
5
|
-
const Limiter = require('./rate-limiter.js')
|
|
6
|
-
const { dex_rate, token_dict } = require('./dex-rate.js')
|
|
7
|
-
|
|
8
|
-
let limiter = new Limiter({
|
|
9
|
-
max_concurrent: 1,
|
|
10
|
-
interval_concurrent: 3000,
|
|
11
|
-
})
|
|
12
|
-
|
|
13
|
-
const options = {}
|
|
14
|
-
|
|
15
|
-
let result = {}
|
|
16
|
-
let get_crypto_rates = (cb, pair_or_pairs_or_cur_curs, type) => {
|
|
17
|
-
function get_crypto_rates_base(cb, pair_or_pairs_or_cur_curs, type) {
|
|
18
|
-
if (type === 'cur') {
|
|
19
|
-
let cur = pair_or_pairs_or_cur_curs
|
|
20
|
-
// Fetch all tickers and find by symbol
|
|
21
|
-
limiter.process(needle.get, 'https://api.coinpaprika.com/v1/tickers', (err, res, body) => {
|
|
22
|
-
if (err) {
|
|
23
|
-
console.error(cur, err)
|
|
24
|
-
cb(null)
|
|
25
|
-
return
|
|
26
|
-
}
|
|
27
|
-
if (body && Array.isArray(body)) {
|
|
28
|
-
for (let ticker of body) {
|
|
29
|
-
if (ticker.symbol.toUpperCase() === cur.toUpperCase()) {
|
|
30
|
-
cb(ticker.quotes.USD.price)
|
|
31
|
-
return
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
console.error('coinpaprika cur not found:', cur)
|
|
36
|
-
cb(null)
|
|
37
|
-
})
|
|
38
|
-
} else if (type === 'curs') {
|
|
39
|
-
let curs = pair_or_pairs_or_cur_curs
|
|
40
|
-
let curs_upper = curs.map((c) => c.toUpperCase())
|
|
41
|
-
// Fetch all tickers and filter by symbol
|
|
42
|
-
let url = `https://api.coinpaprika.com/v1/tickers`
|
|
43
|
-
limiter.process(needle.get, url, (err, res, body) => {
|
|
44
|
-
if (err) {
|
|
45
|
-
console.error(err)
|
|
46
|
-
return
|
|
47
|
-
}
|
|
48
|
-
if (body && Array.isArray(body)) {
|
|
49
|
-
for (let ticker of body) {
|
|
50
|
-
let symbol = ticker.symbol.toUpperCase()
|
|
51
|
-
if (curs_upper.includes(symbol)) {
|
|
52
|
-
let price = ticker.quotes.USD.price
|
|
53
|
-
result[symbol] = {
|
|
54
|
-
USD: price,
|
|
55
|
-
USDT: price,
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
cb(result)
|
|
61
|
-
})
|
|
62
|
-
} else if (type === 'curs_dict') {
|
|
63
|
-
let curs_dict = pair_or_pairs_or_cur_curs
|
|
64
|
-
let length = _.keys(curs_dict).length
|
|
65
|
-
for (let cur in curs_dict) {
|
|
66
|
-
let exchange = curs_dict[cur]
|
|
67
|
-
if (exchange === 'ascendex') {
|
|
68
|
-
let pair = cur + '/USDT'
|
|
69
|
-
let url = `https://ascendex.com/api/pro/v1/depth?symbol=${pair}`
|
|
70
|
-
limiter.process(needle.get, url, (err, res, body) => {
|
|
71
|
-
try {
|
|
72
|
-
const asks = body.data.data.asks
|
|
73
|
-
const bids = body.data.data.bids
|
|
74
|
-
let rate = (parseFloat(asks[0][0]) + parseFloat(bids[0][0])) / 2
|
|
75
|
-
result[cur] = {
|
|
76
|
-
USD: rate,
|
|
77
|
-
USDT: rate,
|
|
78
|
-
}
|
|
79
|
-
let result_length = _.keys(result).length
|
|
80
|
-
if (result_length === length) {
|
|
81
|
-
cb(result)
|
|
82
|
-
}
|
|
83
|
-
} catch (error) {
|
|
84
|
-
console.error(pair, error)
|
|
85
|
-
}
|
|
86
|
-
})
|
|
87
|
-
} else if (exchange === 'binance') {
|
|
88
|
-
let pair = cur + 'USDT'
|
|
89
|
-
let url = `https://www.binance.com/api/v3/depth?symbol=${pair}&limit=20`
|
|
90
|
-
limiter.process(needle.get, url, (err, res, body) => {
|
|
91
|
-
try {
|
|
92
|
-
const { asks, bids } = body
|
|
93
|
-
let rate = (parseFloat(asks[0][0]) + parseFloat(bids[0][0])) / 2
|
|
94
|
-
result[cur] = {
|
|
95
|
-
USD: rate,
|
|
96
|
-
USDT: rate,
|
|
97
|
-
}
|
|
98
|
-
let result_length = _.keys(result).length
|
|
99
|
-
if (result_length === length) {
|
|
100
|
-
cb(result)
|
|
101
|
-
}
|
|
102
|
-
} catch (error) {
|
|
103
|
-
console.error(pair, error)
|
|
104
|
-
}
|
|
105
|
-
})
|
|
106
|
-
} else if (exchange === 'biconomy') {
|
|
107
|
-
let pair = cur + '_USDT'
|
|
108
|
-
let url = `https://api.biconomy.com/api/v1/depth?symbol=${pair}&size=5`
|
|
109
|
-
limiter.process(needle.get, url, (err, res, body) => {
|
|
110
|
-
try {
|
|
111
|
-
const { asks, bids } = body
|
|
112
|
-
let rate = (parseFloat(asks[0][0]) + parseFloat(bids[0][0])) / 2
|
|
113
|
-
result[cur] = {
|
|
114
|
-
USD: rate,
|
|
115
|
-
USDT: rate,
|
|
116
|
-
}
|
|
117
|
-
let result_length = _.keys(result).length
|
|
118
|
-
if (result_length === length) {
|
|
119
|
-
cb(result)
|
|
120
|
-
}
|
|
121
|
-
} catch (error) {
|
|
122
|
-
console.error(pair, error)
|
|
123
|
-
}
|
|
124
|
-
})
|
|
125
|
-
} else if (exchange === 'bingx') {
|
|
126
|
-
let pair = cur + 'USDT'
|
|
127
|
-
let url = `https://www.binance.com/api/v3/depth?symbol=${pair}&limit=20`
|
|
128
|
-
limiter.process(needle.get, url, (err, res, body) => {
|
|
129
|
-
try {
|
|
130
|
-
const { asks, bids } = body
|
|
131
|
-
let rate = (parseFloat(asks[0][0]) + parseFloat(bids[0][0])) / 2
|
|
132
|
-
result[cur] = {
|
|
133
|
-
USD: rate,
|
|
134
|
-
USDT: rate,
|
|
135
|
-
}
|
|
136
|
-
let result_length = _.keys(result).length
|
|
137
|
-
if (result_length === length) {
|
|
138
|
-
cb(result)
|
|
139
|
-
}
|
|
140
|
-
} catch (error) {
|
|
141
|
-
console.error(pair, error)
|
|
142
|
-
}
|
|
143
|
-
})
|
|
144
|
-
} else if (exchange === 'bitget') {
|
|
145
|
-
let pair = cur + 'USDT'
|
|
146
|
-
let url = `https://api.bitget.com/api/v2/spot/market/orderbook?symbol=${pair}&type=step0`
|
|
147
|
-
limiter.process(needle.get, url, (err, res, body) => {
|
|
148
|
-
try {
|
|
149
|
-
const asks = body.data.asks
|
|
150
|
-
const bids = body.data.bids
|
|
151
|
-
let rate = (parseFloat(asks[0][0]) + parseFloat(bids[0][0])) / 2
|
|
152
|
-
result[cur] = {
|
|
153
|
-
USD: rate,
|
|
154
|
-
USDT: rate,
|
|
155
|
-
}
|
|
156
|
-
let result_length = _.keys(result).length
|
|
157
|
-
if (result_length === length) {
|
|
158
|
-
cb(result)
|
|
159
|
-
}
|
|
160
|
-
} catch (error) {
|
|
161
|
-
console.error(pair, error)
|
|
162
|
-
}
|
|
163
|
-
})
|
|
164
|
-
} else if (exchange === 'bitmart') {
|
|
165
|
-
let pair = cur + '_USDT'
|
|
166
|
-
if (pair === 'ELON_USDT') {
|
|
167
|
-
pair = '$ELONSOL_USDT'
|
|
168
|
-
}
|
|
169
|
-
let url = `https://api-cloud.bitmart.com/spot/v1/symbols/book?symbol=${pair}`
|
|
170
|
-
limiter.process(needle.get, url, (err, res, body) => {
|
|
171
|
-
try {
|
|
172
|
-
const asks = body.data.sells
|
|
173
|
-
const bids = body.data.buys
|
|
174
|
-
let rate = (parseFloat(asks[0].price) + parseFloat(bids[0].price)) / 2
|
|
175
|
-
result[cur] = {
|
|
176
|
-
USD: rate,
|
|
177
|
-
USDT: rate,
|
|
178
|
-
}
|
|
179
|
-
let result_length = _.keys(result).length
|
|
180
|
-
if (result_length === length) {
|
|
181
|
-
cb(result)
|
|
182
|
-
}
|
|
183
|
-
} catch (error) {
|
|
184
|
-
console.error(pair, error)
|
|
185
|
-
}
|
|
186
|
-
})
|
|
187
|
-
} else if (exchange === 'bybit') {
|
|
188
|
-
let pair = cur + 'USDT'
|
|
189
|
-
let url = `https://api.bybit.com/v5/market/orderbook?category=spot&symbol=${pair}&limit=20`
|
|
190
|
-
limiter.process(needle.get, url, (err, res, body) => {
|
|
191
|
-
try {
|
|
192
|
-
const asks = body.result.a
|
|
193
|
-
const bids = body.result.b
|
|
194
|
-
let rate = (parseFloat(asks[0][0]) + parseFloat(bids[0][0])) / 2
|
|
195
|
-
result[cur] = {
|
|
196
|
-
USD: rate,
|
|
197
|
-
USDT: rate,
|
|
198
|
-
}
|
|
199
|
-
let result_length = _.keys(result).length
|
|
200
|
-
if (result_length === length) {
|
|
201
|
-
cb(result)
|
|
202
|
-
}
|
|
203
|
-
} catch (error) {
|
|
204
|
-
console.error(pair, error)
|
|
205
|
-
}
|
|
206
|
-
})
|
|
207
|
-
} else if (exchange === 'coinex') {
|
|
208
|
-
let pair = cur.toLowerCase() + 'usdt'
|
|
209
|
-
let url = `https://api.coinex.com/v1/market/depth?market=${pair}&limit=20&merge=0`
|
|
210
|
-
limiter.process(needle.get, url, (err, res, body) => {
|
|
211
|
-
try {
|
|
212
|
-
const asks = body.data.asks
|
|
213
|
-
const bids = body.data.bids
|
|
214
|
-
let rate = (parseFloat(asks[0][0]) + parseFloat(bids[0][0])) / 2
|
|
215
|
-
result[cur] = {
|
|
216
|
-
USD: rate,
|
|
217
|
-
USDT: rate,
|
|
218
|
-
}
|
|
219
|
-
let result_length = _.keys(result).length
|
|
220
|
-
if (result_length === length) {
|
|
221
|
-
cb(result)
|
|
222
|
-
}
|
|
223
|
-
} catch (error) {
|
|
224
|
-
console.error(pair, error)
|
|
225
|
-
}
|
|
226
|
-
})
|
|
227
|
-
} else if (exchange === 'coinstore') {
|
|
228
|
-
if (cur === 'SWAGG') {
|
|
229
|
-
cur = 'SWAGGERC20'
|
|
230
|
-
}
|
|
231
|
-
let pair = cur.toLowerCase() + 'usdt'
|
|
232
|
-
let url = `https://api.coinstore.com/api/v1/market/depth/${pair}`
|
|
233
|
-
limiter.process(needle.get, url, (err, res, body) => {
|
|
234
|
-
try {
|
|
235
|
-
const asks = body.data.a
|
|
236
|
-
const bids = body.data.b
|
|
237
|
-
let rate = (parseFloat(asks[0][0]) + parseFloat(bids[0][0])) / 2
|
|
238
|
-
result[cur] = {
|
|
239
|
-
USD: rate,
|
|
240
|
-
USDT: rate,
|
|
241
|
-
}
|
|
242
|
-
let result_length = _.keys(result).length
|
|
243
|
-
if (result_length === length) {
|
|
244
|
-
cb(result)
|
|
245
|
-
}
|
|
246
|
-
} catch (error) {
|
|
247
|
-
console.error(pair, error)
|
|
248
|
-
}
|
|
249
|
-
})
|
|
250
|
-
} else if (exchange === 'cryptocom') {
|
|
251
|
-
let pair = cur + '_USDT'
|
|
252
|
-
let url = `https://api.crypto.com/v2/public/get-book?instrument_name=${pair}&depth=20`
|
|
253
|
-
limiter.process(needle.get, url, (err, res, body) => {
|
|
254
|
-
try {
|
|
255
|
-
const asks = body.result.data[0].asks
|
|
256
|
-
const bids = body.result.data[0].bids
|
|
257
|
-
let rate = (parseFloat(asks[0][0]) + parseFloat(bids[0][0])) / 2
|
|
258
|
-
result[cur] = {
|
|
259
|
-
USD: rate,
|
|
260
|
-
USDT: rate,
|
|
261
|
-
}
|
|
262
|
-
let result_length = _.keys(result).length
|
|
263
|
-
if (result_length === length) {
|
|
264
|
-
cb(result)
|
|
265
|
-
}
|
|
266
|
-
} catch (error) {
|
|
267
|
-
console.error(pair, error)
|
|
268
|
-
}
|
|
269
|
-
})
|
|
270
|
-
} else if (exchange === 'digifinex') {
|
|
271
|
-
let pair = cur + '_USDT'
|
|
272
|
-
let url = `https://openapi.digifinex.com/v3/spot/orderbook?symbol=${pair}`
|
|
273
|
-
limiter.process(needle.get, url, (err, res, body) => {
|
|
274
|
-
try {
|
|
275
|
-
const asks = body.data.asks
|
|
276
|
-
const bids = body.data.bids
|
|
277
|
-
let rate = (parseFloat(asks[0][0]) + parseFloat(bids[0][0])) / 2
|
|
278
|
-
result[cur] = {
|
|
279
|
-
USD: rate,
|
|
280
|
-
USDT: rate,
|
|
281
|
-
}
|
|
282
|
-
let result_length = _.keys(result).length
|
|
283
|
-
if (result_length === length) {
|
|
284
|
-
cb(result)
|
|
285
|
-
}
|
|
286
|
-
} catch (error) {
|
|
287
|
-
console.error(pair, error)
|
|
288
|
-
}
|
|
289
|
-
})
|
|
290
|
-
} else if (exchange === 'fastex') {
|
|
291
|
-
let pair = cur.toUpperCase() + '-USDT'
|
|
292
|
-
let url = `https://exchange.fastex.com/api/v1/orderbook/info?symbol=${pair}`
|
|
293
|
-
limiter.process(needle.get, url, (err, res, body) => {
|
|
294
|
-
try {
|
|
295
|
-
const asks = body.response.entities.asks
|
|
296
|
-
const bids = body.response.entities.bids
|
|
297
|
-
let rate = (parseFloat(asks[0].price) + parseFloat(bids[0].price)) / 2
|
|
298
|
-
result[cur] = {
|
|
299
|
-
USD: rate,
|
|
300
|
-
USDT: rate,
|
|
301
|
-
}
|
|
302
|
-
let result_length = _.keys(result).length
|
|
303
|
-
if (result_length === length) {
|
|
304
|
-
cb(result)
|
|
305
|
-
}
|
|
306
|
-
} catch (error) {
|
|
307
|
-
console.error(pair, error)
|
|
308
|
-
}
|
|
309
|
-
})
|
|
310
|
-
} else if (exchange === 'gate') {
|
|
311
|
-
let pair = cur + '_USDT'
|
|
312
|
-
let url = `https://api.gateio.ws/api/v4/spot/order_book?currency_pair=${pair}`
|
|
313
|
-
limiter.process(needle.get, url, (err, res, body) => {
|
|
314
|
-
try {
|
|
315
|
-
const { asks, bids } = body
|
|
316
|
-
let rate = (parseFloat(asks[0][0]) + parseFloat(bids[0][0])) / 2
|
|
317
|
-
result[cur] = {
|
|
318
|
-
USD: rate,
|
|
319
|
-
USDT: rate,
|
|
320
|
-
}
|
|
321
|
-
let result_length = _.keys(result).length
|
|
322
|
-
if (result_length === length) {
|
|
323
|
-
cb(result)
|
|
324
|
-
}
|
|
325
|
-
} catch (error) {
|
|
326
|
-
console.error(pair, error)
|
|
327
|
-
}
|
|
328
|
-
})
|
|
329
|
-
} else if (exchange === 'htx') {
|
|
330
|
-
let pair = cur.toLowerCase() + 'usdt'
|
|
331
|
-
let url = `https://api.htx.com/market/depth?symbol=${pair}&type=step0`
|
|
332
|
-
limiter.process(needle.get, url, (err, res, body) => {
|
|
333
|
-
try {
|
|
334
|
-
const { asks, bids } = body.tick
|
|
335
|
-
let rate = (parseFloat(asks[0][0]) + parseFloat(bids[0][0])) / 2
|
|
336
|
-
result[cur] = {
|
|
337
|
-
USD: rate,
|
|
338
|
-
USDT: rate,
|
|
339
|
-
}
|
|
340
|
-
let result_length = _.keys(result).length
|
|
341
|
-
if (result_length === length) {
|
|
342
|
-
cb(result)
|
|
343
|
-
}
|
|
344
|
-
} catch (error) {
|
|
345
|
-
console.error(pair, error)
|
|
346
|
-
}
|
|
347
|
-
})
|
|
348
|
-
} else if (exchange === 'kucoin') {
|
|
349
|
-
let pair = cur + '-USDT'
|
|
350
|
-
let url = `https://api.kucoin.com/api/v1/market/orderbook/level2_100?symbol=${pair}`
|
|
351
|
-
limiter.process(needle.get, url, (err, res, body) => {
|
|
352
|
-
try {
|
|
353
|
-
const asks = body.data.asks
|
|
354
|
-
const bids = body.data.bids
|
|
355
|
-
let rate = (parseFloat(asks[0][0]) + parseFloat(bids[0][0])) / 2
|
|
356
|
-
result[cur] = {
|
|
357
|
-
USD: rate,
|
|
358
|
-
USDT: rate,
|
|
359
|
-
}
|
|
360
|
-
let result_length = _.keys(result).length
|
|
361
|
-
if (result_length === length) {
|
|
362
|
-
cb(result)
|
|
363
|
-
}
|
|
364
|
-
} catch (error) {
|
|
365
|
-
console.error(pair, error)
|
|
366
|
-
}
|
|
367
|
-
})
|
|
368
|
-
} else if (exchange === 'lbank') {
|
|
369
|
-
let pair = cur.toLowerCase() + '_usdt'
|
|
370
|
-
let url = `https://api.lbank.info/v2/depth.do?symbol=${pair}&size=10`
|
|
371
|
-
limiter.process(needle.get, url, (err, res, body) => {
|
|
372
|
-
try {
|
|
373
|
-
const { asks, bids } = body.data
|
|
374
|
-
let rate = (parseFloat(asks[0][0]) + parseFloat(bids[0][0])) / 2
|
|
375
|
-
result[cur] = {
|
|
376
|
-
USD: rate,
|
|
377
|
-
USDT: rate,
|
|
378
|
-
}
|
|
379
|
-
let result_length = _.keys(result).length
|
|
380
|
-
if (result_length === length) {
|
|
381
|
-
cb(result)
|
|
382
|
-
}
|
|
383
|
-
} catch (error) {
|
|
384
|
-
console.error(pair, error)
|
|
385
|
-
}
|
|
386
|
-
})
|
|
387
|
-
} else if (exchange === 'mexc') {
|
|
388
|
-
let pair = cur + 'USDT'
|
|
389
|
-
let url = `https://api.mexc.com/api/v3/depth?symbol=${pair}`
|
|
390
|
-
limiter.process(needle.get, url, (err, res, body) => {
|
|
391
|
-
try {
|
|
392
|
-
const { asks, bids } = body
|
|
393
|
-
let rate = (parseFloat(asks[0][0]) + parseFloat(bids[0][0])) / 2
|
|
394
|
-
result[cur] = {
|
|
395
|
-
USD: rate,
|
|
396
|
-
USDT: rate,
|
|
397
|
-
}
|
|
398
|
-
let result_length = _.keys(result).length
|
|
399
|
-
if (result_length === length) {
|
|
400
|
-
cb(result)
|
|
401
|
-
}
|
|
402
|
-
} catch (error) {
|
|
403
|
-
console.error(pair, error)
|
|
404
|
-
}
|
|
405
|
-
})
|
|
406
|
-
} else if (exchange === 'okx') {
|
|
407
|
-
let pair = cur + 'USDT'
|
|
408
|
-
let url = `https://www.okx.com/api/v5/market/books?instId=${pair}&sz=20`
|
|
409
|
-
limiter.process(needle.get, url, (err, res, body) => {
|
|
410
|
-
try {
|
|
411
|
-
if (typeof body === 'string') {
|
|
412
|
-
body = JSON.parse(body)
|
|
413
|
-
}
|
|
414
|
-
const asks = body.data[0].asks
|
|
415
|
-
const bids = body.data[0].bids
|
|
416
|
-
let rate = (parseFloat(asks[0][0]) + parseFloat(bids[0][0])) / 2
|
|
417
|
-
result[cur] = {
|
|
418
|
-
USD: rate,
|
|
419
|
-
USDT: rate,
|
|
420
|
-
}
|
|
421
|
-
let result_length = _.keys(result).length
|
|
422
|
-
if (result_length === length) {
|
|
423
|
-
cb(result)
|
|
424
|
-
}
|
|
425
|
-
} catch (error) {
|
|
426
|
-
console.error(pair, error)
|
|
427
|
-
}
|
|
428
|
-
})
|
|
429
|
-
} else if (exchange === 'p2b') {
|
|
430
|
-
let pair = cur + '_USDT'
|
|
431
|
-
let url = `https://api.p2pb2b.com/api/v2/public/depth/result?market=${pair}`
|
|
432
|
-
limiter.process(needle.get, url, (err, res, body) => {
|
|
433
|
-
try {
|
|
434
|
-
const asks = body.result.asks
|
|
435
|
-
const bids = body.result.bids
|
|
436
|
-
let rate = (parseFloat(asks[0][0]) + parseFloat(bids[0][0])) / 2
|
|
437
|
-
result[cur] = {
|
|
438
|
-
USD: rate,
|
|
439
|
-
USDT: rate,
|
|
440
|
-
}
|
|
441
|
-
let result_length = _.keys(result).length
|
|
442
|
-
if (result_length === length) {
|
|
443
|
-
cb(result)
|
|
444
|
-
}
|
|
445
|
-
} catch (error) {
|
|
446
|
-
console.error(pair, error)
|
|
447
|
-
}
|
|
448
|
-
})
|
|
449
|
-
} else if (exchange === 'weex') {
|
|
450
|
-
let pair = cur.toUpperCase() + 'USDT'
|
|
451
|
-
let url = `https://api-spot.weex.com/api/v2/market/depth?symbol=${pair}_SPBL&type=step0&limit=200`
|
|
452
|
-
limiter.process(needle.get, url, (err, res, body) => {
|
|
453
|
-
try {
|
|
454
|
-
const asks = body.data.asks
|
|
455
|
-
const bids = body.data.bids
|
|
456
|
-
let rate = (parseFloat(asks[0][0]) + parseFloat(bids[0][0])) / 2
|
|
457
|
-
result[cur] = {
|
|
458
|
-
USD: rate,
|
|
459
|
-
USDT: rate,
|
|
460
|
-
}
|
|
461
|
-
let result_length = _.keys(result).length
|
|
462
|
-
if (result_length === length) {
|
|
463
|
-
cb(result)
|
|
464
|
-
}
|
|
465
|
-
} catch (error) {
|
|
466
|
-
console.error(pair, error)
|
|
467
|
-
}
|
|
468
|
-
})
|
|
469
|
-
} else if (exchange === 'xt') {
|
|
470
|
-
let pair = cur.toLowerCase() + '_usdt'
|
|
471
|
-
let url = `https://sapi.xt.com/v4/public/depth?symbol=${pair}&limit=20`
|
|
472
|
-
limiter.process(needle.get, url, (err, res, body) => {
|
|
473
|
-
try {
|
|
474
|
-
const asks = body.result.asks
|
|
475
|
-
const bids = body.result.bids
|
|
476
|
-
let rate = (parseFloat(asks[0][0]) + parseFloat(bids[0][0])) / 2
|
|
477
|
-
result[cur] = {
|
|
478
|
-
USD: rate,
|
|
479
|
-
USDT: rate,
|
|
480
|
-
}
|
|
481
|
-
let result_length = _.keys(result).length
|
|
482
|
-
if (result_length === length) {
|
|
483
|
-
cb(result)
|
|
484
|
-
}
|
|
485
|
-
} catch (error) {
|
|
486
|
-
console.error(pair, error)
|
|
487
|
-
}
|
|
488
|
-
})
|
|
489
|
-
} else if (exchange === 'dex') {
|
|
490
|
-
if (!token_dict[cur]) {
|
|
491
|
-
console.error('dex token not in token_dict:', cur)
|
|
492
|
-
let result_length = _.keys(result).length
|
|
493
|
-
if (result_length === length) {
|
|
494
|
-
cb(result)
|
|
495
|
-
}
|
|
496
|
-
} else {
|
|
497
|
-
dex_rate(cur, (rate_result) => {
|
|
498
|
-
let rate = rate_result[cur]
|
|
499
|
-
result[cur] = {
|
|
500
|
-
USD: rate,
|
|
501
|
-
USDT: rate,
|
|
502
|
-
}
|
|
503
|
-
let result_length = _.keys(result).length
|
|
504
|
-
if (result_length === length) {
|
|
505
|
-
cb(result)
|
|
506
|
-
}
|
|
507
|
-
})
|
|
508
|
-
}
|
|
509
|
-
}
|
|
510
|
-
}
|
|
511
|
-
} else {
|
|
512
|
-
// Coinpaprika returns all tickers in one call (no pagination needed)
|
|
513
|
-
let get_coinpaprika_rates = (cb) => {
|
|
514
|
-
let url = 'https://api.coinpaprika.com/v1/tickers'
|
|
515
|
-
limiter.process(needle.get, url, options, (err, res, body) => {
|
|
516
|
-
console.log('fetching coinpaprika rate: body length', _.get(body, 'length', body))
|
|
517
|
-
if (body && Array.isArray(body) && body.length > 0) {
|
|
518
|
-
for (let coin of body) {
|
|
519
|
-
let symbol = coin.symbol
|
|
520
|
-
if (!symbol || !coin.quotes || !coin.quotes.USD || !coin.quotes.USD.price) {
|
|
521
|
-
continue
|
|
522
|
-
}
|
|
523
|
-
symbol = symbol.toUpperCase()
|
|
524
|
-
if (symbol === 'BAT' && coin.id !== 'bat-basic-attention-token') {
|
|
525
|
-
continue
|
|
526
|
-
} else if (symbol === 'SBTC' && coin.id !== 'sbtc-super-bitcoin') {
|
|
527
|
-
continue
|
|
528
|
-
}
|
|
529
|
-
result[symbol] = {
|
|
530
|
-
USD: parseFloat(coin.quotes.USD.price),
|
|
531
|
-
USDT: parseFloat(coin.quotes.USD.price),
|
|
532
|
-
'24h_change': parseFloat(coin.quotes.USD.percent_change_24h),
|
|
533
|
-
'24h_volume_usd': parseFloat(coin.quotes.USD.volume_24h),
|
|
534
|
-
rank: parseInt(coin.rank) || 10000,
|
|
535
|
-
market_cap: parseInt(coin.quotes.USD.market_cap),
|
|
536
|
-
update_time: new Date(coin.last_updated),
|
|
537
|
-
}
|
|
538
|
-
}
|
|
539
|
-
// Calculate BTC/ETH rates for all currencies
|
|
540
|
-
if (result.BTC && result.ETH) {
|
|
541
|
-
for (let cur in result) {
|
|
542
|
-
result[cur].BTC = result[cur].USD / result.BTC.USD
|
|
543
|
-
result[cur].ETH = result[cur].USD / result.ETH.USD
|
|
544
|
-
}
|
|
545
|
-
cb(result)
|
|
546
|
-
} else {
|
|
547
|
-
console.warn('BTC or ETH not found in Coinpaprika response, returning partial results')
|
|
548
|
-
for (let cur in result) {
|
|
549
|
-
if (result.BTC) result[cur].BTC = result[cur].USD / result.BTC.USD
|
|
550
|
-
if (result.ETH) result[cur].ETH = result[cur].USD / result.ETH.USD
|
|
551
|
-
}
|
|
552
|
-
cb(result)
|
|
553
|
-
}
|
|
554
|
-
} else {
|
|
555
|
-
// Handle error or empty response
|
|
556
|
-
if (err) {
|
|
557
|
-
console.error('Coinpaprika error:', err)
|
|
558
|
-
setTimeout(() => {
|
|
559
|
-
console.log('fetching coinpaprika rate: retry after error')
|
|
560
|
-
get_coinpaprika_rates(cb)
|
|
561
|
-
}, 60000)
|
|
562
|
-
} else if (body && body.error) {
|
|
563
|
-
// API error response
|
|
564
|
-
console.error('Coinpaprika API error:', body.error)
|
|
565
|
-
setTimeout(() => {
|
|
566
|
-
console.log('fetching coinpaprika rate: retry after API error')
|
|
567
|
-
get_coinpaprika_rates(cb)
|
|
568
|
-
}, 60000)
|
|
569
|
-
} else {
|
|
570
|
-
// Unknown error - retry
|
|
571
|
-
console.error('fetching coinpaprika rate: error', err || body)
|
|
572
|
-
setTimeout(() => {
|
|
573
|
-
get_coinpaprika_rates(cb)
|
|
574
|
-
}, 60000)
|
|
575
|
-
}
|
|
576
|
-
}
|
|
577
|
-
})
|
|
578
|
-
}
|
|
579
|
-
console.log('fetching coinpaprika rate: starting')
|
|
580
|
-
get_coinpaprika_rates(cb)
|
|
581
|
-
}
|
|
582
|
-
}
|
|
583
|
-
get_crypto_rates_base(cb, pair_or_pairs_or_cur_curs, type)
|
|
584
|
-
}
|
|
585
|
-
|
|
586
|
-
module.exports = get_crypto_rates
|
package/lib/dex-rate.js
DELETED
|
@@ -1,99 +0,0 @@
|
|
|
1
|
-
const _ = require('lodash')
|
|
2
|
-
|
|
3
|
-
const needle = require('needle')
|
|
4
|
-
|
|
5
|
-
const token_dict = {
|
|
6
|
-
// BSC tokens (using token_price API)
|
|
7
|
-
FREN: {
|
|
8
|
-
address: '0xe6a768464b042a6d029394db1fdef360cb60bbeb',
|
|
9
|
-
network: 'bsc',
|
|
10
|
-
type: 'token',
|
|
11
|
-
},
|
|
12
|
-
RWX: {
|
|
13
|
-
address: '0xb0a1c87b890fe171371f81a59f7ed4636e82595d',
|
|
14
|
-
network: 'bsc',
|
|
15
|
-
type: 'token',
|
|
16
|
-
},
|
|
17
|
-
// Pool-based tokens (using pools API)
|
|
18
|
-
ARMY: {
|
|
19
|
-
pool: '41524D5900000000000000000000000000000000.rGG3wQ4kUzd7Jnmk1n5NWPZjjut62kCBfC_XRP',
|
|
20
|
-
network: 'xrpl',
|
|
21
|
-
type: 'pool',
|
|
22
|
-
},
|
|
23
|
-
CCDOG: {
|
|
24
|
-
pool: '0x9ea8d5d68a4b6bd5cc5ed6b505f920ce6db9e93a',
|
|
25
|
-
network: 'eth',
|
|
26
|
-
type: 'pool',
|
|
27
|
-
},
|
|
28
|
-
JYAI: {
|
|
29
|
-
pool: '0x2623edc6008d057786a6bf9dd34185dcddebbf2f',
|
|
30
|
-
network: 'eth',
|
|
31
|
-
type: 'pool',
|
|
32
|
-
},
|
|
33
|
-
LBAI: {
|
|
34
|
-
pool: '0x65a33e74e18c99388f4989c296ab8e860219ee76',
|
|
35
|
-
network: 'eth',
|
|
36
|
-
type: 'pool',
|
|
37
|
-
},
|
|
38
|
-
MINDFAK: {
|
|
39
|
-
pool: '0x40c03fb952df5f4ec72414efb0b378d70cded263',
|
|
40
|
-
network: 'eth',
|
|
41
|
-
type: 'pool',
|
|
42
|
-
},
|
|
43
|
-
SHRAP: {
|
|
44
|
-
pool: '0x58f0caa7a5a3a92e33a348c282b4e415b84d863a',
|
|
45
|
-
network: 'avax',
|
|
46
|
-
type: 'pool',
|
|
47
|
-
},
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
let dex_rate = function (cur, cb) {
|
|
51
|
-
const token = token_dict[cur]
|
|
52
|
-
if (!token) {
|
|
53
|
-
console.error('dex_rate: token not found in token_dict:', cur)
|
|
54
|
-
cb(null)
|
|
55
|
-
return
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
if (token.type === 'pool') {
|
|
59
|
-
// Pool-based API
|
|
60
|
-
const url = `https://api.geckoterminal.com/api/v2/networks/${token.network}/pools/${token.pool}`
|
|
61
|
-
needle.get(url, (err, res, body) => {
|
|
62
|
-
try {
|
|
63
|
-
if (body && body.data && body.data.attributes) {
|
|
64
|
-
let rate = parseFloat(body.data.attributes.base_token_price_usd)
|
|
65
|
-
cb({
|
|
66
|
-
[cur]: rate,
|
|
67
|
-
})
|
|
68
|
-
} else {
|
|
69
|
-
console.error('dex_rate pool error:', cur, body)
|
|
70
|
-
cb(null)
|
|
71
|
-
}
|
|
72
|
-
} catch (error) {
|
|
73
|
-
console.error('dex_rate pool error:', cur, error)
|
|
74
|
-
cb(null)
|
|
75
|
-
}
|
|
76
|
-
})
|
|
77
|
-
} else {
|
|
78
|
-
// Token price API
|
|
79
|
-
const url = `https://api.geckoterminal.com/api/v2/simple/networks/${token.network}/token_price/${token.address}`
|
|
80
|
-
needle.get(url, (err, res, body) => {
|
|
81
|
-
try {
|
|
82
|
-
if (body && body.data && body.data.id) {
|
|
83
|
-
let rate = parseFloat(body.data.attributes.token_prices[token.address])
|
|
84
|
-
cb({
|
|
85
|
-
[cur]: rate,
|
|
86
|
-
})
|
|
87
|
-
} else {
|
|
88
|
-
console.error('dex_rate token error:', cur, body)
|
|
89
|
-
cb(null)
|
|
90
|
-
}
|
|
91
|
-
} catch (error) {
|
|
92
|
-
console.error('dex_rate token error:', cur, error)
|
|
93
|
-
cb(null)
|
|
94
|
-
}
|
|
95
|
-
})
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
module.exports = { dex_rate, token_dict }
|
package/lib/forex-rate.js
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
const needle = require('needle')
|
|
2
|
-
const utils = require('./utils.js')
|
|
3
|
-
|
|
4
|
-
let get_forex_rate_usd = function (curs, cb) {
|
|
5
|
-
needle.get(`https://cdn.jsdelivr.net/npm/@fawazahmed0/currency-api@latest/v1/currencies/usd.json`, (err, res, body) => {
|
|
6
|
-
if (body && body.usd) {
|
|
7
|
-
let rates = {}
|
|
8
|
-
for (let cur of curs) {
|
|
9
|
-
rates[cur] = 1 / body.usd[cur.toLowerCase()]
|
|
10
|
-
}
|
|
11
|
-
cb(rates)
|
|
12
|
-
}
|
|
13
|
-
})
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
module.exports.get_forex_rate_usd = get_forex_rate_usd
|
|
17
|
-
|
|
18
|
-
// get_forex_rate_usd(['BTC', 'CNY', 'HKD'], (rate) => {
|
|
19
|
-
// console.log(rate)
|
|
20
|
-
// })
|