Bitquery's Balance API

Bitquery’s tools and widgets allow you to query the token balance for Smart contract chains like Ethereum, Binance, Tron, Matic Bitcoin and Dogecoin, and other Bitcoin Platform Explorers like Bitcoin mainnet and Dogecoin etc.

In this tutorial we’ll explore some examples of how to get the token balances for the various different addresses with the queries and outputs.

First of all, we’ll take a look at the examples from the bitcoin platform explorers, i.e., the layer-1 blockchains such as Bitcoin, Dogecoin, Litecoin etc. Layer-1 scaling solutions augment the base layer of the blockchain protocol itself in order to improve scalability.

Bitcoin Network

Let’s see the query for the bitcoin network to get the balance for the address “bc1qxy2kgdygjrsqtzq2n0yrf2493p83kkfjhx0wlh” .

{
  bitcoin(network: bitcoin) {
    inputs(inputAddress: {is: "bc1qxy2kgdygjrsqtzq2n0yrf2493p83kkfjhx0wlh"}) {
      value
      count
 
    }
    outputs(outputAddress: {is: "bc1qxy2kgdygjrsqtzq2n0yrf2493p83kkfjhx0wlh"}) {
      value
      count
     
    }
  }
}

You can even get this query here:

Dogecoin Network

The query to get the balance of an address on another layer-1 blockchain network, Dogecoin can be written in a similar way, take a look!

{
  bitcoin(network: dogecoin) {
    inputs(inputAddress: {is: "DBs4WcRE7eysKwRxHNX88XZVCQ9M6QSUSz"}) {
      count
      value
      min_date: minimum(of: date)
      max_date: maximum(of: date)
    }
    outputs(outputAddress: {is: "DBs4WcRE7eysKwRxHNX88XZVCQ9M6QSUSz"}) {
      count
      value
      min_date: minimum(of: date)
      max_date: maximum(of: date)
 
    }
   
  }
 
}

You can get this query using the following link:

Moving on, now we’ll take examples from the smart contract platform explorers. These are often called the Layer-2 networks. It refers to a network or technology that operates on top of an underlying blockchain protocol to improve its scalability and efficiency.

Binance Smart Chain Network

Let’s look at a query to get the balance of an address on the Binance network now.

{
  ethereum(network: bsc) {
    address(address: {is: "0x30cc0553f6fa1faf6d7847891b9b36eb559dc618"}) {
      balances {
        currency {
      	symbol
      	address
        }
        value
      }
    }
  }
}

Ethereum Network

We can get the balance for an address on the Ethereum network in a similar way.

{
  ethereum(network: ethereum) {
	address(address: {is: "0x2a20380dca5bc24d052acfbf79ba23e988ad0050"}) {
  	balances {
    	currency {
      	symbol
      	address
    	}
    	value
  	}
	}
  }
}

As you can see, the queries to get the balance for a specific address on the Ethereum and bsc network different from those used to get the balances of addresses on the Bitcoin and Dogecoin etc. networks.

Balances and History (Ethereum)

You can even get the transfer history and balance of an address till a particular block. Let’s consider an example from the Ethereum network for the same. Here the address whose balance history we’re fetching is “0x30cc0553f6fa1faf6d7847891b9b36eb559dc618”

{
  ethereum(network: bsc) {
    address(address: {is: "0x30cc0553f6fa1faf6d7847891b9b36eb559dc618"}) {
      balances {
        currency {
      	address
      	symbol
      	tokenType
      	name
        }
        value
        history {
          timestamp
      	block
      	transferAmount
        }
      }
    }
  }
}

Polygon Matic Network

Moving on with the smart contract platform explorers, let’s take a look at the Polygon matic network next for the address “0xfc5b1921938d5a2208b73bce72abb53d2aa215eb”

{
  ethereum(network: matic) {
    address(address: {is: "0xfc5b1921938d5a2208b73bce72abb53d2aa215eb"}) {
      balances {
        currency {
      	symbol
        }
        value
      }
    }
  }
}

Here the query is again similar to the ones that we have discussed for other networks like Bitcoin etc.

Tron Network

The query to get the balances of addresses on the tron network are a bit different. Let’s take the address “TKzDm2ZvmjgZipJxLesRkdtaJMJdu5VjzH” for example

{
  tron {
    fees: smartContractCalls(txFrom: {is: "TKzDm2ZvmjgZipJxLesRkdtaJMJdu5VjzH"}) {
      count
      fee
    }
    in: transfers(
      receiver: {is: "TKzDm2ZvmjgZipJxLesRkdtaJMJdu5VjzH"}
      currency: {is: "TRX"}
    ) {
      count
      amount
      currency {
        name
      }
    }
    out: transfers(sender: {is: "TKzDm2ZvmjgZipJxLesRkdtaJMJdu5VjzH"}, currency: {is: "TRX"}) {
      count
      amount
      currency {
        name
      }
    }
  }
}

Avalanche Balance and History

For the Avalanche network, we’ll try to find the balance and history for the address “0x28c65dcb3a5f0d456624aff91ca03e4e315bee49”.

{
  balance_of_avalanche: ethereum(network: avalanche){
    address(address: {is: "0x28c65dcb3a5f0d456624aff91ca03e4e315bee49"}){
      balances{
        value
        currency{
      	symbol
        }
      }
    }
  }
  balance_history_of_avalanche: ethereum(network: avalanche){
    address(address: {is: "0x28c65dcb3a5f0d456624aff91ca03e4e315bee49"}){
      balances{
        value
        currency{
      	symbol
        }
        history{
      	transferAmount
      	value
      	timestamp
        }
      }
    }
  }
}

You can get this query here:

Avalanche balance and history (bitquery.io)

Conclusion

Bitquery’s Balance API supports various blockchains like Ethereum,Binance smart chain, Avalanche, Tron, Polygon Matic etc. You can easily calculate the balance of any address on various blockchains by simply using our Balance API.As you can see, the basic structure of the queries remains the same i.e.,

{
          	Blockchain(network: $network){
                         	Inputs(inputAddress:{is:$address}){
                                       	value
                         	}
                         	outputs(outputAddress: {is: $address}){
                                       	value
                         	}
          	}
}

To calculate the balance, you just need to add one line in the code of your webapp/ terminal

balance: subtract(outputs.value, inputs.value)

This is how easy it is to get balances of any given addresses across a plethora of blockchain networks using Bitquery.

Our Other DEX APIs are comprehensive and available for a number of blockchain networks are of great help in case you want to build DEX related tools or fetch data related to the DEXs. Check out our APIs today!!

If you have questions, you can also contact us.

Also Read

· Ethereum DEX GraphQL APIs with Examples - Bitquery

· Bitquery API integration with TradingView Technical Analysis Charts (DEX APIs)

· PancakeSwap - Querying DEXs on Binance Smart Chain using GraphQL APIs

· Price Index for DEX Tokens - Bitquery

· How to get started with Bitquery’s Blockchain GraphQL APIs? 1

About Bitquery

Bitquery is a set of software tools that parse, index, access, search, and use information across blockchain networks in a unified way. We are crossing the ‘chain-chasm’ by delivering set of products that can work across blockchains. Our products include:

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. You can refer to this article 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, SushiSwap, 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 and stay updated about the latest in the cryptocurrency world.

1 Like