UNPKG

ccxt

Version:

A cryptocurrency trading API with more than 100 exchanges in JavaScript / TypeScript / Python / C# / PHP / Go

245 lines (240 loc) • 9.42 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var mudrex$1 = require('../mudrex.js'); var errors = require('../base/errors.js'); var Cache = require('../base/ws/Cache.js'); // ---------------------------------------------------------------------------- // --------------------------------------------------------------------------- class mudrex extends mudrex$1["default"] { describe() { return this.deepExtend(super.describe(), { 'has': { 'ws': true, 'watchOHLCV': true, 'watchTicker': true, 'watchTickers': true, }, 'urls': { 'api': { 'ws': 'wss://trade.mudrex.com/fapi/v1/price/ws/linear', }, }, 'options': { 'broker': '42ce8902-8585-448c-a1e8-0371a6ca7ca8', }, 'streaming': { 'ping': this.ping, 'keepAlive': 20000, }, }); } ping(client) { return { 'id': this.requestId(), 'method': 'PING', }; } requestId() { const reqid = this.sum(this.safeInteger(this.options, 'correlationId', 0), 1); this.options['correlationId'] = reqid; return reqid; } /** * @ignore * @method * @description injects the broker Partner-Id into the websocket connection headers */ setBrokerHeaders() { const brokerId = this.safeString(this.options, 'broker'); if (brokerId === undefined) { return; } const wsOptions = this.safeDict(this.options, 'ws', {}); const innerOptions = this.safeDict(wsOptions, 'options', {}); const headers = this.safeDict(innerOptions, 'headers', {}); headers['Partner-Id'] = brokerId; innerOptions['headers'] = headers; wsOptions['options'] = innerOptions; this.options['ws'] = wsOptions; } async watchTicker(symbol, params = {}) { if (this.markets === undefined) { await this.loadMarkets(); } const market = this.market(symbol); symbol = market['symbol']; const messageHash = 'ticker:' + symbol; const url = this.urls['api']['ws']; this.setBrokerHeaders(); const baseIdString = (market['baseId'] !== undefined) ? market['baseId'] : ''; const quoteIdString = (market['quoteId'] !== undefined) ? market['quoteId'] : ''; const assetId = baseIdString.toLowerCase() + quoteIdString.toLowerCase(); const subscribe = { 'id': this.requestId(), 'method': 'SUBSCRIBE', 'params': ['ticker@1s'], 'assets': [assetId], }; const request = this.extend(subscribe, params); return await this.watch(url, messageHash, request, messageHash); } async watchTickers(symbols = undefined, params = {}) { if (this.markets === undefined) { await this.loadMarkets(); } symbols = this.marketSymbols(symbols); const messageHashes = []; const assets = []; if (symbols !== undefined) { for (let i = 0; i < symbols.length; i++) { const market = this.market(symbols[i]); messageHashes.push('ticker:' + market['symbol']); const baseIdString = (market['baseId'] !== undefined) ? market['baseId'] : ''; const quoteIdString = (market['quoteId'] !== undefined) ? market['quoteId'] : ''; assets.push(baseIdString.toLowerCase() + quoteIdString.toLowerCase()); } } const url = this.urls['api']['ws']; this.setBrokerHeaders(); const subscribe = { 'id': this.requestId(), 'method': 'SUBSCRIBE', 'params': ['ticker@1s'], 'assets': assets, }; const request = this.extend(subscribe, params); const ticker = await this.watchMultiple(url, messageHashes, request, messageHashes); if (this.newUpdates) { const result = {}; result[ticker['symbol']] = ticker; return result; } return this.filterByArrayTickers(this.tickers, 'symbol', symbols); } async watchOHLCV(symbol, timeframe = '1m', since = undefined, limit = undefined, params = {}) { if (this.markets === undefined) { await this.loadMarkets(); } const market = this.market(symbol); symbol = market['symbol']; const priceType = this.safeString(params, 'price'); params = this.omit(params, 'price'); const interval = this.safeString(this.timeframes, timeframe, timeframe); if (interval !== '1s' && interval !== '1m') { throw new errors.NotSupported(this.id + ' watchOHLCV() supports 1s and 1m timeframes only'); } let prefix = 'kline'; if (priceType === 'mark') { prefix = 'markKline'; } const streamBaseId = (market['baseId'] !== undefined) ? market['baseId'] : ''; const streamQuoteId = (market['quoteId'] !== undefined) ? market['quoteId'] : ''; const stream = prefix + '@' + interval + '@' + streamBaseId.toLowerCase() + streamQuoteId.toLowerCase(); const messageHash = stream; const url = this.urls['api']['ws']; this.setBrokerHeaders(); const subscribe = { 'id': this.requestId(), 'method': 'SUBSCRIBE', 'params': [stream], }; const request = this.extend(subscribe, params); const ohlcv = await this.watch(url, messageHash, request, messageHash); if (this.newUpdates) { limit = ohlcv.getLimit(symbol, limit); } return this.filterBySinceLimit(ohlcv, since, limit, 0, true); } handleMessage(client, message) { if (this.safeString(message, 'method') === 'PONG') { return; } const error = this.safeDict(message, 'error'); if (error !== undefined) { this.handleErrorMessage(client, message); return; } const stream = this.safeString(message, 'stream'); if (stream !== undefined) { if (stream.indexOf('kline') >= 0 || stream.indexOf('markKline') >= 0) { this.handleOHLCV(client, message); } else if (stream.indexOf('ticker') >= 0) { this.handleTicker(client, message); } } } handleErrorMessage(client, message) { const error = this.safeDict(message, 'error', {}); const code = this.safeString(error, 'code'); const msg = this.safeString(error, 'msg'); const feedback = this.id + ' ' + msg; if (code === '429') { throw new errors.RateLimitExceeded(feedback); } throw new errors.ExchangeError(feedback); } handleOHLCV(client, message) { const stream = this.safeString(message, 'stream'); if (stream === undefined) { return; } const parts = stream.split('@'); const interval = parts[1]; const tf = this.findTimeframe(interval); const data = this.safeDict(message, 'data', {}); const s = this.safeString(data, 's'); if (s === undefined) { return; } const market = this.safeMarket(s.toUpperCase()); const symbol = market['symbol']; const parsed = [ this.safeTimestamp(data, 't'), this.safeNumber(data, 'o'), this.safeNumber(data, 'h'), this.safeNumber(data, 'l'), this.safeNumber(data, 'c'), this.safeNumber(data, 'v'), ]; this.ohlcvs[symbol] = this.safeValue(this.ohlcvs, symbol, {}); let stored = this.safeValue(this.safeValue(this.ohlcvs, symbol), tf); if (stored === undefined) { const limit = this.safeInteger(this.options, 'OHLCVLimit', 1000); stored = new Cache.ArrayCacheByTimestamp(limit); if (symbol !== undefined && tf !== undefined) { this.ohlcvs[symbol][tf] = stored; } } stored.append(parsed); const messageHash = stream; client.resolve(stored, messageHash); } handleTicker(client, message) { const data = this.safeList(message, 'data', []); for (let i = 0; i < data.length; i++) { const t = data[i]; const s = this.safeString(t, 's'); if (s === undefined) { continue; } const market = this.safeMarket(s.toUpperCase()); const symbol = market['symbol']; const timestamp = this.milliseconds(); const last = this.safeNumber(t, 'p'); const result = this.safeTicker({ 'symbol': symbol, 'timestamp': timestamp, 'datetime': this.iso8601(timestamp), 'last': last, 'close': last, 'info': t, }); this.tickers[symbol] = result; const messageHash = 'ticker:' + symbol; client.resolve(result, messageHash); client.resolve(result, 'tickers'); } } } exports["default"] = mudrex;