How To Get Global Crypto Market Data and Approximate Price Info

Using CoinMarketCap API #HowToBUIDL (6/n)

Dan Emmons
Coinmonks
Published in
5 min readJul 9, 2018

--

There’s a multitude of ways to obtain information about cryptocurrency and tokens. If you’re curious about exploring, here’s a few ideas just to get you started and on your way to #BUIDLing your own site with price, volume or ranking data. Please note this is not a comprehensive suite of available sources, but I will be updating this blog from time to time with various methods of obtaining market data.

CoinMarketCap is a site with a set of basic crypto market facts and figures. You’ll see a total number of Cryptocurrencies, Markets, MarketCap, 24H Volume and BTC Dominance. Then a listing of all the available Cryptocurrencies and Tokens appear, listed in order of their — you guessed it — coin market cap, which is just an arbitrary figure of number in circulation times current price. It is used as one measure for comparing coins. We can get this same information that we see on the site within our code.

Let’s first install the open source javascript RESTful API wrapper for CMC.
I’m using a forked version of the tiaanduplessis/coinmarketcap-api.
The full API documentation can be read here.

Now that it’s available in our project, obtain a reference to the library, and instantiate an instance of the API object.

Obtain this same information using the cmc.getGlobal() async function.

The response comes back with the data property populated with active_cryptocurrencies, active_markets, bitcoin_percentage_of_market_cap, and quotes.USD with the total_market_cap, and total_volume_24h. If you so desired, you could create a site using the same data, but styled in a different way.

Next, let’s look into obtaining information about all the different currencies that are available. Maybe you’re interested in finding out the id of a particular coin, like Monero.

The cmc.getListings() async function will return a full set of coins:

Notice each coin has an id name symbol and website_slug.
listings.data.length is 1619 which matches the active_cryptocurrencies.

Now let’s search for the first listing with the name ‘Monero’.

We’ll use that to find some details regarding that currency, since another part of the API requires it. Reference it with our variable property: xmr.id.

Let’s do the same thing for another crypto with symbolWAX'.

Great. We can ask the API for ticker information regarding xmr and wax now.

There’s an async function getTicker that takes an optional parameter:

cmc.getTicker({id:wax.id})

Again, you’ll notice the data.quotes.USD.price matches the same value you’d see on the website (as of this date and time: $0.123043).

Let’s do the same thing for xmr:

cmc.getTicker({id:xmr.id})

Once again we see that the price info returned by the API matches the site.

Now that we have two different ticker quotes stored in wax_ticker and xmr_ticker, we can do some quick math to approximate the conversion rate between WAX and XMR.

We can derive from that information that it would take about ~1124 wax tokens to equal the same amount of dollar value in a single xmr token. Or you could think of it like this: 1 Monero token could buy you ~1124 wax tokens.

Most exchanges use common base pairs like BTC/USD or ETH/BTC, or XMR/BTC, but it is not usual to find an uncommon base pair listed. Since you now know how to obtain price information for a given ticker, you can obtain a a good estimate using the coinmarketcap-api for yourself, and maybe build a custom ticker on your site.

Keep in mind that the price information on CoinMarketCap may be delayed and does not accurately account for the bid/ask spread of a given currency. As such, this data should be used for informational purposes only, and not for trading or risk management decisions. If nothing else, I hope it triggers your curiosity for other things you could #BUIDL and maybe even a more user-friendly way of comparing completely unrelated currencies on your own.

In the above examples, we were running functions within the truffle console but if you need to wait on the results of a function, you’ll want to make use of the handy async/await libraries. I included a short little example here for you to follow. The get_cmc_price_in_usd is an async function that takes an id and returns a Promise that resolves when getTicker results become available. Similar functions can be written for any other API data.

In the next post, we’ll look at how to obtain more accurate and up-to-date pricing information from an exchange like Kucoin. CoinMarketCap is ok for one-off exploration and giving you a longer timeframe reference point for prices, but trading decisions should be made based off real time price data.

Dan Emmons is a Blockchain Developer, owner of Emmonspired LLC, a Certified Bitcoin Professional, Certified Ethereum Developer, Full Stack Developer and Advisor on Cryptocurrency projects. He is also the creator of a Youtube Channel and iTunes Podcast called #ByteSizeBlockchain.

--

--

Dan Emmons
Coinmonks

A leader who strives to make issues that seem complex, overwhelming, or insurmountable more manageable for the Team & provide exceptional service to my clients.