|
1 | 1 | import BigNumber from 'bignumber.js';
|
2 |
| -import { Observable, throwError } from 'rxjs'; |
3 |
| -import { catchError, share, distinctUntilChanged } from 'rxjs/operators'; |
4 |
| -import WebSocket from 'ws'; |
| 2 | +import { Observable } from 'rxjs'; |
5 | 3 | import { Config } from '../config';
|
6 | 4 | import { Logger } from '../logger';
|
7 |
| -import { errors } from '../opendex/errors'; |
| 5 | + |
| 6 | +type GetPriceParams = { |
| 7 | + config: Config; |
| 8 | + logger: Logger; |
| 9 | +}; |
8 | 10 |
|
9 | 11 | type CentralizedExchangePriceParams = {
|
10 | 12 | config: Config;
|
11 | 13 | logger: Logger;
|
| 14 | + getKrakenPrice$: ({ |
| 15 | + config, |
| 16 | + logger, |
| 17 | + }: GetPriceParams) => Observable<BigNumber>; |
| 18 | + getBinancePrice$: ({ |
| 19 | + config, |
| 20 | + logger, |
| 21 | + }: GetPriceParams) => Observable<BigNumber>; |
12 | 22 | };
|
13 | 23 |
|
14 | 24 | const getCentralizedExchangePrice$ = ({
|
15 | 25 | config,
|
16 | 26 | logger,
|
| 27 | + getKrakenPrice$, |
| 28 | + getBinancePrice$, |
17 | 29 | }: CentralizedExchangePriceParams): Observable<BigNumber> => {
|
18 |
| - const priceObservable: Observable<BigNumber> = new Observable(observer => { |
19 |
| - const tradingPair = `${config.CEX_BASEASSET}${config.CEX_QUOTEASSET}`; |
20 |
| - const url = `wss://stream.binance.com:9443/ws/${tradingPair.toLowerCase()}@aggTrade`; |
21 |
| - const socket = new WebSocket(url); |
22 |
| - socket.onopen = () => { |
23 |
| - logger.trace(`${tradingPair} established connection to ${url}`); |
24 |
| - }; |
25 |
| - socket.on('error', e => { |
26 |
| - observer.error(e); |
27 |
| - }); |
28 |
| - const heartbeat = () => { |
29 |
| - logger.trace(`heartbeat from ${tradingPair} socket`); |
30 |
| - }; |
31 |
| - socket.onclose = (event: WebSocket.CloseEvent) => { |
32 |
| - if (event.reason) { |
33 |
| - logger.trace( |
34 |
| - `${tradingPair} stream closed with reason: ${event.reason}` |
35 |
| - ); |
36 |
| - } else { |
37 |
| - logger.trace(`${tradingPair} stream closed`); |
38 |
| - } |
39 |
| - }; |
40 |
| - socket.on('ping', heartbeat); |
41 |
| - socket.on('open', heartbeat); |
42 |
| - socket.onmessage = (event: WebSocket.MessageEvent) => { |
43 |
| - const aggTrade = JSON.parse(event.data.toString()); |
44 |
| - const { p: priceString } = aggTrade; |
45 |
| - const price = new BigNumber(priceString); |
46 |
| - observer.next(price); |
47 |
| - }; |
48 |
| - return () => { |
49 |
| - socket.terminate(); |
50 |
| - }; |
51 |
| - }); |
52 |
| - return priceObservable.pipe( |
53 |
| - catchError(() => { |
54 |
| - return throwError(errors.CENTRALIZED_EXCHANGE_PRICE_FEED_ERROR); |
55 |
| - }), |
56 |
| - distinctUntilChanged((a, b) => a.isEqualTo(b)), |
57 |
| - share() |
58 |
| - ); |
| 30 | + switch (config.CEX) { |
| 31 | + case 'BINANCE': |
| 32 | + return getBinancePrice$({ config, logger }); |
| 33 | + case 'KRAKEN': |
| 34 | + return getKrakenPrice$({ config, logger }); |
| 35 | + default: |
| 36 | + throw new Error( |
| 37 | + `Could not get price feed for unknown exchange: ${config.CEX}` |
| 38 | + ); |
| 39 | + } |
59 | 40 | };
|
60 | 41 |
|
61 |
| -export { getCentralizedExchangePrice$, CentralizedExchangePriceParams }; |
| 42 | +export { |
| 43 | + getCentralizedExchangePrice$, |
| 44 | + CentralizedExchangePriceParams, |
| 45 | + GetPriceParams, |
| 46 | +}; |
0 commit comments