tardis-dev
Version:
Convenient access to tick-level historical and real-time cryptocurrency market data via Node.js
187 lines • 7.16 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.DydxV4LiquidationsMapper = exports.DydxV4DerivativeTickerMapper = exports.DydxV4BookChangeMapper = exports.DydxV4TradesMapper = void 0;
const handy_1 = require("../handy");
const mapper_1 = require("./mapper");
class DydxV4TradesMapper {
canHandle(message) {
return message.channel === 'v4_trades' && message.type === 'channel_data';
}
getFilters(symbols) {
symbols = (0, handy_1.upperCaseSymbols)(symbols);
return [
{
channel: 'v4_trades',
symbols
}
];
}
*map(message, localTimestamp) {
for (let trade of message.contents.trades) {
yield {
type: 'trade',
symbol: message.id,
exchange: 'dydx-v4',
id: trade.id,
price: Number(trade.price),
amount: Number(trade.size),
side: trade.side === 'SELL' ? 'sell' : 'buy',
timestamp: trade.createdAt ? new Date(trade.createdAt) : localTimestamp,
localTimestamp: localTimestamp
};
}
}
}
exports.DydxV4TradesMapper = DydxV4TradesMapper;
function mapSnapshotPriceLevel(level) {
return {
price: Number(level.price),
amount: Number(level.size)
};
}
function mapUpdatePriceLevel(level) {
return {
price: Number(level[0]),
amount: Number(level[1])
};
}
class DydxV4BookChangeMapper {
canHandle(message) {
return message.channel === 'v4_orderbook';
}
getFilters(symbols) {
symbols = (0, handy_1.upperCaseSymbols)(symbols);
return [
{
channel: 'v4_orderbook',
symbols
}
];
}
*map(message, localTimestamp) {
if (message.type === 'subscribed') {
yield {
type: 'book_change',
symbol: message.id,
exchange: 'dydx-v4',
isSnapshot: true,
bids: message.contents.bids.map(mapSnapshotPriceLevel),
asks: message.contents.asks.map(mapSnapshotPriceLevel),
timestamp: localTimestamp,
localTimestamp
};
}
else {
if (!message.contents) {
return;
}
const bookChange = {
type: 'book_change',
symbol: message.id,
exchange: 'dydx-v4',
isSnapshot: false,
bids: message.contents.bids !== undefined ? message.contents.bids.map(mapUpdatePriceLevel) : [],
asks: message.contents.asks !== undefined ? message.contents.asks.map(mapUpdatePriceLevel) : [],
timestamp: localTimestamp,
localTimestamp
};
if (bookChange.bids.length > 0 || bookChange.asks.length > 0) {
yield bookChange;
}
}
}
}
exports.DydxV4BookChangeMapper = DydxV4BookChangeMapper;
class DydxV4DerivativeTickerMapper {
pendingTickerInfoHelper = new mapper_1.PendingTickerInfoHelper();
canHandle(message) {
return message.channel === 'v4_markets' || (message.channel === 'v4_trades' && message.type === 'channel_data');
}
getFilters(symbols) {
symbols = (0, handy_1.upperCaseSymbols)(symbols);
return [
{
channel: 'v4_markets',
symbols: []
},
{
channel: 'v4_trades',
symbols
}
];
}
*map(message, localTimestamp) {
if (message.channel === 'v4_trades') {
const pendingTickerInfo = this.pendingTickerInfoHelper.getPendingTickerInfo(message.id, 'dydx-v4');
pendingTickerInfo.updateLastPrice(Number(message.contents.trades[message.contents.trades.length - 1].price));
return;
}
if (message.type === 'subscribed' || (message.type === 'channel_data' && message.contents.trading !== undefined)) {
const contents = message.type === 'subscribed' ? message.contents.markets : message.contents.trading;
for (const key in contents) {
const marketInfo = contents[key];
const pendingTickerInfo = this.pendingTickerInfoHelper.getPendingTickerInfo(key, 'dydx-v4');
if (marketInfo.oraclePrice !== undefined) {
pendingTickerInfo.updateMarkPrice(Number(marketInfo.oraclePrice));
}
if (marketInfo.openInterest !== undefined) {
pendingTickerInfo.updateOpenInterest(Number(marketInfo.openInterest));
}
if (marketInfo.nextFundingRate !== undefined) {
pendingTickerInfo.updateFundingRate(Number(marketInfo.nextFundingRate));
}
pendingTickerInfo.updateTimestamp(localTimestamp);
if (pendingTickerInfo.hasChanged()) {
yield pendingTickerInfo.getSnapshot(localTimestamp);
}
}
}
if (message.type === 'channel_data' && message.contents.oraclePrices !== undefined) {
for (const key in message.contents.oraclePrices) {
const oraclePriceInfo = message.contents.oraclePrices[key];
const pendingTickerInfo = this.pendingTickerInfoHelper.getPendingTickerInfo(key, 'dydx-v4');
if (oraclePriceInfo.oraclePrice !== undefined) {
pendingTickerInfo.updateMarkPrice(Number(oraclePriceInfo.oraclePrice));
}
pendingTickerInfo.updateTimestamp(localTimestamp);
if (pendingTickerInfo.hasChanged()) {
yield pendingTickerInfo.getSnapshot(localTimestamp);
}
}
}
}
}
exports.DydxV4DerivativeTickerMapper = DydxV4DerivativeTickerMapper;
class DydxV4LiquidationsMapper {
canHandle(message) {
return message.channel === 'v4_trades' && message.type === 'channel_data';
}
getFilters(symbols) {
symbols = (0, handy_1.upperCaseSymbols)(symbols);
return [
{
channel: 'v4_trades',
symbols
}
];
}
*map(message, localTimestamp) {
for (let trade of message.contents.trades) {
if (trade.type === 'LIQUIDATED') {
yield {
type: 'liquidation',
symbol: message.id,
exchange: 'dydx-v4',
id: trade.id,
price: Number(trade.price),
amount: Number(trade.size),
side: trade.side === 'SELL' ? 'sell' : 'buy',
timestamp: trade.createdAt ? new Date(trade.createdAt) : localTimestamp,
localTimestamp: localTimestamp
};
}
}
}
}
exports.DydxV4LiquidationsMapper = DydxV4LiquidationsMapper;
//# sourceMappingURL=dydxv4.js.map