@ex-master/core
Advanced tools
Comparing version 0.1.67 to 0.1.68
@@ -61,2 +61,3 @@ /// <reference types="node" /> | ||
private balanceMap; | ||
private pendingOrderTaskMap; | ||
readonly balance$: Observable<Map<string, AccountBalance>>; | ||
@@ -63,0 +64,0 @@ readonly operationalBalance$: Observable<Map<string, Decimal>>; |
@@ -12,2 +12,3 @@ "use strict"; | ||
const debug = Debug('ex-master:core:exchange'); | ||
const PENDING_ORDER_TTL = 500; | ||
class Exchange extends events_1.EventEmitter { | ||
@@ -18,2 +19,3 @@ constructor(client, dashboard, { pendingUpdatesPriorityScale = 2 } = {}) { | ||
this.dashboard = dashboard; | ||
this.pendingOrderTaskMap = new Map(); | ||
this.balance$ = this.client.balance$.pipe(operators_1.debounce(() => rxjs_1.EMPTY), operators_1.distinctUntilChanged(object_1.isEqual), operators_1.shareReplay(1)); | ||
@@ -39,2 +41,8 @@ this.operationalBalance$ = this.balance$.pipe(operators_1.map((balanceMap) => new Map(Array.from(balanceMap.entries()).map(([key, { available, frozen }]) => [ | ||
if (!activeOrder) { | ||
let { type, market, size, price } = order; | ||
let pendingOrderTask = this.pendingOrderTaskMap.get(generatePendingOrderTaskKey(type, market, size, price)); | ||
if (pendingOrderTask && Date.now() < pendingOrderTask.expiredAt) { | ||
pendingOrderTask.done(order); | ||
return; | ||
} | ||
this.dashboard.info(`order ${id} has not been tracked by this exchange.`); | ||
@@ -117,3 +125,19 @@ return; | ||
this.dashboard.info(`create order [${group}] "${label}" ${price} * ${size}`); | ||
let order = await this.client.order(market, type, price, size, options); | ||
let pendingOrderKey = generatePendingOrderTaskKey(type, market, size, price); | ||
let order; | ||
let pendingOrderTaskPromise = new Promise(resolve => { | ||
this.pendingOrderTaskMap.set(pendingOrderKey, { | ||
expiredAt: Date.now() + PENDING_ORDER_TTL, | ||
done: resolve, | ||
}); | ||
}); | ||
try { | ||
order = await Promise.race([ | ||
this.client.order(market, type, price, size, options), | ||
pendingOrderTaskPromise, | ||
]); | ||
} | ||
finally { | ||
this.pendingOrderTaskMap.delete(pendingOrderKey); | ||
} | ||
if (!order) { | ||
@@ -417,2 +441,5 @@ this.dashboard.info(`failed creating ${type} order [${group}] "${label}" ${price} * ${size}`); | ||
} | ||
function generatePendingOrderTaskKey(type, market, size, price) { | ||
return `${type}_${market.stock}:${size}_${market.money}:${price}`; | ||
} | ||
//# sourceMappingURL=exchange.js.map |
{ | ||
"name": "@ex-master/core", | ||
"version": "0.1.67", | ||
"version": "0.1.68", | ||
"main": "bld/index.js", | ||
@@ -5,0 +5,0 @@ "types": "bld/index.d.ts", |
36237
890