UNPKG

tardis-dev

Version:

Convenient access to tick-level historical and real-time cryptocurrency market data via Node.js

113 lines 3.87 kB
import { parseμs, upperCaseSymbols } from "../handy.js"; export const bitflyerTradesMapper = { canHandle(message) { return message.params.channel.startsWith('lightning_executions'); }, getFilters(symbols) { symbols = upperCaseSymbols(symbols); return [ { channel: 'lightning_executions', symbols } ]; }, *map(bitflyerExecutions, localTimestamp) { const symbol = bitflyerExecutions.params.channel.replace('lightning_executions_', ''); for (const execution of bitflyerExecutions.params.message) { const timestamp = new Date(execution.exec_date); timestamp.μs = parseμs(execution.exec_date); const trade = { type: 'trade', symbol, exchange: 'bitflyer', id: String(execution.id), price: execution.price, amount: execution.size, side: execution.side === 'BUY' ? 'buy' : execution.side === 'SELL' ? 'sell' : 'unknown', timestamp, localTimestamp: localTimestamp }; yield trade; } } }; const mapBookLevel = ({ price, size }) => { return { price, amount: size }; }; export class BitflyerBookChangeMapper { _snapshotsInfo = new Map(); canHandle(message) { return message.params.channel.startsWith('lightning_board'); } getFilters(symbols) { symbols = upperCaseSymbols(symbols); return [ { channel: 'lightning_board_snapshot', symbols }, { channel: 'lightning_board', symbols } ]; } *map(bitflyerBoard, localTimestamp) { const channel = bitflyerBoard.params.channel; const isSnapshot = channel.startsWith('lightning_board_snapshot_'); const symbol = isSnapshot ? channel.replace('lightning_board_snapshot_', '') : channel.replace('lightning_board_', ''); if (this._snapshotsInfo.has(symbol) === false) { if (isSnapshot) { this._snapshotsInfo.set(symbol, true); } else { // skip change messages until we've received book snapshot return; } } yield { type: 'book_change', symbol, exchange: 'bitflyer', isSnapshot, bids: bitflyerBoard.params.message.bids.map(mapBookLevel), asks: bitflyerBoard.params.message.asks.map(mapBookLevel), timestamp: localTimestamp, localTimestamp }; } } export const bitflyerBookTickerMapper = { canHandle(message) { return message.params.channel.startsWith('lightning_ticker'); }, getFilters(symbols) { symbols = upperCaseSymbols(symbols); return [ { channel: 'lightning_ticker', symbols } ]; }, *map(bitflyerTickerMessage, localTimestamp) { const symbol = bitflyerTickerMessage.params.channel.replace('lightning_ticker_', ''); const bitflyerTicker = bitflyerTickerMessage.params.message; const timestamp = new Date(bitflyerTicker.timestamp); timestamp.μs = parseμs(bitflyerTicker.timestamp); const ticker = { type: 'book_ticker', symbol, exchange: 'bitflyer', askAmount: bitflyerTicker.best_ask_size, askPrice: bitflyerTicker.best_ask, bidPrice: bitflyerTicker.best_bid, bidAmount: bitflyerTicker.best_bid_size, timestamp, localTimestamp: localTimestamp }; yield ticker; } }; //# sourceMappingURL=bitflyer.js.map