Skip to content

Instantly share code, notes, and snippets.

@marktellez
Created July 3, 2018 19:23
Show Gist options
  • Save marktellez/1213a1ae87684227ba8eef8d231e88b8 to your computer and use it in GitHub Desktop.
Save marktellez/1213a1ae87684227ba8eef8d231e88b8 to your computer and use it in GitHub Desktop.
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