feat: 优化订单去重和取消逻辑,确保在处理STOP订单时避免精度损失

This commit is contained in:
discountry
2025-10-11 21:41:39 +08:00
parent 8b20e60753
commit 2627fa9db8
4 changed files with 27 additions and 15 deletions
+3 -2
View File
@@ -108,13 +108,14 @@ export class LighterExchangeAdapter implements ExchangeAdapter {
async cancelOrder(params: { symbol: string; orderId: number | string }): Promise<void> {
await this.ensureInitialized("cancelOrder");
await this.gateway.cancelOrder({ orderId: params.orderId });
// Accept both clientOrderId and order_index as strings; forward as-is to preserve precision
await this.gateway.cancelOrder({ orderId: String(params.orderId) });
}
async cancelOrders(params: { symbol: string; orderIdList: Array<number | string> }): Promise<void> {
await this.ensureInitialized("cancelOrders");
for (const orderId of params.orderIdList) {
await this.gateway.cancelOrder({ orderId });
await this.gateway.cancelOrder({ orderId: String(orderId) });
}
}
+13 -1
View File
@@ -312,7 +312,14 @@ export class LighterGateway {
await this.ensureInitialized();
const marketIndex = params.marketIndex ?? this.marketId;
if (marketIndex == null) throw new Error("Market index unknown");
const indexValue = BigInt(typeof params.orderId === "string" ? Number(params.orderId) : params.orderId);
// Parse order id to BigInt without precision loss; prefer string input
let indexValue: bigint;
if (typeof params.orderId === "string") {
indexValue = BigInt(params.orderId);
} else {
// Fallback for numeric ids (may be unsafe if beyond 2^53-1)
indexValue = BigInt(Math.trunc(params.orderId));
}
const { apiKeyIndex, nonce } = this.nonceManager.next();
try {
const signed = await this.signer.signCancelOrder({
@@ -323,6 +330,11 @@ export class LighterGateway {
});
const auth = await this.ensureAuthToken();
await this.http.sendTransaction(signed.txType, signed.txInfo, { authToken: auth });
// Optimistically remove the order locally to avoid stale duplicates until WS confirms
const key = String(params.orderId);
this.orderMap.delete(key);
this.orders = Array.from(this.orderMap.values());
this.emitOrders();
} catch (error) {
this.nonceManager.acknowledgeFailure(apiKeyIndex);
throw error;
+2 -1
View File
@@ -78,7 +78,8 @@ export function lighterOrderToAster(symbol: string, order: LighterOrder): AsterO
? "SELL"
: "BUY";
return {
orderId: order.order_index,
// Use string order id to avoid precision loss; prefer on-chain order_index for cancellation
orderId: String(order.order_index ?? order.client_order_index ?? ""),
clientOrderId: String(order.client_order_index ?? order.order_index ?? ""),
symbol,
side,