Cover Image for Binance Smart Chain API | BNB Chain Indexing (BSC API)

Binance Smart Chain API | BNB Chain Indexing (BSC API)

Product
Tutorial

On September 1st, Binance launched one of the most awaited projects BNB chain. 

BNB chain is an EVM-compatible blockchain to create Dapps and digital assets. Now developers can create smart contracts and build decentralized applications atop BSC.

Migrating Dapps made easy

Currently, a growing ecosystem of Dapps on Ethereum is struggling with scalability problems. That’s why the BNB chain is built to tackle the problem of scalability. 

However, to make migration easy for Ethereum Dapps, BSC is based on Ethereum EVM and supports solidity. You can copy-paste your smart contract from Ethereum to BSC, and it will just work.

But, deploying a smart contract is just a part of building a Dapp. You need to show your smart contract-related data to your Dapp users.

To accomplish this, you need to run a Binance chain node and store, index data related to your application, and then use it in your Dapp. This is a huge bottleneck for many developers.

Bitquery solves this problem by providing a scalable platform for building GraphQL queries for your on-chain data needs and consuming them as APIs. Conceptually Similar to what TheGraph protocol on Ethereum.

This article shows how to query your Dapp data from BNB chain mainnet and testnet using our GraphQL APIs.

But before we start, let’s understand a few important concepts.

  • Our GraphQL endpoint is graphql.bitquery.io. It also hosts a GraphQL playground, where you can create, test, and run your queries.
  • We organize blockchain protocols into a few specific categories, for example — “Bitcoin Type,” “Ethereum Type.”
  • We put BNB chain in the “Ethereum Type” category.

Now, let’s understand how Bitquery helps Dapp developers using a few examples.

Band protocol’s Oracle smart contract event API

Band Protocol is a cross-chain data oracle platform, using which you can get real-world data and APIs to smart contracts.

An Oracle smart contract of Band protocol is currently deployed on an Ethereum testnet Kovan and provides token prices to other smart contracts.

To check the smart contract price updates, the Band protocol created a subgraph on the The Graph protocol. A practice followed by many Dapps in the Ethereum ecosystem.

When deployed in this subgraph, it exposes API for Oracle smart contract price update events. 

An exact version of that Oracle smart contract is deployed on the BNB testnet. But how to create the same APIs of this BSC testnet smart contract? Because the graph protocol only supports Ethereum. 

Bitquery solves this problem.

To get the API for Oracle contract (deployed on BSC) price update events, you just need to create the following query and consume it as API.

That’s it, no subgraph, just a simple query.

{
    ethereum(network: bsc_testnet) {
        smartContractEvents(options: {desc: "block.height", limit: 10}, 
        smartContractEvent: {is: "RefDataUpdate(string,uint64,uint64,uint64)"},
        smartContractAddress: {is: "0x352aaf9ad953d1edd41c872a84bfa70b6374c87a"}) {
            transaction {
                hash
            }
            block {
                height
                timestamp {
                iso8601
                unixtime
                }
            }
            eventIndex
            arguments {
                value
                argument
            }
        }
    }
}

As you can see, we are using “bsc_testnet” as a parameter for the network.

bsc” represents the BSC mainnet and “bsc_testnet” the BSC testnet.

Besides, we are wrapping the query in “ethereum” because, as we mentioned, we categorize BSC as an “Ethereum Type” protocol.

Besides, we are passing the Oracle’s event signature RefDataUpdate(string,uint64,uint64,uint64) and the contract address as parameters in our query to get the data. 

You can change those parameters to get APIs for other BSC smart contract events.

Result

The Oracle price event query above gives the following results.

{
  "data": {
    "ethereum": {
        "smartContractEvents": [
            {
            "transaction": {
                "hash": "0xf5cb9a16e21d25e667e89f4505e828f5d6c57e720c378b330a44c3dac26ebd0e"
            },
            "block": {
                "height": 2557438,
                "timestamp": {
                    "iso8601": "2020-10-07T03:43:25Z",
                    "unixtime": 1602042205
                }
            },
            "eventIndex": "12",
            "arguments": [
                {
                    "value": "PBTC",
                    "argument": "symbol"
                },
                {
                    "value": "10283373250000",
                    "argument": "rate"
                },
                {
                    "value": "1602042133",
                    "argument": "resolveTime"
                },
                {
                    "value": "451199",
                    "argument": "requestId"
                }
            ]
            },
            ...
            ...

Querying BNB chain

The query above is just an example to get smart contract events as APIs. 

You can write different types of queries, simple or complex, to get any type of on-chain data for more than 20 blockchains, including the BNB chain.

Let’s see a few more examples of how to get different types of data from the BNB.

Getting Latest Transaction

To get the latest transactions on BNB chain, use the following query. 

You can run this query here — graphql.bitquery.io

{
    ethereum(network: bsc) {
        transactions(options: {desc: "block.height", limit: 10}) {
            block {
                timestamp {
                    time(format: "%Y-%m-%d %H:%M:%S")
                }
                height
            }
            address: sender {
                address
                annotation
            }
            hash
            gasValue
        }
    }
}

Result

The query above gives the following result.

"data": {
    "ethereum": {
        "transactions": [
            {
            "block": {
                "timestamp": {
                    "time": "2020-10-07 09:17:37"
                },
                "height": 1130159
            },
            "address": {
                "address": "0x2d4c407bbe49438ed859fe965b140dcf1aab71a9",
                "annotation": null
            },
            "hash": "0xac7f663477324708eeb6e49ab1e29b04e4f7fb115f714b959b4df8eefe65b347",
            "gasValue": 0
            },
        ...
        ...

Token Transfer analytics on BSC

Let’s say we want to see the most transferred token on the BNB chain and related analytics, use the query below.

{
    ethereum(network: bsc) {
        transfers(options: {desc: "count", limit: 10}, 
        amount: {gt: 0}) {
            currency {
                symbol
                address
            }
            count
            senders: count(uniq: senders)
            receivers: count(uniq: receivers)
            days: count(uniq: dates)
            from_date: minimum(of: date)
            till_date: maximum(of: date)
            amount
        }
    }
}

Result

The above query provides the following result.

{
    "data": {
        "ethereum": {
            "transfers": [
                {
                    "currency": {
                        "symbol": "BNB",
                        "address": "-"
                    },
                    "count": 3116636,
                    "senders": 57247,
                    "receivers": 74561,
                    "days": 41,
                    "from_date": "2020-04-20",
                    "till_date": "2020-10-07",
                    "amount": 409963716.082384
                },
                {
                    "currency": {
                        "symbol": "Cake",
                        "address": "0x0e09fabb73bd3ade0a17ecc321fd13a19e81ce82"
                    },
                    "count": 910592,
                    "senders": 9406,
                    "receivers": 14671,
                    "days": 16,
                    "from_date": "2020-09-22",
                    "till_date": "2020-10-07",
                    "amount": 257813488.92522457
                },
                ...
                ...

Wrapping up

We are excited to see what developers build on the BNB chain. Also, check out our BNB chain explorer.

If you have any questions or need help with queries, just hop on our Telegram channel. Also, let us know if you are looking for blockchain data APIs.

You might also be interested in:

About Bitquery

Bitquery is a set of software tools that parse, index, access, search, and use information across blockchain networks in a unified way. Our products are:

  • Coinpath® APIs provide blockchain money flow analysis for more than 24 blockchains. With Coinpath’s APIs, you can monitor blockchain transactions, investigate crypto crimes such as bitcoin money laundering, and create crypto forensics tools. Read this to get started with Coinpath®.

  • Digital Assets API provides index information related to all major cryptocurrencies, coins, and tokens.

  • DEX API provides real-time deposits and transactions, trades, and other related data on different DEX protocols like Uniswap, Kyber Network, Airswap, Matching Network, etc.

If you have any questions about our products, ask them on our Telegram channel or email us at hello@bitquery.io. Also, subscribe to our newsletter below, we will keep you updated with the latest in the cryptocurrency world.

Subscribe to our newsletter

Subscribe and never miss any updates related to our APIs, new developments & latest news etc. Our newsletter is sent once a week on Monday.