|
| 1 | +import type { Exchange } from "@usherlabs/ccxt"; |
| 2 | +import { asRecord } from "./shared/guards"; |
| 3 | + |
| 4 | +function requireQuantity( |
| 5 | + entry: Record<string, unknown>, |
| 6 | + key: "f" | "l", |
| 7 | +): string { |
| 8 | + const value = entry[key]; |
| 9 | + if (typeof value === "string" && value.trim().length > 0) { |
| 10 | + return value; |
| 11 | + } |
| 12 | + if (typeof value === "number" && Number.isFinite(value)) { |
| 13 | + return String(value); |
| 14 | + } |
| 15 | + throw new Error(`Invalid Binance balance quantity: ${key}`); |
| 16 | +} |
| 17 | + |
| 18 | +export async function normalizeBinanceSpotBalanceEvent( |
| 19 | + exchange: Exchange, |
| 20 | + event: Record<string, unknown>, |
| 21 | +): Promise<unknown> { |
| 22 | + if (event.e !== "outboundAccountPosition") { |
| 23 | + return exchange.fetchBalance({ type: "spot" }); |
| 24 | + } |
| 25 | + |
| 26 | + if (!Array.isArray(event.B)) { |
| 27 | + throw new Error("Invalid Binance outboundAccountPosition balances"); |
| 28 | + } |
| 29 | + |
| 30 | + const timestamp = |
| 31 | + typeof event.E === "number" && Number.isFinite(event.E) |
| 32 | + ? event.E |
| 33 | + : undefined; |
| 34 | + const balance: Record<string, unknown> = { |
| 35 | + info: event, |
| 36 | + ...(timestamp !== undefined && { |
| 37 | + timestamp, |
| 38 | + datetime: new Date(timestamp).toISOString(), |
| 39 | + }), |
| 40 | + }; |
| 41 | + |
| 42 | + for (const rawEntry of event.B) { |
| 43 | + const entry = asRecord(rawEntry); |
| 44 | + const asset = entry?.a; |
| 45 | + if (!entry || typeof asset !== "string" || asset.length === 0) { |
| 46 | + throw new Error("Invalid Binance outboundAccountPosition asset"); |
| 47 | + } |
| 48 | + balance[asset] = { |
| 49 | + free: requireQuantity(entry, "f"), |
| 50 | + used: requireQuantity(entry, "l"), |
| 51 | + }; |
| 52 | + } |
| 53 | + |
| 54 | + return exchange.safeBalance(balance); |
| 55 | +} |
| 56 | + |
| 57 | +function getTradeId(value: unknown): string | undefined { |
| 58 | + if ( |
| 59 | + (typeof value === "number" && Number.isFinite(value)) || |
| 60 | + (typeof value === "string" && value.length > 0) |
| 61 | + ) { |
| 62 | + const tradeId = String(value); |
| 63 | + return tradeId === "-1" ? undefined : tradeId; |
| 64 | + } |
| 65 | + return undefined; |
| 66 | +} |
| 67 | + |
| 68 | +export function normalizeBinanceExecutionReport( |
| 69 | + exchange: Exchange, |
| 70 | + event: Record<string, unknown>, |
| 71 | +): Record<string, unknown> { |
| 72 | + const parsed = asRecord(exchange.parseWsOrder(event)); |
| 73 | + if (!parsed) { |
| 74 | + throw new Error("Binance executionReport did not parse as an order"); |
| 75 | + } |
| 76 | + |
| 77 | + // executionReport commission is for the latest fill, while order stream |
| 78 | + // consumers interpret fee fields as cumulative snapshots. |
| 79 | + const { fee: _fee, fees: _fees, ...order } = parsed; |
| 80 | + const tradeId = getTradeId(event.t); |
| 81 | + return tradeId === undefined ? order : { ...order, tradeId }; |
| 82 | +} |
0 commit comments