@icgio/icg-utils 1.9.58 → 1.9.60
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 +18 -43
- package/package.json +1 -1
- package/test/merge-trades.test.js +72 -0
package/lib/utils.js
CHANGED
|
@@ -47,48 +47,11 @@ Number.prototype.toStringNoExponents = function () {
|
|
|
47
47
|
return str + z
|
|
48
48
|
}
|
|
49
49
|
|
|
50
|
-
Date.prototype.addHours = function (h) {
|
|
51
|
-
return new Date(this.getTime() + h * 60 * 60 * 1000)
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
Date.prototype.roundHours = function (m = 1) {
|
|
55
|
-
let coeff = m * 60 * 60 * 1000
|
|
56
|
-
return new Date(Math.round(this.getTime() / coeff) * coeff)
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
Date.prototype.ceilHours = function (m = 1) {
|
|
60
|
-
let coeff = m * 60 * 60 * 1000
|
|
61
|
-
return new Date(Math.ceil(this.getTime() / coeff) * coeff)
|
|
62
|
-
}
|
|
63
|
-
|
|
64
50
|
Date.prototype.floorHours = function (m = 1) {
|
|
65
51
|
let coeff = m * 60 * 60 * 1000
|
|
66
52
|
return new Date(Math.floor(this.getTime() / coeff) * coeff)
|
|
67
53
|
}
|
|
68
54
|
|
|
69
|
-
Date.prototype.addMinutes = function (m) {
|
|
70
|
-
return new Date(this.getTime() + m * 60 * 1000)
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
Date.prototype.roundMinutes = function (m = 1) {
|
|
74
|
-
let coeff = m * 60 * 1000
|
|
75
|
-
return new Date(Math.round(this.getTime() / coeff) * coeff)
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
Date.prototype.ceilMinutes = function (m = 1) {
|
|
79
|
-
let coeff = m * 60 * 1000
|
|
80
|
-
return new Date(Math.ceil(this.getTime() / coeff) * coeff)
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
Date.prototype.floorMinutes = function (m = 1) {
|
|
84
|
-
let coeff = m * 60 * 1000
|
|
85
|
-
return new Date(Math.floor(this.getTime() / coeff) * coeff)
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
Date.prototype.addSeconds = function (s) {
|
|
89
|
-
return new Date(this.getTime() + s * 1000)
|
|
90
|
-
}
|
|
91
|
-
|
|
92
55
|
String.prototype.replaceAt = function (index, replacement) {
|
|
93
56
|
return this.substr(0, index) + replacement + this.substr(index + replacement.length)
|
|
94
57
|
}
|
|
@@ -505,14 +468,24 @@ function merge_trades_with_same_time(array) {
|
|
|
505
468
|
}
|
|
506
469
|
|
|
507
470
|
function merge_trades_with_same_id(array) {
|
|
471
|
+
let normalize_fee_amount = (value) => {
|
|
472
|
+
const normalized = parseFloat(value || 0)
|
|
473
|
+
return Number.isFinite(normalized) ? normalized : 0
|
|
474
|
+
}
|
|
475
|
+
let normalize_fee_map = (fees) => {
|
|
476
|
+
if (!fees || typeof fees !== 'object') return fees
|
|
477
|
+
return Object.fromEntries(Object.entries(fees).map(([cur, amount]) => [cur, normalize_fee_amount(amount)]))
|
|
478
|
+
}
|
|
508
479
|
let sum_numeric_fee_amount = (left, right) => {
|
|
509
|
-
|
|
510
|
-
const right_num = parseFloat(right || 0)
|
|
511
|
-
return (Number.isFinite(left_num) ? left_num : 0) + (Number.isFinite(right_num) ? right_num : 0)
|
|
480
|
+
return normalize_fee_amount(left) + normalize_fee_amount(right)
|
|
512
481
|
}
|
|
513
482
|
let get_fill_history = (trade) => {
|
|
514
483
|
if (Array.isArray(trade.fill_history) && trade.fill_history.length > 0) {
|
|
515
|
-
return trade.fill_history.map((fill) =>
|
|
484
|
+
return trade.fill_history.map((fill) => {
|
|
485
|
+
let normalized_fill = Object.assign({}, fill)
|
|
486
|
+
if (normalized_fill.fees && typeof normalized_fill.fees === 'object') normalized_fill.fees = normalize_fee_map(normalized_fill.fees)
|
|
487
|
+
return normalized_fill
|
|
488
|
+
})
|
|
516
489
|
}
|
|
517
490
|
let fill = {
|
|
518
491
|
price: parseFloat(trade.price),
|
|
@@ -522,7 +495,7 @@ function merge_trades_with_same_id(array) {
|
|
|
522
495
|
let time = trade.close_time || trade.open_time
|
|
523
496
|
if (time) fill.time = time
|
|
524
497
|
if (trade.trade_id != null) fill.trade_id = String(trade.trade_id)
|
|
525
|
-
if (trade.fees && typeof trade.fees === 'object') fill.fees =
|
|
498
|
+
if (trade.fees && typeof trade.fees === 'object') fill.fees = normalize_fee_map(trade.fees)
|
|
526
499
|
if (trade.role != null) fill.role = trade.role
|
|
527
500
|
return [fill]
|
|
528
501
|
}
|
|
@@ -532,6 +505,7 @@ function merge_trades_with_same_id(array) {
|
|
|
532
505
|
}
|
|
533
506
|
|
|
534
507
|
if (array.length === 1) {
|
|
508
|
+
if (array[0].fees && typeof array[0].fees === 'object') array[0].fees = normalize_fee_map(array[0].fees)
|
|
535
509
|
array[0].fill_history = get_fill_history(array[0])
|
|
536
510
|
strip_trade_fill_id(array[0])
|
|
537
511
|
return array
|
|
@@ -541,6 +515,7 @@ function merge_trades_with_same_id(array) {
|
|
|
541
515
|
for (let i = 0; i < array.length; i++) {
|
|
542
516
|
if (!processed_index_set.has(i)) {
|
|
543
517
|
let trade = array[i]
|
|
518
|
+
if (trade.fees && typeof trade.fees === 'object') trade.fees = normalize_fee_map(trade.fees)
|
|
544
519
|
trade.fill_history = get_fill_history(trade)
|
|
545
520
|
processed_index_set.add(i)
|
|
546
521
|
for (let j = i + 1; j < array.length; j++) {
|
|
@@ -552,7 +527,7 @@ function merge_trades_with_same_id(array) {
|
|
|
552
527
|
trade.fill_history = trade.fill_history.concat(get_fill_history(to_compare_trade))
|
|
553
528
|
if (to_compare_trade.fees && typeof to_compare_trade.fees === 'object') {
|
|
554
529
|
if (!trade.fees || typeof trade.fees !== 'object') {
|
|
555
|
-
trade.fees =
|
|
530
|
+
trade.fees = normalize_fee_map(to_compare_trade.fees)
|
|
556
531
|
} else {
|
|
557
532
|
for (const [cur, amount] of Object.entries(to_compare_trade.fees)) {
|
|
558
533
|
trade.fees[cur] = sum_numeric_fee_amount(trade.fees[cur], amount)
|
package/package.json
CHANGED
|
@@ -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)
|