@icgio/icg-utils 1.9.58 → 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 CHANGED
@@ -505,14 +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
+ }
508
516
  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)
517
+ return normalize_fee_amount(left) + normalize_fee_amount(right)
512
518
  }
513
519
  let get_fill_history = (trade) => {
514
520
  if (Array.isArray(trade.fill_history) && trade.fill_history.length > 0) {
515
- return trade.fill_history.map((fill) => Object.assign({}, 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
+ })
516
526
  }
517
527
  let fill = {
518
528
  price: parseFloat(trade.price),
@@ -522,7 +532,7 @@ function merge_trades_with_same_id(array) {
522
532
  let time = trade.close_time || trade.open_time
523
533
  if (time) fill.time = time
524
534
  if (trade.trade_id != null) fill.trade_id = String(trade.trade_id)
525
- if (trade.fees && typeof trade.fees === 'object') fill.fees = Object.assign({}, trade.fees)
535
+ if (trade.fees && typeof trade.fees === 'object') fill.fees = normalize_fee_map(trade.fees)
526
536
  if (trade.role != null) fill.role = trade.role
527
537
  return [fill]
528
538
  }
@@ -532,6 +542,7 @@ function merge_trades_with_same_id(array) {
532
542
  }
533
543
 
534
544
  if (array.length === 1) {
545
+ if (array[0].fees && typeof array[0].fees === 'object') array[0].fees = normalize_fee_map(array[0].fees)
535
546
  array[0].fill_history = get_fill_history(array[0])
536
547
  strip_trade_fill_id(array[0])
537
548
  return array
@@ -541,6 +552,7 @@ function merge_trades_with_same_id(array) {
541
552
  for (let i = 0; i < array.length; i++) {
542
553
  if (!processed_index_set.has(i)) {
543
554
  let trade = array[i]
555
+ if (trade.fees && typeof trade.fees === 'object') trade.fees = normalize_fee_map(trade.fees)
544
556
  trade.fill_history = get_fill_history(trade)
545
557
  processed_index_set.add(i)
546
558
  for (let j = i + 1; j < array.length; j++) {
@@ -552,7 +564,7 @@ function merge_trades_with_same_id(array) {
552
564
  trade.fill_history = trade.fill_history.concat(get_fill_history(to_compare_trade))
553
565
  if (to_compare_trade.fees && typeof to_compare_trade.fees === 'object') {
554
566
  if (!trade.fees || typeof trade.fees !== 'object') {
555
- trade.fees = Object.assign({}, to_compare_trade.fees)
567
+ trade.fees = normalize_fee_map(to_compare_trade.fees)
556
568
  } else {
557
569
  for (const [cur, amount] of Object.entries(to_compare_trade.fees)) {
558
570
  trade.fees[cur] = sum_numeric_fee_amount(trade.fees[cur], amount)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@icgio/icg-utils",
3
- "version": "1.9.58",
3
+ "version": "1.9.59",
4
4
  "description": "Utils package.",
5
5
  "main": "index.js",
6
6
  "directories": {
@@ -49,9 +49,81 @@ eq(merged.length, 1, 'merges trades with same order_id')
49
49
  approx(merged[0].amount, 6385.7518, 1e-10, 'sums amount numerically')
50
50
  approx(merged[0].total, 21.59513992601, 1e-10, 'sums total numerically')
51
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')
52
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')
53
55
  eq(merged[0].close_time.toISOString(), '2026-04-12T11:57:31.380Z', 'keeps latest close_time')
54
56
  eq(merged[0].trade_id, undefined, 'strips fill ids from merged trade')
55
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
+
56
128
  console.log(`pass=${pass} fail=${fail}`)
57
129
  if (fail > 0) process.exit(1)