How To Get Global Crypto Market Data and Approximate Price Info
Using CoinMarketCap API #HowToBUIDL (6/n)
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.
npm install coinmarketcap-api`
Now that it’s available in our project, obtain a reference to the library, and instantiate an instance of the API object.
CMC = require("coinmarketcap-api")
cmc = new CMC();
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:
{ data: [
{ id: 1, name: 'Bitcoin', symbol: 'BTC', website_slug: 'bitcoin'},
{ id: 2, name: 'Litecoin', symbol: 'LTC',website_slug: 'litecoin'},
{ id: 3, name: 'Namecoin', symbol: 'NMC',website_slug: 'namecoin' },
// ... more items ],
metadata: { timestamp: 1531157458, num_cryptocurrencies: 1619, error: null } }
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’.
xmr = listings.data.filter(l => l.name === 'Monero' )[0];
{ id: 328, name: 'Monero', symbol: 'XMR', website_slug: '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
'.
wax = listings.data.filter(l => l.symbol === 'WAX' )[0];
{ id: 2300, name: 'WAX', symbol: 'WAX', website_slug: 'wax' }
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})
{ data: { id: 2300, name: 'WAX', symbol: 'WAX', website_slug: 'wax', rank: 96, circulating_supply: 828063006, total_supply: 1850000000, max_supply: null, quotes: { USD: { price: 0.123043, volume_24h: 941801, market_cap: 101887356, percent_change_1h: 0.21, percent_change_24h: -4.22, percent_change_7d: -6.92 } }, last_updated: 1531158940 }, metadata: { timestamp: 1531158616, error: null } }wax_ticker = _;
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})
{ data: { id: 328, name: 'Monero', symbol: 'XMR', website_slug: 'monero', rank: 13, circulating_supply: 16209366, total_supply: 16209366, max_supply: null, quotes: { USD: { price: 138.291, volume_24h: 32556600, market_cap: 2241609385, percent_change_1h: 0.56, percent_change_24h: -0.59, percent_change_7d: -0.16 } }, last_updated: 1531159466 }, metadata: { timestamp: 1531159117, error: null } }xmr_ticker = _;
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.
xmr_price = xmr_ticker.data.quotes.USD.price; // 138.291
wax_price = wax_ticker.data.quotes.USD.price; // 0.123043
xmrwax = xmr_price / wax_price; // 1123.9241565956618
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.
const async = require('asyncawait/async');
const await = require('asyncawait/await');async function get_cmc_price_in_usd(cmc_id) { return new Promise(function(res, rej) {
cmc.getTicker({id:cmc_id}).then( function(t) {
res(t.data.quotes.USD.price);
} ); });
};let xmr_price = await get_cmc_price_in_usd(xmr.id);
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.