DEV Community

CoinMonks
CoinMonks

Posted on • Originally published at bitquery.io on

Querying Binance Smart Chain (BSC)

On September 1st, Binance launched 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 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 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. 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 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 provide token prices to other smart contracts.

To check the smart contract price updates, Band protocol created a subgraph on TheGraph 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 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
 }
 }
 }
}
Enter fullscreen mode Exit fullscreen mode

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"
 }
 ]
 },
 ...
 ...
Enter fullscreen mode Exit fullscreen mode

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

{
 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
 }
 }
}
Enter fullscreen mode Exit fullscreen mode

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
 },
 ...
 ...
Enter fullscreen mode Exit fullscreen mode

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
 }
 }
}
Enter fullscreen mode Exit fullscreen mode

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
 },
 ...
 ...
Enter fullscreen mode Exit fullscreen mode

Wrapping up

We are excited to see what developers build on Binance smart chain. Also, check out our Binance smart 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.

The post Querying Binance Smart Chain (BSC) appeared first on Bitquery.

Top comments (1)

Collapse
 
stevedantes profile image
stevedantes

Can we use these codes to form Binance Smart Chain node a software that can help me to predict the future of crypto.