Skip to content

Commit 27512b9

Browse files
committed
BREAKING: Switched to https://min-api.cryptocompare.com/ API endpoint
- Removed coinList() - Changed the return value of price() & priceHistorical()
1 parent 47f22cc commit 27512b9

File tree

4 files changed

+82
-65
lines changed

4 files changed

+82
-65
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
Unreleased
22
----------
33

4+
Switched to https://min-api.cryptocompare.com/
5+
6+
- removed: `coinList()`
7+
- changed: `price()` returns a different data structure
8+
- changed: `priceHistorical()` returns a different data structure
9+
410
0.0.2 / 2016-11-21
511
------------------
612
- bug fix: `priceHistorical()` - wrong endpoint

README.md

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -25,51 +25,53 @@ Usage
2525

2626
### Methods
2727

28-
#### coinList
28+
### `price()`
2929

30-
Reference: https://www.cryptocompare.com/api/#-api-data-coinlist-
30+
`price(fsym, tsyms, [tryConversion])`
3131

32-
- Signature: `coinList()`
33-
- Parameters: (none)
34-
- Returns: A Promise function that returns contains an associative array of coins.
35-
36-
**Example:**
32+
- `fsym` (String) From Symbol
33+
- `tsym` (Array of Strings | String) To Symbol(s)
34+
- `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.
3735

3836
```js
39-
var cc = require('cryptocompare')
40-
cc.coinList()
41-
.then(coinList => {
42-
console.dir(coinList.BTC)
43-
})
44-
.catch(console.error.bind(console)
45-
```
37+
const cc = require('cryptocompare')
4638

47-
**Outputs:**
39+
// Basic Usage:
40+
cc.price('BTC', ['USD', 'EUR'])
41+
.then(prices => {
42+
console.log(prices)
43+
// -> { USD: 1100.24, EUR: 1039.63 }
44+
})
45+
.catch(console.error)
4846

49-
```
50-
{ Id: '1182',
51-
Url: '/coins/btc/overview',
52-
ImageUrl: '/media/19633/btc.png',
53-
Name: 'BTC',
54-
CoinName: 'Bitcoin',
55-
FullName: 'Bitcoin (BTC)',
56-
Algorithm: 'SHA256',
57-
ProofType: 'PoW',
58-
FullyPremined: '0',
59-
TotalCoinSupply: '21000000',
60-
PreMinedValue: 'N/A',
61-
TotalCoinsFreeFloat: 'N/A',
62-
SortOrder: '1' }
47+
// Passing a single pair of currencies:
48+
cc.price('BTC', 'USD')
49+
.then(prices => {
50+
console.log(prices)
51+
// -> { USD: 1100.24 }
52+
})
53+
.catch(console.error)
6354
```
6455

65-
### price
56+
### priceHistorical
6657

67-
Reference: https://www.cryptocompare.com/api/#-api-data-price-
58+
`priceHistorical(fsym, tsyms, time)`
6859

60+
- `fsym` (String) From Symbol
61+
- `tsym` (Array of Strings | String) To Symbol(s)
62+
- `time` (Date) Date in history that you want price data for
6963

70-
### priceHistorical
64+
```js
65+
const cc = require('cryptocompare')
7166

72-
https://www.cryptocompare.com/api/#-api-data-pricehistorical-
67+
// Basic Usage:
68+
cc.priceHistorical('BTC', ['USD', 'EUR'], new Date('2017-01-01'))
69+
.then(prices => {
70+
console.log(prices)
71+
// -> { BTC: { USD: 997, EUR: 948.17 } }
72+
})
73+
.catch(console.error)
74+
```
7375

7476

7577

index.js

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,32 @@
11
'use strict'
22
/* global fetch */
33

4-
const baseUrl = 'https://www.cryptocompare.com/api/data/'
4+
const baseUrl = 'https://min-api.cryptocompare.com/data/'
55

66
function fetchJSON (url) {
77
return fetch(url)
88
.then(res => res.json())
99
.then(body => {
10-
// 'response' is a CryptoCompare field
11-
if (body.Response !== 'Success') throw new Error(body.Message)
12-
return body.Data
10+
if (body.Response === 'Error') throw body.Message
11+
return body
1312
})
1413
}
1514

16-
function coinList () {
17-
let url = `${baseUrl}coinlist/`
18-
return fetchJSON(url)
19-
}
20-
21-
function price (fsym, tsyms, useBtc) {
15+
function price (fsym, tsyms, tryConversion) {
2216
let url = `${baseUrl}price?fsym=${fsym}&tsyms=${tsyms}`
23-
if (useBtc) url += 'usebtc=true'
17+
if (tryConversion === false) url += '&tryConversion=false'
2418
return fetchJSON(url)
2519
}
2620

2721
function priceHistorical (fsym, tsyms, time) {
2822
if (!(time instanceof Date)) throw new Error('time parameter must be an instance of Date.')
2923
time = Math.floor(time.getTime() / 1000)
3024
let url = `${baseUrl}pricehistorical?fsym=${fsym}&tsyms=${tsyms}&ts=${time}`
31-
return fetchJSON(url)
25+
// The API returns json with an extra layer of nesting, so remove it
26+
return fetchJSON(url).then(result => result[fsym])
3227
}
3328

3429
module.exports = {
35-
coinList,
3630
price,
3731
priceHistorical
3832
}

test/cryptocompare.test.js

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,49 @@ const test = require('tape')
55
global.fetch = require('node-fetch')
66
const cc = require('../')
77

8-
test('coinList()', t => {
8+
test('price()', t => {
9+
t.plan(2)
10+
cc.price('BTC', ['USD', 'EUR']).then(prices => {
11+
t.strictEqual(typeof prices.USD, 'number', 'prices.USD is a number')
12+
t.strictEqual(typeof prices.EUR, 'number', 'prices.EUR is a number')
13+
t.end()
14+
}).catch(t.end)
15+
})
16+
17+
test('price() allows passing a string as the second parameter', t => {
918
t.plan(1)
10-
cc.coinList().then(coinList => {
11-
t.strictEqual(coinList.BTC.CoinName, 'Bitcoin', 'CoinName field should be Bitcoin')
19+
cc.price('BTC', 'USD').then(prices => {
20+
t.strictEqual(typeof prices.USD, 'number', 'prices.USD is a number')
21+
t.end()
1222
}).catch(t.end)
1323
})
1424

15-
test('price()', t => {
16-
t.plan(4)
17-
cc.price('BTC', ['USD', 'CNY']).then(prices => {
18-
t.strictEqual(prices[0].Symbol, 'USD', 'prices[0].Symbol === USD')
19-
t.strictEqual(typeof prices[0].Price, 'number', 'prices[0].Price is a number')
20-
t.strictEqual(prices[1].Symbol, 'CNY')
21-
t.strictEqual(typeof prices[1].Price, 'number')
25+
test("price()'s tryConversion=false works", t => {
26+
cc.price('LTD', 'USD', false).then(prices => {
27+
t.end('Promise should not resolve')
28+
}).catch(e => {
29+
t.ok(e, 'Converting LTD to USD fails')
2230
t.end()
2331
}).catch(t.end)
2432
})
2533

2634
test('priceHistorical()', t => {
27-
t.plan(5)
28-
// Arbitrary timestamp; historical values are hard-coded into this test
29-
const timestamp = new Date(1456149600 * 1000)
30-
cc.priceHistorical('BTC', ['USD', 'CNY'], timestamp).then(prices => {
31-
t.strictEqual(prices[0].Symbol, 'USD', 'prices[0].Symbol === USD')
32-
t.strictEqual(typeof prices[0].Price, 'number', 'prices[0].Price is a number')
33-
t.is(prices[0].Price, 438.26)
34-
t.strictEqual(prices[1].Symbol, 'CNY')
35-
t.strictEqual(typeof prices[1].Price, 'number')
35+
t.plan(3)
36+
// NOTE: Historical values are hard-coded into this test
37+
const timestamp = new Date('2017-01-01')
38+
cc.priceHistorical('BTC', ['USD', 'EUR'], timestamp).then(prices => {
39+
t.strictEqual(typeof prices.USD, 'number', 'prices.USD is a number')
40+
t.is(prices.USD, 997, 'Correct historical value')
41+
t.strictEqual(typeof prices.EUR, 'number', 'prices.EUR is a number')
42+
t.end()
43+
}).catch(t.end)
44+
})
45+
46+
test('error handling', t => {
47+
cc.price('BTC').then(prices => {
48+
t.end('Promise should not resolve')
49+
}).catch(e => {
50+
t.ok(e, 'Errors')
3651
t.end()
3752
}).catch(t.end)
3853
})

0 commit comments

Comments
 (0)