@@ -12,13 +12,19 @@ import { ExchangeCandlestick } from '../dict/exchange_candlestick';
1212import { Position } from '../dict/position' ;
1313import { CcxtExchangeOrder } from './ccxt/ccxt_exchange_order' ;
1414import { EventEmitter } from 'events' ;
15+ import type { Logger } from '../modules/services' ;
16+ import type { QueueManager } from '../utils/queue' ;
17+ import type { CandleImporter } from '../modules/system/candle_importer' ;
18+ import type { Throttler } from '../utils/throttler' ;
19+ import type { RequestClient } from '../utils/request_client' ;
20+ import type { CandlestickResample } from '../modules/system/candlestick_resample' ;
1521
1622export class BinanceFutures {
1723 private eventEmitter : EventEmitter ;
18- private logger : any ;
19- private queue : any ;
20- private candleImporter : any ;
21- private throttler : any ;
24+ private logger : Logger ;
25+ private queue : QueueManager ;
26+ private candleImporter : CandleImporter ;
27+ private throttler : Throttler ;
2228 private exchange : any ;
2329 private ccxtExchangeOrder : CcxtExchangeOrder ;
2430 private positions : Record < string , Position > ;
@@ -28,7 +34,15 @@ export class BinanceFutures {
2834 private intervals : NodeJS . Timeout [ ] ;
2935 private ccxtClient ?: any ;
3036
31- constructor ( eventEmitter : EventEmitter , requestClient : any , candlestickResample : any , logger : any , queue : any , candleImporter : any , throttler : any ) {
37+ constructor (
38+ eventEmitter : EventEmitter ,
39+ requestClient : RequestClient ,
40+ candlestickResample : CandlestickResample ,
41+ logger : Logger ,
42+ queue : QueueManager ,
43+ candleImporter : CandleImporter ,
44+ throttler : Throttler
45+ ) {
3246 this . eventEmitter = eventEmitter ;
3347 this . logger = logger ;
3448 this . queue = queue ;
@@ -150,9 +164,7 @@ export class BinanceFutures {
150164 try {
151165 ohlcvs = await ccxtClient . fetchOHLCV ( symbol . symbol . replace ( 'USDT' , '/USDT' ) , period , undefined , 500 ) ;
152166 } catch ( e ) {
153- me . logger . info (
154- `Binance Futures: candles fetch error: ${ JSON . stringify ( [ symbol . symbol , period , String ( e ) ] ) } `
155- ) ;
167+ me . logger . info ( `Binance Futures: candles fetch error: ${ JSON . stringify ( [ symbol . symbol , period , String ( e ) ] ) } ` ) ;
156168
157169 return ;
158170 }
@@ -302,16 +314,7 @@ export class BinanceFutures {
302314 const positionAmt = parseFloat ( position . pa ) ;
303315 const entryPrice = parseFloat ( position . ep ) ;
304316
305- return new Position (
306- position . s ,
307- positionAmt < 0 ? 'short' : 'long' ,
308- positionAmt ,
309- undefined ,
310- new Date ( ) ,
311- entryPrice ,
312- undefined ,
313- position
314- ) ;
317+ return new Position ( position . s , positionAmt < 0 ? 'short' : 'long' , positionAmt , undefined , new Date ( ) , entryPrice , undefined , position ) ;
315318 }
316319
317320 /**
@@ -328,9 +331,7 @@ export class BinanceFutures {
328331 if ( position . s in this . positions && position . pa === '0' ) {
329332 delete this . positions [ position . s ] ;
330333
331- this . logger . info (
332- `Binance Futures: Websocket position closed/removed: ${ JSON . stringify ( [ position . s , position ] ) } `
333- ) ;
334+ this . logger . info ( `Binance Futures: Websocket position closed/removed: ${ JSON . stringify ( [ position . s , position ] ) } ` ) ;
334335
335336 return ;
336337 }
@@ -350,13 +351,7 @@ export class BinanceFutures {
350351
351352 // position update
352353 if ( position . s in this . positions ) {
353- this . logger . info (
354- `Binance Futures: Websocket position update: ${ JSON . stringify ( [
355- position . s ,
356- position . pa ,
357- this . positions [ position . s ] . getAmount ( )
358- ] ) } `
359- ) ;
354+ this . logger . info ( `Binance Futures: Websocket position update: ${ JSON . stringify ( [ position . s , position . pa , this . positions [ position . s ] . getAmount ( ) ] ) } ` ) ;
360355 }
361356 } , this ) ;
362357 }
@@ -411,27 +406,21 @@ export class BinanceFutures {
411406 const me = this ;
412407 const ws = new WebSocket ( 'wss://fstream.binance.com/stream' ) ;
413408
414- ws . onerror = function ( event : any ) {
415- me . logger . error (
416- `Binance Futures: Public stream (${ indexConnection } ) error: ${ JSON . stringify ( [ event . code , event . message ] ) } `
417- ) ;
409+ ws . onerror = function ( event : any ) {
410+ me . logger . error ( `Binance Futures: Public stream (${ indexConnection } ) error: ${ JSON . stringify ( [ event . code , event . message ] ) } ` ) ;
418411 } ;
419412
420413 const subscriptionTimeouts : Record < number , NodeJS . Timeout > = { } ;
421414
422- ws . onopen = function ( ) {
415+ ws . onopen = function ( ) {
423416 me . logger . info ( `Binance Futures: Public stream (${ indexConnection } ) opened.` ) ;
424417
425- me . logger . info (
426- `Binance Futures: Needed Websocket (${ indexConnection } ) subscriptions: ${ JSON . stringify ( subscriptions . length ) } `
427- ) ;
418+ me . logger . info ( `Binance Futures: Needed Websocket (${ indexConnection } ) subscriptions: ${ JSON . stringify ( subscriptions . length ) } ` ) ;
428419
429420 // "we are only allowed to send 5 requests per second"; but limit it also for the "SUBSCRIBE" itself who knows upcoming changes on this
430421 _ . chunk ( subscriptions , 15 ) . forEach ( ( subscriptionChunk : string [ ] , index : number ) => {
431422 subscriptionTimeouts [ index ] = setTimeout ( ( ) => {
432- me . logger . debug (
433- `Binance Futures: Public stream (${ indexConnection } ) subscribing: ${ JSON . stringify ( subscriptionChunk ) } `
434- ) ;
423+ me . logger . debug ( `Binance Futures: Public stream (${ indexConnection } ) subscribing: ${ JSON . stringify ( subscriptionChunk ) } ` ) ;
435424
436425 ws . send (
437426 JSON . stringify ( {
@@ -446,7 +435,7 @@ export class BinanceFutures {
446435 } ) ;
447436 } ;
448437
449- ws . onmessage = async function ( event : any ) {
438+ ws . onmessage = async function ( event : any ) {
450439 if ( event . type && event . type === 'message' ) {
451440 const body = JSON . parse ( event . data ) ;
452441
@@ -483,13 +472,8 @@ export class BinanceFutures {
483472 }
484473 } ;
485474
486- ws . onclose = function ( event : any ) {
487- me . logger . error (
488- `Binance Futures: Public Stream (${ indexConnection } ) connection closed: ${ JSON . stringify ( [
489- event . code ,
490- event . message
491- ] ) } `
492- ) ;
475+ ws . onclose = function ( event : any ) {
476+ me . logger . error ( `Binance Futures: Public Stream (${ indexConnection } ) connection closed: ${ JSON . stringify ( [ event . code , event . message ] ) } ` ) ;
493477
494478 Object . values ( subscriptionTimeouts ) . forEach ( timeout => {
495479 clearTimeout ( timeout ) ;
@@ -513,15 +497,15 @@ export class BinanceFutures {
513497
514498 const me = this ;
515499 const ws = new WebSocket ( `wss://fstream.binance.com/ws/${ response . listenKey } ` ) ;
516- ws . onerror = function ( e : any ) {
500+ ws . onerror = function ( e : any ) {
517501 me . logger . info ( `Binance Futures: Connection error: ${ String ( e ) } ` ) ;
518502 } ;
519503
520- ws . onopen = function ( ) {
504+ ws . onopen = function ( ) {
521505 me . logger . info ( `Binance Futures: Opened user stream` ) ;
522506 } ;
523507
524- ws . onmessage = async function ( event : any ) {
508+ ws . onmessage = async function ( event : any ) {
525509 if ( event && event . type === 'message' ) {
526510 const message = JSON . parse ( event . data ) ;
527511
@@ -556,7 +540,7 @@ export class BinanceFutures {
556540 }
557541 } , 1000 * 60 * 10 ) ;
558542
559- ws . onclose = function ( event : any ) {
543+ ws . onclose = function ( event : any ) {
560544 me . logger . error ( `Binance futures: User stream connection closed: ${ JSON . stringify ( [ event . code , event . message ] ) } ` ) ;
561545 clearInterval ( heartbeat ) ;
562546
0 commit comments