@icgio/icg-utils 1.9.57 → 1.9.58

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 CHANGED
@@ -505,6 +505,11 @@ function merge_trades_with_same_time(array) {
505
505
  }
506
506
 
507
507
  function merge_trades_with_same_id(array) {
508
+ let sum_numeric_fee_amount = (left, right) => {
509
+ const left_num = parseFloat(left || 0)
510
+ const right_num = parseFloat(right || 0)
511
+ return (Number.isFinite(left_num) ? left_num : 0) + (Number.isFinite(right_num) ? right_num : 0)
512
+ }
508
513
  let get_fill_history = (trade) => {
509
514
  if (Array.isArray(trade.fill_history) && trade.fill_history.length > 0) {
510
515
  return trade.fill_history.map((fill) => Object.assign({}, fill))
@@ -550,7 +555,7 @@ function merge_trades_with_same_id(array) {
550
555
  trade.fees = Object.assign({}, to_compare_trade.fees)
551
556
  } else {
552
557
  for (const [cur, amount] of Object.entries(to_compare_trade.fees)) {
553
- trade.fees[cur] = (trade.fees[cur] || 0) + amount
558
+ trade.fees[cur] = sum_numeric_fee_amount(trade.fees[cur], amount)
554
559
  }
555
560
  }
556
561
  }
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@icgio/icg-utils",
3
- "version": "1.9.57",
3
+ "version": "1.9.58",
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,57 @@
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(merged[0].fill_history.length, 2, 'preserves both fills in fill_history')
53
+ eq(merged[0].close_time.toISOString(), '2026-04-12T11:57:31.380Z', 'keeps latest close_time')
54
+ eq(merged[0].trade_id, undefined, 'strips fill ids from merged trade')
55
+
56
+ console.log(`pass=${pass} fail=${fail}`)
57
+ if (fail > 0) process.exit(1)