exchange/bitfinexv2.js
/**
     * Open the socket connection and attaches handles to events we need to know about
     * @returns {Promise<any>}
     */
Open the socket connection and attaches handles to events we need to know about
@returns {Promise}
Open the socket connection and attaches handles to events we need to know about @returns { Promise }
init(symbols) {
        const self = this;
        this.calcs = symbols.map(symbol => `wallet_funding_${symbol}`);
        return new Promise((resolve) => {
            const ws = self.ws;
            ws.on('error', (err) => {
                logger.error('Error detected on socket connection');
                logger.error(err);
            });
            ws.on('open', () => { logger.debug('socket opened'); ws.auth(); });
            ws.on('close', () => { logger.debug('socket closed'); });
            ws.once('auth', () => {
                logger.progress('bfx v2 API Authenticated');
                logger.progress(symbols);
                symbols.forEach((symbol) => {
                    const bfxSymbol = `f${symbol}`;
                    this.state.lastRates[symbol] = 0;
                    ws.subscribeTicker(bfxSymbol);
                    ws.subscribeTrades(bfxSymbol);
                });
                this.refreshAvailableFunds();
                setTimeout(() => {
                    resolve();
                }, 1000);
            });
            ws.onWalletSnapshot({}, (wallet) => {
                logger.debug('wallet snapshot');
                self.onWalletUpdate(wallet);
                self.refreshAvailableFunds();
            });
            ws.onWalletUpdate({}, (wallet) => {
                logger.debug('wallet update');
                self.onWalletUpdate([wallet]);
            });
            symbols.forEach((symbol) => {
                const eventFilter = { symbol: `f${symbol}` };
                logger.debug(`Register handlers for ${symbol}`);
                ws.onTicker(eventFilter, (ticker) => {
                    logger.debug(`ticker for ${symbol}`);
                    this.onTicker(ticker);
                });
                ws.onTrades(eventFilter, (trades) => {
                    trades.forEach((trade) => {
                        const previous = this.state.lastRates[symbol];
                        this.state.lastRates[symbol] = +trade.rate;
                        if (trade.rate !== previous) {
                            this.fundingRateChanged(symbol, previous, +trade.rate);
                        }
                    });
                });
                ws.onFundingOfferSnapshot(eventFilter, (offers) => {
                    logger.debug(`${symbol} funding offer snapshot`);
                    this.onFundingOffers(offers.map(offer => offer.toJS()));
                });
                ws.onFundingOfferNew(eventFilter, (offer) => {
                    logger.debug(`${symbol} funding offer created`);
                    this.onFundingOffers([offer.toJS()]);
                });
                ws.onFundingOfferUpdate(eventFilter, (offer) => {
                    logger.debug(`${symbol} funding offer updated`);
                    this.onFundingOffers([offer.toJS()]);
                });
                ws.onFundingOfferClose(eventFilter, (offer) => {
                    logger.debug(`${symbol} funding offer closed`);
                    this.onRemoveFundingOffers([offer.toJS()]);
                });
                ws.onFundingCreditSnapshot(eventFilter, (loans) => {
                    logger.debug(`${symbol} funding credits snapshot`);
                    this.onLoans(loans.map(loan => loan.toJS()));
                });
                ws.onFundingCreditNew(eventFilter, (loan) => {
                    logger.debug(`${symbol} funding credit created`);
                    this.onLoans([loan.toJS()]);
                });
                ws.onFundingCreditUpdate(eventFilter, (loan) => {
                    logger.debug(`${symbol} funding credit updated`);
                    this.onLoans([loan.toJS()]);
                });
                ws.onFundingCreditClose(eventFilter, (loan) => {
                    logger.debug(`${symbol} funding credit closed`);
                    this.onRemoveLoans([loan.toJS()]);
                });
            });
            ws.open();
        });
    }
init ( symbols ) { const self = this ; this . calcs = symbols . map ( symbol => ` ${ symbol } ` ) ; return new Promise ( ( resolve ) => { const ws = self . ws ; ws . on ( 'error' , ( err ) => { logger . error ( 'Error detected on socket connection' ) ; logger . error ( err ) ; } ) ; ws . on ( 'open' , ( ) => { logger . debug ( 'socket opened' ) ; ws . auth ( ) ; } ) ; ws . on ( 'close' , ( ) => { logger . debug ( 'socket closed' ) ; } ) ; ws . once ( 'auth' , ( ) => { logger . progress ( 'bfx v2 API Authenticated' ) ; logger . progress ( symbols ) ; symbols . forEach ( ( symbol ) => { const bfxSymbol = ` ${ symbol } ` ; this . state . lastRates [ symbol ] = 0 ; ws . subscribeTicker ( bfxSymbol ) ; ws . subscribeTrades ( bfxSymbol ) ; } ) ; this . refreshAvailableFunds ( ) ; setTimeout ( ( ) => { resolve ( ) ; } , 1000 ) ; } ) ; ws . onWalletSnapshot ( { } , ( wallet ) => { logger . debug ( 'wallet snapshot' ) ; self . onWalletUpdate ( wallet ) ; self . refreshAvailableFunds ( ) ; } ) ; ws . onWalletUpdate ( { } , ( wallet ) => { logger . debug ( 'wallet update' ) ; self . onWalletUpdate ( [ wallet ] ) ; } ) ; symbols . forEach ( ( symbol ) => { const eventFilter = { symbol : ` ${ symbol } ` } ; logger . debug ( ` ${ symbol } ` ) ; ws . onTicker ( eventFilter , ( ticker ) => { logger . debug ( ` ${ symbol } ` ) ; this . onTicker ( ticker ) ; } ) ; ws . onTrades ( eventFilter , ( trades ) => { trades . forEach ( ( trade ) => { const previous = this . state . lastRates [ symbol ] ; this . state . lastRates [ symbol ] = + trade . rate ; if ( trade . rate !== previous ) { this . fundingRateChanged ( symbol , previous , + trade . rate ) ; } } ) ; } ) ; ws . onFundingOfferSnapshot ( eventFilter , ( offers ) => { logger . debug ( ` ${ symbol } ` ) ; this . onFundingOffers ( offers . map ( offer => offer . toJS ( ) ) ) ; } ) ; ws . onFundingOfferNew ( eventFilter , ( offer ) => { logger . debug ( ` ${ symbol } ` ) ; this . onFundingOffers ( [ offer . toJS ( ) ] ) ; } ) ; ws . onFundingOfferUpdate ( eventFilter , ( offer ) => { logger . debug ( ` ${ symbol } ` ) ; this . onFundingOffers ( [ offer . toJS ( ) ] ) ; } ) ; ws . onFundingOfferClose ( eventFilter , ( offer ) => { logger . debug ( ` ${ symbol } ` ) ; this . onRemoveFundingOffers ( [ offer . toJS ( ) ] ) ; } ) ; ws . onFundingCreditSnapshot ( eventFilter , ( loans ) => { logger . debug ( ` ${ symbol } ` ) ; this . onLoans ( loans . map ( loan => loan . toJS ( ) ) ) ; } ) ; ws . onFundingCreditNew ( eventFilter , ( loan ) => { logger . debug ( ` ${ symbol } ` ) ; this . onLoans ( [ loan . toJS ( ) ] ) ; } ) ; ws . onFundingCreditUpdate ( eventFilter , ( loan ) => { logger . debug ( ` ${ symbol } ` ) ; this . onLoans ( [ loan . toJS ( ) ] ) ; } ) ; ws . onFundingCreditClose ( eventFilter , ( loan ) => { logger . debug ( ` ${ symbol } ` ) ; this . onRemoveLoans ( [ loan . toJS ( ) ] ) ; } ) ; } ) ; ws . open ( ) ; } ) ; }
Open the socket connection and attaches handles to events we need to know about
 @returns {Promise<any>}
Open the socket connection and attaches handles to events we need to know about @returns { Promise<any > }
// ws.on('message', msg => console.log(msg)); // Happens once when we are authenticated. We use this to complete set up // subscribe tickers // calc the wallet // give it a little bit of time to settle. // Some handlers to track the state our of wallet // Every time the price changes, this happens. // Funds I'm offering to lend out // Limit orders offering my BTC for funding // remove an older version of this order // Lent out funds // These are funds I have lent to someone - waiting for them to give them back. // Now all the handlers are set up, open the connection