NodeReal DeFi API - PancakeSwap GraphQL Deep Dive

NodeReal has collaborated with the PancakeSwap team on providing the GraphQL interface to allow developers to query the PancakeSwap information, like the daily PancakeSwap insight, pair information, token information, etc.

NodeReal DeFi API - PancakeSwap GraphQL Deep Dive

PancakeSwap is one of the largest multi-chain DEXs (decentralized exchange) that runs on BNB Smart Chain, Ethereum, and Aptos which allows for trading cryptocurrencies without an intermediary. It is built on an automated market maker (AMM) system, which relies on user-fueled liquidity pools between token pairs to enable trades.

NodeReal, as a one-stop Web3 infrastructure service provider, has enabled JSON-RPC and enhanced APIs to help Web3 developers retrieve the data they need. We are committed to providing a comprehensive and high-performance suite of DeFi APIs to improve transparency and insights into DeFi, such as last trade price, trade size, current pool liquidity, etc.

As a first pilot, NodeReal has collaborated with the PancakeSwap team on the GraphQL interface to allow developers to query the PancakeSwap information, like the daily PancakeSwap insight, pair information, token information, etc.

In this blog, we will cover the following:

  1. Why do you need a different DeFi API? We will explain typical business scenarios and opportunities.
  2. How do we achieve high-performance DeFi API? NodeReal provides a faster and more reliable interface, which could make your application respond faster to market changes. It is up to 50 times faster than the Graph Pancake APIs.
  3. Demonstrate 4 typical usages on PancakeSwap API through hands-on demos.

Why do you Need the DeFi API?

DeFi, or decentralized finance, refers to financial services that are built on top of blockchain technology, allowing for greater transparency, security, and accessibility. The DeFi API is a set of protocols and tools that will enable developers to interact with DeFi platforms and build applications on top of them.

DeFi APIs like the NodeReal PancakeSwap GraphQL API enable developers to build your applications without being overwhelmed by the complexities and high costs of blockchain data indexing, which usually requires not only a professional team of DevOps/SREs to maintain the infrastructure, storage, and database, but also a team of DeFi experts that have the deep knowledge of each DeFi Exchange data format on different chains.

With DeFi APIs from a reliable provider like NodeReal, it would be much easier for developers to get all necessary real-time DeFi data for each trade, e.g. the Liquidity Pool volume and locked values, token transactions, and trade size.

All these data are able to be retrieved by one or very few calls of the DeFi API. The DeFi API provider like NodeReal is responsible for the data indexing and aggregation, which is usually maintained and managed by a team of DeFi experts and infrastructure gurus, and the indexer and corresponding infrastructure are optimized for mass adoption. The optimization work includes the infrastructure architecture design for elastic workload pattern, global database replication to make sure the data are accessible from different locations, and parallel data indexing that can provide real-time data synchronization, which is especially critical for the large traffic DeFi protocol on popular chains, like BSC and ETH.

The DeFi API users base is of significant size, including but not limited to:

  • DEX indexing e.g. DexScreener,  APY Vision, DefiLlama, DEXTools, DEXGuru
  • DEX liquidity monitoring is needed by traders who want to ensure they are able to execute trades quickly and at competitive prices, and market makers who may use it to identify opportunities to provide liquidity to DEXs with low liquidity, and security monitoring platforms e.g.Certik Skynet,Chainalysis
  • Trading house and tools e.g. Woofi, Zapper, InstaDApp
  • Industry data analytics and research organizations e.g. Coinmarketcap, Coingecko, Nansen
  • Financial institutions who need to build risk management and compliance systems to monitor their exposures to DEXs and their underlying digital assets, or build trading algorithms and systems that take advantage of the real-time data to make informed trading decisions, or even build analytics and reporting tools that provide insights into the performance.

The NodeReal PancakeSwap GraphQL API is just the beginning to provide a more solid foundation for DeFi builders.

How do we Achieve High-Performance DeFi API

Below is a comparison of the Subgraph and the NodeReal real-time data pipeline. Different from the PancakeSwap Subgraph, NodeReal implements the high-speed PancakeSwap API through the real-time parallel data pipeline. It is a concurrent data indexing engine and the extractor extracts the data from the chain and saves the data to our database and event handler.

Because of the parallel data pipeline and pre-indexed data, NodeReal Pancake API could provide up to 50 times faster response.

SubGraph Architecture

NodeReal Real-Time Data Pipeline Architecture

Performance Comparison

In this blog, we will take two simple examples to demonstrate the huge performance boost powered by NodeReal compared with SubGraph. Hope that this can give you a high level of understanding of what we have achieved from the performance perspective.

What you can do with NodeReal Pancake API

NodeReal has collaborated with the Pancake Swap team to build the API platform, and the APIs are being used by the PancakeSwap team first. You can log in to the PancakeSwap Info page to view the real-time data powered by NodeReal.

The PancakeSwap API can be very useful for many different use cases, not just for DeFi developers who want to query the latest swap and liquidity info on Pancake Swap, but also various other applications or platforms, for example, distributed market data analysis platform, and security monitoring platform like Certik Skynet powered by NodeReal.

In this blog, let us introduce you to 4 different use cases in your applications.

Scenario 1: PancakeSwap Liquidity and Volume Data Monitor

On the Pancake Info page, you can view the daily liquidity and volume of the PancakeSwap. It is a pervasive case if you want to monitor the status of the pancake swap, especially for the DeFi info insight and aggregation.

We can get the corresponding information from NodeReal Pancake API. In the GraphQL request body, we can set the filter to query the day data from a particular time.

For example, in the example below, we want to query the data from December 1st of 2023, we can set the time filter to later than 1669852800, which is the unix time format of “1 st December 2022 00:00:00”.

{
   pancakeDayDatas(first: 1000, orderBy: date, orderDirection: asc, where: {
       date_gt:1669852800
   })
   {
       id
       date
       dailyVolumeUSD
       totalLiquidityUSD
   }
}


Scenario 2: Token Daily Monitor

For DeFi users who want to monitor a particular token on Pancake Swap, you can use the token daily info to retrieve the token data. For instance, the daily volume, liquidity, transaction number, etc.

Without the NodeReal PancakeSwap  API, you may need to index the data into your database and build your own API to support your front end to display this info to your users. In some cases, you may want to set notifications or alarms for some threshold, such as when there is a volume spike or liquidity threshold, the token daily info data is very helpful.

On the PancakeSwap Info page, you can also find the token watch list page as below. You can view the daily liquidity and volume data, which gives you insight in how this token is performing on PancakeSwap.

For example, when you want to query the BUSD token on the PancakeSwap, first, you need to find the ID of the BUSD through BSCTrace.

On the tokens page, find the BUSD token, and copy the smart contract address of BUSD. It will be part of the id of the BUSD token on PancakeSwap. The smart contract address of BUSD is 0xe9e7cea3dedca5984780bafc599bd69add087d56, and the ID of the token of BUSD on the Pancake Swap is 0xe9e7cea3dedca5984780bafc599bd69add087d56-19939.

In the request below, we are trying to query the token data of 13 December 2022 (unix time of 1670889600).

{
   tokenDayDatas (first: 100, where: {date : 1670889600, id_in: "0xe9e7CEA3DedcA5984780Bafc599bD69ADd087D56-19339"}){
       id
       dailyVolumeToken
       dailyVolumeBNB
       dailyVolumeUSD
       dailyTxns
       totalLiquidityToken
       totalLiquidityUSD
       date
   }
}

In our case, we use the dailyVolumeUSD and totalLiquidityUSD fields to monitor the token status. Of course, you can make your own chart with the data we provided through the API.

Scenario 3: Pair Daily(LP) Monitor

PancakeSwap offers a variety of tokens for users to trade. It is very easy compared to most exchanges. You aren't going to be overwhelmed by charts or jargon, and calculations are all handled for you. You can refer to the PancakeSwap info page to find your favorite tokens.


For example, if I want to monitor the Cake/BUSD pair, first, I need to know the pair address. You can check the address of the pair from “View On BSCScan” on the pair page.


Another way is to query the pair address by the NodeReal API. We already know the cake address is “0x0E09FaBB73Bd3Ade0a17ECC321fD13a19e81cE82”, and “0xe9e7CEA3DedcA5984780Bafc599bD69ADd087D56” BUSD. Therefore we can use filter token0, token0 to query the Cake/BUSD pair address.

{
pairs (first: 1000 where: {token0: "0x0E09FaBB73Bd3Ade0a17ECC321fD13a19e81cE82", token1: "0xe9e7CEA3DedcA5984780Bafc599bD69ADd087D56" }){
   id
   name
   token0{
       id
       name
   }
   token1 {
       id
       name
   }

}
}

And it will return, the pair id is the address.

{
   "data": {
       "pairs": [
           {
               "id": "0x804678fa97d91B974ec2af3c843270886528a9E6",
               "name": "Cake-BUSD",
               "token0": {
                   "id": "0x0E09FaBB73Bd3Ade0a17ECC321fD13a19e81cE82",
                   "name": "PancakeSwap Token"
               },
               "token1": {
                   "id": "0xe9e7CEA3DedcA5984780Bafc599bD69ADd087D56",
                   "name": "BUSD Token"
               }
           }
       ]
   }
}




Then we can use this address to query the pair's daily data.

{
   "data": {
       "pairs": [
           {
               "id": "0x804678fa97d91B974ec2af3c843270886528a9E6",
               "name": "Cake-BUSD",
               "token0": {
                   "id": "0x0E09FaBB73Bd3Ade0a17ECC321fD13a19e81cE82",
                   "name": "PancakeSwap Token"
               },
               "token1": {
                   "id": "0xe9e7CEA3DedcA5984780Bafc599bD69ADd087D56",
                   "name": "BUSD Token"
               }
           }
       ]
   }
}


The reserve0 and reserve1 fields are the number of total locked token in the pair, and you can also get the pair price from the token0price and token1price, which could be used to monitor the price movement of the pair.

Scenario 4: Pair Hour Data Monitor

This is very similar to the daily data monitor. Let’s also take the Cake/BUSD pair as our example, and I would like to monitor the last 24 hours data.

{
   pairHourDatas(first: 1000, orderBy: hourStartUnix, where: { pair: "0x804678fa97d91B974ec2af3c843270886528a9E6", hourStartUnix_gt: 1670803200, hourStartUnix_lt: 1670889599 })
   {
       id
       hourStartUnix
       reserve0
       reserve1
       reserveUSD
       pair{
           token0{
               id
               name
           }
           token1{
               id
               name
           }
           token0Price
           token1Price
       }
       hourlyVolumeUSD
       hourlyTxns
      
   }
}


Get Started!

With all being said, NodeReal PancakeSwap GraphQL enables developers worldwide to query PancakeSwap information at high speed. If you are looking for this kind of API to power your applications, here is the right place! Here is a handy doc for you to get started easily.

About NodeReal

NodeReal is a one-stop blockchain infrastructure and service provider that embraces the high-speed blockchain era and empowers developers by “Make your Web3 Real”. We provide scalable, reliable, and efficient blockchain solutions for everyone, aiming to support the adoption, growth, and long-term success of the Web3 ecosystem.

Join Our Community

Join our community to learn more about NodeReal and stay up to date!

Discord | Twitter| Youtube | LinkedIn