Avalanche APIs: Tap into Smart Contracts, Transactions,Trades, and More
Avalanche, a groundbreaking open-source blockchain and crypto platform, provides a robust infrastructure for decentralized applications (dApps) and smart contracts. It rivals its competitor Ethereum in terms of scalability and transaction speed, while growing at an impressive rate.
So, developers looking to leverage the power of Avalanche can do so through its diverse APIs, which makes applications super easy! To give developers a leg up, Bitquery offers plug-and-play APIs through their user-friendly application.
In this article, we dive into more details on Avalanche, the power of its APIs, and how you can make use of Bitquery’s interface for using them.
What is Avalanche?
Avalanche is a fairly recent blockchain that debuted in 2020. It’s USP is that it is quick, secure, accessible, and affordable. It is a multi-chain platform that uses 3 blockchains. Each of them handle different tasks, which makes Avalanche very quick and highly effective.
Avalanche stands out in the blockchain landscape due to its high-performance proof-of-stake consensus protocol, called Avalanche consensus. This protocol is designed to provide fast and secure transactions, while being energy efficient. The native cryptocurrency or token of Avalanche is called AVAX.
Avalanche's architecture uses a directed acyclic graph (DAG) structure and supports the creation of custom blockchain networks, known as subnets, tailored to specific use cases.
Avalanche Architecture
Avalanche’s architecture is made up of 3 dynamic chains:
-
Contract Chain (C-Chain): This chain enables developers to create smart contracts.
-
Platform Chain (P-Chain): This chain facilitates both the creation and tracking of subnets, which in turn helps developers to build their own blockchains.
-
Exchange Chain (X-Chain): This provides a decentralized platform for transactions, such as trading of crypto tokens. Transaction fees are paid in AVAX.
How does Avalanche help developers?
Avalanche is multi-chain, which means the developers can use it for more versatile applications and parallel processing. With its high transaction speed and low fees, Avalanche has high potential for dApps. This makes it a more viable option than Ethereum.
Scalability is another attractive feature of Avalanche. Use cases can range from micropayments, NFTs, and blockchain real-time gaming, apart from dApps.
Overview of Avalanche APIs
The API simplifies complex blockchain interactions, allowing developers to focus on building innovative solutions without delving into intricate blockchain protocols.
-
Node API: Provides real-time data. Node API consists of a bunch of APIs that developers can use to query information about blocks, transactions, addresses, and network status.
-
C-Chain and X-Chain: The API enables developers to create and execute smart contracts on the C-Chain and the transfer of assets on the X-Chain. Bitquery provides C-Chain APIs for seamless integration.
-
Wallet API: Developers can create wallets, check balances, and initiate transactions.
To learn more about Avalanche APIs, refer to the Avalanche API documentation.
Exploring Bitquery APIs to Obtain Avalanche C-Chain Data
Bitquery offers a variety of Avalanche APIs to obtain real-time Avalanche on chain data. It primarily focuses on Avalanche’s C-Chain data by providing the C-Chain explorer. Use the explorer to seamlessly view data related to nodes, addresses, transactions, calls, and more.
Avalanche C-Chain Explorer API: Overview of Avalanche C-Chain
To view key metrics of Avalanche C-Chain on chain data, use the Bitquery Explorer. This provides a bird’s-eye-view of all the data related to Avalanche C-Chain.
To use Avalanche APIs, just use Bitquery’s in-built GraphQL IDE, and write your custom queries. Or, simply click the Get API
on the bottom right of the screen to get your query.
For example, to see blocks related data on the C-Chain, click Get API. This is the query you will see in the IDE.
{
ethereum(network: avalanche) {
blocks(
options: {desc: "height", limit: 10, offset: 0}
time: {since: "2023-12-19T06:35:32.000Z", till: "2023-12-19T07:05:32.999Z"}
) {
timestamp {
time(format: "%Y-%m-%d %H:%M:%S")
}
height
transactionCount
address: miner {
address
annotation
}
reward
reward_usd: reward(in: USD)
rewardCurrency {
symbol
}
}
}
}
Avalanche Address API
This API retrieves addresses on the Avalanche chain. You can filter this API to get data on transactions, rewards earned, gas used, and so on.
To obtain address data, use the following query in IDE. To get data on a specific address, specify the address variable with the required one, say, 0x8495839993851e772887791fc5d274ec990bf7af
. Use a date filter to narrow down your results as shown here.
{
ethereum(network: avalanche) {
blocks(options: {desc: "height", limit: 10}, date: {after: "2023-12-01"}) {
timestamp {
time(format: "%Y-%m-%d %H:%M:%S")
}
height
transactionCount
address: miner {
address
annotation
}
reward
reward_usd: reward(in: USD)
rewardCurrency {
symbol
decimals
name
address
tokenId
tokenType
}
totalDifficulty
uncleCount
gasLimit
difficulty
gasUsed
hash
}
address(address: {is: "0x8495839993851e772887791fc5d274ec990bf7af"}) {
address
balance
}
}
}
Avalanche Blocks API
This API by Bitquery helps you retrieve transactions on all the blocks or a specific block on the Avalanche blockchain.
Obtaining specific block transactions
To retrieve specific block data, either use the Bitquery Explorer or try building the query in IDE. This gives you the number of transactions in the block, miner, and reward information.
For example, you can use the following query in IDE to retrieve a specific block’s data, say 39239987.
Open the query in IDE and replace “height” with the block number. Replace network variable with “avalanche” . This gives you the transactions in the specified block.
{
ethereum(network: avalanche) {
blocks(height: {is: 39239987}) {
timestamp {
time(format: "%Y-%m-%d %H:%M:%S")
}
difficulty
hash
miner {
address
annotation
}
totalDifficulty
transactionCount
uncleCount
size
reward
reward_usd: reward(in: USD)
parentHash
rewardCurrency {
address
decimals
name
symbol
tokenId
tokenType
}
}
}
}
Avalanche Transactions API
Use this API to retrieve data related to transactions on Avalanche, such as the sender, number of transactions, gas costs, AVAX traded, and more.
Obtaining transactions by date
To get data on the number of transactions and transaction amount for specific dates, say, 10th Dec to 12th Dec, use the following query. Set the date parameter options “since” and “till” to the required dates. Specify the network as “avalanche” as shown here and add the parameter “amount”.
{
ethereum(network: avalanche) {
transactions(
options: {desc: "block.height", limit: 10, offset: 0}
date: {since: "2023-12-10", till: "2023-12-12"}
) {
block {
timestamp {
time(format: "%Y-%m-%d %H:%M:%S")
}
height
}
address: sender {
address
annotation
}
hash
gasValue
gas_value_usd: gasValue(in: USD)
creates {
address
}
currency {
name
}
error
gas
gasCurrency {
name
symbol
}
gasPrice
gasValue
nonce
success
to {
address
}
amount
}
}
}
Obtaining transactions between users (addresses)
To retrieve transactions between 2 specific addresses, build the query in IDE as follows.
Specify the network as “avalanche” and specify the sender and receiver addresses by using the parameters “txSender” and “txTo” as shown here.
{
ethereum(network: avalanche) {
transactions(
options: {desc: "block.height", limit: 10, offset: 0}
date: {since: "2023-01-01"}
txSender: {is: "0x1b56b0ea603c46a521e418c593224419142a572b"}
txTo: {is: "0x77777777777d4554c39223c354a05825b2e8faa3"}
) {
block {
timestamp {
time(format: "%Y-%m-%d %H:%M:%S")
}
height
}
address: sender {
address
annotation
}
hash
gasValue
gas_value_usd: gasValue(in: USD)
creates {
address
}
currency {
name
}
error
gas
gasCurrency {
name
symbol
}
gasPrice
gasValue
nonce
success
to {
address
}
}
}
}
Retrieving daily transactions on Avalanche
You can retrieve the daily count of transactions on Avalanche. For example, let’s retrieve transactions done per day for the last 10 days, by using this query.
query {
ethereum(network: avalanche) {
transactions(options: {desc: "date.date", limit: 10}) {
date {
date
}
count
}
}
}
Determining top gas burners
With the Avalanche Transactions API, you can even determine and analyze gas costs incurred on the network. For example, to analyze the top gas burners for smart contracts on the chain for a specific address, use the following query in IDE. Specify the network as “avalanche” and address as required, say, 0xb4315e873dbcf96ffd0acd8ea43f689d8c20fb30.
{
ethereum(network: avalanche) {
smartContractCalls(
options: {desc: "gasValue", limit: 10, offset: 0}
date: {since: "2024-01-08", till: "2024-01-09"}
smartContractAddress: {is: "0xb4315e873dbcf96ffd0acd8ea43f689d8c20fb30"}
) {
address: caller {
address
annotation
}
max_date: maximum(of: date)
gasValue(calculate: sum)
gas_value_usd: gasValue(in: USD, calculate: sum)
count
}
}
}
You can do further gas analysis by using filters such as gas price (to know the latest or average gas price), average transaction costs etc.
Avalanche Calls API
As mentioned before, Avalanche supports smart contracts. The Smart contract API or the Avalanche Calls API enables you to get all the smart contract calls made on the Avalanche network.
Obtaining list of smart contract calls
To obtain all the smart contract calls for the past month on the Avalanche blockchain, use the following query in IDE. Specify the network as “avalanche” and provide the dates as required for the “since” and the “till” parameter as shown.
{
ethereum(network: avalanche) {
smartContractCalls(
options: {desc: "block.height", limit: 10, offset: 0}
date: {since: "2023-12-01", till: "2023-12-06"}
external: true
) {
block {
timestamp {
time(format: "%Y-%m-%d %H:%M:%S")
}
height
}
address: caller {
address
annotation
}
arguments {
argument
value
}
callDepth
smartContract {
address {
address
annotation
}
}
smartContractMethod {
name
signatureHash
}
transaction {
hash
}
}
}
}
Retrieving number of unique smart contract calls
To obtain unique smart contract calls (such as NFTs) on the avalanche chain during a specific week, use the following query in IDE. Replace the “from” and “till” variables with the dates you want. Replace the variable network with “avalanche”.
{
ethereum(network: avalanche) {
smartContractCalls(
options: {asc: "date.date"}
date: {since: "2023-12-01", till: "2023-12-19"}
) {
date: date {
date(format: "%Y-%m-%d")
}
count: countBigInt
contracts: countBigInt(uniq: smart_contracts)
callers: countBigInt(uniq: senders)
methods: countBigInt(uniq: smart_contract_methods)
}
}
}
Obtaining specific method calls for smart contracts
Similarly, you can query for specific methods from a specific smart contract. For example, if you want to know all the smart contract calls for method “swap”, from 1st December to 19th December, use the following query in IDE.
Specify the method “swap” for the parameter, “smartContractMethod” and specify the network variable as “avalanche”. Specify the “from” variable date as 1st December and the “till” variable as 19th December. Filter the query for any address as required (shown here).
{
ethereum(network: avalanche) {
smartContractCalls(
options: {desc: "block.timestamp.time", limit: 10, offset: 0}
date: {since: "2023-12-01", till: "2023-12-19"}
caller: {is: "0x3fc91a3afd70395cd496c647d5a6cc9d4b2b7fad"}
smartContractMethod: {is: "swap"}
) {
block {
timestamp {
time(format: "%Y-%m-%d %H:%M:%S")
}
height
}
smartContractMethod {
name
signatureHash
}
smartContract {
address {
address
annotation
}
}
transaction {
hash
}
external
gasValue
gas_value_usd: gasValue(in: USD)
}
}
}
Similarly, you can have specific queries for method calls by specific addresses and specific smart contracts. Learn more here.
Obtaining top callers of smart contracts
The Smart Contracts API also helps you obtain who the top callers of a smart contract are, which includes the number of calls by each caller, date of the call, and the gas costs incurred.
For example, to find out the top callers of the smart contract 0xb4315e873dbcf96ffd0acd8ea43f689d8c20fb30 on 19th December, build the query as follows in the IDE. Replace the network variable with “avalanche” and the smart contract address with the one above. Specify the from and to dates to be 19th Dec.
{
ethereum(network: avalanche) {
smartContractCalls(
options: {desc: "count", limit: 10, offset: 0}
date: {since: "2023-12-01", till: "2023-12-19"}
smartContractAddress: {is: "0xb4315e873dbcf96ffd0acd8ea43f689d8c20fb30"}
) {
address: caller {
address
annotation
}
max_date: maximum(of: date)
count
gasValue(calculate: average)
gas_value_usd: gasValue(in: USD, calculate: average)
}
}
}
Avalanche Arguments API
This API returns information on smart contract call arguments.
For example, to obtain all the arguments for a specific smart contract, say 0x262dcfb36766c88e6a7a2953c16f8defc40c378a, use this query in IDE. Specify the network as avalanche.
{
ethereum(network: avalanche) {
arguments(
options: {desc: ["block.height", "transaction.hash"], limit: 10}
smartContractAddress: {is: "0x262dcfb36766c88e6a7a2953c16f8defc40c378a"}
) {
block {
height
}
argument {
name
}
value {
value
}
transaction {
hash
}
smartContract {
address {
address
}
}
smartContractSignature {
__typename
... on Method {
name
signature
}
}
}
}
}
Filtering arguments
You can also filter arguments based on a number of parameters. For example, to filter smart contracts using the method “swapExactTokensForTokens” and specific argument type, say “path”, use the following query in IDE.
Specify the network as “avalanche”, argument as “path” and the smart contract method as “swapExactTokensForTokens”.
{
ethereum(network: avalanche) {
arguments(
options: {desc: ["block.height", "transaction.hash"], limit: 10}
smartContractAddress: {is: "0x262dcfb36766c88e6a7a2953c16f8defc40c378a"}
smartContractMethod:{is: "swapExactTokensForTokens"}
argument: {is: "path"}
) {
block {
height
}
argument {
name
}
value {
value
}
transaction {
hash
}
smartContract {
address {
address
}
}
smartContractSignature {
__typename
... on Method {
name
signature
}
}
}
}
}
Avalanche Transfers API
This API helps you get data on token transfers among participants. You can get data on the amount, the currency, and more.
Obtaining AVAX token transfer statistics
Use the following query to see all transfers of the token AVAX, which is the native token of Avalanche.
For example, to see all AVAX transfers for the period 1st December to 12th December, do the following. Specify the token as AVAX for the “currency” variable as shown. You will get data related to the number of unique receivers/senders, total amount transferred, and dates when they were transferred.
{
ethereum(network: avalanche) {
transfers(
options: {desc: "block.height", limit: 10, offset: 0}
date: {since: "2023-12-01", till: "2023-12-12"}
currency: {is: "AVAX"}
amount: {gt: 0}
) {
block {
timestamp {
time(format: "%Y-%m-%d %H:%M:%S")
}
height
}
sender {
address
annotation
}
receiver {
address
annotation
}
currency {
address
symbol
}
amount
amount_usd: amount(in: USD)
transaction {
hash
}
external
}
}
}
Viewing a list of all tokens
To see all tokens transferred on Avalanche, use the following query. Specify the network as “avalanche” and specify dates as required. This query returns all token transfers done on Avalanche after 1st of December 2023.
{
ethereum(network: avalanche) {
transfers(
options: {desc: "count", limit: 1000}
amount: {gt: 0}
time: {after: "2023-12-01"}
) {
currency {
symbol
address
}
count
senders: count(uniq: senders)
receivers: count(uniq: receivers)
amount
amount_usd: amount(in: USD)
}
}
}
Obtaining largest transfer data
You can also retrieve data on the maximum amount that was transferred during a specific time period by using this query.
For example, to see the highest amount transferred during a month (1st November to 1st December), build this query. Specify the network as avalanche, dates as required, and use the parameter “max_amount” to determine the highest transfer amount on Avalanche.
{
ethereum(network: avalanche) {
transfers(
date: {after: "2023-11-01", before: "2023-12-01"}
currency: {is: "AVAX"}
height: {gt: 0}
) {
max_amount: maximum(of: amount, get: amount)
}
}
}
You can also use the API to build queries for obtaining transfers to and from addresses. Learn more here.
Avalanche Coinpath API
This API helps in tracing and tracking the money flow between addresses. In other words, you can track levels of fund movement on the Avalanche blockchain. Bitquery provides a visual representation of the same.
However, if you want to use this query in your application, you can build it in IDE.
Tracking destination of funds
Let’s say you want to track the destination of funds from the address, 0x0ef812f4c68dc84c22a4821ef30ba2ffab9c2f3a. Specify the address in the “initialAddress” parameter and the currency as “AVAX” as shown below. The query returns the fund movement for the period from 1st October 2023 to 12th December 2023.
{
ethereum(network: avalanche) {
outbound: coinpath(
initialAddress: {is: "0x0ef812f4c68dc84c22a4821ef30ba2ffab9c2f3a"}
currency: {is: "AVAX"}
depth: {lteq: 2}
options: {asc: "depth", desc: "amount", limitBy: {each: "depth", limit: 10}}
date: {since: "2023-10-01", till: "2023-12-12"}
) {
sender {
address
annotation
smartContract {
contractType
currency {
symbol
name
}
}
}
receiver {
address
annotation
smartContract {
contractType
currency {
symbol
name
}
}
}
amount
currency {
symbol
name
}
transaction {
hash
value
}
block {
height
timestamp {
time(format: "%y-%d-%m")
}
}
depth
count
}
}
}
Avalanche DEX Trading API
This API enables you to obtain data from different exchanges. It returns information about the exchanges involved in DEX, currencies traded, the protocols used, smart contract calls, and more.
Obtaining data on DEX protocols
In this example, let’s get the DEX data on trades that use the Uniswap V2 protocol. To do so, build this query in IDE. Specify the network as “avalanche” and dates as required. Specify the “protocol” parameter from the schema and set it to “Uniswap v2” as shown here.
{
ethereum(network: avalanche) {
dexTrades(
options: {desc: ["block.height", "tradeIndex"], limit: 10, offset: 0}
date: {since: "2023-10-01", till: "2023-12-11"}
) {
block {
timestamp {
time(format: "%Y-%m-%d %H:%M:%S")
}
height
}
tradeIndex
protocol (protocol: {is: "Uniswap v2"})
exchange {
fullName
address {
address
}
}
smartContract {
address {
address
annotation
}
}
buyAmount
buy_amount_usd: buyAmount(in: USD)
buyCurrency {
address
symbol
}
sellAmount
sell_amount_usd: sellAmount(in: USD)
sellCurrency {
address
symbol
}
transaction {
hash
}
}
}
}
Obtaining Token Trades
Use the buy/sell currency filter or base/quote currency filter to retrieve data on token trades.
For example, to get trades on WAVAX ERC20 tokens, use the following query in IDE. Specify the network as “avalanche” and set the filter basecurrency address to the one you require, say, 0xb31f66aa3c1e785363f0875a1b74e27b85fd66c7. Specify the dates as required.
{
ethereum(network: avalanche) {
dexTrades(
options: {desc: "count", limit: 10}
date: {since: "2023-12-01", till: "2023-12-19"}
) {
sellCurrency {
symbol
address
}
sellAmount
sell_amount_usd: sellAmount(in: USD)
buyCurrency {
symbol
address
}
buyAmount
buy_amount_usd: buyAmount(in: USD)
count(baseCurrency: {is: "0xb31f66aa3c1e785363f0875a1b74e27b85fd66c7"})
median_price: price(calculate: median)
last_price: maximum(of: block, get: price)
dates: count(uniq: dates)
started: minimum(of: date)
}
}
}
Similarly, you can get trade data on token pairs, trade pairs, and more using the Bitquery explorer and DEX API filters. Learn more here.
Conclusion
Bitquery Avalanche APIs are powerful tools that empower developers to unlock the full potential of the Avalanche blockchain. In this article, we focused on important APIs, which you can build using the GraphQL IDE interface from Bitquery.
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.