Accessing CoinGecko Market Info through Band Protocol’s OpenAPI

Sorawit Suriyakarn
Band Protocol
Published in
6 min readNov 22, 2019

Two days ago, we announced our new OpenAPI feature. Today, we are giving a clear demonstration of how smart contracts can leverage such feature with one of our reputable partners, CoinGecko!

We are seeing the most traction in terms of smart contract adoptions due to decentralized finance (DeFi). Yet all categories of DeFi applications — such as lending, derivatives, and decentralized exchanges — require access to reliable real-time price data and metrics on trading pairs to operate securely on a trustless basis.

More than ever, DeFi applications need access to high quality real-world information to build a truly robust and trustless protocol.

Decentralized finance applications are on the forefront of revolutionizing the way we know and understand traditional finance, but require accurate and timely data to do so. This calibre of data can only come from industry leaders such as Binance and scalable decentralized oracles like Band Protocol.

Blockchain applications in DeFi, which all require accurate and timely data, are revolutionizing the traditional financial systems we know and use today. Without precise and real-time sources of data, applications leave themselves open to central points of failure and delayed or invalid data.

As such, we’ll be walking through how any DeFi project can access real-time price and trading volume data aggregated through a reputable provider, CoinGecko, who is also one of our partners.

1. Defining the Spec

First, let’s explore CoinGecko’s API documentation which complies nicely with the Open API standard v2 (also known as Swagger standard). https://www.coingecko.com/api/documentations/v3

Let’s say we want to get the total 24hr trading volume of BAND token. To retrieve the market data, all that’s required is the URL from the Coingecko interactive documentation which uses the /coin/{id} API endpoint in this case.

Next, we’ll define a spec file for the endpoint.

Notice that we can break down the spec into 3 chunks:

  • The first ismetawhich defines the version of Band Protocol’s OpenAPI that we’re working with — in this case, it’s 1. It also shows that we’re taking one string parameter, which is the token id, defined on CoinGecko platform.
  • The second is requestwhich defines the actual URL and method we’re calling. We use {0} in the url to represent the only parameter, the token id, passed to this request.
  • Lastly isresponse which tells the OpenAPI how to interpret the JSON result from the call. In this case, we’re getting the value along the market_data > total_volume > usd path. Note that the multiplier that we set to 10¹⁸ is the predefined fixed denominator and turns the floating-point result into a nice uint256 that’s supported in Solidity.

2. Testing and Submitting the Spec to Band Protocol

Band Protocol provides a nice, easy-to-use interface for testing and submitting the spec. Let’s head over to the Data Governance Portal and test this out.

To add the spec, click on the “New Endpoint” button.

There, you’ll find the text area where we’ll paste the spec we’ve prepared and hit “Next.”

Next, you’ll see the page where you can test the result of calling API from the spec we just added. In this case, we’ll input band-protocol as an id and click “Test Call”. You should be seeing the volume multiplied by 10¹⁸.

If the result is correct, we can proceed and click “Commit.”

Congratulations! You’ve set up a spec for OpenAPI endpoint in Band Protocol.

Let’s now make a request so that data becomes available on the blockchain and into a working oracle on Ethereum.

3. Making a Data Query

On the web interface, let’s click on our newly created endpoint and then click “New Request”.

Now we’ll add band-protocol as a parameter and click “Query.”

Congratulations! The trading volume of BAND is now available for smart contracts. To tie it all together, let’s create a simple smart contract to query the data from Band Protocol oracle contract.

4. Calling the Spec via Smart Contract

We’ve created a simple smart contract to demonstrate how the volume data can be used on Ethereum.

This demo contract simply queries data from Band Protocol and stores it in thevolume variable. The contract was deployed to address 0x1a20bF092B4b32B926B1Ec082b7a3F5Ba26a6334 where we can use Etherscan to demonstrate that the contract works.
https://kovan.etherscan.io/address/0x1a20bF092B4b32B926B1Ec082b7a3F5Ba26a6334#writeContract

Now, let’s call the function update and supply the parameter coinId with the hex string version of band-protocol which is 0x62616e642d70726f746f636f6c00.

(Note that we’ll need to send a small amount of Ether for the query fee as well)

After the transaction is confirmed, we then head over to the “Read Contract” tab. We should now see 24hr volume of BAND token shown in thevolume variable multiplied by 10¹⁸.

Final Words

With the availability of the OpenAPI feature, DeFi applications can easily tap into any existing, trusted data sources on the open internet with just a few lines of code.

You can find the full documentation for the Open API feature in Band Protocol’s official developer documentation.

Band Protocol is pushing the boundary of decentralized technology. We are collaborating with leading projects in the space to define the standard for oracles and Open API to make the data access on-chain more secure and transparent. On the demand side, we are working with multiple projects and enterprises to leverage these oracle capabilities to enhance their smart contract and create expansive, real-world use cases.

Onwards!

About Band Protocol
Band Protocol is a decentralized oracle framework for Web3.0 applications. Band Protocol connects smart contracts with trusted off-chain information, provided through community-curated oracle data providers. Blockchains are enabled to connect to any web API with assured data integrity through dPoS economic incentives through one simple function call. Developers using Band Protocol will be able to easily build and manage off-chain oracles, reputation scores, identity management systems and much more.

Website | Whitepaper | Telegram | Medium | Twitter | Reddit | Github

--

--