@icgio/icg-utils 1.9.51 → 1.9.53
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/rate-limiter.js +43 -8
- package/lib/utils.js +0 -114
- package/package.json +2 -2
- package/test/rate-limiter.test.js +60 -0
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/utils.js
CHANGED
|
@@ -543,119 +543,6 @@ function combine_orderbooks(orderbooks_dict, length = undefined) {
|
|
|
543
543
|
// )
|
|
544
544
|
// console.log(combine_orderbooks({}, 100))
|
|
545
545
|
|
|
546
|
-
function arb_opportunities_from_combined_orderbook(orderbook_combined, profit_percentage = 0, amount_cap = 0, quote_amount_cap = 0) {
|
|
547
|
-
let i = 0,
|
|
548
|
-
j = 0,
|
|
549
|
-
ask_amount = 0,
|
|
550
|
-
bid_amount = 0
|
|
551
|
-
let asks = orderbook_combined.asks,
|
|
552
|
-
bids = orderbook_combined.bids
|
|
553
|
-
if (asks[0][0] * (1 + profit_percentage) >= bids[0][0]) return {}
|
|
554
|
-
let last_step = ''
|
|
555
|
-
while (i < asks.length && j < bids.length) {
|
|
556
|
-
if (asks[i][0] * (1 + profit_percentage) < bids[j][0]) {
|
|
557
|
-
if (ask_amount === 0 && bid_amount === 0) {
|
|
558
|
-
;((ask_amount = asks[i][1]), (bid_amount = bids[j][1]))
|
|
559
|
-
} else if (ask_amount <= bid_amount) {
|
|
560
|
-
i++
|
|
561
|
-
ask_amount += asks[i][1]
|
|
562
|
-
last_step = 'ask'
|
|
563
|
-
} else {
|
|
564
|
-
j++
|
|
565
|
-
bid_amount += bids[j][1]
|
|
566
|
-
last_step = 'bid'
|
|
567
|
-
}
|
|
568
|
-
} else {
|
|
569
|
-
if (last_step === 'ask') {
|
|
570
|
-
ask_amount -= asks[i][1]
|
|
571
|
-
i--
|
|
572
|
-
} else if (last_step === 'bid') {
|
|
573
|
-
bid_amount -= bids[j][1]
|
|
574
|
-
j--
|
|
575
|
-
}
|
|
576
|
-
break
|
|
577
|
-
}
|
|
578
|
-
}
|
|
579
|
-
let opportunities = {},
|
|
580
|
-
diff = ask_amount - bid_amount
|
|
581
|
-
asks = asks.slice(0, i + 1)
|
|
582
|
-
bids = bids.slice(0, j + 1)
|
|
583
|
-
if (diff > 0) {
|
|
584
|
-
asks[i][1] -= diff
|
|
585
|
-
} else {
|
|
586
|
-
bids[j][1] += diff
|
|
587
|
-
}
|
|
588
|
-
asks.map((ask) => {
|
|
589
|
-
if (!opportunities[ask[2]]) {
|
|
590
|
-
opportunities[ask[2]] = {
|
|
591
|
-
price: 0,
|
|
592
|
-
amount: 0,
|
|
593
|
-
exchange: ask[2],
|
|
594
|
-
method: 'buy',
|
|
595
|
-
}
|
|
596
|
-
}
|
|
597
|
-
opportunities[ask[2]].avg_price = (opportunities[ask[2]].price * opportunities[ask[2]].amount + ask[0] * ask[1]) / (opportunities[ask[2]].amount + ask[1])
|
|
598
|
-
opportunities[ask[2]].price = ask[0]
|
|
599
|
-
opportunities[ask[2]].amount += ask[1]
|
|
600
|
-
opportunities[ask[2]].total = opportunities[ask[2]].avg_price * opportunities[ask[2]].amount
|
|
601
|
-
})
|
|
602
|
-
bids.map((bid) => {
|
|
603
|
-
if (!opportunities[bid[2]]) {
|
|
604
|
-
opportunities[bid[2]] = {
|
|
605
|
-
price: 0,
|
|
606
|
-
amount: 0,
|
|
607
|
-
exchange: bid[2],
|
|
608
|
-
method: 'sell',
|
|
609
|
-
}
|
|
610
|
-
}
|
|
611
|
-
opportunities[bid[2]].avg_price = (opportunities[bid[2]].price * opportunities[bid[2]].amount + bid[0] * bid[1]) / (opportunities[bid[2]].amount + bid[1])
|
|
612
|
-
opportunities[bid[2]].price = bid[0]
|
|
613
|
-
opportunities[bid[2]].amount += bid[1]
|
|
614
|
-
opportunities[bid[2]].total = opportunities[bid[2]].avg_price * opportunities[bid[2]].amount
|
|
615
|
-
})
|
|
616
|
-
|
|
617
|
-
// Apply amount caps if needed
|
|
618
|
-
let amount_total = _.sumBy(_.values(opportunities), 'amount')
|
|
619
|
-
let quote_amount_total = _.sumBy(_.values(opportunities), 'total')
|
|
620
|
-
let amount_coeff = 1
|
|
621
|
-
let quote_amount_coeff = 1
|
|
622
|
-
|
|
623
|
-
if (amount_total > amount_cap && amount_cap !== 0) {
|
|
624
|
-
amount_coeff = amount_cap / amount_total
|
|
625
|
-
}
|
|
626
|
-
|
|
627
|
-
if (quote_amount_total > quote_amount_cap && quote_amount_cap !== 0) {
|
|
628
|
-
quote_amount_coeff = quote_amount_cap / quote_amount_total
|
|
629
|
-
}
|
|
630
|
-
|
|
631
|
-
// Use the more restrictive coefficient
|
|
632
|
-
let final_coeff = Math.min(amount_coeff, quote_amount_coeff)
|
|
633
|
-
|
|
634
|
-
if (final_coeff < 1) {
|
|
635
|
-
for (let exchange in opportunities) {
|
|
636
|
-
let opportunity = opportunities[exchange]
|
|
637
|
-
opportunity.reduced_amount = opportunity.amount * final_coeff
|
|
638
|
-
opportunity.reduced_total = opportunity.total * final_coeff
|
|
639
|
-
opportunities[exchange] = opportunity
|
|
640
|
-
}
|
|
641
|
-
}
|
|
642
|
-
|
|
643
|
-
return opportunities
|
|
644
|
-
}
|
|
645
|
-
|
|
646
|
-
// console.log(arb_opportunities_from_combined_orderbook({
|
|
647
|
-
// asks: [ [ 0.3049, 150, 'bitget' ],
|
|
648
|
-
// [ 0.30791, 50.5556, 'bitget' ],
|
|
649
|
-
// [ 0.30792, 175.49623901, 'bitfinex' ],
|
|
650
|
-
// [ 0.30793, 86.89887994, 'bitfinex' ],
|
|
651
|
-
// [ 0.3081, 2.4526, 'huobi' ] ],
|
|
652
|
-
// bids: [ [ 0.3079, 100, 'huobi' ],
|
|
653
|
-
// [ 0.305, 100, 'huobi' ],
|
|
654
|
-
// [ 0.3049, 5956.5837, 'huobi' ],
|
|
655
|
-
// [ 0.3038, 4373.4668, 'huobi' ],
|
|
656
|
-
// [ 0.3027, 867.8638, 'huobi' ] ]
|
|
657
|
-
// }, 0.0004, amount_cap=1))
|
|
658
|
-
|
|
659
546
|
function vol_position_combined(input_array, vol_target) {
|
|
660
547
|
if (input_array.length === 1) {
|
|
661
548
|
return {
|
|
@@ -1073,7 +960,6 @@ module.exports = {
|
|
|
1073
960
|
sum_position_orderbook,
|
|
1074
961
|
vol_position_orderbook,
|
|
1075
962
|
combine_orderbooks,
|
|
1076
|
-
arb_opportunities_from_combined_orderbook,
|
|
1077
963
|
vol_position_combined,
|
|
1078
964
|
sort_dict,
|
|
1079
965
|
merge_trades_with_same_time,
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@icgio/icg-utils",
|
|
3
|
-
"version": "1.9.
|
|
3
|
+
"version": "1.9.53",
|
|
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,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
|
+
})
|