Binance Smart Chain: Bitquery's BSC GraphQL API
Launched in September 2020 by the renowned cryptocurrency exchange Binance, BSC represents a groundbreaking development in decentralized finance (DeFi) and blockchain ecosystems.
At its core, BSC is a blockchain platform designed to facilitate the creation and execution of decentralized applications (DApps) and smart contracts. What sets BSC apart is its compatibility with the Ethereum Virtual Machine (EVM), allowing developers to easily port their Ethereum-based projects to the BSC network.
This interoperability opens doors for seamless migration and expansion of existing dApps, leveraging the advantages of BSC's high throughput and low transaction fees.
BSC provides a lot of advantages to developers for building applications. Let’s discuss how you can leverage Bitquery APIs to extract BSC network’s real-time data for easy implementation.
BSC Overview
BSC stands for Binance Smart Chain and uses a proof-of-stake algorithm. It has a double-chain architecture, which makes it compatible with Ehtereum, while allowing it to process transactions faster and at lower fees.
BSC can provide both historic and real-time data. Historic data can be obtained starting from the very first block (29th August 2020) to the latest; however, certain block data during April 2021 to June 2022 is not accessible.
BSC's native token is the Binance Coin (BNB).
Key Features/Advantages
BSC offers key advantages for developers, such as:
-
Performance, Cost-effectiveness: With significantly lower transaction fees compared to Ethereum, BSC offers users a cost-effective solution for executing transactions and interacting with DApps.It can run a high volume of transactions per second, through DPoS (delegated proof-of-stake).
-
Established community and ecosystem: Through on-chain governance mechanisms, BSC community members can participate in protocol upgrades, fee adjustments, and other critical decisions, fostering a decentralized and community-driven ecosystem. With comprehensive documentation, developer resources, and support from the Binance ecosystem, building and deploying DApps on BSC has never been easier. Active promotions of the blockchain by the community has fast-tracked the growth and adoption by developers.
-
Interoperability: Porting applications from Ethereum to Binance is very easy.
-
Tokens: Both BSC and Ethereum have similar token standards, which enables developers to seamlessly work on both the blockchains.
-
NFTs: BSC’s vibrant NFT marketplace further showcases its versatility, enabling creators and collectors to tokenize and trade digital assets seamlessly.
BSC Explorer API from Bitquery
Overview of Binance (BNB) Smart Chain Mainnet
Here’s the BSC Smart Contract Explorer data that gives you a quick overview of the number of transactions on the BSC network during a time period, as shown here.
To embed queries in your applications, simply click the Get API on the bottom right of the screen (check the above image). To write your own queries, use Bitquery’s in-built GraphQL IDE.
Obtaining Real-Time BSC Data
Bitquery provides a variety of streaming APIs to extract real-time or historic BSC chain data. Here are some examples that showcase how to get real-time data on the BSC network using Bitquery’s APIs.
NFT API
Fetching NFT Holders
This API retrieves BSC addresses that hold NFT tokens related to smart contracts.
In this query, let’s check the NFT holders for a specific smart contract, 0xDf7952B35f24aCF7fC0487D01c8d5690a60DBa07 (as above). Specify the network as “bsc” and the smart contract address as given. Set the dataset parameter to “realtime” to get the latest data on NFT holders. This query returns NFT tokens and their balances in descending order.
{
EVM(network: bsc, dataset: realtime) {
BalanceUpdates(
limit: {count: 100}
orderBy: {descendingByField: "balance"}
where: {Currency: {SmartContract: {is: "0xDf7952B35f24aCF7fC0487D01c8d5690a60DBa07"}}}
) {
BalanceUpdate {
Address
}
balance: sum(of: BalanceUpdate_Amount)
}
}
}
Obtaining All NFTs of an Address
You can get the details of all NFTs of a specific address on the BSC network using this API. For example, to get all the NFTs of the same smart contract as above, 0xDf7952B35f24aCF7fC0487D01c8d5690a60DBa07, use this query. Specify the network as “bsc” and the smart contract address as above. Set the dataset parameter to “combined” to get both archive and real-time data on the given smart contract NFTs.
{
EVM(network: bsc, dataset: combined) {
BalanceUpdates(
limit: {count: 100}
orderBy: {descending: BalanceUpdate_Amount}
where: {BalanceUpdate: {Address: {is: "0xDf7952B35f24aCF7fC0487D01c8d5690a60DBa07"}}, Currency: {Fungible: false}}
) {
Currency {
Fungible
Symbol
SmartContract
Name
HasURI
Delegated
Decimals
}
BalanceUpdate {
Id
Amount
Address
URI
}
}
}
}
Obtaining Recent NFT Trades
Now, let’s try to use the NFT API to obtain the latest NFT trades on BSC DEX for a given address or project. For example, in this query, we will retrieve the NFT DEX trades for the same smart contract, 0xDf7952B35f24aCF7fC0487D01c8d5690a60DBa07. Specify the network as “bsc” and the address (Buy Currency) as given.
{
EVM(network: bsc) {
DEXTrades(
orderBy: {descending: Trade_Dex_Pair_SmartContract}
where: {Trade: {Buy: {Currency: {SmartContract: {is: "0xDf7952B35f24aCF7fC0487D01c8d5690a60DBa07"}}}}}
) {
Block {
Time
}
Transaction {
Hash
}
Trade {
Dex {
ProtocolFamily
ProtocolName
ProtocolVersion
SmartContract
}
Buy {
Price
Buyer
Ids
URIs
}
Sell {
Seller
Amount
Currency {
Symbol
SmartContract
}
}
}
}
}
}
NFT Ownership API
Tracking NFT owners
Use this API to get the latest data on NFT owners on the BSC network. For example, let’s get the latest NFT owner on BSC for this address 0xd17584633bc8d190e5a14502976dad9640456d6d using this query. Specify the network as “bsc” and set the dataset parameter to “combined”. Also, set the parameter Fungible to “false” to get the NFT owner.
{
EVM(dataset: combined, network: bsc) {
BalanceUpdates(
orderBy: {descending: Block_Time}
limit: {count: 1}
where: {BalanceUpdate: {}, Currency: {Fungible: false, SmartContract: {is: "0xd17584633bc8d190e5a14502976dad9640456d6d"}}}
) {
Currency {
Name
SmartContract
}
BalanceUpdate {
Address
Amount
Id
}
Block {
Number
Date
}
}
}
}
Obtaining NFT Creator Address
Even though you cannot query for the NFT creator address directly, you can obtain the same by inferring the sender of the first transfer. For this, we will use the Transfers parameter as shown in this query. Specify the network as “bsc” and set the dataset parameter to “archive” (to get the first sender). And, set the smart contract address as required, in this case, we’ll use 0xd17584633bc8d190e5a14502976dad9640456d6d.
{
EVM(dataset: archive, network: bsc) {
Transfers(
limit: {count: 1}
orderBy: {ascending: Block_Time}
where: {Transfer: {Currency: {SmartContract: {is: "0xd17584633bc8d190e5a14502976dad9640456d6d"}}}}
) {
Block {
Time
}
Transfer {
Amount
Currency {
Symbol
SmartContract
ProtocolName
Native
Name
HasURI
Fungible
DelegatedTo
Delegated
Decimals
}
Data
Id
Receiver
Success
Type
}
}
}
}
NFT Transfers API
Tracking daily NFT transfers
Let’s check the number of daily NFT transfers on BSC using this query. Here, we’ll get data on NFT transfers for the period of 1 week (15-22 February 2024). Specify the network as “bsc” set the dataset parameter to “combined”, Fungible as “false” and dates as given.
{
EVM(dataset: combined, network: bsc) {
Transfers(
orderBy: {ascending: Block_Date}
where: {Block: {Date: {since: "2024-02-15", till: "2024-02-22"}}, Transfer: {Currency: {Fungible: false}}}
) {
Block {
Date
}
count
}
}
}
Frequently Transferred NFTs on BSC
By using this query, you can fetch the most transferred NFTs on the BSC blockchain. Specify the network as “bsc” set the dataset parameter to “combined”, Fungible as “false” and dates from 15-22 February as shown here.
{
EVM(dataset: combined, network: bsc) {
Transfers(
orderBy: {descendingByField: "count"}
limit: {offset: 10, count: 0}
where: {Block: {Date: {since: "2024-02-15", till: "2024-02-22"}}, Transfer: {Currency: {Fungible: false}}}
) {
Transfer {
Currency {
Symbol
SmartContract
}
}
count
senders: uniq(of: Transfer_Sender, method: approximate)
receivers: uniq(of: Transfer_Receiver, method: approximate)
ids: uniq(of: Transfer_Id, method: approximate)
}
}
}
Similarly, you can obtain more NFT data using various NFT APIs by Bitquery. To learn more, refer to this page.
DEX Trades API
Obtaining token trades
To see details of all the token trades including the protocol used, DEX exchanges, currency traded etc. on a particular day, check this query. Specify the network as “bsc” and set the dates as required.
query ($network: EthereumNetwork!, $limit: Int!, $offset: Int!, $from: ISO8601DateTime, $till: ISO8601DateTime) {
ethereum(network: $network) {
dexTrades(
options: {desc: ["block.height", "tradeIndex"], limit: $limit, offset: $offset}
time: {since: $from, till: $till}
) {
block {
timestamp {
time(format: "%Y-%m-%d %H:%M:%S")
}
height
}
tradeIndex
protocol
exchange {
fullName
}
smartContract {
address {
address
annotation
}
}
buyAmount
buyCurrency {
address
symbol
}
buy_amount_usd: buyAmount(in: USD)
sellAmount
sellCurrency {
address
symbol
}
sell_amount_usd: sellAmount(in: USD)
transaction {
hash
}
}
}
}
Checking DEX Smart Contract Protocols
You can get a list of all smart contract DEX protocols on BSC during a 1-week period, with this query. Specify the network as “bsc”, set the dataset parameter to “combined”, and dates equal to 21-28 February as shown here.
query ($network: evm_network, $limit: Int!, $offset: Int!, $from: String, $till: String) {
EVM(dataset: combined, network: $network) {
DEXTrades(
limit: {count: $limit, offset: $offset}
where: {Block: {Date: {since: $from, till: $till}}}
orderBy: {descending: Trade_Dex_ProtocolName}
) {
ChainId
currencies: uniq(of: Trade_Buy_Currency_Name)
contracts: uniq(of: Trade_Dex_SmartContract)
trades: count
Trade {
Dex {
ProtocolName
ProtocolFamily
ProtocolVersion
}
}
}
}
}
Pools API
Latest pools for a specific DEX
Use this query to get recent data on pools on BSC including trading volume, fees and other metrics.
{
EVM(dataset: combined, network: bsc) {
Events(
orderBy: {descending: Block_Number}
limit: {count: 10}
where: {Log: {SmartContract: {is: "0xdB1d10011AD0Ff90774D0C6Bb92e5C5c8b4461F7"}, Signature: {Name: {is: "PoolCreated"}}}}
) {
Log {
Signature {
Name
Parsed
Signature
}
SmartContract
}
Transaction {
Hash
}
Block {
Date
Number
}
Arguments {
Type
Value {
... on EVM_ABI_Boolean_Value_Arg {
bool
}
... on EVM_ABI_Bytes_Value_Arg {
hex
}
... on EVM_ABI_BigInt_Value_Arg {
bigInteger
}
... on EVM_ABI_Address_Value_Arg {
address
}
... on EVM_ABI_String_Value_Arg {
string
}
... on EVM_ABI_Integer_Value_Arg {
integer
}
}
Name
}
}
}
}
Monitoring pool liquidity
You can also get the liquidity of a specific pool using the Pool API. Let’s find out token details in the a random pool which has following address, 0xb4c7e9f5ea5e79a66cc93a198c605f1ba9478138, in this query. Specify the network as “bsc”, and address as specified here.
{
EVM(dataset: combined, network: bsc) {
BalanceUpdates(
where: {BalanceUpdate: {Address: {is: "0xb4c7e9f5ea5e79a66cc93a198c605f1ba9478138"}}}
orderBy: {descendingByField: "balance"}
) {
Currency {
Name
}
balance: sum(of: BalanceUpdate_Amount, selectWhere: {gt: "0"})
BalanceUpdate {
Address
}
}
}
}
Learn more about extracting pool data here.
Smart Contracts API
Tracking Smart Contract Creation
Another cool thing you can explore on BSC through this API is monitoring how many smart contract calls are created in real-time. For this, we will use the “subscription” parameter. Specify the network as “bsc” in this query. You can obtain all the details of the smart contracts created and monitor them through this subscription.
subscription {
eth_creates: EVM(network: bsc) {
creates: Calls(where: {Call: {Create: true}}) {
Block {
Time
}
Transaction {
Hash
From
}
Call {
Input
To
Output
}
}
}
}
Smart Contract Calls API
Fetching latest smart contract calls
To obtain all the latest (last 10 days or so) smart contract calls on BSC, use this query. This query returns 10 recent smart contract calls made on BSC for the last 10 days, in this case, after 20th of February 2024. Specify the network as “bsc”, set the dataset parameter to “realtime”, and date after to “2024-02-20” (or, as required).
query MyQuery {
EVM(dataset: realtime, network: bsc) {
Calls(
limit: {count: 10}
orderBy: {descending: Block_Date}
where: {Block: {Date: {after: "2024-02-20"}}}
) {
Call {
LogCount
InternalCalls
}
Transaction {
Gas
Hash
From
To
Type
Index
}
Block {
Date
}
}
}
}
Events API
Tracking Recent Events
With Events API, you can track the latest events and other statistics on the BSC blockchain. Let’s obtain the latest (last 2 weeks, in this case) 10 events on BSC in this query. Specify the network as “bsc”, set the dataset parameter to “realtime”, and date after to “2024-02-15” (or, as required)
query MyQuery {
EVM(dataset: realtime, network: bsc) {
Events(
limit: {count: 10}
orderBy: {descending: Block_Time}
where: {Block: {Date: {after: "2024-02-15"}}}
) {
Block {
Number
}
Call {
CallPath
InternalCalls
}
Topics {
Hash
}
Receipt {
CumulativeGasUsed
}
Transaction {
From
To
Type
}
Log {
Signature {
Signature
}
SmartContract
}
}
}
}
Transfers API
Retrieving Highest BNB Transfer on BSC
BNB is the native token of BSC. So, to analyze the highest transactions of BNB on BSC, for a specified time period, use this query. Here, we are getting the largest transactions of the BNB token in February 2024. Specify the network as “bsc”, and dates as specified here.
query ($network: EthereumNetwork!, $token: String!, $dateFormat: String!, $from: ISO8601DateTime, $till: ISO8601DateTime) {
ethereum(network: $network) {
transfers(
currency: {is: $token}
height: {gt: 0}
date: {since: $from, till: $till}
) {
date {
date(format: $dateFormat)
}
max_amount: maximum(of: amount, get: amount)
}
}
}
Transactions API
Latest Transactions
Transactions API lets you fetch the latest transactions on the BSC chain. This query fetches the latest 20 transactions on BSC in real-time. Specify the network as “bsc”, set the dataset parameter to “realtime”, and the count parameter to “20”.
query {
EVM(dataset: realtime network: bsc) {
Transactions(limit: {count: 20}
orderBy: [{descending: Block_Number} {descending: Transaction_Index}]) {
Block {
Time
Number
}
Transaction {
Hash
Cost
}
}
}
}
Token Holders API
Listing unique token holders
This API lets you obtain data on the number of unique token holders on BSC. For example, here we are retrieving the most recent number of unique holders of USDT token on BSC. Specify the network as “bsc”, set the dataset parameter to “archive”, and the smart contract address as “0x55d398326f99059ff775485246999027b3197955” (USDT).
{
EVM(dataset: archive, network: bsc) {
TokenHolders(
date: "2024-02-01"
tokenSmartContract: "0x55d398326f99059ff775485246999027b3197955"
where: {Balance: {Amount: {gt: "0"}}}
) {
uniq(of: Holder_Address)
}
}
}
This API also enables you to track other token related information, such as balance, transfers, holder activity and more. To learn more, refer to the documentation.
Mempool API
This API enables real-time data retrieval from the mempool of EVM chains. With this API, you can track token trades, simulate pending transactions, obtain fee information, track transfers etc., stored in the mempool.
Mempool API is not yet available for the BSC network. It’s expected to come soon. Stay tuned for more information.
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 sales@bitquery.io. Also, subscribe to our newsletter below, we will keep you updated with the latest in the cryptocurrency world.
This article has been written by guest writer Aparna Sridhar
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.