@icgio/icg-exchanges 1.40.47 → 1.40.48

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.
@@ -546,39 +546,64 @@ module.exports = class Kucoin extends ExchangeBase {
546
546
  }
547
547
  get_history_trades(pair, start_time_in_ms, end_time_in_ms, cb) {
548
548
  let get_history_trades_base = (pair, start_time_in_ms, end_time_in_ms, cb) => {
549
- let params = {
550
- symbol: pre_process_pair(pair),
551
- startAt: start_time_in_ms,
552
- endAt: end_time_in_ms,
553
- type: 'limit',
549
+ let page_size = 50
550
+ let build_trade = (fill) => {
551
+ return {
552
+ order_id: fill.orderId.toString(),
553
+ ...(fill.tradeId != null ? { trade_id: fill.tradeId.toString() } : {}),
554
+ pair,
555
+ type: fill.side,
556
+ price: parseFloat(fill.price),
557
+ amount: parseFloat(fill.size),
558
+ total: parseFloat(fill.price) * parseFloat(fill.size),
559
+ open_time: new Date(fill.createdAt),
560
+ fees: {
561
+ [post_process_cur(fill.feeCurrency)]: parseFloat(fill.fee),
562
+ },
563
+ }
554
564
  }
555
- let options = get_options(Date.now(), 'GET', '/api/v1/fills', params, this.api_secret_key, this.passphrase)
556
- needle.get('https://api.kucoin.com/api/v1/fills?' + qs.stringify(params), options, (err, res, body) => {
557
- if (body && body.code === '200000') {
558
- let trades = body.data.items.map((o) => {
559
- return {
560
- order_id: o.orderId.toString(),
561
- ...(o.tradeId != null ? { trade_id: o.tradeId.toString() } : {}),
562
- pair,
563
- type: o.side,
564
- price: parseFloat(o.price),
565
- amount: parseFloat(o.size),
566
- total: parseFloat(o.price) * parseFloat(o.size),
567
- open_time: new Date(o.createdAt),
568
- fees: {
569
- [post_process_cur(o.feeCurrency)]: parseFloat(o.fee),
570
- },
571
- }
572
- })
573
- cb({ success: true, body: trades })
574
- } else if (body && body.code === '429000') {
575
- cb({ success: false, error: error_codes.service_unavailable, body })
576
- } else if (body) {
577
- cb({ success: false, error: error_codes.other, body })
578
- } else {
579
- cb({ success: false, error: error_codes.timeout })
565
+ let dedupe_history_trades = (trades) => {
566
+ let by_fill_identity = new Map()
567
+ trades.forEach((trade) => {
568
+ let time = trade.open_time instanceof Date ? trade.open_time.getTime() : new Date(trade.open_time).getTime()
569
+ let fees = trade.fees && typeof trade.fees === 'object' ? JSON.stringify(trade.fees) : ''
570
+ let key =
571
+ trade.trade_id ||
572
+ [trade.order_id || '', pair, trade.type || '', trade.price || 0, trade.amount || 0, trade.total || 0, Number.isFinite(time) ? time : 0, fees].join('|')
573
+ if (!by_fill_identity.has(key)) by_fill_identity.set(key, trade)
574
+ })
575
+ return [...by_fill_identity.values()]
576
+ }
577
+ let fetch_page = (current_page, collected_trades) => {
578
+ let params = {
579
+ symbol: pre_process_pair(pair),
580
+ startAt: start_time_in_ms,
581
+ endAt: end_time_in_ms,
582
+ type: 'limit',
583
+ currentPage: current_page,
584
+ pageSize: page_size,
580
585
  }
581
- })
586
+ let options = get_options(Date.now(), 'GET', '/api/v1/fills', params, this.api_secret_key, this.passphrase)
587
+ needle.get('https://api.kucoin.com/api/v1/fills?' + qs.stringify(params), options, (err, res, body) => {
588
+ if (body && body.code === '200000' && body.data && Array.isArray(body.data.items)) {
589
+ let trades = body.data.items.map((o) => build_trade(o))
590
+ let all_trades = collected_trades.concat(trades)
591
+ let total_pages = Math.max(parseInt(body.data.totalPage || 1), current_page)
592
+ if (current_page < total_pages && trades.length > 0) {
593
+ fetch_page(current_page + 1, all_trades)
594
+ } else {
595
+ cb({ success: true, body: dedupe_history_trades(all_trades) })
596
+ }
597
+ } else if (body && body.code === '429000') {
598
+ cb({ success: false, error: error_codes.service_unavailable, body })
599
+ } else if (body) {
600
+ cb({ success: false, error: error_codes.other, body })
601
+ } else {
602
+ cb({ success: false, error: error_codes.timeout })
603
+ }
604
+ })
605
+ }
606
+ fetch_page(1, [])
582
607
  }
583
608
  this.private_limiter.process(get_history_trades_base, pair, start_time_in_ms, end_time_in_ms, cb)
584
609
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@icgio/icg-exchanges",
3
- "version": "1.40.47",
3
+ "version": "1.40.48",
4
4
  "description": "icgio exchanges package",
5
5
  "main": "./exchanges.js",
6
6
  "scripts": {