@icgio/icg-utils 1.9.57 → 1.9.59
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/utils.js +21 -4
- package/package.json +2 -2
- package/test/merge-trades.test.js +129 -0
package/lib/utils.js
CHANGED
|
@@ -505,9 +505,24 @@ function merge_trades_with_same_time(array) {
|
|
|
505
505
|
}
|
|
506
506
|
|
|
507
507
|
function merge_trades_with_same_id(array) {
|
|
508
|
+
let normalize_fee_amount = (value) => {
|
|
509
|
+
const normalized = parseFloat(value || 0)
|
|
510
|
+
return Number.isFinite(normalized) ? normalized : 0
|
|
511
|
+
}
|
|
512
|
+
let normalize_fee_map = (fees) => {
|
|
513
|
+
if (!fees || typeof fees !== 'object') return fees
|
|
514
|
+
return Object.fromEntries(Object.entries(fees).map(([cur, amount]) => [cur, normalize_fee_amount(amount)]))
|
|
515
|
+
}
|
|
516
|
+
let sum_numeric_fee_amount = (left, right) => {
|
|
517
|
+
return normalize_fee_amount(left) + normalize_fee_amount(right)
|
|
518
|
+
}
|
|
508
519
|
let get_fill_history = (trade) => {
|
|
509
520
|
if (Array.isArray(trade.fill_history) && trade.fill_history.length > 0) {
|
|
510
|
-
return trade.fill_history.map((fill) =>
|
|
521
|
+
return trade.fill_history.map((fill) => {
|
|
522
|
+
let normalized_fill = Object.assign({}, fill)
|
|
523
|
+
if (normalized_fill.fees && typeof normalized_fill.fees === 'object') normalized_fill.fees = normalize_fee_map(normalized_fill.fees)
|
|
524
|
+
return normalized_fill
|
|
525
|
+
})
|
|
511
526
|
}
|
|
512
527
|
let fill = {
|
|
513
528
|
price: parseFloat(trade.price),
|
|
@@ -517,7 +532,7 @@ function merge_trades_with_same_id(array) {
|
|
|
517
532
|
let time = trade.close_time || trade.open_time
|
|
518
533
|
if (time) fill.time = time
|
|
519
534
|
if (trade.trade_id != null) fill.trade_id = String(trade.trade_id)
|
|
520
|
-
if (trade.fees && typeof trade.fees === 'object') fill.fees =
|
|
535
|
+
if (trade.fees && typeof trade.fees === 'object') fill.fees = normalize_fee_map(trade.fees)
|
|
521
536
|
if (trade.role != null) fill.role = trade.role
|
|
522
537
|
return [fill]
|
|
523
538
|
}
|
|
@@ -527,6 +542,7 @@ function merge_trades_with_same_id(array) {
|
|
|
527
542
|
}
|
|
528
543
|
|
|
529
544
|
if (array.length === 1) {
|
|
545
|
+
if (array[0].fees && typeof array[0].fees === 'object') array[0].fees = normalize_fee_map(array[0].fees)
|
|
530
546
|
array[0].fill_history = get_fill_history(array[0])
|
|
531
547
|
strip_trade_fill_id(array[0])
|
|
532
548
|
return array
|
|
@@ -536,6 +552,7 @@ function merge_trades_with_same_id(array) {
|
|
|
536
552
|
for (let i = 0; i < array.length; i++) {
|
|
537
553
|
if (!processed_index_set.has(i)) {
|
|
538
554
|
let trade = array[i]
|
|
555
|
+
if (trade.fees && typeof trade.fees === 'object') trade.fees = normalize_fee_map(trade.fees)
|
|
539
556
|
trade.fill_history = get_fill_history(trade)
|
|
540
557
|
processed_index_set.add(i)
|
|
541
558
|
for (let j = i + 1; j < array.length; j++) {
|
|
@@ -547,10 +564,10 @@ function merge_trades_with_same_id(array) {
|
|
|
547
564
|
trade.fill_history = trade.fill_history.concat(get_fill_history(to_compare_trade))
|
|
548
565
|
if (to_compare_trade.fees && typeof to_compare_trade.fees === 'object') {
|
|
549
566
|
if (!trade.fees || typeof trade.fees !== 'object') {
|
|
550
|
-
trade.fees =
|
|
567
|
+
trade.fees = normalize_fee_map(to_compare_trade.fees)
|
|
551
568
|
} else {
|
|
552
569
|
for (const [cur, amount] of Object.entries(to_compare_trade.fees)) {
|
|
553
|
-
trade.fees[cur] = (trade.fees[cur]
|
|
570
|
+
trade.fees[cur] = sum_numeric_fee_amount(trade.fees[cur], amount)
|
|
554
571
|
}
|
|
555
572
|
}
|
|
556
573
|
}
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@icgio/icg-utils",
|
|
3
|
-
"version": "1.9.
|
|
3
|
+
"version": "1.9.59",
|
|
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"
|
|
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"
|
|
11
11
|
},
|
|
12
12
|
"repository": {
|
|
13
13
|
"type": "git",
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
const { merge_trades_with_same_id } = require('../lib/utils.js')
|
|
3
|
+
|
|
4
|
+
let pass = 0, fail = 0
|
|
5
|
+
function assert(condition, msg) {
|
|
6
|
+
if (condition) { pass++ }
|
|
7
|
+
else { fail++; console.log(' FAIL:', msg) }
|
|
8
|
+
}
|
|
9
|
+
function eq(a, b, msg) {
|
|
10
|
+
const as = JSON.stringify(a), bs = JSON.stringify(b)
|
|
11
|
+
if (as === bs) { pass++ }
|
|
12
|
+
else { fail++; console.log(' FAIL:', msg, '— got', as, 'expected', bs) }
|
|
13
|
+
}
|
|
14
|
+
function approx(a, b, epsilon, msg) {
|
|
15
|
+
if (Math.abs(a - b) <= epsilon) { pass++ }
|
|
16
|
+
else { fail++; console.log(' FAIL:', msg, '— got', a, 'expected', b) }
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
console.log('--- merge_trades_with_same_id ---')
|
|
20
|
+
|
|
21
|
+
const merged = merge_trades_with_same_id([
|
|
22
|
+
{
|
|
23
|
+
order_id: 'order-1',
|
|
24
|
+
trade_id: 'fill-1',
|
|
25
|
+
pair: 'SWFTCUSDT',
|
|
26
|
+
type: 'buy',
|
|
27
|
+
price: 0.0033818,
|
|
28
|
+
amount: 4430.6295,
|
|
29
|
+
total: 14.9835028431,
|
|
30
|
+
open_time: new Date('2026-04-12T11:57:17.154Z'),
|
|
31
|
+
close_time: new Date('2026-04-12T11:57:17.154Z'),
|
|
32
|
+
fees: { USDT: '0.0299670056862' },
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
order_id: 'order-1',
|
|
36
|
+
trade_id: 'fill-2',
|
|
37
|
+
pair: 'SWFTCUSDT',
|
|
38
|
+
type: 'buy',
|
|
39
|
+
price: 0.0033818,
|
|
40
|
+
amount: 1955.1223,
|
|
41
|
+
total: 6.61163708291,
|
|
42
|
+
open_time: new Date('2026-04-12T11:57:17.537Z'),
|
|
43
|
+
close_time: new Date('2026-04-12T11:57:31.380Z'),
|
|
44
|
+
fees: { USDT: '0.01322327416382' },
|
|
45
|
+
},
|
|
46
|
+
])
|
|
47
|
+
|
|
48
|
+
eq(merged.length, 1, 'merges trades with same order_id')
|
|
49
|
+
approx(merged[0].amount, 6385.7518, 1e-10, 'sums amount numerically')
|
|
50
|
+
approx(merged[0].total, 21.59513992601, 1e-10, 'sums total numerically')
|
|
51
|
+
approx(merged[0].fees.USDT, 0.04319027985002, 1e-14, 'sums string fees numerically')
|
|
52
|
+
eq(typeof merged[0].fees.USDT, 'number', 'normalizes merged fee type to number')
|
|
53
|
+
eq(merged[0].fill_history.length, 2, 'preserves both fills in fill_history')
|
|
54
|
+
eq(typeof merged[0].fill_history[0].fees.USDT, 'number', 'normalizes fill_history fee type to number')
|
|
55
|
+
eq(merged[0].close_time.toISOString(), '2026-04-12T11:57:31.380Z', 'keeps latest close_time')
|
|
56
|
+
eq(merged[0].trade_id, undefined, 'strips fill ids from merged trade')
|
|
57
|
+
|
|
58
|
+
const disjointFees = merge_trades_with_same_id([
|
|
59
|
+
{
|
|
60
|
+
order_id: 'order-2',
|
|
61
|
+
trade_id: 'fill-1',
|
|
62
|
+
pair: 'SWFTCUSDT',
|
|
63
|
+
type: 'buy',
|
|
64
|
+
price: 1,
|
|
65
|
+
amount: 1,
|
|
66
|
+
total: 1,
|
|
67
|
+
fees: { USDT: '0.1' },
|
|
68
|
+
},
|
|
69
|
+
{
|
|
70
|
+
order_id: 'order-2',
|
|
71
|
+
trade_id: 'fill-2',
|
|
72
|
+
pair: 'SWFTCUSDT',
|
|
73
|
+
type: 'buy',
|
|
74
|
+
price: 1,
|
|
75
|
+
amount: 2,
|
|
76
|
+
total: 2,
|
|
77
|
+
fees: { BTC: '0.2' },
|
|
78
|
+
},
|
|
79
|
+
])
|
|
80
|
+
|
|
81
|
+
approx(disjointFees[0].fees.USDT, 0.1, 1e-14, 'preserves first copied fee numerically')
|
|
82
|
+
approx(disjointFees[0].fees.BTC, 0.2, 1e-14, 'preserves second copied fee numerically')
|
|
83
|
+
eq(typeof disjointFees[0].fees.USDT, 'number', 'normalizes first copied fee type to number')
|
|
84
|
+
eq(typeof disjointFees[0].fees.BTC, 'number', 'normalizes second copied fee type to number')
|
|
85
|
+
|
|
86
|
+
const copiedFees = merge_trades_with_same_id([
|
|
87
|
+
{
|
|
88
|
+
order_id: 'order-3',
|
|
89
|
+
trade_id: 'fill-1',
|
|
90
|
+
pair: 'SWFTCUSDT',
|
|
91
|
+
type: 'buy',
|
|
92
|
+
price: 1,
|
|
93
|
+
amount: 1,
|
|
94
|
+
total: 1,
|
|
95
|
+
},
|
|
96
|
+
{
|
|
97
|
+
order_id: 'order-3',
|
|
98
|
+
trade_id: 'fill-2',
|
|
99
|
+
pair: 'SWFTCUSDT',
|
|
100
|
+
type: 'buy',
|
|
101
|
+
price: 1,
|
|
102
|
+
amount: 2,
|
|
103
|
+
total: 2,
|
|
104
|
+
fees: { USDT: '0.3' },
|
|
105
|
+
},
|
|
106
|
+
])
|
|
107
|
+
|
|
108
|
+
approx(copiedFees[0].fees.USDT, 0.3, 1e-14, 'normalizes fee map when copied from later fill')
|
|
109
|
+
eq(typeof copiedFees[0].fees.USDT, 'number', 'normalizes copied later-fill fee type to number')
|
|
110
|
+
|
|
111
|
+
const singleTrade = merge_trades_with_same_id([
|
|
112
|
+
{
|
|
113
|
+
order_id: 'order-4',
|
|
114
|
+
trade_id: 'fill-1',
|
|
115
|
+
pair: 'SWFTCUSDT',
|
|
116
|
+
type: 'buy',
|
|
117
|
+
price: 1,
|
|
118
|
+
amount: 1,
|
|
119
|
+
total: 1,
|
|
120
|
+
fees: { USDT: '0.4' },
|
|
121
|
+
},
|
|
122
|
+
])
|
|
123
|
+
|
|
124
|
+
approx(singleTrade[0].fees.USDT, 0.4, 1e-14, 'normalizes fee map for single trade result')
|
|
125
|
+
eq(typeof singleTrade[0].fees.USDT, 'number', 'normalizes single trade fee type to number')
|
|
126
|
+
eq(typeof singleTrade[0].fill_history[0].fees.USDT, 'number', 'normalizes single trade fill_history fee type to number')
|
|
127
|
+
|
|
128
|
+
console.log(`pass=${pass} fail=${fail}`)
|
|
129
|
+
if (fail > 0) process.exit(1)
|