From 1085c58ebc84eb99025fc91cdc7110f75c704975 Mon Sep 17 00:00:00 2001 From: darkeye Date: Sat, 11 Jan 2025 12:07:29 +0100 Subject: [PATCH] [#22] Handle order update ws events --- src/js/apis/binance/BinanceApiClient.js | 36 ++++++++++++++++--------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/src/js/apis/binance/BinanceApiClient.js b/src/js/apis/binance/BinanceApiClient.js index 9dac402..7a386d3 100644 --- a/src/js/apis/binance/BinanceApiClient.js +++ b/src/js/apis/binance/BinanceApiClient.js @@ -80,6 +80,9 @@ export default class BinanceApiClient extends APIClient { case BinanceWSEvent.TRADE_EVENT: this.#fireTradeEvent(data); break; + case BinanceWSEvent.EXECUTION_REPORT: + this.#requestOrderUpdate(data.orderId, data.originalClientOrderId, data.symbol); + break; default: this.getLogger().warn("Unknown event type!", data); } @@ -328,35 +331,42 @@ export default class BinanceApiClient extends APIClient { */ requestTransactionUpdate(transaction, type = "buy"){ const id = type == "buy" ? transaction.buySettings.apiID : transaction.sellSettings.apiID; + this.#requestOrderUpdate(id, transaction.id, transaction.symbol.getKey()); + } + /** + * @param {string} orderId + * @param {string} clientId + * @param {string} symbol + */ + #requestOrderUpdate(orderId, clientId, symbol){ this.#restClient.getOrder({ - "symbol": transaction.symbol.getKey(), - "orderId": id, - "origClientOrderId": transaction.id + "symbol": symbol, + "orderId": orderId, + "origClientOrderId": clientId, }).then(resp => { const event = new TransactionUpdateEvent(resp.clientOrderId, resp.orderId, resp.status, resp.updateTime); - this.#requestTransactionFills(transaction, type, event); + this.#requestTransactionFills(orderId, clientId, symbol, event); }).catch(e => { - this.getLogger().error("Unable to get order status for " + transaction.id, {code: e.code, msg: e.message, url: e.requestUrl}); + this.getLogger().error("Unable to get order status for " + clientId + "(" + orderId + ")", {code: e.code, msg: e.message, url: e.requestUrl}); }); } /** - * @param {Transaction} transaction - * @param {"buy" | "sell"} type + * @param {string} orderId + * @param {string} clientId + * @param {string} symbol * @param {TransactionUpdateEvent} event */ - #requestTransactionFills(transaction, type, event){ - const id = type == "buy" ? transaction.buySettings.apiID : transaction.sellSettings.apiID; - + #requestTransactionFills(orderId, clientId, symbol, event){ this.#restClient.getAccountTradeList({ - "symbol": transaction.symbol.getKey(), - "orderId": id, + "symbol": symbol, + "orderId": orderId, }).then(resp => { resp.forEach(trade => event.fills.push(new TransactionFill(trade.price, trade.qty))); this.dispatchAccountEvent(event); }).catch(e => { - this.getLogger().error("Unable to get order fills for " + transaction.id, {code: e.code, msg: e.message, url: e.requestUrl}); + this.getLogger().error("Unable to get order fills for " + clientId + "(" + orderId + ")", {code: e.code, msg: e.message, url: e.requestUrl}); }); } } \ No newline at end of file