From 19c91502c5c29fa3f0e8dcbd420779f32e2a3677 Mon Sep 17 00:00:00 2001 From: RyanZim Date: Mon, 27 Feb 2017 10:47:28 -0500 Subject: [PATCH] Add exchanges option --- CHANGELOG.md | 1 + README.md | 4 +++ index.js | 4 +++ test/cryptocompare.test.js | 56 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 65 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 250e49d..eb4479e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ Switched to https://min-api.cryptocompare.com/ - added: `histoHour()` - added: `histoMinute()` - added: `generateAvg()` +- added: `exchanges` option for methods that support it 0.0.2 / 2016-11-21 ------------------ diff --git a/README.md b/README.md index 68e45ae..6037b5e 100644 --- a/README.md +++ b/README.md @@ -46,6 +46,7 @@ Get the current price of any cryptocurrency in any other currency. - `tsyms` (Array of Strings | String) To Symbol(s) - `options` (Object) - `tryConversion` (Boolean) By default, if the crypto does not trade directly into the toSymbol requested, BTC will be used for conversion. Set `tryConversion` to `false` to disable using BTC for conversion. + - `exchanges` (Array of Strings | Array) Exchanges to get price data from. By default, average data is used. (You can get a list of top exchanges for a given pair with `topExchanges()`.) ```js const cc = require('cryptocompare') @@ -77,6 +78,7 @@ Works like `price()`, except it allows you to specify a matrix of From Symbols. - `tsyms` (Array of Strings | String) To Symbol(s) - `options` (Object) - `tryConversion` (Boolean) By default, if the crypto does not trade directly into the toSymbol requested, BTC will be used for conversion. Set `tryConversion` to `false` to disable using BTC for conversion. + - `exchanges` (Array of Strings | Array) Exchanges to get price data from. By default, average data is used. (You can get a list of top exchanges for a given pair with `topExchanges()`.) ```js const cc = require('cryptocompare') @@ -109,6 +111,7 @@ Get all the current trading info (price, vol, open, high, low, etc.) of any list - `tsyms` (Array of Strings | String) To Symbol(s) - `options` (Object) - `tryConversion` (Boolean) By default, if the crypto does not trade directly into the toSymbol requested, BTC will be used for conversion. Set `tryConversion` to `false` to disable using BTC for conversion. + - `exchanges` (Array of Strings | Array) Exchanges to get price data from. By default, average data is used. (You can get a list of top exchanges for a given pair with `topExchanges()`.) ```js const cc = require('cryptocompare') @@ -159,6 +162,7 @@ Get the price of any cryptocurrency in any other currency at a given timestamp. - `time` (Date) Date in history that you want price data for - `options` (Object) - `tryConversion` (Boolean) By default, if the crypto does not trade directly into the toSymbol requested, BTC will be used for conversion. Set `tryConversion` to `false` to disable using BTC for conversion. + - `exchanges` (Array of Strings | Array) Exchanges to get price data from. By default, average data is used. (You can get a list of top exchanges for a given pair with `topExchanges()`.) ```js const cc = require('cryptocompare') diff --git a/index.js b/index.js index 84e0e8d..f226056 100644 --- a/index.js +++ b/index.js @@ -15,6 +15,7 @@ function fetchJSON (url) { function price (fsym, tsyms, options) { options = options || {} let url = `${baseUrl}price?fsym=${fsym}&tsyms=${tsyms}` + if (options.exchanges) url += `&e=${options.exchanges}` if (options.tryConversion === false) url += '&tryConversion=false' return fetchJSON(url) } @@ -22,6 +23,7 @@ function price (fsym, tsyms, options) { function priceMulti (fsyms, tsyms, options) { options = options || {} let url = `${baseUrl}pricemulti?fsyms=${fsyms}&tsyms=${tsyms}` + if (options.exchanges) url += `&e=${options.exchanges}` if (options.tryConversion === false) url += '&tryConversion=false' return fetchJSON(url) } @@ -29,6 +31,7 @@ function priceMulti (fsyms, tsyms, options) { function priceFull (fsyms, tsyms, options) { options = options || {} let url = `${baseUrl}pricemultifull?fsyms=${fsyms}&tsyms=${tsyms}` + if (options.exchanges) url += `&e=${options.exchanges}` if (options.tryConversion === false) url += '&tryConversion=false' // We want the RAW data, not the DISPLAY data: return fetchJSON(url).then(result => result.RAW) @@ -38,6 +41,7 @@ function priceHistorical (fsym, tsyms, time, options) { options = options || {} time = dateToTimestamp(time) let url = `${baseUrl}pricehistorical?fsym=${fsym}&tsyms=${tsyms}&ts=${time}` + if (options.exchanges) url += `&e=${options.exchanges}` if (options.tryConversion === false) url += '&tryConversion=false' // The API returns json with an extra layer of nesting, so remove it return fetchJSON(url).then(result => result[fsym]) diff --git a/test/cryptocompare.test.js b/test/cryptocompare.test.js index dea96bb..30391eb 100644 --- a/test/cryptocompare.test.js +++ b/test/cryptocompare.test.js @@ -26,6 +26,28 @@ test("price()'s tryConversion=false works", t => { helpers.testTryConversion(cc.price(helpers.NOT_USD_TRADABLE, 'USD', { tryConversion: false }), t) }) +test("price()'s exchanges option works", t => { + t.plan(1) + Promise.all([ + cc.price('BTC', 'USD'), + cc.price('BTC', 'USD', { exchanges: ['Coinbase'] }) + ]).then(data => { + t.notDeepEqual(data[0], data[1]) + t.end() + }) +}) + +test('exchanges option allows passing a string', t => { + t.plan(1) + Promise.all([ + cc.price('BTC', 'USD'), + cc.price('BTC', 'USD', { exchanges: 'Coinbase' }) + ]).then(data => { + t.notDeepEqual(data[0], data[1]) + t.end() + }) +}) + test('priceMulti()', t => { t.plan(4) cc.priceMulti(['BTC', 'ETH'], ['USD', 'EUR']).then(prices => { @@ -49,6 +71,17 @@ test("priceMulti()'s tryConversion=false works", t => { helpers.testTryConversion(cc.priceMulti(helpers.NOT_USD_TRADABLE, 'USD', { tryConversion: false }), t) }) +test("priceMulti()'s exchanges option works", t => { + t.plan(1) + Promise.all([ + cc.priceMulti('BTC', 'USD'), + cc.priceMulti('BTC', 'USD', { exchanges: ['Coinbase'] }) + ]).then(data => { + t.notDeepEqual(data[0], data[1]) + t.end() + }) +}) + test('priceFull()', t => { t.plan(5 * 2 * 2) cc.priceFull(['BTC', 'ETH'], ['USD', 'EUR']).then(prices => { @@ -83,6 +116,17 @@ test("priceFull()'s tryConversion=false works", t => { helpers.testTryConversion(cc.priceFull(helpers.NOT_USD_TRADABLE, 'USD', { tryConversion: false }), t) }) +test("priceFull()'s exchanges option works", t => { + t.plan(1) + Promise.all([ + cc.priceFull('BTC', 'USD'), + cc.priceFull('BTC', 'USD', { exchanges: ['Coinbase'] }) + ]).then(data => { + t.notDeepEqual(data[0], data[1]) + t.end() + }) +}) + test('priceHistorical()', t => { t.plan(3) // NOTE: Historical values are hard-coded into this test @@ -106,6 +150,18 @@ test("priceHistorical()'s tryConversion=false works", t => { ), t) }) +test("priceHistorical()'s exchanges option works", t => { + t.plan(1) + const timestamp = new Date() + Promise.all([ + cc.priceHistorical('BTC', 'USD', timestamp), + cc.priceHistorical('BTC', 'USD', timestamp, { exchanges: ['Coinbase'] }) + ]).then(data => { + t.notDeepEqual(data[0], data[1]) + t.end() + }) +}) + test('generateAvg()', t => { t.plan(6) cc.generateAvg('BTC', 'USD', ['Coinbase', 'Kraken', 'Bitstamp', 'Bitfinex']).then(data => {