UNPKG

@melonproject/ea-bitfinex

Version:
54 lines (53 loc) 2.45 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const operators_1 = require("rxjs/operators"); const bignumber_js_1 = __importDefault(require("bignumber.js")); const socket_1 = require("./socket"); const watchBook = (options) => { const subProps = Object.assign(Object.assign(Object.assign({ channel: 'book', symbol: options.symbol }, (options.prec && { prec: options.prec })), (options.freq && { freq: options.freq })), (options.length && { length: options.length })); const subscribe = () => (Object.assign({ event: 'subscribe' }, subProps)); return socket_1.socket(subscribe); }; exports.watchAssetPair = (pair, options) => { const stream$ = watchBook(Object.assign({ symbol: `t${pair.split('/').join('')}` }, options)); return stream$.pipe(operators_1.map(value => { if (Array.isArray(value[0])) { const snapshot = value; return snapshot.map(([price, count, amount]) => ({ price: new bignumber_js_1.default(price), count: new bignumber_js_1.default(count), amount: new bignumber_js_1.default(amount), })); } const [price, count, amount] = value; return { price: new bignumber_js_1.default(price), count: new bignumber_js_1.default(count), amount: new bignumber_js_1.default(amount), }; })); }; exports.watchCurrency = (symbol, options) => { const stream$ = watchBook(Object.assign({ symbol: `f${symbol}` }, options)); return stream$.pipe(operators_1.map(value => { if (Array.isArray(value[0])) { const snapshot = value; return snapshot.map(([rate, period, count, amount]) => ({ rate: new bignumber_js_1.default(rate), period: new bignumber_js_1.default(period), count: new bignumber_js_1.default(count), amount: new bignumber_js_1.default(amount), })); } const [rate, period, count, amount] = value; return { rate: new bignumber_js_1.default(rate), period: new bignumber_js_1.default(period), count: new bignumber_js_1.default(count), amount: new bignumber_js_1.default(amount), }; })); };