UNPKG

tlab-trading-toolkit

Version:

A trading toolkit for building advanced trading bots on the GDAX platform

62 lines (61 loc) 2.67 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const ProductMap_1 = require("../exchanges/ProductMap"); const BittrexAPI_1 = require("../exchanges/bittrex/BittrexAPI"); const BittrexFeed_1 = require("../exchanges/bittrex/BittrexFeed"); let publicAPIInstance; function getExchangeProduct(genericProduct) { return ProductMap_1.ProductMap.ExchangeMap.get('Bittrex').getExchangeProduct(genericProduct); } /** * A convenience function that returns a GDAXExchangeAPI instance for accessing REST methods conveniently. If API * key details are found in the GDAX_KEY etc. envars, they will be used */ function DefaultAPI(logger) { if (!publicAPIInstance) { publicAPIInstance = new BittrexAPI_1.BittrexAPI({ key: process.env.BITTREX_KEY, secret: process.env.BITTREX_SECRET }, logger); } return publicAPIInstance; } exports.DefaultAPI = DefaultAPI; /** * Convenience function to connect to and subscribe to the given channels. Bittrex uses SignalR, which handles reconnects for us, * so this is a much simpler function than some of the other exchanges' methods. */ function getSubscribedFeeds(options, products) { return new Promise((resolve, reject) => { const feed = new BittrexFeed_1.BittrexFeed(options); const timeout = setTimeout(() => { return reject(new Error('TIMEOUT. Could not connect to Bittrex Feed server')); }, 30000); feed.on('websocket-connection', () => { feed.subscribe(products); clearTimeout(timeout); return resolve(feed); }); }); } exports.getSubscribedFeeds = getSubscribedFeeds; /** * This is a straightforward wrapper around getSubscribedFeeds using the Factory pattern with the most commonly used * defaults. For customised feeds, use getSubscribedFeeds instead. It's really not adding much, but we keep it here * to maintain a consistent method naming strategy amongst all the exchanges * * It is assumed that your API keys are stored in the BITTREX_KEY and BITTREX_SECRET envars */ function FeedFactory(logger, productIds, auth) { auth = auth || { key: process.env.BITTREX_KEY, secret: process.env.BITTREX_SECRET }; productIds = productIds.map((genericProduct) => { console.log('Product ID', genericProduct); return getExchangeProduct(genericProduct) || genericProduct; }); // There are too many books on Bittrex to just subscribe to all of them, so productIds is a required param return getSubscribedFeeds({ auth: auth, logger: logger, wsUrl: null }, productIds); } exports.FeedFactory = FeedFactory;