Skip to content

Commit

Permalink
Add priceFull()
Browse files Browse the repository at this point in the history
  • Loading branch information
RyanZim committed Feb 25, 2017
1 parent 04fa44b commit e34e81b
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Switched to https://min-api.cryptocompare.com/
- changed: `price()` returns a different data structure
- changed: `priceHistorical()` returns a different data structure
- added: `priceMulti()`
- added: `priceFull()`

0.0.2 / 2016-11-21
------------------
Expand Down
48 changes: 48 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,54 @@ cc.priceMulti('BTC', 'USD')
.catch(console.error)
```

### `priceFull()`

Get all the current trading info (price, vol, open, high, low etc) of any list of cryptocurrencies in any other currency.

`priceFull(fsym, tsyms[, tryConversion])`

- `fsyms` (Array of Strings | String) From Symbol(s)
- `tsyms` (Array of Strings | String) To Symbol(s)
- `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.

```js
const cc = require('cryptocompare')

cc.priceFull(['BTC', 'ETH'], ['USD', 'EUR'])
.then(prices => {
console.log(prices)
// {
// BTC: {
// USD: {
// TYPE: '5',
// MARKET: 'CCCAGG',
// FROMSYMBOL: 'BTC',
// TOSYMBOL: 'USD',
// FLAGS: '4',
// PRICE: 1152.42,
// LASTUPDATE: 1487865689,
// LASTVOLUME: 0.21,
// LASTVOLUMETO: 242.20349999999996,
// LASTTRADEID: 1224703,
// VOLUME24HOUR: 53435.45299122338,
// VOLUME24HOURTO: 60671593.843186244,
// OPEN24HOUR: 1119.31,
// HIGH24HOUR: 1170,
// LOW24HOUR: 1086.641,
// LASTMARKET: 'itBit',
// CHANGE24HOUR: 33.11000000000013,
// CHANGEPCT24HOUR: 2.958072383879366,
// SUPPLY: 16177825,
// MKTCAP: 18643649086.5
// },
// EUR: ...
// },
// ETH: ...
// }
})
.catch(console.error)
```

### priceHistorical

`priceHistorical(fsym, tsyms, time[, tryConversion])`
Expand Down
8 changes: 8 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ function priceMulti (fsyms, tsyms, tryConversion) {
return fetchJSON(url)
}

function priceFull (fsyms, tsyms, tryConversion) {
let url = `${baseUrl}pricemultifull?fsyms=${fsyms}&tsyms=${tsyms}`
if (tryConversion === false) url += '&tryConversion=false'
// We want the RAW data, not the DISPLAY data:
return fetchJSON(url).then(result => result.RAW)
}

function priceHistorical (fsym, tsyms, time, tryConversion) {
if (!(time instanceof Date)) throw new Error('time parameter must be an instance of Date.')
time = Math.floor(time.getTime() / 1000)
Expand All @@ -36,5 +43,6 @@ function priceHistorical (fsym, tsyms, time, tryConversion) {
module.exports = {
price,
priceMulti,
priceFull,
priceHistorical
}
30 changes: 30 additions & 0 deletions test/cryptocompare.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,36 @@ test('priceMulti() allows passing strings instead of arrays', t => {
}).catch(t.end)
})

test('priceFull()', t => {
t.plan(5 * 2 * 2)
cc.priceFull(['BTC', 'ETH'], ['USD', 'EUR']).then(prices => {
for (let fsym in prices) {
for (let tsym in prices[fsym]) {
t.is(typeof prices[fsym][tsym].PRICE, 'number', `prices.${fsym}.${tsym}.PRICE is a number`)
t.is(typeof prices[fsym][tsym].SUPPLY, 'number', `prices.${fsym}.${tsym}.SUPPLY is a number`)
t.is(typeof prices[fsym][tsym].MKTCAP, 'number', `prices.${fsym}.${tsym}.MKTCAP is a number`)

t.is(typeof prices[fsym][tsym].MARKET, 'string', `prices.${fsym}.${tsym}.MARKET is a string`)
t.is(typeof prices[fsym][tsym].LASTMARKET, 'string', `prices.${fsym}.${tsym}.LASTMARKET is a string`)
}
}
t.end()
}).catch(t.end)
})

test('priceFull() allows passing strings instead of arrays', t => {
t.plan(5)
cc.priceFull('BTC', 'USD').then(prices => {
t.is(typeof prices.BTC.USD.PRICE, 'number', `prices.BTC.USD.PRICE is a number`)
t.is(typeof prices.BTC.USD.SUPPLY, 'number', `prices.BTC.USD.SUPPLY is a number`)
t.is(typeof prices.BTC.USD.MKTCAP, 'number', `prices.BTC.USD.MKTCAP is a number`)

t.is(typeof prices.BTC.USD.MARKET, 'string', `prices.BTC.USD.MARKET is a string`)
t.is(typeof prices.BTC.USD.LASTMARKET, 'string', `prices.BTC.USD.LASTMARKET is a string`)
t.end()
}).catch(t.end)
})

test('priceHistorical()', t => {
t.plan(3)
// NOTE: Historical values are hard-coded into this test
Expand Down

0 comments on commit e34e81b

Please sign in to comment.