ws server works

This commit is contained in:
Nour Alharithi
2023-10-24 15:20:35 -07:00
parent 22203fd20a
commit 568ab5a239
6 changed files with 260 additions and 31 deletions

View File

@@ -0,0 +1,56 @@
import {
BN,
DLOBSubscriber,
DLOBSubscriptionConfig,
L2OrderBookGenerator,
MarketType,
groupL2,
} from '@drift-labs/sdk';
import { l2WithBNToStrings } from '../utils';
import { Server } from 'socket.io';
import { getIOServer } from '..';
type wsMarketL2Args = {
marketIndex: number;
marketType: MarketType;
marketName: string;
depth: number;
includeVamm: boolean;
grouping?: number;
fallbackL2Generators?: L2OrderBookGenerator[];
};
export class DLOBSubscriberIO extends DLOBSubscriber {
public marketL2Args: wsMarketL2Args[] = [];
io: Server;
constructor(config: DLOBSubscriptionConfig) {
super(config);
}
override async updateDLOB(): Promise<void> {
this.io = getIOServer();
await super.updateDLOB();
for (const l2Args of this.marketL2Args) {
this.getL2AndSendMsg(l2Args);
}
}
getL2AndSendMsg(l2Args: wsMarketL2Args): void {
console.time('getL2AndSendMsg');
const grouping = l2Args.grouping;
const l2 = this.getL2(l2Args);
let l2Formatted: any;
if (grouping) {
const groupingBN = new BN(grouping);
l2Formatted = l2WithBNToStrings(groupL2(l2, groupingBN, l2Args.depth));
} else {
l2Formatted = l2WithBNToStrings(l2);
}
l2Formatted['marketName'] = l2Args.marketName;
l2Formatted['marketType'] = l2Args.marketType;
l2Formatted['marketIndex'] = l2Args.marketIndex;
this.io.emit('l2Update', l2Formatted);
console.timeEnd('getL2AndSendMsg');
}
}