DEX Tool API | How to build a DEX trading terminal

In this tutorial, we’ll discuss how you can create a DEX tool or DEX trading terminal of your own using Bitquery APIs. Bitquery helps many of its clients access data from the blockchain network, that can be used to build different blockchain data-related products.

Our Bitquery explorer is the best way to start with our APIs. Therefore, we will use to guide on how to get APIs data relevant to your project.

Our Bitquery explorer has a number of widgets to fetch different sorts of data from the blockchain networks. Every API available on the explorer is an independent HTML, JS code that can be embedded easily on any website or app on the internet.

First of all, let’s look at some of the data that a DEX tool should provide to its users.

For this tutorial, we have taken certain blockchain networks and DEX exchanges as examples to demonstrate the use of Bitquery’s APIs and tool. You can find the data related to various other networks and exchanges as per your requirement.

New pairs

Your website needs to have information about the new token pairs being created on the blockchain networks to attract users. You can get it using this query. Here we have taken PancakeSwap for example, you can change it as per your need.

OHLC data

To start off, you might want to display the OHLC (Open High Low Close) data on your trading terminal. You can use this query to get the OHLC data for PancakeSwap. Using our DEX APIs you can easily get USD, ETH, BTC, BNB and USDT prices for DEX assets. TradingView library is widely in use for the same. Bitquery’s APIs make the integration of the TradingView library and enable all sorts of trading indicators easy. You can refer to these blogs to get more information regarding the same.

Trade Volume for a particular DEX

The total volume and count of trades taking place on a particular DEX and other related information becomes important for the traders when it comes to trading using your trading terminal to get the overall statistics at a glance. Using this query, you can get all that data and display it on your website.

Highest volume of token traded

This is another important data point that needs to be there on your trading terminal which can act as a bonus feature for traders. You can get the data for the same using this query.

Latest trades

You can display the latest trades that have been made on various networks too. We’ll consider the Ethereum blockchain network here and display the latest trades. The Bitquery explorer provides the list of tokens being traded on a particular network using a specific DEX on the basis of timestamp, count, amount etc. You can use this query to get the same or refer to the following page to get more queries and data.

Ethereum Mainnet DEX Trades (bitquery.io)

You can add Buy currency in filters to get trades for a given token and add both buy and sell currency to data for a given trading pair.

DEX Trades

You can fetch a lot of data from the DEX exchanges as well to display on your websites/web apps like the top DEX exchange on a network as per the count of the number of total trades taking place. You can use this query for the same. For other data related to DEXs or the graphical representation please visit the following page

Ethereum Mainnet Decentralized Exchanges (bitquery.io)

Token specific trades

Bitquery’s APIs also offer you to get token specific information easily using simple queries and integrate it in your website. For example, this query gives the token transfer statistics for the token address 0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2. You can even get the transfers, senders, receivers, inflow, outflow, smart contracts etc related to this address.

Gas Price, Gas Cost, Transaction Average Cost of tokens

A lot of data related to the gas value can be fetched using Bitquery’s APIs. Here is a query giving the smart contracts in the descending order of their gas cost in ETH. To get a graphical representation of this data and other queries related to the gas analytics please visit.

Ethereum Mainnet Gas Cost and usage and other analytics (bitquery.io)

You can even find a tutorial explaining the implementation of offline dashboards using Bitquery’s APIs and adding graphical representations to your Webapp/website too!

Tradingview Charting

For the integration of Trading view Analysis Charts in your trading terminal, you can refer to our tutorials. All you need to do is follow the simple steps described in the article and you’ll have the charts integrated into your website/webapp.

You can use our GraphQL APIs to create charts for your local dashboards.

We have created a simple page displaying the above queries by integrating the Bitquery APIs and using the GraphQL IDE.

There are a number of tutorials available on our community forum which you can refer to for getting started with the creation of your terminal by integration of graphQL queries. We have created a very basic page using the same.

2

We begin by installing the dependencies, importing libraries and setting up the view js engine. The steps to do the same have been described in detail in the tutorial itself. You can also get this code from Github using this link

We have used aliasing for this sample page, i.e., we have clubbed multiple queries in a single query which can be found here. This has been done to be able to display the results of multiple queries on a single page. You can use whatever method suits you better.

import express from 'express'
import fetch from 'node-fetch'
const app = express()
app.set('view engine', 'ejs')

const endpoint = "https://graphql.bitquery.io/" 
const query = `
{
  OHLC: ethereum(network: bsc) {
    dexTrades(
      options: {limit: 100, asc: "timeInterval.minute"}
      date: {since: "2020-11-01"}
      exchangeName: {in: ["Pancake", "Pancake v2"]}
      baseCurrency: {is: "0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c"}
      quoteCurrency: {is: "0xe9e7cea3dedca5984780bafc599bd69add087d56"}
    ) {
      timeInterval {
        minute(count: 5)
      }
      <h2>The most traded token on Ethereum network is : <%= result6 %></h2>
      <h2>The name of the DEX exchange on the Ethereum network with highest number of trades is : <%= result7 %></h2>
      <h2>The amount traded for DEX trades by exchange on Ethereum is : <%= result8 %></h2>
      <h2>The number of trades on sorting traded currencies by time is : <%= result10 %></h2>
      <h2>The number of contracts for traded currencies by count is : <%= result11 %></h2>
      <h2>The number of trades in the trade taker segment on Ethereum network is : <%= result12 %></h2>
      <h2>The top trader in the Trade takers segment on the Ethereum network is : <%= result13 %></h2>
      <h2>The number of trades taking place on the ethereum network by time is : <%= result15 %></h2>
    </body>
    </html>
      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)
    }
  }
  New_Pairs: 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
      }
    }
  }
  Trade_Volume_For_DEX: ethereum(network: ethereum) {
    dexTrades(exchangeName: {is: "Uniswap"}) {
      count
      tradeAmount(calculate: sum, in: USD)
      medianTrade: tradeAmount(calculate: median, in: USD)
      averageTrade : tradeAmount (calculate: average, in: USD)
    }
  }
  Most_Traded_Ethereum_Tokens: ethereum(network: ethereum) {
    transfers(
      options: {desc: "count", limit: 10, offset: 0}
      amount: {gt: 0}
      date: {since: "2021-07-01", till: "2022-02-09T23:59:59"}
    ) {
      currency {
        symbol
        address
      }
      count
      senders: count(uniq: senders)
      receivers: count(uniq: receivers)
      days: count(uniq: dates)
      from_date: minimum(of: date)
      till_date: maximum(of: date)
      amount
    }
  }
	Gas_Unit_Price_By_Date: ethereum(network: ethereum) {
    transactions(options: {asc: "date.date"}, date: {since: "2022-01-13", till: "2022-02-11T23:59:59"}) {
      date: date {
        date(format: "%Y-%m-%d")
      }
      gasPrice
      gasValue
      average: gasValue(calculate: average)
      maxGasPrice: gasPrice(calculate: maximum)
      medianGasPrice: gasPrice(calculate: median)
    }
  }
  Gas_Cost_By_Date: ethereum(network: ethereum) {
    transactions(options: {asc: "date.date"}, date: {since: "2022-01-13", till: "2022-02-11T23:59:59"}) {
      date: date {
        date(format: "%Y-%m-%d")
      }
      gasPrice
      gasValue
      average: gasValue(calculate: average)
      maxGasPrice: gasPrice(calculate: maximum)
      medianGasPrice: gasPrice(calculate: median)
    }
  }
  Gas_Price_By_Date: ethereum(network: ethereum) {
    transactions(options: {asc: "date.date"}, date: {since: "2022-01-13", till: "2022-02-11T23:59:59"}) {
      date: date {
        date(format: "%Y-%m-%d")
      }
      gasPrice
      gasValue
      average: gasValue(calculate: average)
      maxGasPrice: gasPrice(calculate: maximum)
      medianGasPrice: gasPrice(calculate: median)
    }
  }
  Trades_By_Time: ethereum(network: ethereum) {
    dexTrades(options: {asc: "date.date"}, date: {since: "2022-01-13", till: "2022-02-11T23:59:59"}) {
      date: date {
        date(format: "%Y-%m-%d")
      }
      trades: countBigInt
      traders: countBigInt(uniq: takers)
      contracts: countBigInt(uniq: smart_contracts)
      currencies: countBigInt(uniq: buy_currency)
    }
  }
	Trade_Takers: ethereum(network: ethereum) {
    dexTrades(options: {asc: "date.date"}, date: {since: "2022-01-13", till: "2022-02-11T23:59:59"}) {
      date: date {
        date(format: "%Y-%m-%d")
      }
      trades: countBigInt
      traders: countBigInt(uniq: takers)
      contracts: countBigInt(uniq: smart_contracts)
      currencies: countBigInt(uniq: buy_currency)
    }
  }
	Trade_Currencies_By_Time: ethereum(network: ethereum) {
    dexTrades(options: {asc: "date.date"}, date: {since: "2022-01-13", till: "2022-02-11T23:59:59"}) {
      date: date {
        date(format: "%Y-%m-%d")
      }
      trades: countBigInt
      traders: countBigInt(uniq: takers)
      contracts: countBigInt(uniq: smart_contracts)
      currencies: countBigInt(uniq: buy_currency)
    }
  }
	DEX_Smart_Contracts_By_Time: ethereum(network: ethereum) {
    dexTrades(options: {asc: "date.date"}, date: {since: "2022-01-13", till: "2022-02-11T23:59:59"}) {
      date: date {
        date(format: "%Y-%m-%d")
      }
      trades: countBigInt
      traders: countBigInt(uniq: takers)
      contracts: countBigInt(uniq: smart_contracts)
      currencies: countBigInt(uniq: buy_currency)
    }
  }
  DEX_Trades_By_Exchange: ethereum(network: ethereum) {
    dexTrades(options: {asc: "date.date"}, date: {since: "2022-01-13", till: "2022-02-11T23:59:59"}) {
      date: date {
        date(format: "%Y-%m-%d")
      }
      exchange {
        fullName
      }
      count
      tradeAmount(in: USD)
    }
  }
	Amount_Of_DEX_Trades_by_Exchange: ethereum(network: ethereum) {
    dexTrades(options: {desc: "tradeAmount"}, date: {since: "2022-01-13", till: "2022-02-11T23:59:59"}) {
      exchange {
        fullName
      }
      trades: count
      tradeAmount(in: USD)
      currencies: count(uniq: buy_currency)
      contracts: count(uniq: smart_contracts)
    }
  }
  DEX_Exchanges: ethereum(network: ethereum) {
    dexTrades(options: {desc: "tradeAmount"}, date: {since: "2022-01-13", till: "2022-02-11T23:59:59"}) {
      exchange {
        fullName
      }
      trades: count
      tradeAmount(in: USD)
      currencies: count(uniq: buy_currency)
      contracts: count(uniq: smart_contracts)
    }
  }

}

`
let ohlc = 0, newPairs_height = 0, newPairs_name = 0, Trade_Volume_For_DEX_count = 0, Trade_Volume_For_DEX_amount = 0 , Most_Traded_Ethereum_Tokens = 0 , DEX_Exchanges = 0 , Amount_Of_DEX_Trades_by_Exchange = 0; 
let DEX_Trades_By_Exchange = 0, DEX_Smart_Contracts_By_Time =0, Trade_Currencies_By_Time = 0 , Trade_Takers = 0, Trades_By_Time = 0;
let Gas_Price_By_Date = 0 , Gas_Cost_By_Date = 0, Gas_Unit_Price_By_Date = 0 ; 
let countValue = 0;
let DEX_Trades_By_Exchange_count = 0, Trade_Currencies_By_Time_contracts = 0, Trade_Takers_traders = 0, Trades_By_Time_trade = 0;
let finalBaseAmount = 0;
const result2 = 2022; 
function BitqueryAPICall(){
	const data =  fetch(endpoint, {
	    method: "POST",
	    headers: {
	    	"Content-Type": "application/json",
	      	"X-API-KEY": "USE YOUR API KEY HERE"
	    },
	    

After making the API call, we have declared variables for the data that we will display in this page.

body: JSON.stringify({
	        query
	    })
	}).then(res => res.json()).then(data => {
		ohlc = data.data.OHLC.dexTrades
		

		for (let i=0;i<10;i++){
			let baseCurrencyDetails = ohlc[i].baseCurrency.symbol
			let baseCurrencyAmount = ohlc[i].baseAmount
			console.log(baseCurrencyDetails+" is the BASECURRENCY SYMBOL")
			console.log(baseCurrencyAmount+" is the BASEAMOUNT");
			console.log("\n")
		}
		
}

Ultimately we assign these declared variables the values of the results we received by running these queries and send it to the template before running the server.

finalBaseAmount = ohlc[0].baseAmount
		newPairs_name = data.data.New_Pairs.arguments[0].argument.name
		newPairs_height = data.data.New_Pairs.arguments[0].block.height
		Trade_Volume_For_DEX_count = data.data.Trade_Volume_For_DEX.dexTrades[0].count
		Trade_Volume_For_DEX_amount = data.data.Trade_Volume_For_DEX.dexTrades[0].amount
		Most_Traded_Ethereum_Tokens = data.data.Most_Traded_Ethereum_Tokens.transfers[0].currency.symbol
		DEX_Exchanges = data.data.DEX_Exchanges.dexTrades[0].exchange.fullName
		Amount_Of_DEX_Trades_by_Exchange = data.data.Amount_Of_DEX_Trades_by_Exchange.dexTrades[0].tradeAmount
		DEX_Trades_By_Exchange = data.data.DEX_Trades_By_Exchange.dexTrades[0].fullname 
		DEX_Trades_By_Exchange_count = data.data.DEX_Trades_By_Exchange.dexTrades[0].count
		Trade_Currencies_By_Time = data.data.Trade_Currencies_By_Time.dexTrades[0].trades
		Trade_Currencies_By_Time_contracts = data.data.Trade_Currencies_By_Time.dexTrades[0].contracts
		Trade_Takers =data.data.Trade_Takers.dexTrades[0].trades
		Trade_Takers_traders =data.data.Trade_Takers.dexTrades[0].traders
		Trades_By_Time = data.data.Trades_By_Time.dexTrades[0].date
		Trades_By_Time_trade = data.data.Trades_By_Time.dexTrades[0].trades
	})

In the home.ejs file, we have returned the results of the queries using the aliased result# variable. For this example we have just returned a few variables from the queries in a simple format to give you a rough idea about the plethora of information that you can fetch and display using Bitquery’s APIs, widgets and tools.

<header>
		<h1>Using Bitquery GraphQL API with NodeJS</h1>
	</header>
	<h2>The BaseAmount of OHLC data's 1st element is <%= result %></h2>
	<h2>The New Pair's name is : <%= result2 %></h2>
	<h2>The New Pair's height is : <%= result3 %></h2>
	<h2>The count of trades taking place on Uniswap is : <%= result4 %></h2>
	<h2>The most traded token on Ethereum network is : <%= result6 %></h2>
	<h2>The name of the DEX exchange on the Ethereum network with highest number of trades is : <%= result7 %></h2>
	<h2>The amount traded for DEX trades by exchange on Ethereum is : <%= result8 %></h2>
	<h2>The number of trades on sorting traded currencies by time is : <%= result10 %></h2>
	<h2>The number of contracts for traded currencies by count is : <%= result11 %></h2>
	<h2>The number of trades in the trade taker segment on Ethereum network is : <%= result12 %></h2>
	<h2>The top trader in the Trade takers segment on the Ethereum network is : <%= result13 %></h2>
	<h2>The number of trades taking place on the ethereum network by time is : <%= result15 %></h2>

8

9

Conclusion

We have almost all the DEXs on blockchain networks like Ethereum, Binance, Avalanche, Velas etc and adding more every day, our DEX APIs are comprehensive and available for a wide range of blockchain networks. If you are building a DEX related tool or want to get DEX trading data, you can try our APIs and by referring to our tutorials and blogs, you can easily create your own trading terminal.

If you have any questions or queries, you can also contact us.

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. We are crossing the ‘chain-chasm’ by delivering a 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, SushiSawap, 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.