tardis-dev
Version:
Convenient access to tick-level historical and real-time cryptocurrency market data via Node.js
152 lines • 6.49 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.OkexOptionsRealTimeFeed = exports.OKCoinRealTimeFeed = exports.OkexRealTimeFeed = void 0;
const crypto_1 = __importDefault(require("crypto"));
const handy_1 = require("../handy");
const realtimefeed_1 = require("./realtimefeed");
class OkexRealTimeFeed extends realtimefeed_1.MultiConnectionRealTimeFeedBase {
*_getRealTimeFeeds(exchange, filters, timeoutIntervalMS, onError) {
const nonBusinessFilters = filters.filter((f) => f.channel !== 'trades-all');
if (nonBusinessFilters.length > 0) {
yield new OkexSingleRealTimeFeed('wss://ws.okx.com:8443/ws/v5/public', exchange, nonBusinessFilters, timeoutIntervalMS, onError);
}
const businessFilters = filters.filter((f) => f.channel === 'trades-all');
if (businessFilters.length > 0) {
yield new OkexSingleRealTimeFeed('wss://ws.okx.com:8443/ws/v5/business', exchange, businessFilters, timeoutIntervalMS, onError);
}
}
}
exports.OkexRealTimeFeed = OkexRealTimeFeed;
class OkexSingleRealTimeFeed extends realtimefeed_1.RealTimeFeedBase {
constructor(wssURL, exchange, filters, timeoutIntervalMS, onError) {
super(exchange, filters, timeoutIntervalMS, onError);
this.wssURL = wssURL;
this._hasCredentials = process.env.OKX_API_KEY !== undefined;
}
secondsSinceEpoch() {
return Math.floor(Date.now() / 1000);
}
async onConnected() {
if (this._hasCredentials) {
const timestamp = this.secondsSinceEpoch().toString();
const sign = crypto_1.default.createHmac('sha256', process.env.OKX_API_SECRET_KEY).update(`${timestamp}GET/users/self/verify`).digest('base64');
this.send({
op: 'login',
args: [
{
apiKey: process.env.OKX_API_KEY,
passphrase: process.env.OKX_API_PASSPHRASE,
timestamp,
sign
}
]
});
await (0, handy_1.wait)(50);
}
}
mapToSubscribeMessages(filters) {
const args = filters
.filter((f) => f.channel != 'liquidations')
.map((filter) => {
if (!filter.symbols || filter.symbols.length === 0) {
throw new Error(`${this._exchange} RealTimeFeed requires explicitly specified symbols when subscribing to live feed`);
}
return filter.symbols.map((symbol) => {
if (filter.channel === 'liquidation-orders') {
return {
channel: filter.channel,
instType: symbol.endsWith('SWAP') ? 'SWAP' : 'FUTURES'
};
}
return {
channel: filter.channel,
instId: symbol
};
});
})
.flatMap((s) => s);
return [
{
op: 'subscribe',
args: [...new Set(args)]
}
];
}
messageIsError(message) {
return message.event === 'error';
}
isIgnoredError(message) {
if (message.msg.includes('channel') && message.msg.includes(`doesn't exist`)) {
return true;
}
return false;
}
}
class OKCoinRealTimeFeed extends OkexSingleRealTimeFeed {
constructor(exchange, filters, timeoutIntervalMS, onError) {
super('wss://real.okcoin.com:8443/ws/v5/public', exchange, filters, timeoutIntervalMS, onError);
}
}
exports.OKCoinRealTimeFeed = OKCoinRealTimeFeed;
class OkexOptionsRealTimeFeed extends realtimefeed_1.MultiConnectionRealTimeFeedBase {
*_getRealTimeFeeds(exchange, filters, timeoutIntervalMS, onError) {
const nonBusinessFilters = filters.filter((f) => f.channel !== 'trades-all');
if (nonBusinessFilters.length > 0) {
yield new OkexOptionsSingleRealTimeFeed('wss://ws.okx.com:8443/ws/v5/public', exchange, nonBusinessFilters, timeoutIntervalMS, onError);
}
const businessFilters = filters.filter((f) => f.channel === 'trades-all');
if (businessFilters.length > 0) {
yield new OkexOptionsSingleRealTimeFeed('wss://ws.okx.com:8443/ws/v5/business', exchange, businessFilters, timeoutIntervalMS, onError);
}
}
}
exports.OkexOptionsRealTimeFeed = OkexOptionsRealTimeFeed;
class OkexOptionsSingleRealTimeFeed extends OkexSingleRealTimeFeed {
constructor(wssURL, exchange, filters, timeoutIntervalMS, onError) {
super(wssURL, exchange, filters, timeoutIntervalMS, onError);
this.wssURL = wssURL;
this._defaultIndexes = ['BTC-USD', 'ETH-USD'];
}
_channelRequiresIndexNotSymbol(channel) {
if (channel === 'index-tickers' || channel === 'opt-summary') {
return true;
}
return false;
}
mapToSubscribeMessages(filters) {
const args = filters
.map((filter) => {
let symbols = filter.symbols || [];
const channelRequiresIndexNotSymbol = this._channelRequiresIndexNotSymbol(filter.channel);
if (symbols.length === 0 && channelRequiresIndexNotSymbol) {
symbols = this._defaultIndexes;
}
if (symbols.length === 0) {
throw new Error(`${this._exchange} RealTimeFeed requires explicitly specified symbols when subscribing to live feed`);
}
return symbols.map((symbol) => {
let finalSymbol = symbol;
if (channelRequiresIndexNotSymbol) {
const symbolParts = symbol.split('-');
finalSymbol = `${symbolParts[0]}-${symbolParts[1]}`;
}
return {
channel: filter.channel,
instId: filter.channel !== 'opt-summary' ? finalSymbol : undefined,
instFamily: filter.channel === 'opt-summary' ? finalSymbol : undefined
};
});
})
.flatMap((s) => s);
return [
{
op: 'subscribe',
args: [...new Set(args)]
}
];
}
}
//# sourceMappingURL=okex.js.map