Skip to content

Commit

Permalink
Add exchanges option
Browse files Browse the repository at this point in the history
  • Loading branch information
RyanZim committed Feb 27, 2017
1 parent ecc6590 commit 19c9150
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
------------------
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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')
Expand Down
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,23 @@ 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)
}

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)
}

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)
Expand All @@ -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])
Expand Down
56 changes: 56 additions & 0 deletions test/cryptocompare.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand All @@ -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 => {
Expand Down Expand Up @@ -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
Expand All @@ -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 => {
Expand Down

0 comments on commit 19c9150

Please sign in to comment.