Skip to content

Commit b64de6f

Browse files
committed
replace dic related any types with real ones
1 parent 115b5d3 commit b64de6f

24 files changed

+388
-525
lines changed

src/exchange/binance.ts

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,17 @@ import { Order } from '../dict/order';
1212
import { OrderBag } from './utils/order_bag';
1313
import { TradesUtil } from './utils/trades_util';
1414
import { 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';
1519

1620
export class Binance {
1721
private eventEmitter: EventEmitter;
18-
private logger: any;
19-
private queue: any;
20-
private candleImport: any;
21-
private throttler: any;
22+
private logger: Logger;
23+
private queue: QueueManager;
24+
private candleImport: CandleImporter;
25+
private throttler: Throttler;
2226
private client: any;
2327
private exchangePairs: Record<string, ExchangePairInfo>;
2428
private symbols: any[];
@@ -27,7 +31,7 @@ export class Binance {
2731
private balances: any[];
2832
private orderbag: OrderBag;
2933

30-
constructor(eventEmitter: EventEmitter, logger: any, queue: any, candleImport: any, throttler: any) {
34+
constructor(eventEmitter: EventEmitter, logger: Logger, queue: QueueManager, candleImport: CandleImporter, throttler: Throttler) {
3135
this.eventEmitter = eventEmitter;
3236
this.logger = logger;
3337
this.queue = queue;
@@ -113,25 +117,24 @@ export class Binance {
113117

114118
symbol.periods.forEach((interval: string) => {
115119
// backfill
116-
this.queue.add(() => {
117-
client.candles({ symbol: symbol.symbol, limit: 500, interval: interval as any }).then(async candles => {
118-
const ourCandles = candles.map(
119-
candle =>
120-
new ExchangeCandlestick(
121-
'binance',
122-
symbol.symbol,
123-
interval,
124-
Math.round(candle.openTime / 1000),
125-
parseFloat(candle.open),
126-
parseFloat(candle.high),
127-
parseFloat(candle.low),
128-
parseFloat(candle.close),
129-
parseFloat(candle.volume)
130-
)
131-
);
132-
133-
await this.candleImport.insertThrottledCandles(ourCandles);
134-
});
120+
this.queue.add(async () => {
121+
const candles = await client.candles({ symbol: symbol.symbol, limit: 500, interval: interval as any });
122+
const ourCandles = candles.map(
123+
candle =>
124+
new ExchangeCandlestick(
125+
'binance',
126+
symbol.symbol,
127+
interval,
128+
Math.round(candle.openTime / 1000),
129+
parseFloat(candle.open),
130+
parseFloat(candle.high),
131+
parseFloat(candle.low),
132+
parseFloat(candle.close),
133+
parseFloat(candle.volume)
134+
)
135+
);
136+
137+
await this.candleImport.insertThrottledCandles(ourCandles);
135138
});
136139

137140
// live candles
@@ -518,7 +521,8 @@ export class Binance {
518521
// on multiple pair path orders with latest date wins
519522
const assetPositionsOrdered = assetPositions.sort(
520523
// order by latest
521-
(a, b) => (b.createdAt ? b.createdAt.getTime() : new Date('1970-01-01').getTime()) - (a.createdAt ? a.createdAt.getTime() : new Date('1970-01-01').getTime())
524+
(a, b) =>
525+
(b.createdAt ? b.createdAt.getTime() : new Date('1970-01-01').getTime()) - (a.createdAt ? a.createdAt.getTime() : new Date('1970-01-01').getTime())
522526
);
523527

524528
positions.push(assetPositionsOrdered[0]);

src/exchange/binance_futures.ts

Lines changed: 35 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,19 @@ import { ExchangeCandlestick } from '../dict/exchange_candlestick';
1212
import { Position } from '../dict/position';
1313
import { CcxtExchangeOrder } from './ccxt/ccxt_exchange_order';
1414
import { 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

1622
export 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

Comments
 (0)