@icgio/icg-exchanges-wrapper 1.14.231 → 1.14.232

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.
@@ -84,6 +84,21 @@ class AllOrdersStore {
84
84
  }
85
85
  }
86
86
 
87
+ /**
88
+ * Remove all rows for a specific exchange/pair (fresh start).
89
+ * @param {string} exchange
90
+ * @param {string} pair
91
+ */
92
+ async clear_orders(exchange, pair) {
93
+ if (!this.enabled || !this.table_ready) return
94
+ const client = await this.pool.connect()
95
+ try {
96
+ await client.query('DELETE FROM all_orders WHERE exchange = $1 AND pair = $2', [exchange, pair])
97
+ } finally {
98
+ client.release()
99
+ }
100
+ }
101
+
87
102
  start_flush_interval() {
88
103
  if (this.flush_interval) return
89
104
 
@@ -179,28 +194,38 @@ class AllOrdersStore {
179
194
  updated_at = (NOW() AT TIME ZONE 'UTC')
180
195
  `
181
196
 
197
+ const delete_all_orders_query = `
198
+ DELETE FROM all_orders
199
+ WHERE exchange = $1 AND pair = $2 AND type = $3 AND order_id = $4
200
+ `
201
+
182
202
  const client = await this.pool.connect()
183
203
  try {
184
204
  for (const record of batch) {
185
- // Always persist in all_orders
186
- await client.query(upsert_all_orders_query, [
187
- record.id,
188
- record.exchange,
189
- record.pair,
190
- record.type,
191
- record.order_id,
192
- record.order_type,
193
- record.status,
194
- record.price,
195
- record.amount,
196
- record.f_amount,
197
- record.f_percentage,
198
- record.t_amount,
199
- record.fees,
200
- record.open_time,
201
- record.close_time,
202
- record.note,
203
- ])
205
+ if (record.status === 'DELETED') {
206
+ // Physically delete rows for deleted orders to limit DB size
207
+ await client.query(delete_all_orders_query, [record.exchange, record.pair, record.type, record.order_id])
208
+ } else {
209
+ // Upsert active / updated orders
210
+ await client.query(upsert_all_orders_query, [
211
+ record.id,
212
+ record.exchange,
213
+ record.pair,
214
+ record.type,
215
+ record.order_id,
216
+ record.order_type,
217
+ record.status,
218
+ record.price,
219
+ record.amount,
220
+ record.f_amount,
221
+ record.f_percentage,
222
+ record.t_amount,
223
+ record.fees,
224
+ record.open_time,
225
+ record.close_time,
226
+ record.note,
227
+ ])
228
+ }
204
229
  }
205
230
  } catch (err) {
206
231
  // Re-queue failed records
@@ -886,6 +886,8 @@ module.exports = class Orders {
886
886
  }
887
887
  // cancel previous orders
888
888
  else if (this.cancel_previous_orders && !this.previous_orders_canceled) {
889
+ // remove all previous orders for this exchange/pair from all_orders (fire-and-forget)
890
+ this.all_orders_store.clear_orders(exchange, pair).catch((err) => console.error('clear_all_orders error', exchange, pair, err.message))
889
891
  if (this.cancel_previous_orders_with_delay) {
890
892
  setTimeout(() => {
891
893
  orders.map((order) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@icgio/icg-exchanges-wrapper",
3
- "version": "1.14.231",
3
+ "version": "1.14.232",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {