Created
July 7, 2018 14:22
-
-
Save QzSG/20dd06dde0e674dbacb3541f92036af4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const axios = require('axios'); | |
const _ = require('lodash'); | |
const baseUrl = 'https://min-api.cryptocompare.com/data/'; | |
/** Retrieves the full list of coins supported by CryptoCompare. | |
* @method | |
* @param {boolean} [expanded=false] - Option to return all fields including SortOrder/Algorith/Etc instead of simplified list of Symbol/ImageUrl. Defaults to false. | |
* @returns {Promise<{ [CoinName : string] : { ImageUrl : string, Symbol : string }} | Error>} Returns Promise resolving to the full list of coins and their Image Url and Symbol. | |
* @example <caption>Example return data.</caption> | |
* // { | |
* // BTC : { | |
* // ImageUrl: '..media/19633/btc.png', | |
* // Symbol : 'BTC' | |
* // } | |
* // ... | |
* // ... | |
* // } | |
* // | |
*/ | |
const listCoins = async( expanded = false) => { | |
const endPoint = 'all/coinlist'; | |
const apiUrl = `${baseUrl}${endPoint}`; | |
try { | |
const promise = await axios.get(apiUrl); | |
return expanded ? | |
(_.mapValues(promise.data.Data, | |
(obj) => { | |
return { | |
...obj, | |
ImageUrl : `https://www.cryptocompare.com${obj.ImageUrl}` | |
}; | |
} | |
)) | |
: | |
(_.mapValues(promise.data.Data, | |
(obj) => { | |
return { | |
ImageUrl : `https://www.cryptocompare.com${obj.ImageUrl}`, | |
Symbol : obj.Symbol | |
}; | |
} | |
)); | |
} | |
catch (error) { | |
return Promise.reject(new Error(error)); | |
} | |
}; | |
/** Retrieves the full list of coins supported by CryptoCompare. | |
* @method | |
* @param {boolean} [details=false] - Option to return all tracked cryptocurrencies per exchange. Defaults to returning purely a list of exchange names. | |
* @returns {Promise<{ [ExchangeName : string] : { [CoinName :string] : {CurrencyPairs: Array<string>} } } } | Error>} Returns Promise resolving to the full list of integrated exchanges. Optional : Returns Coins and Currecy Pairs if enabled | |
* @example <caption>Example return data.</caption> | |
* // { | |
* // ... | |
* // "Coinbase" : { | |
* // "LTC": [ "BTC", "USD", "EUR" ], | |
* // "BCH": [ "EUR", "USD", "BTC" ], | |
* // "ETH": [ "BTC", "USD", "EUR" ], | |
* // "BTC": [ "USD", "GBP", "EUR", "CAD" ] | |
* // } | |
* // ... | |
* // } | |
* // | |
*/ | |
const listExchanges = async( details = false) => { | |
const endPoint = 'all/exchanges'; | |
const apiUrl = `${baseUrl}${endPoint}`; | |
try { | |
const promise = await axios.get(apiUrl); | |
return details ? | |
promise.data | |
: | |
(_.map(promise.data, | |
(value, key) => { | |
return key; | |
} | |
)); | |
} | |
catch (error) { | |
return Promise.reject(new Error(error)); | |
} | |
}; | |
/** Get the current price of cryptocurrency in any other currency that you need. | |
* BTC will be used for conversion if no direct conversion to required symbol exists. | |
* @method | |
* @param {string} fromSymbol - From Symbol of Cryptocurrency of which you require the price | |
* @param {string | string[] } toSymbols - To Symbol/s of one or more currencies you want to convert to. Accepts either a single symbol or an array of symbols | |
* @param {Object} [options={}] - Additional options to pass in | |
* @param {string} [options.exchange=] - Pass in a specific exchange to get prices from. Defaults to aggregated from CCCAGG list of exchanges. | |
* @param {boolean} [options.tryConversion=] If set to false, get only direct trading values | |
* @returns {Promise<{ [ToSymbolName : string] : number } | Error>} Returns Promise resolving to the full list of integrated exchanges. Optional : Returns Coins and Currecy Pairs if enabled | |
* @example <caption>Example return data.</caption> | |
* const result = await CryptoCompareService.getPrice( | |
* 'ETH', ['USD', 'BTC'], { | |
* exchange : 'Coinbase',tryConversion: false | |
* }); | |
* // { | |
* // "USD": 469.2, | |
* // "BTC": 0.07143 | |
* // } | |
* // | |
*/ | |
const getPrice = async( fromSymbol, toSymbols, options = {}) => { | |
var endPoint = `price?fsym=${fromSymbol}&tsyms=${toSymbols}`; | |
if (options.exchange) { | |
endPoint = endPoint.concat(`&e=${options.exchange}`); | |
} | |
if (options.tryConversion === false) { | |
endPoint = endPoint.concat(`&tryConversion=false`); | |
} | |
const apiUrl = `${baseUrl}${endPoint}`; | |
try { | |
const promise = await axios.get(apiUrl); | |
console.log(apiUrl); | |
return promise.data; | |
} | |
catch (error) { | |
return Promise.reject(new Error(error)); | |
} | |
}; | |
/** Get the current price of cryptocurrency in any other currency that you need. | |
* BTC will be used for conversion if no direct conversion to required symbol exists. | |
* @method | |
* @param {string} fromSymbols - From Symbols of Cryptocurrencys of which you require the price. Accepts either a single symbol or an array of symbols. | |
* @param {string | string[] } toSymbols - To Symbols of Currencys you want to convert to. Accepts either a single symbol or an array of symbols. | |
* @param {Object} [options={}] - Additional options to pass in | |
* @param {string} [options.exchange=] - Pass in a specific exchange to get prices from. Defaults to aggregated from CCCAGG list of exchanges. | |
* @param {boolean} [options.tryConversion=] If set to false, get only direct trading values | |
* @returns {Promise<{ [ToSymbolName : string] : { [FromSymbolName : string] : number }} } | Error>} Returns Promise resolving to the full list of integrated exchanges. Optional : Returns Coins and Currecy Pairs if enabled | |
* @example <caption>Example return data.</caption> | |
* const result = await CryptoCompareService.getPriceMulti( | |
* 'ETH,LTC', ['USD', 'EUR'], { | |
* exchange : 'Coinbase' | |
* }); | |
* // { | |
* // "ETH": { "USD": 470.29, "EUR": 402 }, | |
* // "LTC": { "USD": 82.04, "EUR": 70.44 } | |
* // } | |
* // | |
*/ | |
const getPriceMulti = async( fromSymbols, toSymbols, options = {}) => { | |
var endPoint = `pricemulti?fsyms=${fromSymbols}&tsyms=${toSymbols}`; | |
if (options.exchange) { | |
endPoint = endPoint.concat(`&e=${options.exchange}`); | |
} | |
if (options.tryConversion === false) { | |
endPoint = endPoint.concat(`&tryConversion=false`); | |
} | |
const apiUrl = `${baseUrl}${endPoint}`; | |
try { | |
const promise = await axios.get(apiUrl); | |
console.log(apiUrl); | |
return promise.data; | |
} | |
catch (error) { | |
return Promise.reject(new Error(error)); | |
} | |
}; | |
module.exports = { | |
listCoins, listExchanges, getPrice, getPriceMulti | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const ListCoinTest = async () => { | |
//const result = await CryptoCompareService.listCoins(true); | |
try { | |
const result = await CryptoCompareService.getPriceMulti('ETH,LTC', ['USD', 'EUR'], {exchange : 'Coinbase'}); | |
console.log(result) | |
} | |
catch(err) { | |
console.log(err); | |
} | |
}; | |
ListCoinTest(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment