11import * as grpc from "@grpc/grpc-js" ;
22import type { Exchange } from "@usherlabs/ccxt" ;
33import { authenticateRequest } from "../../helpers/auth" ;
4+ import {
5+ normalizeBinanceExecutionReport ,
6+ normalizeBinanceSpotBalanceEvent ,
7+ } from "../../helpers/binance-user-data-normalization" ;
48import {
59 BinanceSpotUserDataStream ,
610 isBinanceBalanceUserDataEvent ,
711 isBinanceOrderUserDataEvent ,
812} from "../../helpers/binance-user-data-stream" ;
9- import {
10- normalizeBinanceExecutionReport ,
11- normalizeBinanceSpotBalanceEvent ,
12- } from "../../helpers/binance-user-data-normalization" ;
1313import {
1414 type BrokerPoolEntry ,
1515 createBroker ,
@@ -46,6 +46,10 @@ import {
4646} from "../../helpers/order-book" ;
4747import type { OtelMetrics } from "../../helpers/otel" ;
4848import { getErrorMessage } from "../../helpers/shared/errors" ;
49+ import type {
50+ UserDataStreamSupervisor ,
51+ UserDataSubscription ,
52+ } from "../../helpers/user-data-stream-supervisor" ;
4953import type { SubscribeRequest , SubscribeResponse } from "../types" ;
5054import { SubscribeBrokerLifecycle } from "./broker-lifecycle" ;
5155
@@ -55,6 +59,7 @@ export type SubscribeDeps = {
5559 otelMetrics ?: OtelMetrics ;
5660 brokerArchiver ?: BrokerExecutionArchiver ;
5761 brokerLifecycle ?: SubscribeBrokerLifecycle ;
62+ userDataStreamSupervisor ?: UserDataStreamSupervisor ;
5863} ;
5964
6065type SubscribeCall = grpc . ServerWritableStream <
@@ -179,16 +184,22 @@ async function streamBinanceUserData(
179184 deploymentId : string ;
180185 assetType : BrokerMarketType ;
181186 } ,
187+ userDataSource ?: UserDataSubscription ,
188+ knownMarketId ?: string | null ,
182189) : Promise < void > {
183- const userDataStream = new BinanceSpotUserDataStream ( broker ) ;
184- call . once ( "close" , ( ) => userDataStream . close ( ) ) ;
185- call . once ( "cancelled" , ( ) => userDataStream . close ( ) ) ;
186- call . once ( "error" , ( ) => userDataStream . close ( ) ) ;
190+ const userDataStream =
191+ userDataSource ?? new BinanceSpotUserDataStream ( broker ) ;
192+ if ( ! userDataSource ) {
193+ call . once ( "close" , ( ) => userDataStream . close ( ) ) ;
194+ call . once ( "cancelled" , ( ) => userDataStream . close ( ) ) ;
195+ call . once ( "error" , ( ) => userDataStream . close ( ) ) ;
196+ }
187197
188198 const marketId =
189- subscriptionType === SubscriptionType . ORDERS
199+ knownMarketId ??
200+ ( subscriptionType === SubscriptionType . ORDERS
190201 ? await getBinanceMarketId ( broker , symbol )
191- : null ;
202+ : null ) ;
192203
193204 try {
194205 for await ( const message of userDataStream ) {
@@ -319,7 +330,13 @@ async function runCcxtSubscribeLoop(
319330}
320331
321332export function createSubscribeHandler ( deps : SubscribeDeps ) {
322- const { brokers, whitelistIps, otelMetrics, brokerArchiver } = deps ;
333+ const {
334+ brokers,
335+ whitelistIps,
336+ otelMetrics,
337+ brokerArchiver,
338+ userDataStreamSupervisor,
339+ } = deps ;
323340 const brokerLifecycle =
324341 deps . brokerLifecycle ?? new SubscribeBrokerLifecycle ( ) ;
325342 return async ( call : SubscribeCall ) => {
@@ -485,13 +502,42 @@ export function createSubscribeHandler(deps: SubscribeDeps) {
485502 } ) ;
486503 return ;
487504 }
505+ const marketId =
506+ subscriptionType === SubscriptionType . ORDERS
507+ ? await getBinanceMarketId ( accountBroker , resolvedSymbol )
508+ : undefined ;
509+ let userDataSource : UserDataSubscription | undefined ;
510+ if ( selectedBrokerAccount ) {
511+ if ( ! userDataStreamSupervisor ) {
512+ await writeSubscribeError ( call , isStreamClosed , {
513+ data : JSON . stringify ( {
514+ error : "Configured account user-data supervisor is unavailable" ,
515+ } ) ,
516+ timestamp : Date . now ( ) ,
517+ symbol : resolvedSymbol ,
518+ type : subscriptionType ,
519+ } ) ;
520+ return ;
521+ }
522+ userDataSource = userDataStreamSupervisor . subscribe ( {
523+ exchange : normalizedCex ,
524+ accountSelector : selectedBrokerAccount . label ,
525+ kind :
526+ subscriptionType === SubscriptionType . BALANCE
527+ ? "balance"
528+ : "orders" ,
529+ marketId,
530+ } ) ;
531+ }
488532 await streamBinanceUserData (
489533 call ,
490534 accountBroker ,
491535 resolvedSymbol ,
492536 subscriptionType ,
493537 isStreamClosed ,
494538 streamArchiveContext ,
539+ userDataSource ,
540+ marketId ,
495541 ) ;
496542 return ;
497543 }
0 commit comments