Created
July 3, 2018 19:23
-
-
Save marktellez/1213a1ae87684227ba8eef8d231e88b8 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
require('es6-promise').polyfill() | |
require('isomorphic-fetch') | |
// get listings - https://api.coinmarketcap.com/v2/listings/ | |
// get ticker - https://api.coinmarketcap.com/v2/ticker/1 | |
function getAssets(symbols) { | |
return fetch('//api.coinmarketcap.com/v2/listings/') | |
.then(resp => resp.json()) | |
.then(json => { | |
return Object.values(json.data).reduce( (acc, asset) => { | |
if (symbols.find(symbol => symbol === asset.symbol)) acc.push(asset) | |
return acc | |
}, []) | |
}) | |
} | |
function getPrices(assets) { | |
const promises = assets.map(async asset => { | |
return await fetch(`//api.coinmarketcap.com/v2/ticker/${asset.id}`) | |
.then(resp => resp.json()) | |
}) | |
return Promise.all(promises) | |
} | |
symbols = ['BTC', 'BCH', 'ETH', 'XMR', 'EOS'] | |
getAssets(symbols) | |
.then(assets => { | |
return getPrices(assets) | |
.then(prices => console.log(prices)) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment