Querying Binance Smart Chain (BSC)
Tags: product, tutorial, bitqueryapis
Published: 2020-10-14 09:33:23

---
title: Querying Binance Smart Chain (BSC)
published: true
date: 2020-10-07 15:53:40 UTC
tags: Product,Tutorial,bitqueryapis
canonical_url: https://bitquery.io/blog/binance-smart-chain-api?utm_source=rss&utm_medium=rss&utm_campaign=binance-smart-chain-api
---

On September 1st, Binance [launched](https://www.binance.com/en/blog/421499824684900933/Binance-Smart-Chain-Launches-Today) its one of the most awaited project Binance smart chain. 

Binance smart chain(BSC) 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 Binance smart chain is built to tackle the problem of scalability. 

However, to make migration easy for Etheruem 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](https://graphql.org/) queries for your on-chain data needs and consuming them as APIs. Conceptually Similar to what [TheGraph](https://thegraph.com/) protocol on Ethereum.

This article shows how to query your Dapp data from Binance smart 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](https://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 Binance smart 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](https://bandprotocol.com/) is a cross-chain data oracle platform, using which you can get real-world data and APIs to smart contracts.

An [Oracle smart contract](https://kovan.etherscan.io/address/0x65B61aa2D8bA63fC488FF07568558Ad150c3e876#code) of Band protocol is currently deployed on an Ethereum testnet Kovan and provide token prices to other smart contracts.

To check the smart contract price updates, Band protocol created a [subgraph](https://github.com/bandprotocol/kovan-stdref-subgraph) on [TheGraph](https://thegraph.com/) protocol. A practice followed by many Dapps in the Etheruem ecosystem.

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

An [exact version](https://explorer.bitquery.io/bsc_testnet/smart_contract/0x56761c813fecb76b4df87ddb1912f5855b22ae5a) of that Oracle smart contract is deployed on BSC testnet. But how to create the same APIs of this BSC testnet smart contract? Because TheGraph 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: "0x56761c813fecb76b4df87ddb1912f5855b22ae5a"}) {
 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](https://explorer.bitquery.io/bsc_testnet/smart_contract/0x56761c813fecb76b4df87ddb1912f5855b22ae5a) 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 Binance smart 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 Binance smart chain.

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

#### Getting Latest Transaction

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

You can run this query here — [graphql.bitquery.io](https://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 Binance smart 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 Binance smart chain. Also, check out our [Binance smart chain explorer](https://explorer.bitquery.io/bsc).

If you have any questions or need help with queries, just hop on our [Telegram channel](https://t.me/Bloxy_info). Also, let us know if you are looking for blockchain data APIs.

You might also be interested in:

- [API to get Ethereum Smart Contract Events](https://bitquery.io/blog/ethereum-events-api)
- [Why GraphQL is better for blockchain data APIs](https://bitquery.io/blog/blockchain-graphql)
- [APIs to get Latest Uniswap Pair Listing](https://bitquery.io/blog/uniswap-pool-api)
- [Simple rest APIs to get Uniswap data (DEX Data APIs)](https://bitquery.io/blog/dex-data-apis)
- [API to Get Ethereum Token Balance](https://bitquery.io/blog/etheruem-token-api)
- [Simple API To Get Ethereum Supply And Data](https://bitquery.io/blog/get-ethereum-total-supply)
- [Who is actually using Ethereum?](https://bitquery.io/blog/who-is-actually-using-ethereum)
- [How to get newly created Ethereum Tokens?](https://bitquery.io/blog/newly-created-etheruem-token)

#### **About** Bitquery

[**Bitquery**](https://bitquery.io/?source=blog&utm_medium=about_coinpath) 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®](https://bitquery.io/products/coinpath?utm_source=blog) APIs** provide [blockchain money flow analysis](https://blog.bitquery.io/coinpath-blockchain-money-flow-apis) 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®](https://blog.bitquery.io/coinpath-api-get-start).

- **[Digital Assets API](https://bitquery.io/products/digital_assets?utm_source=blog&utm_medium=about)** provides index information related to all major cryptocurrencies, coins, and tokens. 

- **[DEX API](https://bitquery.io/products/dex?utm_source=blog&utm_medium=about)** 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](https://t.me/Bloxy_info) or email us at [hello@bitquery.io](mailto:hello@bitquery.io). Also, subscribe to our newsletter below, we will keep you updated with the latest in the cryptocurrency world.

The post [Querying Binance Smart Chain (BSC)](https://bitquery.io/blog/binance-smart-chain-api) appeared first on [Bitquery](https://bitquery.io).