@icgio/icg-exchanges 1.40.44 → 1.40.45

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.
@@ -1153,7 +1153,19 @@ module.exports = class Coinbase extends ExchangeBase {
1153
1153
  return
1154
1154
  }
1155
1155
  if (this.fix_ord && this.fix_ord.is_ready()) {
1156
- this.fix_ord.send_new_order(pair, method, price, amount, order_options, cb)
1156
+ const normalized = this.normalize_order_options(order_options)
1157
+ const t_order_sent = process.hrtime()
1158
+ if (normalized.on_sent) {
1159
+ normalized.on_sent({ t_order_sent })
1160
+ }
1161
+ this.fix_ord.send_new_order(pair, method, price, amount, normalized.order_options, (res = {}) => {
1162
+ res.meta = {
1163
+ ...(res.meta || {}),
1164
+ t_order_sent,
1165
+ t_ack_recv: process.hrtime(),
1166
+ }
1167
+ cb(res)
1168
+ })
1157
1169
  return
1158
1170
  }
1159
1171
  this.private_limiter.process(this.limit_trade_base.bind(this), pair, method, price, amount, order_options, cb)
@@ -1198,7 +1210,15 @@ module.exports = class Coinbase extends ExchangeBase {
1198
1210
  }
1199
1211
  cancel_order_by_order(order, cb) {
1200
1212
  if (this.fix_ord && this.fix_ord.is_ready()) {
1201
- this.fix_ord.send_cancel(order.pair, order.order_id, order.type, cb)
1213
+ const t_cancel_sent = process.hrtime()
1214
+ this.fix_ord.send_cancel(order.pair, order.order_id, order.type, (res = {}) => {
1215
+ res.meta = {
1216
+ ...(res.meta || {}),
1217
+ t_cancel_sent,
1218
+ t_cancel_ack: process.hrtime(),
1219
+ }
1220
+ cb(res)
1221
+ })
1202
1222
  return
1203
1223
  }
1204
1224
  this.cancel_order(order.pair, order.order_id, cb)
@@ -154,6 +154,59 @@ class ExchangeBase {
154
154
  }
155
155
  /** @type {boolean} Whether deposits/withdrawals go directly to the spot account */
156
156
  this.deposits_to_spot = true
157
+ this._wrap_transport_methods()
158
+ }
159
+
160
+ _wrap_transport_methods() {
161
+ if (typeof this.limit_trade_base === 'function') {
162
+ const original_limit_trade_base = this.limit_trade_base.bind(this)
163
+ this.limit_trade_base = (pair, method, price, amount, order_options, cb) => {
164
+ if (typeof order_options === 'function') {
165
+ cb = order_options
166
+ order_options = {}
167
+ }
168
+ const normalized = this.normalize_order_options(order_options)
169
+ if (parseFloat(amount) === 0) {
170
+ return original_limit_trade_base(pair, method, price, amount, normalized.order_options, cb)
171
+ }
172
+ const t_order_sent = process.hrtime()
173
+ if (normalized.on_sent) {
174
+ normalized.on_sent({ t_order_sent })
175
+ }
176
+ return original_limit_trade_base(pair, method, price, amount, normalized.order_options, (res = {}) => {
177
+ res.meta = {
178
+ ...(res.meta || {}),
179
+ t_order_sent,
180
+ t_ack_recv: process.hrtime(),
181
+ }
182
+ cb(res)
183
+ })
184
+ }
185
+ }
186
+
187
+ if (typeof this.cancel_order === 'function' && this.cancel_order !== ExchangeBase.prototype.cancel_order) {
188
+ const original_cancel_order = this.cancel_order.bind(this)
189
+ this.cancel_order = (pair, order_id, cb) => {
190
+ const t_cancel_sent = process.hrtime()
191
+ return original_cancel_order(pair, order_id, (res = {}) => {
192
+ res.meta = {
193
+ ...(res.meta || {}),
194
+ t_cancel_sent,
195
+ t_cancel_ack: process.hrtime(),
196
+ }
197
+ cb(res)
198
+ })
199
+ }
200
+ }
201
+
202
+ if (typeof this.rate_ws === 'function' && this.rate_ws !== ExchangeBase.prototype.rate_ws) {
203
+ const original_rate_ws = this.rate_ws.bind(this)
204
+ this.rate_ws = (pair, cb) => {
205
+ original_rate_ws(pair, (res) => {
206
+ cb(this.attach_market_ts(pair, res))
207
+ })
208
+ }
209
+ }
157
210
  }
158
211
 
159
212
  get api_secret_key() {
@@ -165,6 +218,45 @@ class ExchangeBase {
165
218
  return key
166
219
  }
167
220
 
221
+ normalize_order_options(order_options) {
222
+ if (!order_options || typeof order_options !== 'object') {
223
+ return { order_options: {}, on_sent: null }
224
+ }
225
+ const normalized = { ...order_options }
226
+ const on_sent = typeof normalized.on_sent === 'function' ? normalized.on_sent : null
227
+ delete normalized.on_sent
228
+ return { order_options: normalized, on_sent }
229
+ }
230
+
231
+ snapshot_market_ts(pair) {
232
+ const ticker_ts = _.get(this, ['ws', 'market', 'tickers', pair, '_ts'])
233
+ if (ticker_ts && typeof ticker_ts === 'object') {
234
+ return { ...ticker_ts }
235
+ }
236
+ const ts = _.get(this, ['ws', 'market', 'ts_dict', pair])
237
+ if (ts && typeof ts === 'object') {
238
+ return { ...ts }
239
+ }
240
+ return undefined
241
+ }
242
+
243
+ attach_market_ts(pair, res) {
244
+ if (!res || !res.success || !res.body || typeof res.body !== 'object') {
245
+ return res
246
+ }
247
+ const market_ts = this.snapshot_market_ts(pair)
248
+ if (!market_ts) {
249
+ return res
250
+ }
251
+ return {
252
+ ...res,
253
+ body: {
254
+ ...res.body,
255
+ _ts: res.body._ts && typeof res.body._ts === 'object' ? { ...res.body._ts } : market_ts,
256
+ },
257
+ }
258
+ }
259
+
168
260
  /**
169
261
  * Normalize a timestamp-like exchange value into a valid Date instance.
170
262
  * @param {Date|string|number|null|undefined} value
@@ -520,12 +612,12 @@ class ExchangeBase {
520
612
  bbo_ws(pair, cb) {
521
613
  const t = this.ws.market.tickers[pair]
522
614
  if (t && t.bid > 0 && t.ask > 0) {
523
- cb({ success: true, body: { asks: [[t.ask, t.ask_size]], bids: [[t.bid, t.bid_size]] } })
615
+ cb({ success: true, body: { asks: [[t.ask, t.ask_size]], bids: [[t.bid, t.bid_size]], _ts: t._ts || this.snapshot_market_ts(pair) } })
524
616
  } else {
525
617
  // Fallback: derive from full orderbook
526
618
  this.rate_ws(pair, (res) => {
527
619
  if (res.success && res.body.bids.length > 0 && res.body.asks.length > 0) {
528
- cb({ success: true, body: { asks: [res.body.asks[0]], bids: [res.body.bids[0]] } })
620
+ cb({ success: true, body: { asks: [res.body.asks[0]], bids: [res.body.bids[0]], _ts: res.body._ts || this.snapshot_market_ts(pair) } })
529
621
  } else {
530
622
  cb(res)
531
623
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@icgio/icg-exchanges",
3
- "version": "1.40.44",
3
+ "version": "1.40.45",
4
4
  "description": "icgio exchanges package",
5
5
  "main": "./exchanges.js",
6
6
  "scripts": {