[#22] Handle order update ws events

This commit is contained in:
darkeye 2025-01-11 12:07:29 +01:00
parent 2c13099a2c
commit 1085c58ebc

View File

@ -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});
});
}
}