Buy Crypto
Pay with

Powering DeFi and Cross-chain Interoperable Decentralized Applications with Binance API using Band Protocol


Binance is proud to support the usage of scalable and decentralized oracles by providing an array of cryptocurrency data to any blockchain network in collaboration with Band Protocol.

This is a collaborative post courtesy of Band Protocol.

Decentralized applications have matured over time and their functionalities disrupting an abundance of industries - most notably, digital finance. More than ever, the market for trustless financial products has grown to expand across an array of categories such as decentralized exchanges, lending, borrowing, and derivatives. With over $641M in value locked up in DeFi products today, it’s difficult not to ignore the security layer of a trustless protocol relying on external data feeds.

Smart contracts now more than ever, need access to high-quality real-world information to power its logic to continue to securely improve the traditional Web 2.0 systems we know today.

While smart contracts can be perfected, the underlying data triggering the behavior of the contract will always harness a factor of dependency. That is, developers need scalable and reliable real-time data to build a truly robust and trustless protocol.

“Binance is committed to growing the blockchain ecosystem, including the ongoing development of Decentralized Finance, an important part of the ecosystem. Binance works with many blockchain projects, including Band Protocol, to bring freedom of money everywhere, and through the help of Binance data and Band Protocol’s solutions, we can help accelerate the growth of DeFi,” said Binance CEO CZ (Changpeng Zhao).

“We are excited to work with Binance to make the promise of DeFi a reality through our OpenAPI. This enables anyone to create decentralised applications that can use Binance’s 600+ crypto data which is a great source of price discovery. What’s most exciting is that this will open doors for more finance and enterprise use cases to come as our platform can connect to any available API online,” said Soravis Srinawakoon, Co-Founder and CEO of Band Protocol.

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

Bringing Data Onto Blockchain via Decentralized Oracle

We know that smart contracts need a bridge to the world outside its own blockchain network, but who or what to build that bridge?

Decentralized oracles - a third party or decentralized data feed service that provides external data / off-chain data onto the blockchain.

Take Band Protocol for example, a data oracle that operates in a trustless manner to securely bring data from the real-world and traditional APIs over the entire internet to blockchain applications. With their recent release of Open API access spec, Band Protocol has enabled any smart contract on any blockchain to access the Binance API.

Through Band Protocol, everything that Binance enables on its API will be accessible on Ethereum smart contract and other blockchain platforms Band integrates with.

Band Protocol relies on its data providers and token economics to guarantee a secure API call while also requiring little effort from developers. To create an API call from smart contracts, developers must create an API spec that specifies to the data providers how the API will be queried. The API spec then resides in decentralized storage on IPFS and when each data request comes in, data providers will query the data based on the spec and unwrap the data before sending it to the smart contract.

But why would this be useful?

Applying Binance Market Data to DeFi Applications

Drawing back onto Set Protocol’s derivatives, using Binance’s API through  Band Protocol would allow Set Protocol to use multiple candlestick patterns as an indicator for rebalancing portfolios. More specifically, through the availability of /api/v3/klines API. With such flexibility, Set Protocol can create unparalleled derivatives on the blockchain with expansive options/strategies for its users in compliments with simpler strategies.

Looking at Kyber Network, a user may want to create a limit order once the global order book has reached a sufficient depth. They can create a smart contract that queries Binance’s order book on /api/v3/depth and incentivise a 3rd party to execute an order with the validation of the order book condition. More interestingly, this strategy can be combined with other information available on Binance’s API such as volume from /api/v3/ticker/24hr and combine it with price data from /api/v3/ticker/price.

The DeFi and entire blockchain ecosystem can now leverage Binance’s cryptocurrency data with Open API access spec and cross-chain compatibility on Band Protocol to supply information on 630+ crypto pairs across all major blockchains.

Deep-dive into the Implementation

We have explored on a high-level on how Band Protocol’s Open API access enables much-needed information from Binance API to smart contracts. Now let’s get specific - not only does Band Protocol’s decentralized oracle allow the decentralized application to connect with the Binance centralized exchange APIs, but it also facilitates communication between varying blockchain networks which we will explore extensively in this article.

If you want to buy BNB on Binance Chain using the ETH you have on Ethereum blockchain, how will you do it without a single point of trust?

Currently, the only way to buy BNB using ETH is to go to a centralized exchange. A more decentralized alternative is to use Atomic Swap, but it would require off-chain coordination between the buyer and seller — you'd need a whole suite of software to make it happen. What if you want to create an entirely decentralized process to exchange tokens cross-chain only using smart contracts? Until the recent advancement in decentralized oracles, there was no obvious solution to this problem.

By utilizing Band Protocol's on-demand web requests, any smart contract can gain access to open APIs on the internet to aggregate traditional and crypto price data to create decentralized financial products and instruments. In addition, using Band provides access to other blockchain networks or globally leading payment networks such as Paypal to allow developers to utilize the more traditionally popular payment flows in addition to the resources of various blockchain networks.

We will explore how to utilize Band Protocol’s cross-chain compatibility to enable cross-chain token exchange between Ethereum and Binance Chain.

Sample Scenario

Let's assume Bob has taken interest in the rapid growth of the Binance Ecosystem, so he wants to convert his bag of ETH to BNB on Binance Chain. Since Bob is a total advocate of decentralization, he refuses to send his fund through a centralized exchange. Here's how he would create a smart contract to buy BNB tokens in a trust-less manner.

1. First, Bob deploys a CrossChainOrder contract along with some ETH and specifies his Binance Chain address and the amount of BNB tokens he'd be willing to buy.

2. In this case, the CrossChainOrder contract will hold the 2 ETH until there is proof that Bob's account on Binance Chain is filled with at least 14 BNB.

3. Alice then comes across the contract, if she determines that the asking price is good, she begins the token exchange by calling deal() function along with 1 ETH as insurance that she will complete the transaction. This would grant Alice an exclusive right to complete this deal.

4. Alice has to send the asked amount of BNB to Bob's address before the time limit of 5 minutes is up.

5. Once her BNB transaction on Binance Chain is confirmed, Alice then calls the claim() function which checks if the transaction on Binance Chain goes through. If the check is successful, the smart contract will automatically transfer Bob's 2 ETH along with Alice's 1 ETH pledge back to Alice.

6. Now Bob has received 14 BNB, and Alice has received 2 ETH. However, in the case that Alice could not complete the transaction within the time limit, she would lose her 1 ETH pledge.

7. At any time, if there is no pending order fulfillment, Bob can call cancel() function to destroy his smart contract and get his 2 ETH back without order fulfillment.

Smart Contract Walkthrough

Now that we covered all the steps required for the token exchange, let's take a close look at the actual Solidity implementation on Ethereum.

- First we create a CrossChainOrder smart contract that has a few global variables and functions as you can see. We will go in detail about each function later. At the top we also declare Band Protocol's query interface which makes our contract be able to ask Band Protocol.

- The variable ETH_PLEDGE is a pledge for the matcher (Alice in this case), if the matcher doesn't do what he/she promises the dealer will lost his/her pledge. For this example, the pledge is always 1 ETH.

- The DURATION is the amount of time for the matcher to send BNB to the owner on Binance Chain.

- The owner is the person who wants XBNB, which in this case is Bob.

- The bnbAddress is the address of the owner in Binance Chain to transfer BNB to.

- The amount is the amount of BNB that the owner wants.

- The matcher is the person who wants ETH in this contract exchange with his/her BNB on Binance Chain. matcher is Alice in this case.

- The expiredAt is the timestamp that the matcher has to send BNB to the owner before he/she loses the exclusive right to make the trade.

- Let's look at the constructor which takes _bnbAddress (the address of Bob on Binance Chain) and amount (amount of BNB that Bob wants to trade with). When Bob deploys this contract he will send the amount of ETH along with the deployment. So Alice can claim all ETH in this contract after she has already sent the _amount of BNB to the _bnbAddress on Binance Chain.

- Next is deal() function. If anyone finds that the deal is interesting, that person will call the deal() function to become the matcher. The matcher only has 5 minutes to send BNB to the owner, if the matcher fails to do so, he/she will lose his/her pledge.

- Function cancel() is for the owner to cancel his/her offer. The owner can call this function if there is no matcher at the time or the current matcher fails to complete the transaction within the time limit. After cancel() has been called successfully, the contract will be destroyed and all ETH will be sent to the owner.

- Let's look at function getBNBBalance(), which is the function that asks Band Protocol for the BNB balance on Binance Chain.

- At the top of getBNBBalance() we instantiate a QueryInterface with address 0x7f525974d824a6C4Efd54b9E7CB268eBEFc94aD8 which refers to the address of Band Protocol's web request oracle dataset on Kovan testnet.

- Next, we create a key for the balance query. The key consists of 3 parts.

* IPFS hash to the JSON request specification object which is QmWSEzgdzaDuJ9Ctc84beTcCDGeKzjvjz5rxXtLsbYH3EE or 1220784c2aa37a5f5ea6465b6c02be1779684681ec1c625ca469e6ba167157f516f7 in hex.

* bnbAddress or the address of the owner on Binance Chain.

* bytes(0) as the null terminator of the string parameter in 2), per Band Protocol string encoding specification.

- Then we call the query function to retrieve the BNB balance.

* Check that the query status is OK before finishing the execution.

* The last function is claim(). This function is for the matcher to claim all ETH in this contract, if the matcher has sent the correct amount of BNB to the owner in time.

* This function will call getBNBBalance() to get the BNB balance in Binance Chian and then check whether the balance is greather than or equal to the required amount.

* After passing all requirements all ETH will be transferred to the matcher.

Explore Band Protocol's Web Request Functionality via Web UI

Throughout this example, we have demonstrated how to use a web request from the smart contract perspective. In the development environment, it’s an inconvenience to always test with smart contract environments. Band Protocol provides a handy web portal for this purpose. Let's explore it.

1. Go to You'll see Band Protocol’s Data Governance Portal, which helps you navigates through different datasets that's live on Kovan testnet.

2. Select Explore Data under On-demand Web Requests.

3. You'll see all the endpoints that are available on Band Protocol. You can always add a new endpoint (API Spec) via + New Endpoint button on the top right.

4. Now, let's try querying BNB Balance from a Binance Chain address. Simply click on Binance Chain: BNB Balance by Address to expand. Under the expanded view, you'll see information about this web request, including a spec hosted on the IPFS and available data.

5. Click NEW REQUEST to make data with a new parameter available on-chain. Under Param {0}, we'll add an address that we want to query BNB balance on. In this case, we'll add bnb15qad793km5r3xmntgz4w0qrumtvxctzhx27ppg, then hit QUERY.

6. The new request will be sent to providers, and Band Protocol will make the data available on-chain.

7. To see the result of your new request, simply go back to the expanded view of Binance Chain: BNB Balance by Address. You'll notice that the address you just queried shows up along with the address balance under the Result column.

8. To make a query in Solidity, you can copy the key from the right-most column and follow the integration guide accessible via the sidebar.

Current Limitations and How to Improve This Idea Further

* Band Protocol has the PriceFeed dataset group, which provides data of many assets. This would be easy for creating a pool of assets in ether, with prices that will automatically be updated. So Alice and Bob don't have to agree on asset price at first; they can freely trade assets cross-chain any time they want.

* Currently, there's one attack vector. Bob can immediately spend prior to Bob call claim on smart contract. To mitigate that, the API must allow querying for balance at a specific timestamp snapshot, or use transaction receipt as a proof, not account balance.

Wrapping Up

Lack of access to off-chain information and interoperability across blockchain networks have always been limiting factors in blockchain ecosystem. However, with the help of emerging decentralized oracles, these burning issues have been solved.

DeFi applications can now leverage Binance market data to power their logic, extending the capabilities and increasing the level of security offered. With the recent release of Band Protocol’s Open API, you can connect to the Binance API in just a few simple lines of code.

We can now create a relatively low effort cross-chain token exchange as shown through the case example with Ethereum and Binance Chain using Band Protocol’s on-demand web request. Prior to decentralized oracles, this has been notoriously difficult to achieve, especially on a blockchain with limited scripting capacity. 

That being said, decentralized oracle projects such as Band Protocol and Chainlink, will become the staple of interoperability between both smart contracts on-chain and to the real world.

Read "DeFi oracle Band Protocol launches on Cosmos, aims to overtake Chainlink" from our friends at Decrypt to learn how Cosmos plans to become the DeFi data oracle of choice by launching Band Protocol 2.0.