import { CircularBuffer, getJSON, wait } from '../handy.ts' import type { MexcFuturesDepthSnapshotData, MexcFuturesDepthSnapshotMessage } from '../mappers/mexcfutures.ts' import { Filter } from '../types.ts' import { RealTimeFeedBase } from './realtimefeed.ts' export class MexcFuturesRealTimeFeed extends RealTimeFeedBase { private readonly pendingDepthSnapshotSymbols = new Set() private readonly bufferedDepthUpdates = new Map>() protected readonly wssURL = 'wss://contract.mexc.com' protected readonly httpURL: string = 'https://contract.mexc.com' protected readonly channelToSubscriptionMethod = new Map([ ['push.deal', 'sub.deal'], ['push.depth', 'sub.depth'], ['push.ticker', 'sub.ticker'], ['push.index.price', 'sub.index.price'], ['push.fair.price', 'sub.fair.price'], ['push.funding.rate', 'sub.funding.rate'], ['push.contract', 'sub.contract'] ]) protected override sendCustomPing = () => { this.send({ method: 'ping' }) } protected override async getWebSocketUrl() { return 'wss://contract.mexc.com/edge' } protected override mapToSubscribeMessages(filters: Filter[]): any[] { const filtersWithSymbols = filters.map>>((filter) => { if (filter.channel === 'push.contract') { return { ...filter, symbols: [] } } if (filter.channel !== 'push.depth.snapshot' && this.channelToSubscriptionMethod.has(filter.channel) === false) { throw new Error(`Unsupported MEXC futures channel ${filter.channel}`) } if (!filter.symbols || filter.symbols.length === 0) { throw new Error('MexcFuturesRealTimeFeed requires explicitly specified symbols when subscribing to live feed') } return filter as Required> }) const depthSnapshotFilters = filtersWithSymbols.filter((filter) => filter.channel === 'push.depth.snapshot') this.validateDepthSnapshotFilters(filtersWithSymbols, depthSnapshotFilters) this.resetDepthSnapshotTracking(depthSnapshotFilters) return filtersWithSymbols.flatMap((filter) => { if (filter.channel === 'push.depth.snapshot') { return [] } if (filter.channel === 'push.contract') { return [{ method: this.channelToSubscriptionMethod.get(filter.channel) }] } return filter.symbols.map((symbol) => ({ method: this.channelToSubscriptionMethod.get(filter.channel), param: { symbol }, gzip: false })) }) } private resetDepthSnapshotTracking(filters: Required>[]) { this.pendingDepthSnapshotSymbols.clear() this.bufferedDepthUpdates.clear() for (const filter of filters) { for (const symbol of filter.symbols) { const upperCaseSymbol = symbol.toUpperCase() this.pendingDepthSnapshotSymbols.add(upperCaseSymbol) this.bufferedDepthUpdates.set(upperCaseSymbol, new CircularBuffer(2000)) } } } private validateDepthSnapshotFilters(filters: Required>[], depthSnapshotFilters: Required>[]) { if (depthSnapshotFilters.length === 0) { return } const depthSymbols = new Set( filters.filter((filter) => filter.channel === 'push.depth').flatMap((filter) => filter.symbols.map((symbol) => symbol.toUpperCase())) ) for (const filter of depthSnapshotFilters) { for (const symbol of filter.symbols) { if (depthSymbols.has(symbol.toUpperCase()) === false) { throw new Error('MexcFuturesRealTimeFeed requires push.depth for every push.depth.snapshot symbol') } } } } protected override onMessage(message: any) { if ( message.channel !== 'push.depth' || message.symbol === undefined || this.pendingDepthSnapshotSymbols.has(message.symbol) === false ) { return } const firstVersion = Number( message.data?.begin !== undefined && message.data.begin !== null && message.data.begin !== 0 ? message.data.begin : message.data?.version ) const lastVersion = Number( message.data?.end !== undefined && message.data.end !== null && message.data.end !== 0 ? message.data.end : message.data?.version ) if (Number.isFinite(firstVersion) === false || Number.isFinite(lastVersion) === false) { return } const bufferedUpdates = this.bufferedDepthUpdates.get(message.symbol) ?? new CircularBuffer(2000) bufferedUpdates.append({ firstVersion, lastVersion }) this.bufferedDepthUpdates.set(message.symbol, bufferedUpdates) } protected override async provideManualSnapshots(filters: Filter[], shouldCancel: () => boolean) { const depthFilter = filters.find((filter) => filter.channel === 'push.depth.snapshot') if (depthFilter === undefined) { return } this.debug('requesting manual snapshots for: %s', depthFilter.symbols!) for (const symbol of depthFilter.symbols!) { await this.provideManualSnapshot(symbol.toUpperCase(), shouldCancel) } this.debug('requested manual snapshots successfully for: %s ', depthFilter.symbols!) } protected override messageIsError(message: any): boolean { return message.channel === 'rs.error' || message.success === false || message.error !== undefined } protected override messageIsHeartbeat(message: any) { return message.channel === 'pong' } private async provideManualSnapshot(symbol: string, shouldCancel: () => boolean) { const maxSnapshotRounds = 4 const maxSnapshotAttemptsPerRound = 3 for (let round = 0; round < maxSnapshotRounds; round++) { for (let attempt = 1; attempt <= maxSnapshotAttemptsPerRound; attempt++) { if (shouldCancel()) { return } const { data } = await getJSON(`${this.httpURL}/api/v1/contract/depth/${symbol}?limit=5000`) if (this.snapshotResponseIsValid(data) === false) { if (attempt < maxSnapshotAttemptsPerRound) { await wait(attempt * 1000) } continue } const hasOverlap = await this.waitForSnapshotOverlap(symbol, data.data.version) if (shouldCancel()) { return } if (hasOverlap === false) { this.trimBufferedUpdates(symbol) if (attempt < maxSnapshotAttemptsPerRound) { await wait(attempt * 1000) } continue } if (hasOverlap === true || attempt === maxSnapshotAttemptsPerRound) { this.manualSnapshotsBuffer.push(this.createManualSnapshot(symbol, data.data)) this.pendingDepthSnapshotSymbols.delete(symbol) this.bufferedDepthUpdates.delete(symbol) return } } } throw new Error(`MexcFuturesRealTimeFeed could not align depth snapshot for ${symbol}`) } private async waitForSnapshotOverlap(symbol: string, snapshotVersion: number) { let hasOverlap = this.validateSnapshotOverlap(symbol, snapshotVersion) for (let attempt = 0; attempt < 60; attempt++) { if (hasOverlap !== undefined) { return hasOverlap } await wait(100) hasOverlap = this.validateSnapshotOverlap(symbol, snapshotVersion) } return hasOverlap } private validateSnapshotOverlap(symbol: string, snapshotVersion: number) { const bufferedUpdates = this.bufferedDepthUpdates.get(symbol) for (const update of bufferedUpdates?.items() ?? []) { if (update.lastVersion <= snapshotVersion) { continue } return update.firstVersion <= snapshotVersion + 1 && update.lastVersion >= snapshotVersion + 1 } return undefined } private trimBufferedUpdates(symbol: string) { const bufferedUpdates = this.bufferedDepthUpdates.get(symbol) if (bufferedUpdates === undefined || bufferedUpdates.count <= 100) { return } const trimmed = new CircularBuffer(2000) for (const update of [...bufferedUpdates.items()].slice(-100)) { trimmed.append(update) } this.bufferedDepthUpdates.set(symbol, trimmed) } private snapshotResponseIsValid(response: MexcFuturesDepthSnapshotResponse) { return ( response.success === true && response.code === 0 && response.data !== undefined && Number.isFinite(response.data.version) && response.data.version >= 0 && Array.isArray(response.data.asks) && Array.isArray(response.data.bids) ) } private createManualSnapshot(symbol: string, data: MexcFuturesDepthSnapshotData): MexcFuturesDepthSnapshotMessage { return { channel: 'push.depth.snapshot', symbol, generated: true, data: { cts: data.cts, asks: data.asks, bids: data.bids, timestamp: data.timestamp, version: data.version } } } } type MexcFuturesDepthSnapshotResponse = { success: boolean code: number message?: string data: MexcFuturesDepthSnapshotData } type MexcFuturesDepthUpdateData = { firstVersion: number lastVersion: number }