Socket
Socket
Sign inDemoInstall

cryptocompare

Package Overview
Dependencies
0
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.0.2 to 0.1.0

test/helpers/index.js

20

CHANGELOG.md

@@ -1,4 +0,20 @@

Unreleased
----------
0.1.0 / 2017-04-04
------------------
Switched to https://min-api.cryptocompare.com/
- removed: `coinList()`
- changed: `price()` returns a different data structure
- changed: `priceHistorical()` returns a different data structure
- changed: `price()` and `priceHistorical)()` function signature has changed
- added: `priceMulti()`
- added: `priceFull()`
- added: `topPairs()`
- added: `topExchanges()`
- added: `histoDay()`
- added: `histoHour()`
- added: `histoMinute()`
- added: `generateAvg()`
- added: `exchanges` option for methods that support it
0.0.2 / 2016-11-21

@@ -5,0 +21,0 @@ ------------------

@@ -0,4 +1,5 @@

'use strict'
/* global fetch */
var baseUrl = 'https://www.cryptocompare.com/api/data/'
const baseUrl = 'https://min-api.cryptocompare.com/data/'

@@ -9,32 +10,110 @@ function fetchJSON (url) {

.then(body => {
// 'response' is a CryptoCompare field
if (body.Response !== 'Success') throw new Error(body.Message)
return body.Data
if (body.Response === 'Error') throw body.Message
return body
})
}
function coinList () {
var url = baseUrl + 'coinlist/'
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 price (fsym, tsyms, useBtc) {
if (!Array.isArray(tsyms)) tsyms = [tsyms]
var url = baseUrl + 'price?fsym=' + fsym + '&tsyms=' + tsyms.join(',')
if (useBtc) url += 'usebtc=true'
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 priceHistorical (fsym, tsyms, time) {
if (!Array.isArray(tsyms)) tsyms = [tsyms]
if (!time instanceof Date) throw new Error('time parameter must be an instance of Date.')
time = Math.floor(time.getTime() / 1000)
var url = baseUrl + 'pricehistorical?fsym=' + fsym + '&tsyms=' + tsyms.join(',') + '&ts=' + time
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)
}
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])
}
function generateAvg (fsym, tsym, markets, tryConversion) {
let url = `${baseUrl}generateAvg?fsym=${fsym}&tsym=${tsym}&markets=${markets}`
if (tryConversion === false) url += '&tryConversion=false'
return fetchJSON(url).then(result => result.RAW)
}
function topPairs (fsym, limit) {
let url = `${baseUrl}top/pairs?fsym=${fsym}`
if (limit) url += `&limit=${limit}`
return fetchJSON(url).then(result => result.Data)
}
function topExchanges (fsym, tsym, limit) {
let url = `${baseUrl}top/exchanges?fsym=${fsym}&tsym=${tsym}`
if (limit) url += `&limit=${limit}`
return fetchJSON(url).then(result => result.Data)
}
function histoDay (fsym, tsym, options) {
options = options || {}
if (options.timestamp) options.timestamp = dateToTimestamp(options.timestamp)
let url = `${baseUrl}histoday?fsym=${fsym}&tsym=${tsym}`
if (options.limit === 'none') url += '&allData=true'
else if (options.limit) url += `&limit=${options.limit}`
if (options.tryConversion === false) url += '&tryConversion=false'
if (options.aggregate) url += `&aggregate=${options.aggregate}`
if (options.timestamp) url += `&toTs=${options.timestamp}`
return fetchJSON(url).then(result => result.Data)
}
function histoHour (fsym, tsym, options) {
options = options || {}
if (options.timestamp) options.timestamp = dateToTimestamp(options.timestamp)
let url = `${baseUrl}histohour?fsym=${fsym}&tsym=${tsym}`
if (options.limit) url += `&limit=${options.limit}`
if (options.tryConversion === false) url += '&tryConversion=false'
if (options.aggregate) url += `&aggregate=${options.aggregate}`
if (options.timestamp) url += `&toTs=${options.timestamp}`
return fetchJSON(url).then(result => result.Data)
}
function histoMinute (fsym, tsym, options) {
options = options || {}
if (options.timestamp) options.timestamp = dateToTimestamp(options.timestamp)
let url = `${baseUrl}histominute?fsym=${fsym}&tsym=${tsym}`
if (options.limit) url += `&limit=${options.limit}`
if (options.tryConversion === false) url += '&tryConversion=false'
if (options.aggregate) url += `&aggregate=${options.aggregate}`
if (options.timestamp) url += `&toTs=${options.timestamp}`
return fetchJSON(url).then(result => result.Data)
}
function dateToTimestamp (date) {
if (!(date instanceof Date)) throw new Error('timestamp must be an instance of Date.')
return Math.floor(date.getTime() / 1000)
}
module.exports = {
coinList,
price,
priceHistorical
priceMulti,
priceFull,
priceHistorical,
generateAvg,
topPairs,
topExchanges,
histoDay,
histoHour,
histoMinute
}

4

package.json
{
"name": "cryptocompare",
"description": "CryptoCompare JavaScript API",
"version": "0.0.2",
"version": "0.1.0",
"author": "JP Richardson <jprichardson@gmail.com>",

@@ -11,3 +11,3 @@ "bugs": {

"node-fetch": "^1.5.3",
"standard": "*",
"standard": "9.x",
"tap-spec": "^4.0.2",

@@ -14,0 +14,0 @@ "tape": "^4.0.0"

@@ -26,56 +26,345 @@ cryptocompare

**Note:** cryptocompare depends on [`fetch()`](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch) being defined globally.
- If you are using this in electron, it should work without any configuration.
- If you are using this in Node.js, you will need to use [`node-fetch`](https://www.npmjs.com/package/node-fetch).
**Example:**
```js
global.fetch = require('node-fetch')
const cc = require('cryptocompare')
```
### Methods
#### coinList
### `price()`
Reference: https://www.cryptocompare.com/api/#-api-data-coinlist-
Get the current price of any cryptocurrency in any other currency.
- Signature: `coinList()`
- Parameters: (none)
- Returns: A Promise function that returns contains an associative array of coins.
`price(fsym, tsyms[, options])`
**Example:**
- `fsym` (String) From Symbol
- `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
var cc = require('cryptocompare')
cc.coinList()
.then(coinList => {
console.dir(coinList.BTC)
const cc = require('cryptocompare')
// Basic Usage:
cc.price('BTC', ['USD', 'EUR'])
.then(prices => {
console.log(prices)
// -> { USD: 1100.24, EUR: 1039.63 }
})
.catch(console.error.bind(console)
.catch(console.error)
// Passing a single pair of currencies:
cc.price('BTC', 'USD')
.then(prices => {
console.log(prices)
// -> { USD: 1100.24 }
})
.catch(console.error)
```
**Outputs:**
### `priceMulti()`
Works like `price()`, except it allows you to specify a matrix of From Symbols.
`priceMulti(fsyms, tsyms[, options])`
- `fsyms` (Array of Strings | String) From Symbol(s)
- `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')
// Basic Usage:
cc.priceMulti(['BTC', 'ETH'], ['USD', 'EUR'])
.then(prices => {
console.log(prices)
// -> { BTC: { USD: 1114.63, EUR: 1055.82 },
// ETH: { USD: 12.74, EUR: 12.06 } }
})
.catch(console.error)
// Passing a single pair of currencies:
cc.priceMulti('BTC', 'USD')
.then(prices => {
console.log(prices)
// -> { BTC: { USD: 1114.63 } }
})
.catch(console.error)
```
{ Id: '1182',
Url: '/coins/btc/overview',
ImageUrl: '/media/19633/btc.png',
Name: 'BTC',
CoinName: 'Bitcoin',
FullName: 'Bitcoin (BTC)',
Algorithm: 'SHA256',
ProofType: 'PoW',
FullyPremined: '0',
TotalCoinSupply: '21000000',
PreMinedValue: 'N/A',
TotalCoinsFreeFloat: 'N/A',
SortOrder: '1' }
### `priceFull()`
Get all the current trading info (price, vol, open, high, low, etc.) of any list of cryptocurrencies in any other currency.
`priceFull(fsyms, tsyms[, options])`
- `fsyms` (Array of Strings | String) From Symbol(s)
- `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')
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)
```
### price
### `priceHistorical()`
Reference: https://www.cryptocompare.com/api/#-api-data-price-
Get the price of any cryptocurrency in any other currency at a given timestamp. The price comes from the daily info - so it would be the price at the end of the day GMT based on the requested timestamp.
`priceHistorical(fsym, tsyms, time[, options])`
### priceHistorical
- `fsym` (String) From Symbol
- `tsyms` (Array of Strings | String) To Symbol(s)
- `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()`.)
https://www.cryptocompare.com/api/#-api-data-pricehistorical-
```js
const cc = require('cryptocompare')
// Basic Usage:
cc.priceHistorical('BTC', ['USD', 'EUR'], new Date('2017-01-01'))
.then(prices => {
console.log(prices)
// -> { BTC: { USD: 997, EUR: 948.17 } }
})
.catch(console.error)
```
### `generateAvg()`
Compute the current trading info (price, vol, open, high, low etc) of the requested pair as a volume weighted average based on the markets requested.
`generateAvg(fsym, tsym, markets[, tryConversion])`
- `fsym` (String) From Symbol
- `tsym` (String) To Symbol
- `markets` (Array) Array of markets to base the average on. (You can get a list of top exchanges for a given pair with `topExchanges()`.)
- `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')
// Basic Usage:
cc.generateAvg('BTC', 'USD', ['Coinbase', 'Kraken', 'Bitstamp', 'Bitfinex'])
.then(data => {
console.log(data)
// -> { MARKET: 'CUSTOMAGG',
// FROMSYMBOL: 'BTC',
// TOSYMBOL: 'USD',
// FLAGS: '2',
// PRICE: 1155.61,
// LASTUPDATE: 1488059738,
// LASTVOLUME: 0.25546663,
// LASTVOLUMETO: 294.93622433499996,
// LASTTRADEID: 26533969,
// VOLUME24HOUR: 27318.892083369985,
// VOLUME24HOURTO: 31652183.38370657,
// OPEN24HOUR: 1177.16,
// HIGH24HOUR: 1189.9,
// LOW24HOUR: 1110,
// LASTMARKET: 'Bitfinex',
// CHANGE24HOUR: -21.550000000000182,
// CHANGEPCT24HOUR: -1.830677223147251 }
})
.catch(console.error)
```
### `topPairs()`
Get top pairs by volume for a currency.
`topPairs(fsym[, limit])`
- `fsym` (String) From Symbol
- `limit` (Number) Limit the number of pairs you receive (default 5).
```js
const cc = require('cryptocompare')
cc.topPairs('BTC', 2)
.then(pairs => {
console.log(pairs)
// -> [ { exchange: 'CCCAGG',
// fromSymbol: 'BTC',
// toSymbol: 'JPY',
// volume24h: 235602.43493487104,
// volume24hTo: 31888554862.766888 },
// { exchange: 'CCCAGG',
// fromSymbol: 'BTC',
// toSymbol: 'USD',
// volume24h: 124504.4477389583,
// volume24hTo: 145514032.93780443 } ]
})
.catch(console.error)
```
### `topExchanges()`
Get top exchanges by volume for a currency pair.
`topExchanges(fsym, tsym[, limit])`
- `fsym` (String) From Symbol
- `tsym` (String) To Symbol
- `limit` (Number) Limit the number of exchanges you receive (default 5).
```js
const cc = require('cryptocompare')
cc.topExchanges('BTC', 'USD', 2)
.then(exchanges => {
console.log(exchanges)
// -> [ { exchange: 'Bitfinex',
// fromSymbol: 'BTC',
// toSymbol: 'USD',
// volume24h: 35239.36701090003,
// volume24hTo: 41472258.85534388 },
// { exchange: 'Bitstamp',
// fromSymbol: 'BTC',
// toSymbol: 'USD',
// volume24h: 19658.748675010014,
// volume24hTo: 23047071.74260772 } ]
})
.catch(console.error)
```
### `histoDay()`
Get open, high, low, close, volumefrom and volumeto from the daily historical data. The values are based on 00:00 GMT time.
`histoDay(fsym, tsym[, options])`
- `fsym` (String) From Symbol
- `tsym` (String) To Symbol
- `options` (Object)
- `aggregate` (Number) Number of data points to aggregate.
- `limit` (Number | `'none'`) Limit the number of days to lookup. Default is 30. If you set it to the string `'none'`, you will get all available data.
- `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.
- `timestamp` (Date) By default, `histoDay()` gets historical data for the past several days. Use the `timestamp` option to set a historical start point.
```js
cc.histoDay('BTC', 'USD')
.then(data => {
console.log(data)
// -> [ { time: 1485388800,
// close: 915.65,
// high: 917.71,
// low: 893.81,
// open: 893.97,
// volumefrom: 35494.93,
// volumeto: 32333344.2 },
// ... ]
})
.catch(console.error)
```
### `histoHour()`
Get open, high, low, close, volumefrom and volumeto from the hourly historical data.
`histoHour(fsym, tsym[, options])`
- `fsym` (String) From Symbol
- `tsym` (String) To Symbol
- `options` (Object)
- `aggregate` (Number) Number of data points to aggregate.
- `limit` (Number) Limit the number of hours to lookup. Default is 168.
- `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.
- `timestamp` (Date) By default, `histoHour()` gets historical data for the past several hours. Use the `timestamp` option to set a historical start point.
```js
cc.histoHour('BTC', 'USD')
.then(data => {
console.log(data)
// -> [ { time: 1487448000,
// close: 1060.34,
// high: 1061.44,
// low: 1058.85,
// open: 1059.24,
// volumefrom: 739.6,
// volumeto: 790019.22 },
// ... ]
})
.catch(console.error)
```
### `histoMinute()`
Get open, high, low, close, volumefrom and volumeto from the minute-by-minute historical data.
`histoMinute(fsym, tsym[, options])`
- `fsym` (String) From Symbol
- `tsym` (String) To Symbol
- `options` (Object)
- `aggregate` (Number) Number of data points to aggregate.
- `limit` (Number) Limit the number of minutes to lookup. Default is 1440.
- `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.
- `timestamp` (Date) By default, `histoMinute()` gets historical data for the past several minutes. Use the `timestamp` option to set a historical start point.
```js
cc.histoMinute('BTC', 'USD')
.then(data => {
console.log(data)
// -> [ { time: 1487970960,
// close: 1171.97,
// high: 1172.72,
// low: 1171.97,
// open: 1172.37,
// volumefrom: 25.06,
// volumeto: 29324.12 },
// ... ]
})
.catch(console.error)
```
## License
[MIT](LICENSE.md)

@@ -1,21 +0,58 @@

var test = require('tape')
'use strict'
const test = require('tape')
// set to global
global.fetch = require('node-fetch')
var cc = require('../')
if (!global.fetch) global.fetch = require('node-fetch')
const cc = require('../')
const helpers = require('./helpers')
test('coinList()', function (t) {
test('price()', t => {
t.plan(2)
cc.price('BTC', ['USD', 'EUR']).then(prices => {
t.strictEqual(typeof prices.USD, 'number', 'prices.USD is a number')
t.strictEqual(typeof prices.EUR, 'number', 'prices.EUR is a number')
t.end()
}).catch(t.end)
})
test('price() allows passing a string as the second parameter', t => {
t.plan(1)
cc.coinList().then(coinList => {
t.strictEqual(coinList.BTC.CoinName, 'Bitcoin', 'CoinName field should be Bitcoin')
cc.price('BTC', 'USD').then(prices => {
t.strictEqual(typeof prices.USD, 'number', 'prices.USD is a number')
t.end()
}).catch(t.end)
})
test('price()', function (t) {
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.price('BTC', ['USD', 'CNY']).then(prices => {
t.strictEqual(prices[0].Symbol, 'USD', 'prices[0].Symbol === USD')
t.strictEqual(typeof prices[0].Price, 'number', 'prices[0].Price is a number')
t.strictEqual(prices[1].Symbol, 'CNY')
t.strictEqual(typeof prices[1].Price, 'number')
cc.priceMulti(['BTC', 'ETH'], ['USD', 'EUR']).then(prices => {
t.strictEqual(typeof prices.BTC.USD, 'number', 'prices.BTC.USD is a number')
t.strictEqual(typeof prices.BTC.EUR, 'number', 'prices.BTC.EUR is a number')
t.strictEqual(typeof prices.ETH.USD, 'number', 'prices.ETH.USD is a number')
t.strictEqual(typeof prices.ETH.EUR, 'number', 'prices.ETH.EUR is a number')
t.end()

@@ -25,13 +62,179 @@ }).catch(t.end)

test('priceHistorical()', function (t) {
test('priceMulti() allows passing strings instead of arrays', t => {
t.plan(1)
cc.priceMulti('BTC', 'USD').then(prices => {
t.strictEqual(typeof prices.BTC.USD, 'number', 'prices.BTC.USD is a number')
t.end()
}).catch(t.end)
})
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 => {
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)
var oneYearAgo = new Date((Date.now() / 1000 - 31536000) * 1000)
cc.priceHistorical('BTC', ['USD', 'CNY'], oneYearAgo).then(prices => {
t.strictEqual(prices[0].Symbol, 'USD', 'prices[0].Symbol === USD')
t.strictEqual(typeof prices[0].Price, 'number', 'prices[0].Price is a number')
t.is(prices[0].Price, 321.68, 'prices[0] is set')
t.strictEqual(prices[1].Symbol, 'CNY')
t.strictEqual(typeof prices[1].Price, 'number')
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("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
const timestamp = new Date('2017-01-01')
cc.priceHistorical('BTC', ['USD', 'EUR'], timestamp).then(prices => {
t.strictEqual(typeof prices.USD, 'number', 'prices.USD is a number')
t.is(prices.USD, 997, 'Correct historical value')
t.strictEqual(typeof prices.EUR, 'number', 'prices.EUR is a number')
t.end()
}).catch(t.end)
})
test("priceHistorical()'s tryConversion=false works", t => {
const timestamp = new Date('2017-01-01')
helpers.testTryConversion(
cc.priceHistorical(
helpers.NOT_USD_TRADABLE,
'USD',
timestamp,
{ tryConversion: false }
), 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 => {
t.is(typeof data.PRICE, 'number', `data.PRICE is a number`)
t.is(typeof data.VOLUME24HOUR, 'number', `data.VOLUME24HOUR is a number`)
t.is(typeof data.VOLUME24HOURTO, 'number', `data.VOLUME24HOURTo is a number`)
t.is(typeof data.OPEN24HOUR, 'number', `data.OPEN24HOUR is a number`)
t.is(typeof data.HIGH24HOUR, 'number', `data.OPEN24HOUR is a number`)
t.is(typeof data.LOW24HOUR, 'number', `data.OPEN24HOUR is a number`)
t.end()
}).catch(t.end)
})
test("generateAvg()'s tryConversion=false works", t => {
helpers.testTryConversion(
cc.generateAvg(helpers.NOT_USD_TRADABLE, 'USD', ['Coinbase', 'Kraken'], false),
t
)
})
test('topPairs()', t => {
t.plan(3 * 5 + 1)
cc.topPairs('BTC').then(pairs => {
t.equal(pairs.length, 5, 'returns 5 pairs by default')
pairs.forEach(pair => {
t.is(typeof pair.toSymbol, 'string', 'pair.toSymbol is a string')
t.is(typeof pair.volume24h, 'number', 'pair.volume24hr is a number')
t.is(typeof pair.volume24hTo, 'number', 'pair.volume24hrTo is a number')
})
t.end()
}).catch(t.end)
})
test("topPairs()'s limit option works", t => {
t.plan(1)
cc.topPairs('BTC', 3).then(pairs => {
t.equal(pairs.length, 3, 'limit works')
t.end()
}).catch(t.end)
})
test('topExchanges()', t => {
t.plan(6 * 5 + 1)
cc.topExchanges('BTC', 'USD').then(exchanges => {
let prev
t.equal(exchanges.length, 5, 'returns 5 pairs by default')
exchanges.forEach(item => {
t.is(typeof item.exchange, 'string', 'item.exchange is a string')
t.notEqual(item.exchange, prev, 'item.exchange is unique')
prev = item.exchange
t.is(typeof item.fromSymbol, 'string', 'item.fromSymbol is a string')
t.is(typeof item.toSymbol, 'string', 'item.toSymbol is a string')
t.is(typeof item.volume24h, 'number', 'item.volume24hr is a number')
t.is(typeof item.volume24hTo, 'number', 'item.volume24hrTo is a number')
})
t.end()
}).catch(t.end)
})
test("topExchanges()'s limit option works", t => {
t.plan(1)
cc.topExchanges('BTC', 'USD', 3).then(exchanges => {
t.equal(exchanges.length, 3, 'limit works')
t.end()
}).catch(t.end)
})
test('error handling', t => {
cc.price('BTC').then(prices => {
t.end('Promise should not resolve')
}).catch(e => {
t.ok(e, 'Errors')
t.end()
}).catch(t.end)
})

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc