publishing messages

This commit is contained in:
Nour Alharithi
2023-10-25 11:15:46 -07:00
parent 255ef1f7eb
commit 9bb5c9c062
2 changed files with 42 additions and 40 deletions

View File

@@ -3,6 +3,8 @@ import {
DLOBSubscriber,
DLOBSubscriptionConfig,
L2OrderBookGenerator,
MainnetPerpMarkets,
MainnetSpotMarkets,
MarketType,
groupL2,
} from '@drift-labs/sdk';
@@ -16,16 +18,49 @@ type wsMarketL2Args = {
marketName: string;
depth: number;
includeVamm: boolean;
numVammOrders?: number;
grouping?: number;
fallbackL2Generators?: L2OrderBookGenerator[];
updateOnChange?: boolean;
};
export class DLOBSubscriberIO extends DLOBSubscriber {
public marketL2Args: wsMarketL2Args[] = [];
public lastSeenL2Formatted: Map<MarketType, Map<number, any>>;
io: Server;
constructor(config: DLOBSubscriptionConfig) {
super(config);
// Set up appropriate maps
this.lastSeenL2Formatted = new Map();
this.lastSeenL2Formatted.set(MarketType.SPOT, new Map());
this.lastSeenL2Formatted.set(MarketType.PERP, new Map());
// Add all active markets to the market L2Args
for (const market of MainnetPerpMarkets) {
this.marketL2Args.push({
marketIndex: market.marketIndex,
marketType: MarketType.PERP,
marketName: market.symbol,
depth: 2,
includeVamm: true,
numVammOrders: 100,
updateOnChange: true,
fallbackL2Generators: []
});
}
for (const market of MainnetSpotMarkets) {
this.marketL2Args.push({
marketIndex: market.marketIndex,
marketType: MarketType.SPOT,
marketName: market.symbol,
depth: 2,
includeVamm: false,
updateOnChange: true,
fallbackL2Generators: []
});
}
}
override async updateDLOB(): Promise<void> {
@@ -38,7 +73,7 @@ export class DLOBSubscriberIO extends DLOBSubscriber {
getL2AndSendMsg(l2Args: wsMarketL2Args): void {
const grouping = l2Args.grouping;
const l2 = this.getL2(l2Args);
const l2 = this.getL2(l2Args);
let l2Formatted: any;
if (grouping) {
const groupingBN = new BN(grouping);
@@ -46,6 +81,11 @@ export class DLOBSubscriberIO extends DLOBSubscriber {
} else {
l2Formatted = l2WithBNToStrings(l2);
}
if (l2Args.updateOnChange && this.lastSeenL2Formatted.get(l2Args.marketType)?.get(l2Args.marketIndex) === JSON.stringify(l2Formatted)) {
return;
}
this.lastSeenL2Formatted.get(l2Args.marketType)?.set(l2Args.marketIndex, JSON.stringify(l2Formatted));
l2Formatted['marketName'] = l2Args.marketName;
l2Formatted['marketType'] = l2Args.marketType;
l2Formatted['marketIndex'] = l2Args.marketIndex;