Cover Image for PancakeSwap API – Real-Time Trades, Volume, Liquidity & Pools

PancakeSwap API – Real-Time Trades, Volume, Liquidity & Pools

DEX
Research

With more than $900 million in trade volume and 1.9 million trades, PancakeSwap is one of the most popular DEX on the Binance smart chain. Today we will show you how to get PancakeSwap data and create charts using Bitquery GraphQL APIs.

If you don’t know about Pancake swap, it’s a fork of Uniswap protocol, a popular DEX protocol on Ethereum.

The way PancakeSwap works is that it allows liquidity providers to create token pairs/pools and provide liquidity to them, which will be used to enable trades on the DEX. In return, liquidity providers earn a fee in every trade.

To show the stats about the DEX, pairs tokens, Pancake has forked Uniswap’s analytical dashboard. However, to maintain this dashboard, they need to dedicate dev and infra resources. Because they need to change the code according to their own protocol and run a Binance node to extract, store, and index data to build analytics.

We at Bitquery solve these types of problems for DApp developers and blockchain companies. This article will show you how to replicate and go beyond the current Pancake dashboard using our APIs.

No need to write code, no need to host an application, no need to run Binance node, just HTML and JS and our GraphQL queries. So let’s start.

Note: If you are not familiar with GraphQL APIs, then you can start from here.

Pancake Volume and Trades

Let’s start small and query Pancake’s total trade volume and trades. For this, you can use the following query.


{
  ethereum(network: bsc) {
    dexTrades(
      exchangeName: {in: ["Pancake", "Pancake v2"]}
      date: {after: "2023-05-03"}
    ) {
      count
      tradeAmount(in: USD)
    }
  }
}

If you tweak the above query a little, you will get this volume monthly. For example, check the following query.

Using our flexible date functionality, you can now get our data for any timeframe.


{
  ethereum(network: bsc) {
    dexTrades(
      options: {asc: ["date.year", "date.month"]}
      date: {after: "2023-04-18"}
      exchangeName: {in: ["Pancake", "Pancake v2"]}
    ) {
      count
      tradeAmount(in: USD)
      date {
        year
        month
      }
    }
  }
}


You can use simple Javascript and HTML and turn the above data into a chart. For example, check this JSFiddle.

All Latest Trades on Pancake

To get all the latest trades on the PancakeSwap, you can use our DEX Trade APIs, which makes is easy to query any trade-related data for any DEX on the blockchain. For example, check the following query.

We also provide market data with our blockchain APIs. As you can see, you will get USD value for different trade attributes, such as buy amount, sell amount, and transaction fee.

{
  ethereum(network: bsc) {
    dexTrades(options: {limit: 5, desc: "trades"}, protocol: {is: "Uniswap v2"}) {
      protocol
      buyCurrency {
        symbol
        address
      }
      buyAmount
      sellCurrency {
        symbol
        address
      }
      sellAmount
      trades: count
    }
  }
}

Pancake OHLC data

Let’s say you want to show charting for a particular pair on Pancake DEX. For this, you will need OHLC (Open High Low Close) data to create candles. We support OHLC data for different time frames. For example, in the following example, we are getting WBNB, and BUSD currency pair’s OHLC data for the 5-minute timeframe.


{
  ethereum(network: bsc) {
    dexTrades(
      options: {limit: 100, asc: "timeInterval.minute"}
      date: {since: "2023-04-18"}
      exchangeName: {in: ["Pancake", "Pancake v2"]}
      baseCurrency: {is: "0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c"}
      quoteCurrency: {is: "0xe9e7cea3dedca5984780bafc599bd69add087d56"}
    ) {
      timeInterval {
        minute(count: 5)
      }
      baseCurrency {
        symbol
        address
      }
      baseAmount
      quoteCurrency {
        symbol
        address
      }
      quoteAmount
      trades: count
      quotePrice
      maximum_price: quotePrice(calculate: maximum)
      minimum_price: quotePrice(calculate: minimum)
      open_price: minimum(of: block, get: quote_price)
      close_price: maximum(of: block, get: quote_price)
    }
  }
}

Latest PancakeSwap Pairs/Pools

As we discussed before, liquidity providers create token pairs/pools on DEX and provide liquidity.

Using the following API, you can pull all newly created Pancake pools. For this, we will use our arguments API, which will extract the pair argument from the PairCreated event of the PancakeFactory contract.

{
  ethereum(network: bsc) {
    arguments(
      smartContractAddress: {is: "0xBCfCcbde45cE874adCB698cC183deBcF17952812"}
      smartContractEvent: {is: "PairCreated"}
      argument: {is: "pair"}
      options: {desc: "block.height", limit: 9}
    ) {
      block {
        height
      }
      argument {
        name
      }
      reference {
        address
      }
    }
  }
}

Information about a specific Pool

Pool’s are essentially BEP20 tokens. You will get these tokens when you add liquidity to the pool. Similarly, you can get your liquidity back by depositing these tokens back.

Let’s see how to get about a specific Pancake pool.

Note: We will take this Pancake pool as an example throughout the example.

Pool’s Volume and other stats

You can use our DEX APIs and just pass the pool’s smart contract address to get the data for a specific pool, as shown below.

{
  ethereum(network: bsc) {
    dexTrades(
      options: {desc: "count"}
      smartContractAddress: {is: "0x1b96b92314c44b159149f7e0303511fb2fc4774f"}
      date: {since: "2023-05-18", till: "2023-05-18"}
    ) {
      count
      tradeAmount(in: USD)
    }
  }
}

Pool’s information date wise

If you tweak the above API slightly, you can also get this above data for different timeframes. For example, check the query below; it gets the same data as the above query but for every day.

{
  ethereum(network: bsc) {
    dexTrades(
      options: {desc: "date.date"}
      smartContractAddress: {is: "0x1b96b92314c44b159149f7e0303511fb2fc4774f"}
    ) {
      count
      tradeAmount(in: USD)
      date {
        date(format: "%y-%m-%d")
      }
    }
  }
}

Also Read

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.

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.